Knowee
Questions
Features
Study Tools

write a code in c to find the shortest distance between a given source and destination nodes and print the shortest path between the source and destination nodes using floyds algorithm

Question

write a code in c to find the shortest distance between a given source and destination nodes and print the shortest path between the source and destination nodes using floyds algorithm

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step guide to writing a code in C to find the shortest distance between a given source and destination nodes using Floyd's algorithm and print the shortest path:

Step 1: Include the necessary header files:

#include <stdio.h>
#include <limits.h>

Step 2: Define the maximum number of vertices in the graph:

#define MAX_VERTICES 100

Step 3: Define a function to find the shortest distance and path using Floyd's algorithm:

void floyd(int graph[MAX_VERTICES][MAX_VERTICES], int numVertices, int source, int destination) {
    int dist[MAX_VERTICES][MAX_VERTICES];
    int path[MAX_VERTICES][MAX_VERTICES];

    // Initialize the distance and path matrices
    for (int i = 0; i < numVertices; i++) {
        for (int j = 0; j < numVertices; j++) {
            dist[i][j] = graph[i][j];
            if (graph[i][j] != INT_MAX && i != j) {
                path[i][j] = i;
            } else {
                path[i][j] = -1;
            }
        }
    }

    // Perform Floyd's algorithm
    for (int k = 0; k < numVertices; k++) {
        for (int i = 0; i < numVertices; i++) {
            for (int j = 0; j < numVertices; j++) {
                if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                    path[i][j] = path[k][j];
                }
            }
        }
    }

    // Print the shortest distance and path
    printf("Shortest distance between node %d and node %d: %d\n", source, destination, dist[source][destination]);
    printf("Shortest path: ");
    int node = destination;
    printf("%d ", node);
    while (node != source) {
        node = path[source][node];
        printf("%d ", node);
    }
    printf("\n");
}

Step 4: Write the main function to test the code:

int main() {
    int numVertices, source, destination;
    printf("Enter the number of vertices in the graph: ");
    scanf("%d", &numVertices);

    int graph[MAX_VERTICES][MAX_VERTICES];
    printf("Enter the adjacency matrix of the graph:\n");
    for (int i = 0; i < numVertices; i++) {
        for (int j = 0; j < numVertices; j++) {
            scanf("%d", &graph[i][j]);
            if (graph[i][j] == 0 && i != j) {
                graph[i][j] = INT_MAX;
            }
        }
    }

    printf("Enter the source node: ");
    scanf("%d", &source);
    printf("Enter the destination node: ");
    scanf("%d", &destination);

    floyd(graph, numVertices, source, destination);

    return 0;
}

That's it! You can now compile and run the code to find the shortest distance between the given source and destination nodes using Floyd's algorithm and print the shortest path.

This problem has been solved

Similar Questions

give only python code for Floyd Warshall Algorithm Given a graph in adjacency matrix from. Find shortest distances between every pair of vertices in a given edge weighted directed Graph. If there is no path from a source to a destination, print -1 at that place. Input Format: First line - n, the number of nodes Then there are n lines each containing n integers, weight from source(i.e. row number) to destination(i.e. column number). Output Format: Print shortest distances between every pair of vertices. If there is no path from a source to a destination, print -1 at that place. Sample Input: 4 0 5 INF 10 INF 0 3 INF INF INF 0 1 INF INF INF 0 Sample Output: 0 5 8 9 INF 0 3 4 INF INF 0 1 INF INF INF 0

Write and explain the Floyd Warshall algorithm to find the all pair shortest path.

Floyd Warshall Algorithm used to solve the shortest path problem has a time complexity of __________

Floyd-Warshall algorithm utilizes __________ to solve the all-pairs shortest paths problem on a directed graph in __________ time.

The shortest path problem can be solved using:A. Depth-first searchB. Breadth-first searchC. Dijkstra's algorithmD. Floyd-Warshall algorithm

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.