-
Notifications
You must be signed in to change notification settings - Fork 0
/
12. Topological Sorting.c
218 lines (177 loc) · 4 KB
/
12. Topological Sorting.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*********************************************************************************
TITLE: Program to implement Topological Sorting.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define S 50
//Queue is created
typedef struct
{
int a[S];
int front, rear;
}Queue;
//Graph is created
typedef struct
{
int adj[S][S];
int e,v;
}Graph;
//isEmpty function
int isEmpty(int front)
{
if(front==-1)
return 1;
else
return 0;
}
//Dequeue function
int dequeue(Queue *q)
{
int x=q->a[q->front];
if(q->front==q->rear)
{
q->front=q->rear=-1;
}
else
{
q->front++;
}
return x;
}
//Enqueue function
void enqueue(Queue *q, int e)
{
q->rear++;
q->a[q->rear]=e;
if(q->front==-1)
{
q->front=0;
}
}
//initializing graph
void intializeGraph(Graph *g)
{
for(int i=0;i<g->v;i++)
{
for(int j=0;j<g->v;j++)
{
g->adj[i][j]=0;
}
}
}
//Add elements into the graph using adj matrix
void add(Graph *g, int src, int dest)
{
g->adj[src][dest]=1;
//g->adj[dest][src]=0; //for undirected graph
}
//Topological Order using queue data structure
void topologicalOrder(Graph g, Queue *t)
{
int indegree[g.v],i;
for( i=0;i<g.v;i++)
{
indegree[i]=0;
for(int j=0;j<g.v;j++)
{
indegree[i]=indegree[i]+g.adj[j][i];
}
}
for(int i=0;i<g.v;i++)
{
if(indegree[i]==0)
{
enqueue(t,i);
}
}
while(!isEmpty(t->front))
{
int x=dequeue(t);
printf("%d ",x);
indegree[x]=-1;
for(int k=0;k<g.v;k++)
{
if(g.adj[x][k]==1)
{
indegree[k]=indegree[k]-1;
if(indegree[k]==0)
{
enqueue(t,k);
}
}
}
}
}
void printadjmatrix(Graph g)
{
for(int i=0;i<g.v;i++)
{
for(int j=0;j<g.v;j++)
{
printf("%d ",g.adj[i][j]);
}
printf("\n");
}
}
int main()
{
//Initializing different data structures
Graph g;
Queue t;
t.front=t.rear=-1;
int src,dest;
//Inputing the no. of vertices
printf("\nEnter the no. of vertices for directed graph : ");
scanf("%d",&g.v);
//Inputing the no. of edges
printf("\nEnter the no. of edges for directed graph : ");
scanf("%d",&g.e);
//Initializing graph
intializeGraph(&g);
//Entering the source code & destination code
for(int i=1;i<=g.e;i++)
{
printf("\nEnter the source node value : ");
scanf("%d",&src);
printf("\nEnter the destination node value : ");
scanf("%d",&dest);
add(&g,src,dest); //Adding elements into the graph
}
printf("\n***** Adjacency Matrix *****\n");
printadjmatrix(g);
printf("\n***** Topological Sort *****\n");
topologicalOrder(g,&t);
return 0;
}
/*********************************************************************************
OUTPUT:
Enter the no. of vertices for directed graph : 7
Enter the no. of edges for directed graph : 8
Enter the source node value : 1
Enter the destination node value : 2
Enter the source node value : 2
Enter the destination node value : 3
Enter the source node value : 2
Enter the destination node value : 5
Enter the source node value : 2
Enter the destination node value : 4
Enter the source node value : 3
Enter the destination node value : 5
Enter the source node value : 4
Enter the destination node value : 5
Enter the source node value : 5
Enter the destination node value : 6
Enter the source node value : 7
Enter the destination node value : 4
***** Adjacency Matrix *****
0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 1 1 0
0 0 0 0 0 1 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
***** Topological Sort *****
0 1 2 3 4 5 6
Process returned 0
*********************************************************************************/