purescript-graphs icon indicating copy to clipboard operation
purescript-graphs copied to clipboard

Multiple changes

Open pseudonom opened this issue 10 years ago • 6 comments

In the course of using this module, I made several changes. I'm not certain they're all universally appealing. How would you like the pull request to happen? Just a single pull request with commits in decreasing order of appeal? Post a summary of the changes, then pick out which to keep, and make a pull request on that basis?

pseudonom avatar Mar 26 '15 18:03 pseudonom

Yes, could you please post a list of changes to start with?

paf31 avatar Mar 26 '15 18:03 paf31

  • Added weight to Edge (i.e. data Edge k e = Edge k e k)
  • Hid Graph behind smart constructor verifying that edges reference actually existing vertices and that there aren't duplicate vertices
  • Added eq and show instances for Edge and Graph
  • Added vertex' :: forall k e v. (Eq k) => (v -> k) -> k -> Graph k e v -> v
  • Added inEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e] (and outEdges)
  • Added bimap' :: forall e f k i v w. (w -> i) -> (k -> v) -> (e -> f) -> (v -> w) -> Graph k e v -> Graph i f w (and bimap, map' and map)
  • Added alter :: forall k e v. (Ord k) => (v -> k) -> (v -> v) -> k -> Graph k e v -> Graph k e v

pseudonom avatar Mar 26 '15 18:03 pseudonom

@paf31 bump

pseudonom avatar Apr 23 '15 01:04 pseudonom

Sorry for the delay. Let's maybe start with the simple ones, like Eq and Show instances, and smart constructors. Then let's look at inEdges and outEdges.

How are weighted edges used?

paf31 avatar Apr 23 '15 01:04 paf31

No problem. Start with as in paste here?

Eq and Show


instance eqEdge :: (Eq k, Eq e) => Eq (Edge k e) where
  (==) (Edge fa ea ta) (Edge fb eb tb) = fa == fb && ea == eb && ta == tb
  (/=) a b = not $ a == b
instance showEdge :: (Show k, Show e) => Show (Edge k e) where
  show (Edge from e to) =
    "Edge (" <> show from <> ") (" <> show e <> ") (" <> show to <>  ")"

instance eqGraph :: (Eq k, Eq e, Eq v) => Eq (Graph k e v) where
  (==) (Graph vsa esa) (Graph vsb esb) = vsa == vsb && esa == esb
  (/=) a b = not $ a == b
instance showGraph :: (Show k, Show e, Show v) => Show (Graph k e v) where
  show (Graph vs es) = "Graph (" <> show vs <> ") (" <> show es <> ")"

Smart constructor

graph :: forall k e v. (Ord k) => (v -> k) -> [v] -> [Edge k e] -> Maybe (Graph k e v)
graph f vs es =
  if S.isEmpty (es' `S.difference` vs') && A.length (S.toList vs') == A.length vs
  then Just $ Graph vs es
  else Nothing where
    vs' = S.fromList $ f <$> vs
    es' = S.fromList $ A.concatMap (\(Edge from _ to) -> [from, to]) es

inEdges and outEdges

inEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e]
inEdges k (Graph _ es) = A.filter (\(Edge _ _ k) -> k == k) es

outEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e]
outEdges k (Graph _ es) = A.filter (\(Edge k _ _) -> k == k) es

Weighted edges

I actually ended up not using graphs. But weights seem pretty useful for shortest path, minimum spanning tree, etc.

pseudonom avatar Apr 23 '15 01:04 pseudonom

I think this issue should be split into separate issues and discussed separately.

JordanMartinez avatar Dec 04 '21 16:12 JordanMartinez