问题
I am trying to solve this question but got stuck.
Need some help,Thanks.
Given an undirected Connected graph G with non-negative values at edges.
Let A be a subgroup of V(G), where V(G) is the group of vertices in G.
-Find a pair of vertices (a,b) that belongs to A, such that the weight of the shortest path between them in G is minimal, in O((E+V)*log(v)))
I got the idea of using Dijkstra's algorithm in each node which will give me O(V*((E+V)logv))),which is too much.
So thought about connecting the vertices in A somehow,did'nt find any useful way.
Also tried changing the way Dijkstra's algorithm work,But it get's to hard to prove with no improvment in time complexity.
回答1:
Note that if the optimal pair is (a, b)
, then from every node u
in the optimal path, a
and b
are the closest two nodes in A
.
I believe we should extend Dijkstra's algorithm in the following manners:
- Start with all nodes in
A
, instead of a singlesource_node
. - For each node, don't just remember the
shortest_distance
and theprevious_node
, but also theclosest_source_node
to remember which node inA
gave the shortest distance. - Also, for each node, remember the
second_shortest_distance
, thesecond_closest_source_node
, andprevious_for_second_closest_source_node
(shorter name suggestions are welcome). Make sure thatsecond_closest_source_node
is never theclosest_source_node
. Also, think carefully about how you update these variables, the optimal path for a node can become part of the second best path for it's neighbour. - Visit the entire graph, don't just stop at the first node whose
closest_source
andsecond_closest_source
are found. - Once the entire graph is covered, search for the node whose
shortest_distance + second_shortest_distance
is smallest.
来源:https://stackoverflow.com/questions/48037215/find-two-vertices-with-lowest-path-weight