-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd.c
125 lines (106 loc) · 3.38 KB
/
floyd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define INF 99999
void printSolution(int n, int **dist);
void printPath(int **path, int i, int j);
void floydWarshall(int n, int **graph) {
int **dist, **path;
int i, j, k;
// Allocate memory for the distance and path matrices
dist = (int **)malloc(n * sizeof(int *));
path = (int **)malloc(n * sizeof(int *));
for (i = 0; i < n; i++) {
dist[i] = (int *)malloc(n * sizeof(int));
path[i] = (int *)malloc(n * sizeof(int));
for (j = 0; j < n; j++) {
dist[i][j] = graph[i][j]; // Initialize the distance same as graph
if (i == j || graph[i][j] == INF)
path[i][j] = -1;
else
path[i][j] = i;
}
}
// Implementing the Floyd-Warshall algorithm
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (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 matrix
printSolution(n, dist);
// Print the shortest paths and their costs
//printf("\nThe shortest paths from each vertex to each other vertex along with their costs:\n");
//for (i = 0; i < n; i++) {
//for (j = 0; j < n; j++) {
//if (i != j && dist[i][j] != INF) {
//printf("Shortest path from %d to %d with cost %d: ", i, j, dist[i][j]);
//printPath(path, i, j);
//printf("%d\n", j); // Destination vertex
//}
//}
//}
// Free the dynamically allocated memory
for (i = 0; i < n; i++) {
free(dist[i]);
free(path[i]);
}
free(dist);
free(path);
}
void printSolution(int n, int **dist) {
printf("The following matrix shows the shortest distances between every pair of vertices:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}
// Function to print the shortest path from i to j
void printPath(int **path, int i, int j) {
if (path[i][j] == i)
return;
printPath(path, i, path[i][j]);
printf("%d -> ", path[i][j]);
}
int main() {
int n, **graph;
clock_t start, end;
double cpu_time_used;
printf("Enter the number of vertices: ");
scanf("%d", &n);
// Allocate memory for graph
graph = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++) {
graph[i] = (int *)malloc(n * sizeof(int));
}
printf("Enter the adjacency matrix (use %d to represent infinity):\n", INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
graph[i][j] = INF; // No path
}
}
}
start = clock(); // Start time
floydWarshall(n, graph);
end = clock(); // End time
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Execution time: %f seconds\n", cpu_time_used);
// Free the dynamically allocated memory
for (int i = 0; i < n; i++) {
free(graph[i]);
}
free(graph);
return 0;
}