diff --git a/src/Data/Graph/DGraph.hs b/src/Data/Graph/DGraph.hs index b737d19..b3934e5 100644 --- a/src/Data/Graph/DGraph.hs +++ b/src/Data/Graph/DGraph.hs @@ -136,10 +136,8 @@ instance Graph DGraph where | otherwise = graph where v1Links' = HM.delete v2 $ getLinks v1 g - - removeVertex v g@(DGraph s _) = DGraph s - $ (\(DGraph _ g') -> HM.delete v g') - $ foldl' (flip removeArc) g $ incidentArcs g v + removeVertex v g = DGraph s $ HM.delete v g' + where (DGraph s g') = foldl' (flip removeArc) g $ incidentArcs g v isSimple g = foldl' go True $ vertices g where go bool v = bool && not (HM.member v $ getLinks v $ unDGraph g) diff --git a/src/Data/Graph/UGraph.hs b/src/Data/Graph/UGraph.hs index a891470..12e24e7 100644 --- a/src/Data/Graph/UGraph.hs +++ b/src/Data/Graph/UGraph.hs @@ -116,9 +116,8 @@ instance Graph UGraph where v2Links = HM.delete v1 $ getLinks v2 g update = HM.adjust . const - removeVertex v g@(UGraph s _) = UGraph s - $ (\(UGraph _ g') -> HM.delete v g') - $ foldl' (flip removeEdge) g $ incidentEdges g v + removeVertex v g = UGraph s $ HM.delete v g' + where (UGraph s g') = foldl' (flip removeEdge) g $ incidentEdges g v isSimple g = foldl' go True $ vertices g where go bool v = bool && not (HM.member v $ getLinks v $ unUGraph g) diff --git a/test/Data/Graph/DGraphSpec.hs b/test/Data/Graph/DGraphSpec.hs index ddd191f..857d9bf 100644 --- a/test/Data/Graph/DGraphSpec.hs +++ b/test/Data/Graph/DGraphSpec.hs @@ -78,3 +78,6 @@ spec = do edgeTriples g2 `shouldBe` [(1, 2, 'a'), (2, 1, 'b')] length (arcs g2) `shouldBe` 2 + it "Decrements its size when vertices incident to an arc are removed" $ property $ + \g v -> (g `containsVertex` v) && (vertexDegree g v > 0) && (size g > 0) + ==> let g1 = removeVertex v (g :: DGraph Int ()) in size g1 == length (arcs g1) diff --git a/test/Data/Graph/UGraphSpec.hs b/test/Data/Graph/UGraphSpec.hs index 64f527e..107ca72 100644 --- a/test/Data/Graph/UGraphSpec.hs +++ b/test/Data/Graph/UGraphSpec.hs @@ -85,3 +85,7 @@ spec = do let g = insertEdge (Edge 3 4 'b') $ insertEdge (Edge 1 2 'a') empty :: UGraph Int Char edgeTriples g `shouldBe` [(1, 2, 'a'), (3, 4, 'b')] length (edges g) `shouldBe` 2 + + it "Decrements its size when vertices incident to an edge are removed" $ property $ + \g v -> (g `containsVertex` v) && (vertexDegree g v > 0) && (size g > 0) + ==> let g1 = removeVertex v (g :: UGraph Int ()) in size g1 == length (edges g1)