0% found this document useful (0 votes)
13 views5 pages

Assignment 6 Set B

The document describes an algorithm to find the shortest paths between all pairs of vertices in a weighted graph. It defines matrices to store the shortest path lengths and predecessors. Functions are used to implement Floyd-Warshall algorithm, display matrices, find paths and create a sample graph.

Uploaded by

Black Adam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Assignment 6 Set B

The document describes an algorithm to find the shortest paths between all pairs of vertices in a weighted graph. It defines matrices to store the shortest path lengths and predecessors. Functions are used to implement Floyd-Warshall algorithm, display matrices, find paths and create a sample graph.

Uploaded by

Black Adam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 6 set B

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

#define infinity 9999


#define MAX 100

int n; // Number of vertices in the graph


int adj[MAX][MAX]; // Weighted Adjacency matrix
int D[MAX][MAX]; // Shortest Path Matrix
int Pred[MAX][MAX]; // Predecessor Matrix

void create_graph();
void FloydWarshalls();
void findPath(int s, int d);
void display(int matrix[MAX][MAX], int n);

int main()
{
int s, d;
create_graph();
FloydWarshalls();
while (1)
{
printf("Enter source vertex (-1 to exit): ");
scanf("%d", &s);
if (s == -1)
break;
printf("Enter destination vertex: ");
scanf("%d", &d);
if (s < 0 || s > n - 1 || d < 0 || d > n - 1) {
printf("Enter valid vertices\n\n");
continue;
}

printf("Shortest path is: ");


findPath(s, d);
printf("Length of this path is %d\n", D[s][d]);
}
// End of main()
}

void FloydWarshalls()
{
int i, j, k;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (adj[i][j] == 0)
{
D[i][j] = infinity;
Pred[i][j] = -1;
}
else
{
D[i][j] = adj[i][j];
Pred[i][j] = i;
}
}
}

for (k = 0; k < n; k++)


{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (D[i][k] + D[k][j] < D[i][j])
{
D[i][j] = D[i][k] + D[k][j];
Pred[i][j] = Pred[k][j];
}
}
}
}

printf("Shortest path matrix is:\n");


display(D, n);
printf("\n\nPredecessor matrix is:\n");
display(Pred, n);
for (i = 0; i < n; i++)
{
if (D[i][i] < 0)
{
printf("Error: negative cycle\n");
exit(1);
}
}
// End of FloydWarshalls()
}

void findPath(int s, int d)


{
int i, path[MAX], count = -1;
if (D[s][d] == infinity)
{
printf("No path\n");
return;
}
do
{
path[++count] = d;
d = Pred[s][d];
} while (d != s);
path[++count] = s;
for (i = count; i >= 0; i--)
printf("%d ", path[i]);
printf("\n");
// End of findPath()
}

void display(int matrix[MAX][MAX], int n)


{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%7d", matrix[i][j]);
}
printf("\n");
}
// End of display()
}

void create_graph()
{
int i, max_edges, origin, destin, wt;
printf("Enter number of vertices: ");
scanf("%d", &n);
max_edges = n * (n - 1);
for (i = 1; i <= max_edges; i++)
{
printf("Enter edge (or -1 -1 to quit): ");
scanf("%d %d", &origin, &destin);
if ((origin == -1) && (destin == -1))
break;
printf("Enter weight for this edge: ");
scanf("%d", &wt);
if (origin >= n || destin >= n || origin < 0 || destin < 0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[origin][destin] = wt;
}
}

/*
[student@localhost ~]$ gcc As6sB.c
[student@localhost ~]$ ./a.out
Enter number of vertices: 4
Enter edge (or -1 -1 to quit): 0 1
Enter weight for this edge: 2
Enter edge (or -1 -1 to quit): 0 3
Enter weight for this edge: 9
Enter edge (or -1 -1 to quit): 1 0
Enter weight for this edge: 3
Enter edge (or -1 -1 to quit): 1 2
Enter weight for this edge: 4
Enter edge (or -1 -1 to quit): 1 3
Enter weight for this edge: 7
Enter edge (or -1 -1 to quit): 2 1
Enter weight for this edge: 6
Enter edge (or -1 -1 to quit): 2 3
Enter weight for this edge: 2
Enter edge (or -1 -1 to quit): 3 2
Enter weight for this edge: 4
Enter edge (or -1 -1 to quit): 3 0
Enter weight for this edge: 14
Enter edge (or -1 -1 to quit): -1 -1
Shortest path matrix is:
5 2 6 8
3 5 4 6
9 6 6 2
13 10 4 6

Predecessor matrix is:


1 0 1 2
1 0 1 2
1 2 3 2
1 2 3 2
Enter source vertex (-1 to exit): 1
Enter destination vertex: 2
Shortest path is: 1 2
Length of this path is 4
Enter source vertex (-1 to exit): 2
Enter destination vertex: 1
Shortest path is: 2 1
Length of this path is 6
Enter source vertex (-1 to exit): 1
Enter destination vertex: 3
Shortest path is: 1 2 3
Length of this path is 6
Enter source vertex (-1 to exit): 3
Enter destination vertex: 1
Shortest path is: 3 2 1
Length of this path is 10
Enter source vertex (-1 to exit): 3
Enter destination vertex: 2
Shortest path is: 3 2
Length of this path is 4
Enter source vertex (-1 to exit): 2
Enter destination vertex: 0
Shortest path is: 2 1 0
Length of this path is 9
Enter source vertex (-1 to exit): -1
[student@localhost ~]$

You might also like