LTCMiner cloud mining
Posts

ADA Program 3A & 3B

Program 3a — Floyd's Algorithm

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

int main(void) {
    int N, i, j, k;
    int A[10][10], Cost[10][10];
    printf("Enter the number of vertices \n");
    scanf("%d",&N);
    printf("Enter the Cost adjacency Matrix \n");
    for(i=0;i<N;i++)
        for(j=0;j<N;j++)
            scanf("%d",&Cost[i][j]);
    
    printf("\n Input Graph\n");
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++)
            printf("%d\t",Cost[i][j]);
        printf("\n");
    }
    
    printf("\nAdjacency matrix (Cost Matrix) \n");
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++)
            A[i][j] = Cost[i][j];
    }
    
    for(k=0;k<N;k++) {
        for(i=0;i<N;i++) {
            for(j=0;j<N;j++) {
                if(A[i][j] > (A[i][k] + A[k][j]))
                    A[i][j] = (A[i][k] + A[k][j]);
            }
        }
    }
    
    printf("All Pair Shortest Path Matrix \n");
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++)
            printf("%d \t",A[i][j]);
        printf("\n");
    }
    printf("\n");
    return 0;
}
OUTPUT:


Program 3b — Warshall's Algorithm

C
#include <stdio.h>

#define MAX 100

void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert);

int main(void) {
    int i, j, n;
    int graph[MAX][MAX];
    printf("Warshall's Transitive Closure \n");
    printf("Enter the number of vertices : ");
    scanf("%d",&n);
    printf("Enter the adjacency matrix :\n");
    for (i=0; i<n; i++)
        for (j=0; j<n; j++)
            scanf("%d",&graph[i][j]);
    WarshallTransitiveClosure(graph, n);
    return 0;
}

void WarshallTransitiveClosure(int graph[MAX][MAX], int n) {
    int i,j,k;
    for (k=0; k<n; k++) {
        for (i=0; i<n; i++) {
            for (j=0; j<n; j++) {
                if (graph[i][j] || (graph[i][k] && graph[k][j]))
                    graph[i][j] = 1;
            }
        }
    }
    
    printf("The transitive closure for the given graph is :\n");
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            printf("%d \t",graph[i][j]);
        }
        printf("\n");
    }
}
OUTPUT:
Enter the number of vertices : 4
Enter the adjacency matrix :-
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
The transitive closure for the given graph is :-
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1

Getting Info...
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.