-
Notifications
You must be signed in to change notification settings - Fork 178
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
Data.Graph: detect cycles utility functions #978
Comments
It's not clear to me what exactly should be offered. I don't follow what the input of your suggested However, your use case seems to be a specific case of graphs where every vertex has at most one outgoing edge (directed pseudoforest), since you wouldn't have two rules cycles :: Ord a => [(a,a)] -> [[a]]
cycles edges = [xs | CyclicSCC xs <- stronglyConnComp (map (\(a, b) -> (a, a, [b])) edges)]
-- cycles [(1,2),(2,3),(3,1),(4,5),(5,6),(6,5)] = [[5,6],[1,2,3]]
-- [5,6] means the cycle is 5->6->5
-- [1,2,3] means the cycle is 1->2->3->1 Also, your |
My thinking there was that it's easy to turn the [[Vertex]] into a Bool, but not vice-versa: if you simply want the
Yes, in most of these cases I think I would regard two such rules as ambiguous and bad: either B or C is the right target, it can't be both. I suppose I shouldn't be surprised that has a name. But I wouldn't want to assume that all my graphs are in fact true directed pseudoforests because I'm quite sure that they are not (for the same sorts of practical reasons that I need to check for cycles in the first place!) and I would need some way to check & fix them before I could replace my cycle detector with a pseudoforest-assuming checker.
I don't think it winds up being an issue for me since I use It may help to give one example of how I use this: I need Wikipedia links on my website to be consistent for various reasons, and avoid WP redirects, so I have a list of URL1->URL2 rewrites; so I might write in an essay a link to the Wikipedia article for * I have been using these Wikipedia links in my writings since ~2009, and I set up the redirect-rewrites like 2 years ago, and I intend to use them indefinitely. |
It would be helpful if Data.Graph provided utility functions for detecting cycles in graphs, which may be problematic and represent infinite loops. (As there is no standard 'utility' module I see for Data.Graph, it would be a helper function for the SCC part.)
I use some sets of rewrite rules like 'A->B', where, due to changes elsewhere and updates over many years, they can inadvertently wind up defining a non-obvious cycle like 'A->B->C->A' which would loop infinitely. These are nasty surprises when they surface, so I look into detecting cycles in the graph defined by sets of rewrite rules. This has gotten me some utility functions of the form:
Which turned up plenty of latent infinite loops, and thus far, has not missed any new infinite loops in the months I've had it.
It's not exactly obvious how you'd turn your convenient little rewrite rule list into a proper graph cycle-detection when you've never used Data.Graph before, even if the resulting code using
stronglyConnComp
+flattenSCC
is pretty short and doubtless seems trivial to a Data.Graph savant, so I think it'd be nice to wrap that up as a utility function of some sort likecycles :: [vertex] -> [[vertex]]
, which would return a list of cycles (where the sublist is length > 1).This would then be very easy to check graphs for issues and support other use-cases where a list of cycles might be useful.
(The helper functions
isCycleLess
andfindCycles
might not be useful enough to include, but the test-suite should probably be kept in some form to avoid regressions and define expected behavior.)The text was updated successfully, but these errors were encountered: