-
Notifications
You must be signed in to change notification settings - Fork 184
Conversation
I'd be interested in the benchmarks. You're essentially doing two traversals here: the first in |
Comparison with Dijkstra. function make_dag(;breadth, len_chain)
sz = breadth * len_chain + 1
dag = LightGraphs.SimpleGraphs.SimpleDiGraph(sz)
mat = spzeros(Int, sz, sz)
for v in 1:breadth
add_edge!(dag, sz, v)
mat[sz, v] = 1
end
for v in breadth+1:sz-1
add_edge!(dag, v-breadth, v)
mat[v-breadth, v] = 1
end
return (dag, mat)
end This gives the following type graph (for breadth=10, len_chain=3): image Tested with sizes from 1K-10K: djk = LightGraphs.ShortestPaths.Dijkstra()
dag_topo = LightGraphs.ShortestPaths.DagTopo()
for b in 1000:1000:10000
(dag, mat) = make_dag(breadth=b, len_chain=10);
println("sz=", nv(dag))
println(" dijkstra:")
@btime LightGraphs.ShortestPaths.shortest_paths(dag, nv(dag), mat, djk);
println(" dag-topo:")
@btime LightGraphs.ShortestPaths.shortest_paths(dag, nv(dag), mat, dag_topo);
end
|
We can optimize dag-topo by writing our own topological sort where we only visit vertices reachable from the source(s). Traversals.topological_sort() visits all the vertices. |
Ah. The issue with this code being in LightGraphs proper is that it depends on being passed a DAG. As I mentioned in the related issue, it's probably better if you create a separate specialized graph type that is ONLY a DAG so that we don't get errors when we try to feed it a general AbstractGraph:
In general within LightGraphs, we don't want to produce errors like this. We'd much rather throw a |
@sbromberger I thought about this and although I agree that a separate DAG type would be nice, I would like to make the case that this algo should be included in
Thoughts? |
I think that throwing an exception in the topological sort is a fine way to handle this. Since the algorithm is not being set as the default and has "Dag" in the name, I think we can rely on people to ensure that their graph is a DAG or catch the exception if they aren't sure. The exception should tell people to use Dijkstra's for graphs that can contain cycles. So far we have used the introduction of dependencies to scope the boundaries of LG. Since this just uses two features we already have (toposort and traversal), I think it is good to include. A bike shedding issue is that DAG is an abbreviation so it should probably be all caps. |
I'm catching the |
I'm not sure I agree, especially given the discussion in the related issue about the usefulness of storing additional topological data in the DAG data structure. If that's beneficial - and I think it is - then this argues for a separate package that handles DAGs, just like we have for weighted graphs, metagraphs, vertex-safe graphs, and what we've always talked about regarding random graphs and small graphs (I think @matbesancon actually implemented a few of these, with O(1) memory for small graphs). |
Oh, I wasn't aware of that. TIL. What would you suggest as an alternative in a case like this?
Yeah that does make sense. |
@kanishk509 have you decided on an approach here? If you wanted to create a DAG graph type in a new package and implement the LightGraphs API, we could transfer it into the JuliaGraphs organization and make you the maintainer. |
Hey @sbromberger, I did sketch out roughly how I would want to implement a DAG:
Here we would construct a topo-order on the creation of the graph and set the The project has been on my to-do list for a while. However, I haven't been able to devote much time to it because of an ongoing internship. I was wondering if JuliaGraphs would be interested in participating in GSoC'21? If so and if this project is not currently urgent, do you think this would make a good project proposal? (I'm a CS undergrad and eligible to apply). I realize that it's still a few months off (student applications start in March), but I would be able to properly work on this then as my internship would be wrapped up by then. Let me know what you think. Cheers. |
That looks good, but I wonder if you can avoid the extra memory. That is, you're carrying the full graph around, and then a topological ordering on the graph, that will be 2V + E. Are there more space-efficient ways of storing a DAG? As far as GSOC, I've been a GSOC mentor the past 4-5 years (when Julia has gotten slots), but this year I won't have time to do it. There are others here who have also been GSOC mentors, though. It's probably worth discussing, and a proposal seems like a good idea. |
Codecov Report
@@ Coverage Diff @@
## master #1486 +/- ##
==========================================
- Coverage 92.66% 92.35% -0.31%
==========================================
Files 195 196 +1
Lines 6325 6345 +20
==========================================
- Hits 5861 5860 -1
- Misses 464 485 +21 |
Also, just a thought:
You should give the user the option. Lazy recalculation in library code could create nondeterministic behavior in end-user code. |
@sbromberger Thanks for the pointers, I'm making a note of them. What would be the proper place to ask about GSoC participation and whether someone would be interested in mentoring such a project? Should I post it on the Gitter channel? |
Sorry about this - this PR was closed because we renamed some branches. Would you mind rebasing on |
#1485
Uses topo sort + DP to find shortest paths in a DAG.
I'll work on writing test cases benchmarks soon.