Dijkstra's Algorithm

Reference:

  1. GeeksforGeelks: Greedy Algorithms | Set 7 (Dijkstra’s shortest path algorithm)
  2. 演算法筆記: Path

Example:

Cowman

#include <limits.h>

#define SIZE 9

int w[SIZE][SIZE] = {   { 0, 4, 0, 0, 0, 0, 0, 8, 0 },        // 0    : can't reach this vertex
                        { 4, 0, 8, 0, 0, 0, 0, 11, 0 },        // >0    : cost of reach 
                        { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
                        { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
                        { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
                        { 0, 0, 4, 0, 10, 0, 2, 0, 0 },
                        { 0, 0, 0, 14, 0, 2, 0, 1, 6 },
                        { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
                        { 0, 0, 2, 0, 0, 0, 6, 7, 0 }};

int minDistance(int dist[], bool sptSet[])
{
    int min = INT_MAX, min_index;

    for (int v = 0; v < SIZE; v++)
        if (!sptSet[v] && dist[v] <= min) {
            min = dist[v];
            min_index = v;
        }
    return min_index;
}

void find_path(int *parent, int x)                            // print all shortest paths from source vertex to x
{
    if (x != parent[x]) {                                    // recursive until find source vertex
        find_path(parent, parent[x]);
        printf(" -> ");
    }
    printf("%d", x);                                        
}

void printSolution(int dist[], int *parent, int nSize, int src)
{
    printf("Source Vertex\tTarget Vertex\tDistance from Source\tShortest Path\n");
    for (int i = 0; i < nSize; i++) {
        printf("%d\t\t%d\t\t%d\t\t\t", src, i, dist[i]);
        find_path(parent, i);
        printf("\n");
    }
}

void dijkstra(int graph[SIZE][SIZE], int src)
{
    int parent[SIZE];                                        //record shortest path
    int dist[SIZE];                                            // The output array.  dist[i] will hold the shortest distance from src to i
    bool sptSet[SIZE];                                        // sptSet[i] will true if vertex i is included in shortest
                                                            // path tree or shortest distance from src to i is finalized

    for (int i = 0; i < SIZE; i++)                            // Initialize all distances as INFINITE and stpSet[] as false
        dist[i] = INT_MAX, sptSet[i] = false;

    dist[src] = 0;                                            //set dist of source vertex as zero, Distance of source vertex from itself is always 0
    parent[src] = src;                                        //set parent of source vertex as itself

    // Find shortest path for all vertices
    for (int count = 0; count < SIZE - 1; count++)
    {
        int u = minDistance(dist, sptSet);                    // Pick the minimum distance vertex from the set of vertices not
                                                            // yet processed. u is always equal to src in first iteration.

        sptSet[u] = true;                                    // Mark the picked vertex as processed

        // Update dist value of the adjacent vertices of the picked vertex.
        for (int v = 0; v < SIZE; v++) {
            // Update dist[v] only if is not in sptSet, there is an edge from 
            // u to v, and total weight of path from src to  v through u is 
            // smaller than current value of dist[v]
            if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
                && dist[u] + graph[u][v] < dist[v]) {
                dist[v] = dist[u] + graph[u][v];
                parent[v] = u;
            }
        }
    }

    // print the constructed distance array and shortest path
    printSolution(dist, parent, SIZE, src);
}

int main()
{
    int source;

    source = 0;
    dijkstra(w, source);

    return 0;
}

output:

source vertex = 0

Source Vertex   Target Vertex   Distance from Source    Shortest Path
0               0               0                       0
0               1               4                       0 -> 1
0               2               12                      0 -> 1 -> 2
0               3               19                      0 -> 1 -> 2 -> 3
0               4               21                      0 -> 7 -> 6 -> 5 -> 4
0               5               11                      0 -> 7 -> 6 -> 5
0               6               9                       0 -> 7 -> 6
0               7               8                       0 -> 7
0               8               14                      0 -> 1 -> 2 -> 8

source vertex = 8

Source Vertex   Target Vertex   Distance from Source    Shortest Path
8               0               14                      8 -> 2 -> 1 -> 0
8               1               10                      8 -> 2 -> 1
8               2               2                       8 -> 2
8               3               9                       8 -> 2 -> 3
8               4               16                      8 -> 2 -> 5 -> 4
8               5               6                       8 -> 2 -> 5
8               6               6                       8 -> 6
8               7               7                       8 -> 7
8               8               0                       8

results matching ""

    No results matching ""