简体   繁体   中英

modifying Dijkstra’s algorithm to undirected graph

Question: Adapt Dijkstra's algorithm to solve the SSSP problem on a weighted undirected graph.

surely there's no need to modify the algorithm? if the graph is undirected then its just a directed graph with edges both ways, right?

Yes, Dijkstra's algorithm works for both types of graphs, and in the undirected case you just allow to use an edge from both end points.

If your implementation works with graphs given by an adjacency list, then this information is already implicitly given by this data structure: in the undirected case you list, for an edge (u,v), u in the adjacency of v and v in the adjacency of u, which gives you both directions. So you can use the same implementation for both types of graphs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM