[jgrapht-users] Getting an ordered set of edges for graph cycles?
Brought to you by:
barak_naveh,
perfecthash
From: <org...@io...> - 2013-12-19 18:39:35
|
Hello. I'm attempting to use jgrapht to handle dependencies in a small programming language. Essentially, I want to ensure that a "module" can only import known modules and that imports cannot introduce cyclic dependencies. I want to give good error messages when the user manages either of the previous errors: A list of the import statements that caused the cycle. Something like: error: cyclic import detected import of N at m.z:23 -> import of M at n.z:12 -> import of N at m.z:23 I've tried the following: 1. Use a DirectedGraph where modules are the vertices and import declarations are the edges. This allows me to catch imports of unknown modules by catching IllegalArgumentExceptions raised by addEdge. I then run the CycleDetector after each addition of an edge. Unfortunately, the CycleDetector can only return a set of vertices rather than the edges (so I can't directly tell the user exactly which series of import declarations caused the cycle). It also comes with a rather scary warning that it might not actually catch all cycles (and recommends the use of the StrongConnectivityInspector, but doesn't say exactly how to use it and the documentation is non-obvious). 2. Use a DirectedAcyclicGraph. This allows me to catch imports of unknown modules by checking to see if an edge could actually be inserted (the boolean return value of addDagEdge). It also raises exceptions when cycles are introduced, but this gives me even less information than the CycleDetector (it just tells me that they've happened, not why). Is there some better way to achieve what I want? My ideal scenario is that I add edges to graphs and upon adding an edge, I get a list of all the *edges and vertices* that led to a cycle in some well-defined order. M |