-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.c
207 lines (161 loc) · 4.08 KB
/
4.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
#include <stdio.h>
#include <stdlib.h>
int time = 0;
struct node {
int key , d, f ;
struct node *pi;
char color ;
};
struct l_node {
struct node *node ;
struct l_node *next ;
};
struct linked_list{
struct l_node *head ;
struct l_node *last ;
};
struct l_node * Create_l_node (struct node *node){
struct l_node *l = (struct l_node*)malloc(sizeof(struct l_node));
l->node = node ;
l->next = NULL ;
}
struct linked_list * Create_List (struct l_node * l_node){
struct linked_list *l = (struct linked_list*)malloc(sizeof(struct linked_list));
l->head = l->last = l_node ;
return l ;
}
struct node *Create_node (int key)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->key = key ;
p->pi = NULL ;
p->color = 'W';
p->d = 0 ;
p->f = 0;
return p ;
}
void Insert(struct linked_list *l , struct l_node * p){
l->last->next = p ;
l->last = p ;
}
struct queue {
int tail;
struct l_node *a[100] ;
int size;
int head;
};
int isempty(struct queue*s){
return (s->head == -1 || s->head == s->tail) ;
}
struct l_node * Dequeue(struct queue *s){
if (isempty(s) )
{
return NULL ;
}
else
{
s->head = (s->head+1)%s->size;
return s->a[s->head-1];
}
}
void Enqueue(struct queue *s,struct l_node * k){
s->a[s->tail] = k ;
s->tail ++ ;
}
void BFS(struct linked_list *a[] , int s, int n ){
int i = 0;
struct l_node *temp ;
for (i=0;i<n;i++){
temp = a[i]->head ;
if ( s == i ){
temp->node->color = 'G';
temp->node->d = 0 ;
temp->node->pi = NULL ;
}
else{
temp->node->color = 'W';
temp->node->d = 9999 ;
temp->node->pi = NULL ;
}
}
struct l_node *u , *v ;
struct queue *q = (struct queue *)malloc(sizeof(struct queue));
q->head = q->tail = 0 ;
q->size = 100 ;
Enqueue(q,a[s]->head);
while (!isempty(q)){
u = Dequeue(q);
printf("%d ",u->node->key);
v = u->next ;
while (v){
if (v->node->color == 'W'){
v->node->color = 'G';
v->node->d ++ ;
v->node->pi = u->node ;
Enqueue(q,a[v->node->key]->head);
}
v = v->next ;
}
u->node->color = 'B';
}
printf("\n");
}
void DFS_Visit (struct linked_list *a[] , struct l_node *u){
time ++ ;
u->node->d = time ;
u->node->color = 'G' ;
struct l_node *temp = u->next ;
while (temp){
if (temp->node->color == 'w'){
temp->node->pi = u->node ;
printf("%d ",temp->node->key);
DFS_Visit(a,a[temp->node->key]->head);
}
temp = temp->next ;
}
u->node->color = 'B';
time ++ ;
u->node->f = time ;
}
void DFS (struct linked_list *a[] , int n ){
int i = 0;
struct l_node *temp ;
for (i=0;i<n;i++){
temp = a[i]->head ;
temp->node->color = 'w';
temp->node->pi = NULL ;
}
printf("%d ",a[0]->head->node->key);
for (i=0;i<n;i++){
temp = a[i]->head ;
if ( temp->node->color = 'w')
DFS_Visit(a,a[temp->node->key]->head);
}
printf("\n");
}
int main(){
int n , m , n1 , key ,i=0 , source ;
scanf("%d %d",&n,&m);
struct linked_list *a[n];
struct node *nodes[n];
for (i= 0;i<n ; i++){
nodes[i] = Create_node(i) ;
}
i = 0;
struct l_node *temp ;
// Initialisation of the adj lists .
while (i<n){
a[i] = Create_List(Create_l_node(nodes[i]));
i++ ;
}
// Marking -Edges
while (m>0){
scanf("%d %d ",&n1, &key);
Insert(a[n1],Create_l_node(nodes[key]));
m-- ;
}
scanf("%d",&source);
BFS(a,source,n);
DFS(a,n);
return 0 ;
}