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

Multiple changes #1

Open
pseudonom opened this issue Mar 26, 2015 · 6 comments
Open

Multiple changes #1

pseudonom opened this issue Mar 26, 2015 · 6 comments
Labels
status: needs more info This issue needs more info before any action can be done.

Comments

@pseudonom
Copy link

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?

@paf31
Copy link
Contributor

paf31 commented Mar 26, 2015

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

@pseudonom
Copy link
Author

  • 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
Copy link
Author

@paf31 bump

@paf31
Copy link
Contributor

paf31 commented Apr 23, 2015

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?

@pseudonom
Copy link
Author

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.

@JordanMartinez JordanMartinez added the status: needs more info This issue needs more info before any action can be done. label Dec 4, 2021
@JordanMartinez
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info This issue needs more info before any action can be done.
Projects
None yet
Development

No branches or pull requests

3 participants