Dijkstra's Algorithm Matrix

 


#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define MAX 100

#define INF INT_MAX

int adj[MAX][MAX];

int dist[MAX];

int pred[MAX];

int visited[MAX];

void create_graph(int n, int gtype);

void dijkstra(int n, int source);

void print_shortest_path(int source, int destination);

int main() {

    int n, gtype, source;

    printf("Enter 1 for undirected graph and -1 for directed graph: ");

    scanf("%d", &gtype);

    printf("Enter the number of vertices: ");

    scanf("%d", &n);

    create_graph(n, gtype);

    printf("Enter the source vertex: ");

    scanf("%d", &source);

    dijkstra(n, source);

    for (int i = 0; i < n; i++) {

        if (i != source) {

            printf("Shortest path from vertex %d to vertex %d: ", source, i);

            print_shortest_path(source, i);

            printf("\n");

        }

    }

    return 0;

}

void create_graph(int n, int gtype) {

    int max_edges, src, dest, weight;

    if (gtype == 1)

        max_edges = (n * (n - 1)) / 2;

    else if (gtype == -1)

        max_edges = n * (n - 1);

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

            if (i == j) {

                adj[i][j] = 0;

            } else {

                adj[i][j] = INF;

            }

        }

    }

    for (int i = 1; i <= max_edges; i++) {

        printf("Enter edge %d (source, destination, weight) (to quit enter -1 -1 -1): ", i);

        scanf("%d%d%d", &src, &dest, &weight);

        if (src == -1 && dest == -1 && weight == -1)

            break;

        if (src < 0 || dest < 0 || src >= n || dest >= n) {

            printf("Invalid source or destination!\n");

            i--;

        } else {

            adj[src][dest] = weight;

            if (gtype == 1) adj[dest][src] = weight;

        }

    }

    printf("Adjacency Matrix is:\n");

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++)

            printf("%4d", adj[i][j] == INF ? 0 : adj[i][j]);

        printf("\n");

    }

}

void dijkstra(int n, int source) {

    int count, mindist, nextnode;

    for (int i = 0; i < n; i++) {

        dist[i] = adj[source][i];

        pred[i] = source;

        visited[i] = 0;

    }

    dist[source] = 0;

    visited[source] = 1;

    count = 1;

    while (count < n - 1) {

        mindist = INF;

        for (int i = 0; i < n; i++) {

            if (dist[i] < mindist && !visited[i]) {

                mindist = dist[i];

                nextnode = i;

            }

        }

        visited[nextnode] = 1;

        for (int i = 0; i < n; i++) {

            if (!visited[i] && adj[nextnode][i] != INF) {

                if (dist[nextnode] + adj[nextnode][i] < dist[i]) {

                    dist[i] = dist[nextnode] + adj[nextnode][i];

                    pred[i] = nextnode;

                }

            }

        }

        count++;

    }

}

void print_shortest_path(int source, int destination) {

    if (destination == source) {

        printf("%d", source);

    } else if (pred[destination] == source) {

        printf("%d -> %d ", source, destination);

    } else {

        print_shortest_path(source, pred[destination]);

        printf("-> %d ", destination);

    }

}


Comments

Popular posts from this blog

AVL Trees

BFS Matrix

BFS List