简体   繁体   中英

Directed graph with non negative weights (adjacency matrix)

图形

邻接矩阵

First off, I apologize for my bad paint drawing of the graph. The weights are obviously not scaled. I am having a hard time coming up with the algorithms to solve a few problems.

First, I want to find all paths that take 3 "stops" or less from C to C (just an example... can be any two vertexes). After researching, I found that BFS might be what I'm looking for to solve this problem. Am I correct in this assumption?

There are two paths that have 3 stops or less:

C -> D -> C

C -> E -> B -> C

I also want to find the shortest path from A to C (just an example.. can be any two vertexes). After doing a little bit of research, I came to the conclusion that I should use Dijkstra's algorithm. Am I correct in this assumption? If so, I saw that there are various implementations. Does it matter if I use binary heap, fibonacci heap, or queue?

Thank you and please let me know if you need any more information!

First, I want to find all paths that take 3 "stops" or less from C to C (just an example... can be any two vertexes). After researching, I found that BFS might be what I'm looking for to solve this problem. Am I correct in this assumption?

Yes, you are. The property of BFS is that processes nodes in level-order, therefore you first process all nodes that are neighbors of the source node, then the nodes that are neighbors of neighbors of the source node etc.

I also want to find the shortest path from A to C (just an example.. can be any two vertexes). After doing a little bit of research, I came to the conclusion that I should use Dijkstra's algorithm. Am I correct in this assumption? If so, I saw that there are various implementations. Does it matter if I use binary heap, fibonacci heap, or queue?

Again, yes, Dijkstra's algorithm is a classic solution for such problems. There are other algorithms better suited for some special situations (eg Bellman-Ford if you have negative weights in your graph), but in most cases (yours as well), go with Dijkstra. Regarding the implementation, theoretically the best implementation is based on a min-priority queue implemented by a Fibonacci heap. The running time of this implementation is O(|E|+|V|/log|V|) (where |V| is the number of vertices and |E| is the number of edges). However, in practice, binary heaps often outperform Fibonacci heaps, see this interesting thread for more information.

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