问题
I have interval data:
A = (0,50)
B = (20,500)
C = (80,420)
....
And realized that there's an associated graph with this data, the interval graph

I'd like to find the most efficient path to go from A to G (assume I know all of the positive vertex weights, wa, wb, wc...). I need to start at A and go to G, so the minimum spanning tree must be bound between these points. One of the constraints in our application is that the interval starting at A and ending at G must be covered in full (no gaps). I'm looking at networkX's minspanning tree method, and don't understand how to specify that A and G must be the start and endpoints.
Some other questions that come to mind are:
Since this problem is NP-hard, should I even bother looking for a min-spanning tree if the number of nodes is high? How many nodes would be too many?
Notice that interval F has a unique region. In other words, to completely cover the interval A-G, one HAS to go through F. Therefore, my minimum spanning tree probably should only connect A-F, not A-G. Is there a standard way, given a larger graph, to find all of the subgraphs whose intervals contain no unique patches? In other words, since all paths have to pass through F to get to G, A-F is the min spanning path of interest, not A-G. How does one reduce a graph in such a way without inspecting it manually?
Becasue I have to go from A-G, I would never go backwards or take a cyclic path. For example, I'd never go A-B-A. Do spanning trees incorporate this? And does this make my graph directed? Consider point C: from C one could go to D, E, or F, but never back to A (for our use case). What does this mean in regard to directionality of the graph?
Sorry for novice Q's, new to most of this.
回答1:
If you must go from A to G in an efficient way, you aren't looking for a minimum spanning tree algorithm. A simple shortest path algorithm is enough. You just have to adapt you graph to put the weights in the edges instead of the nodes. But it's just a matter of setting the node's weight to the incoming edge.
Also, both shortest path and minimum spanning tree problems aren't NP-hard. There are known polynomial algorithms for all these problems. In special, shortest path can be solved by Dijkstra's algorithm (if your graph doesn't have negative edges, which seems to be true) and minimum spanning tree can be solved by Prim's or Kruskal's algorithm.
And finally, any tree, by definition doesn't have cycles.
回答2:
As mentioned in another answer, Dijkstra's algorithm is the solution. What wasn't mentioned is how to implement that solution in networkx. Here it is. Simple as this:
import networkx as nx
my_graph = nx.Graph()
my_graph.add_edges_from([('A','B'),('B','C'),('A','C'),('C','D'),('A','D'),('C','E'),('D','E'),('D','F'),('F','G')])
#graph is now defined.
shortestpath = nx.dijkstra_path(my_graph, 'A', 'G') #optional weight argument here.
shortestpath
> ['A', 'D', 'F', 'G']
In general, more documentation on how to do the shortest path algorithms (and there are many variations thereof) in networkx is here.
Note if you have weights on nodes and you want to minimize the sum of the nodes in the path, what you do is place weights on the edges so that the weight of (u
, v
) is (w[u]+w[v])/2
.
Then run nx.dijkstra_path
with the optional argument telling networkx where to find the weight of the edges. The weight of the entire path will equal the sum of the intermediate weights, plus half the values of the end nodes. You can then correct for the end node weights.
来源:https://stackoverflow.com/questions/28333756/finding-most-efficient-path-between-two-nodes-in-an-interval-graph