[英]Dijkstra's Algorithm - Infinite Loop
作為一項家庭作業,我將使用指向每個頂點的鏈表的指針數組來實現鄰接表。 每個鏈表都有一個元素<destination>
,它聲明鄰接表頂點的頂點鄰居。
鄰接表是無方向性和無權重的,因此我將所有權重都視為1。
/* Adjacency List Node data structure (edge)
* Linked List data structure for storing linked vertices
*/
struct adjacencyListNode
{
int destination;
struct adjacencyListNode *next;
};
/* Adjacency List Vertex data structure (vertex)
* <AdjacencyList> consists of pointers to <n> adjacencyListVertex
*/
struct adjacencyListVertex
{
struct adjacencyListNode *head;
};
我正在嘗試在鄰接表上執行Dijkstra算法,以查找從s到t的最小路徑。
現在,我正在實現以下算法:
/* Prints the length and path taken of the shortest path in adjacency list between s and t.
* Uses Dijkstra’s algorithm to compute shortest path.
* S: source vertex
* V: destination vertex
*/
void shortestPath(int s, int t) {
int known[size]; // shortest distance to vertex is know
int cost[size]; // distance from source <s> to each vertex
int path[size]; //path
// Initialization: Set all distances to infinity (represented by -1), since arrays have not been visited and graph is positively weighted
for (int index = 0; index<size; index++) {
cost[index] = INFINITY;
known[index] = 0;
}
// Set distance from source->source to 0
cost[s-1] = 0;
// Starting at s, traverse towards all reachable unvisited verticies, visit it and repeat
while (isFinished(known, size) == false) {
// Select a vertex from list of unvisited nodes which has the smallest cost
int cheapestVertex, cheapestValue = INFINITY+1;
for (int costCheck = 0; costCheck<size; costCheck++) {
if ((known[costCheck] == 0) && (cost[costCheck] < cheapestValue)) {
// We found a cheaper unvisited vertex
// cout << "Cheapest vertex: " << costCheck << endl;
cheapestVertex = costCheck;
cheapestValue = cost[cheapestVertex];
}
// cout << "found? " << cheapestVertex << " " << cheapestValue << endl;
}
// cout << "Cheapest vertex: " << cheapestVertex << endl;
// For each unvisited neighbor of our cheapest (unvisited) vertex
adjacencyListNode* iterator = A[cheapestVertex].head; // iterator is our first neighbor
while (iterator)
{
// Calculate the new cost from the current vertex <cheapestVertex>
if (cost[cheapestVertex]+1 < cost[iterator->destination] && known[iterator->destination] == 0) {
cost[iterator->destination] = cost[cheapestVertex]+1;
}
iterator = iterator->next; // move to next neighbor, repeat
}
// cout << "Cheapest vertex: " << cheapestVertex << " known." << endl;
// Mark the current vertex <cheapestVertex> as visited
known[cheapestVertex] = 1;
// DEBUG: (REMOVE BEFORE SUBMISSION)
for (int i = 0; i<size; i++) {
cout << "Vertex " << i << " : known? " << known[i] << ", cost? " << cost[i] << endl;
}
cout << endl;
if (cost[t-1] != INFINITY) break; // We already know shortest path, end.
}
// We know the shortest path cost to t
cout << "Cost to t: " << cost[t] << endl;
}
bool isFinished(int array[], int arraySize) {
bool finished = true;
for (int iterator=0; iterator < arraySize; iterator++) {
if (array[iterator] == 0) {
// vertex not known, we're not done.
finished = false;
}
}
return finished;
}
我正在傳遞以下輸入,該輸入僅添加所述的相關頂點並調用我的最短路徑算法。
0 1
1 2
1 3
2 4
3 5
5 38
6 7
6 10
8 9
11 12
12 13
12 15
12 21
13 14
14 15
16 17
17 18
18 19
19 20
20 39
21 22
22 23
22 31
23 24
23 32
24 25
24 33
25 26
26 27
27 28
28 29
29 30
31 40
34 35
34 37
35 36
36 37
1
shortest-path
我的代碼從0-> 1-> 2-> 3-> 4-> 5-> 38遍歷,然后無限重復38。
有人看到我的問題在哪里嗎?
你有幾個問題。 由於這是家庭作業,因此我不會為您提供完整的答案。
問題1:如果存在無法從s到達的節點怎么辦? 這就是您的示例中發生的情況。
提示:您需要確定何時停止循環(已經停止的循環除外)。 看一下您最便宜的選擇-您將如何確定沒有一個有效的選擇?
提示#2-如果所有剩余頂點的成本為INFINITE
,則當前循環將不會為cheapestVertex
設置值,因此您將使用未初始化的值。 也許在繼續之前檢查一下您發現的最便宜的費用是多少。
問題2: cost[iterator->destination] = cost[cheapestVertex]+1;
提示:您確定在每種情況下都這樣做是正確的嗎? 如果該節點已經具有較低的成本或已被訪問該怎么辦?
問題3:您可以停止尋找,一旦你t
知道。 無需檢查整個圖形。 注意:這是您不必要的更改,因為如果沒有它,代碼將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.