昨天我研究了Dijktra的算法来找到最短的路径。我尝试了一些问题,以便我可以对它有一些命令。但是我遇到了这个问题,要求它找到从源到目标的最短路径中的节点,如果它们是从源到目标的路径,则打印"不可能" 。即使经过多次思考我也无法解决这个问题。任何人都可以帮忙吗?
答案 0 :(得分:0)
Djikstra的算法可能在概念上易于理解,但需要付出相当大的努力才能实施。您需要数据结构(例如优先级队列)来有效地存储和检索权重最低的节点。该算法是具有贪心算法概念的BFS导数。
我使用 C ++ STL Set datastructure 而不是优先级队列来存储权重最小的节点(请检查设置数据结构详细信息http://www.cplusplus.com/reference/set/set/set/)。
请记住,此代码仅适用于具有正权重的边。 对于负重的边缘,您需要学习 Bellman-Ford算法 。
计划代码:
#include<set>
#include<iostream>
#include<stdlib>
#include<vector>
#define INF 9999999
using namespace std;
struct vw{
int index;
bool visited=false;
int w=INF;
};
int adj[10][10], v, e, visited[10];
class comparisonClass{
public:
bool operator()(struct vw a, struct vw b){
return (a.w < b.w);
}
};
int main(){
cin>>v>>e;
// Enter the number of edges & vertices.
struct vw *vertex = new struct vw[v];
for(int i=0; i<e; i++){
char a, b;
int w;
cin>>a>>b>>w;
// Enter egde and weights: Eg- A B 5 B D 20
cout<<a<<" "<<b<<" "<<w<<endl;
adj[a-65][b-65]=adj[b-65][a-65]=w;
vertex[i].index=i;
}
set<struct vw, comparisonClass> weightList;
vertex[0].w=0; vertex[0].visited=true;
weightList.insert(vertex[0]);
while(!weightList.empty()){
struct vw source = *(weightList.begin());
weightList.erase(weightList.begin());
int ind = source.index;
source.visited=true;
for(int i=0; i<v; i++){
if(adj[ind][i] && vertex[i].visited==false)
if(adj[ind][i]+source.w < vertex[i].w){
vertex[i].w = adj[ind][i] + source.w;
weightList.insert(vertex[i]);
}
}
}
for(int i=0; i<v; i++)
cout<<"\nA to "<<(char)(65+i)<<" cost="<<vertex[i].w<<endl;
// Printed results in format of Edges and least weight, Eg- A to D cost = 12.
delete[]vertex;
return 0;
}
答案 1 :(得分:-2)
这很容易,请看Wikipedia page有伪代码
1 function Dijkstra(Graph, source):
2
3 dist[source] ← 0 // Distance from source to source
4 prev[source] ← undefined // Previous node in optimal path initialization
5
6 for each vertex v in Graph: // Initialization
7 if v ≠ source // Where v has not yet been removed from Q (unvisited nodes)
8 dist[v] ← infinity // Unknown distance function from source to v
9 prev[v] ← undefined // Previous node in optimal path from source
10 end if
11 add v to Q // All nodes initially in Q (unvisited nodes)
12 end for
13
14 while Q is not empty:
15 u ← vertex in Q with min dist[u] // Source node in first case
16 remove u from Q
17
18 for each neighbor v of u: // where v is still in Q.
19 alt ← dist[u] + length(u, v)
20 if alt < dist[v]: // A shorter path to v has been found
21 dist[v] ← alt
22 prev[v] ← u
23 end if
24 end for
25 end while
26
27 return dist[], prev[]
28
29 end function
如果我们只对顶点源和目标之间的最短路径感兴趣,我们可以在第15行后终止搜索,如果u = target。现在我们可以通过反向迭代读取从源到目标的最短路径:
1 S ← empty sequence
2 u ← target
3 while prev[u] is defined: // Construct the shortest path with a stack S
4 insert u at the beginning of S // Push the vertex onto the stack
5 u ← prev[u] // Traverse from target to source
6 end while