-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add a function to return the list of edges in a graph #17
Changes from all commits
43afb78
3862b9e
654d9bf
2f5dd86
92560ca
4e5d0d5
c724eec
029c360
1dbf1cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,25 @@ | |
|
||
module Data.Graph | ||
( Graph | ||
, Edge | ||
, unfoldGraph | ||
, fromMap | ||
, toMap | ||
, vertices | ||
, edges | ||
, lookup | ||
, outEdges | ||
, topologicalSort | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Bifunctor (lmap) | ||
import Data.CatList (CatList) | ||
import Data.CatList as CL | ||
import Data.Foldable (class Foldable, foldl, foldr, foldMap) | ||
import Data.List (List(..)) | ||
import Data.FoldableWithIndex (foldlWithIndex) | ||
import Data.List (List(..), (:)) | ||
import Data.List as L | ||
import Data.Map (Map) | ||
import Data.Map as M | ||
|
@@ -42,6 +46,9 @@ instance traversableGraph :: Traversable (Graph k) where | |
traverse f (Graph m) = Graph <$> (traverse (\(v /\ ks) -> (_ /\ ks) <$> (f v)) m) | ||
sequence = traverse identity | ||
|
||
-- | An Edge between 2 nodes in a Graph | ||
type Edge k = { start :: k, end :: k } | ||
|
||
-- | Unfold a `Graph` from a collection of keys and functions which label keys | ||
-- | and specify out-edges. | ||
unfoldGraph | ||
|
@@ -54,9 +61,9 @@ unfoldGraph | |
-> (k -> v) | ||
-> (k -> out k) | ||
-> Graph k v | ||
unfoldGraph ks label edges = | ||
unfoldGraph ks label theEdges = | ||
Graph (M.fromFoldable (map (\k -> | ||
Tuple k (Tuple (label k) (L.fromFoldable (edges k)))) ks)) | ||
Tuple k (Tuple (label k) (L.fromFoldable (theEdges k)))) ks)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you revert these two changes since they're not relevant to this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 changes are to avoid shadowing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! Makes sense. Ok. |
||
|
||
-- | Create a `Graph` from a `Map` which maps vertices to their labels and | ||
-- | outgoing edges. | ||
|
@@ -72,6 +79,17 @@ toMap (Graph g) = g | |
vertices :: forall k v. Graph k v -> List v | ||
vertices (Graph g) = map fst (M.values g) | ||
|
||
-- | List all edges in a graph | ||
edges :: forall k v. Graph k v -> List (Edge k) | ||
edges (Graph g) = foldlWithIndex edges' Nil g | ||
where | ||
edges' :: k -> List (Edge k) -> Tuple v (List k) -> List (Edge k) | ||
edges' src acc (_ /\ dests) = | ||
foldl (mkEdge src) acc dests | ||
|
||
mkEdge :: k -> List (Edge k) -> k -> List (Edge k) | ||
mkEdge src acc dest = { start: src, end: dest } : acc | ||
|
||
-- | Lookup a vertex by its key. | ||
lookup :: forall k v. Ord k => k -> Graph k v -> Maybe v | ||
lookup k (Graph g) = map fst (M.lookup k g) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put this back into the correct spot in the changelog?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this because it hasn't been released. Would you prefer a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my bad. No, let's just fix it here.