Dijkastra

 Dijkstra by vishwakaarma




#include <stdio.h>
#include <stdlib.h>

int calculateMinDistance(int cost[10][10], int distance[10], int path[10][10], int n, int v, int p, int row, int column);

int main()
{
    int cost[10][10], distance[10], path[10][10], n, p, i, j, v, min, row, column, index = 1;
    printf("enter the nodes:");
    scanf("%d", &n);

    printf("enter the cost matrix:");
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            scanf("%d", &cost[i][j]);
        }
    }

    printf("enter the node to visit:");
    scanf("%d", &v);

    printf("enter the path :");
    scanf("%d", &p);

    printf("enter the path matrix: ");
    for (i = 1; i <= p; i++)
    {
        for (j = 1; j <= n; j++)
        {
            scanf("%d", &path[i][j]);
        }
    }

    min = calculateMinDistance(cost, distance, path, n, v, p, 1, 0);

    printf("the min distance is %d\n", min);
    printf("the distance path is: ");
    for (i = 1; i <= n; i++)
    {
        if (path[index][i] != 0)
            printf("---->%d", path[index][i]);
    }

    return 0;
}

int calculateMinDistance(int cost[10][10], int distance[10], int path[10][10], int n, int v, int p, int row, int column)
{
    if (row == v)
    {
        return 0;
    }

    distance[p] += cost[row][column];

    int newColumn = path[p][column + 1];

    return distance[p] + calculateMinDistance(cost, distance, path, n, v, p, newColumn, column + 1);
}

Comments

Popular posts from this blog

what is Machenical Engineering

PHOTO ( CHINESE LADKA)

Arithmatic operations, factorial of a number, while loop, prime number, etc