Reputation: 612
I have no training in graph theory, so my terminology is poor. I have a directed tree graph that has "redundant nodes." I am defining "redundant nodes" as those with degree=2 in my tree graph. I would like to find an efficient way to return all the paths between all non-redundant nodes, preferably using NetworkX (Python) tools. This really simple graphic demonstrates what I'm trying to achieve:
So given this graph, I'd like to return three paths (p1, p2, and p3) that represent the connections between 1->4, 5->4, and 4->7.
I can write an algorithm to do this "manually", in the sense that I start at nodes with degree=1 and "walk" along the graph until I hit another non-degree=2 node. However, I suspect that there is already a formalized way to do this kind of analysis, but I can't seem to figure it out.
For more context, my graphs are much larger and more complicated as they're representations of river networks, something like this:
But they're always trees, no cycles.
Upvotes: 1
Views: 353
Reputation: 20616
LOOP over all nodes
calculate degree
IF degree == 2
add to vector deg2
LOOP over all nodes in deg2 to get src
LOOP over all nodes in deg2 starting at src+1 to get dst
find path from src to dst ( dijsktra algorithm )
Upvotes: 1
Reputation: 77910
No, I'm afraid that you've already hit the most effective way to do your mini-paths. You can speed up the processing slightly by working backward from a confluence node, but that's about all you can improve. Doing so will let you remove intermediate nodes a little more effectively than simply looking for source nodes (which you still have to do). However, that algorithm is not as simple. For now, I suggest that you stick with the simple design you already have.
to_visit
.to_visit
is not empty:
to_visit
.Upvotes: 2