-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag_Toplo_Sort.c
268 lines (225 loc) · 5.13 KB
/
dag_Toplo_Sort.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Name - Rajendra Singh
Roll No. - 111601017
Question : indicate if graph dag or not. if then print topological sorting otherwise print print any one cycle(one encountered first)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node//structure of node
{
int vertex;
struct node *next;
}node;
node *head[50];
typedef struct stack//structure of a stack
{
int data;
struct stack *next;
}stack;
stack *top=NULL;
int previs[50];
int posvis[50];
int active[50];
int count=0; //counter for count the on which no i am visiting a vertex and on which no, i'm leaving it
int flag=0;//initialising flag with 0 i.e non cycle if cycle comes flag = 1
int vertexno;//size of graph
void readfile();// function to read inputfile
void dfs(int u);
void printlist(int n);
void push(int );
int pop();
void init();//function for initialising previs, posvis, active as 0
void insertadjver(int ,int ); //inserting a uv edge
int main()////////////////////////////////////////////////////main function
{
int i;
char filename[20];
printf("Enter the filename: " );//asking filename
scanf("%s",filename );
readfile(filename);
for(i=0; i<vertexno; i++)//calling dfs
{
if(previs[i]==0)
dfs(i);
}
if(flag==1)//printing cycle
{
printf("\n\n****Input graph have a cycle and the cycle is:****\n");
int temp1;
for(i=0; temp1 != (-INT_MAX); i++)
{
printf("->" );
temp1 = pop();
}
}
else
{
printf("\n\ntopology sort is a DAG & sorting order is:\t");//printing topological order
for(i=0; i<vertexno; i++)
{
pop();
printf(",");
}
}
return 0;
}////////////////////////////////////////////////////////////////////////////
void init()/////////////
{
for (int i = 0; i < vertexno; i++)
{
previs[i]=0; // array to store previsit of vertex
posvis[i]=0; //array to store postvisit of a vertex
active[i]=0; // array to store whether a vertex is in the current stack or not
}
}
void readfile(char* filename)///////////readfile function
{
FILE *fptr;
int success,r,w;
int i = 0,tempvertex = 0;
// printf("\nfilename = %s\n", filename );
fptr = fopen(filename, "r");
if(fptr == NULL)//checking if file opened properly
{
printf("********File didn't open properly**********\n");
return;
}
else
{
success = fscanf(fptr, "%d", &vertexno);//reading vertex no.
printf("\n%d\n",vertexno);
for(i=0; i<vertexno; i++)
{
head[i] = NULL; //initialising heads to be zero
tempvertex = 0;
while(tempvertex != -1) //reading the adjacent vertex of vertex i
{
success = fscanf(fptr, "%d", &tempvertex);
printf("%d\t", tempvertex);
if(tempvertex == -1)//read until -1 is read
{
break;
}
else
{
insertadjver(i,tempvertex);//if read element is not -1 then insert into adjacency list
}
}
printf("\n");
}
}
fclose(fptr);
}
void insertadjver(int src,int des) //src is source vertex and des is destination vertex
{
node *p,*q;
q=(node*)malloc(sizeof(node));//acquire memory for the new node
q->vertex=des;
q->next=NULL;
if(head[src]==NULL)//insert the node in the linked list number vi
head[src]=q;
else
{
p=head[src];
while(p->next!=NULL)//go to end of the linked list
p=p->next;
p->next=q;
}
}
void dfs(int u)///////////////////////////////////////////
{
printf("\ndfs at %d:\t", u);
if(flag==1)
return ;
count++;
previs[u] = count;
active[u] = 1;
node *p = head[u];
while(p!=NULL)
{
if(previs[p->vertex]==0)
{
printf("tree edge is %d->%d\t",u,p->vertex);
dfs(p->vertex);
}
else if(previs[p->vertex]>previs[u]) //p is previously visited after visiting u
{
printf("forward edge is %d->%d\t",u,p->vertex);
}
else if(active[p->vertex]==0) //previously visited adjacent vertex
{
printf("cross edge is %d->%d\t",u,p->vertex);
}
else//rest cases are back edge case so flag = 1 i.e cycle is present
{
flag=1;
printf("back edge is %d->%d\t",u,p->vertex);
push(p->vertex);
active[u]=0;
count++;
posvis[u]=count;
push(u);
return;
}
p=p->next;
}
active[u]=0;
count++;
posvis[u]=count;
push(u);
}
void push(int data)//////////////////
{
if(top==NULL)
{
top=(stack *) malloc(sizeof(stack));
top->data = data;
top->next = NULL;
}
else
{
stack *temp=(stack *) malloc(sizeof(stack));
temp->data = data;
temp->next=top;
top=temp;
}
return;
}
int pop()////////////////////
{
int y;
struct stack* temp;
if(top==NULL)
return(-INT_MAX);
else
{
temp=(struct stack*) malloc(sizeof(struct stack));
temp=top;
top=top->next;
y=temp->data;
printf("%d",y);
free(temp);
}
return y;
}
/*
SAMPLE GRAPH FILE :with back edge
7
1 6 -1
2 -1
0 3 5 -1
4 5 -1
-1
-1
2 -1
SAMPLE GRAPH FILE 2 :without back edge
7
1 6 -1
2 -1
3 5 -1
4 5 -1
-1
-1
2 -1
*/