Trent
Trent

Reputation: 4306

Dijkstra's Algorithm - Infinite Loop

As a homework assignment, I am to implement an adjacency list using an array of pointers to linked lists for each vertex. Each linked list has an element <destination> that states the vertex neighbor of adjacency list vertex.

The adjacency list is undirected and unweighted, so I am treating all weights as 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;
};

I am trying to perform Dijkstra's Algorithm on the adjacency list to find the minimum path from s to t.

Right now I am implementing the following algorithm:

/* 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;
}

I am passing the following input, which just adds the stated related vertices and calls my shortest-path algorithm.

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

My code traverses from 0->1->2->3->4->5->38 and then repeats 38 infinitely.

Does anyone see where my issue is?

Upvotes: 1

Views: 1549

Answers (1)

The Dark
The Dark

Reputation: 8514

You have a few issues. As this is homework, I won't give you the full answers.

Issue 1: What happens if there are nodes that are unreachable from s? This is what is happening in your example.

Hint: You need to work out when to stop the loop (other than the one you have already). Look at your cheapest selection - how would you determine that there isn't a valid one?

Hint #2 - You current loop won't set a value for cheapestVertex if all remaining vertices have a cost of INFINITE, so you will be using an uninitialized value. Maybe check what the cheapest cost you found was before proceeding.

Issue 2: cost[iterator->destination] = cost[cheapestVertex]+1;

Hint: are you sure this is correct to do on every occasion? What if the node already has a cheaper cost, or has already been visited?

Issue 3: You can stop looking once you have t known. No need to check the whole graph. Note: This is an change that you don't necessarily need as your code will work without it.

Upvotes: 1

Related Questions