-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwarshalls-algorithm.c
162 lines (129 loc) · 2.67 KB
/
warshalls-algorithm.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
/* Path matrix by Warshall's algorithm */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 30
struct Vertex
{
char name[50];
};
struct Vertex vertexList[MAX];
int adj[MAX][MAX]; /* Adjacency Matrix */
int n=0; /* Number of vertices in the graph */
int e=0; /* Number of edges in the graph */
int getIndex(char s[]);
void insertVertex(char s[]);
void deleteVertex(char s[]);
void insertEdge(char s1[], char s2[]);
void deleteEdge(char s1[], char s2[] );
void warshalls();
main()
{
insertVertex("Zero");
insertVertex("One");
insertVertex("Two");
insertVertex("Three");
insertEdge("Zero", "One");
insertEdge("Zero", "Three");
insertEdge("One", "Two");
insertEdge("Two", "One");
insertEdge("Three", "Zero");
insertEdge("Three", "Two");
warshalls();
}
void warshalls()
{
int i,j,k,p[MAX][MAX];
for(i=0; i<n; i++)
for(j = 0; j < n; j++)
p[i][j] = adj[i][j];
for(k=0; k<n; k++)
{
for(i=0; i<n; i++)
for(j=0; j<n; j++)
p[i][j] = ( p[i][j] || ( p[i][k] && p[k][j] ) );
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%d ", p[i][j]);
printf("\n");
}
printf("\n");
}
int getIndex(char s[])
{
int i;
for (i = 0; i<n; i++)
if ( strcmp(s,vertexList[i].name) == 0 )
return i;
printf("Invalid Vertex - %s \n\n", s);
return -1;
}
void insertVertex(char s[])
{
int i;
for(i = 0; i<n; i++ )
if( strcmp(vertexList[i].name, s) == 0)
{
printf("Vertex already present\n\n");
return;
}
strcpy(vertexList[n].name, s);
n++;
}
void deleteVertex(char s[])
{
int i, x = getIndex(s), u;
if(x == -1)
return;
u = x;
while (u < n-1)
{
for (i = 0; i < n; i++) /* shift rows to the left */
adj[i][u] = adj[i][u+1];
for (i = 0; i < n; i++) /* shift columns up */
adj[u][i] = adj[u+1][i];
u++;
}
for(i = x; i < n-1; i++)
vertexList[i] = vertexList[i+1];
n--;
}
/* Insert an edge (s1,s2) */
void insertEdge(char s1[], char s2[])
{
int u = getIndex(s1);
int v = getIndex(s2);
if (u == v)
{
printf("Not a valid edge\n\n");
return;
}
if (adj[u][v] != 0 )
printf("Edge already present\n\n");
else
{
adj[u][v] = 1;
e++;
}
}
/* Delete the edge (s1,s2) */
void deleteEdge(char s1[], char s2[])
{
int u = getIndex(s1);
int v = getIndex(s2);
if(u==-1 || v ==-1)
return;
if( adj[u][v] == 0 )
printf("Edge not present\n\n");
else
{
adj[u][v] = 0;
e--;
}
}