Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrement size of graph when removing vertices #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Data/Graph/DGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/Data/Graph/UGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/Data/Graph/DGraphSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions test/Data/Graph/UGraphSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)