forked from Kraken-ops/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs new.cpp
205 lines (123 loc) · 3.73 KB
/
dfs new.cpp
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
#include <stdio.h>
#include <stdlib.h>
struct Graph {
int V; // number of vertices
int **adj; // adjacency matrix
};
typedef struct Graph Graph;
Graph* new_graph() {
Graph *graph = (Graph*)malloc(sizeof(Graph));
graph->V = 0;
graph->adj = NULL;
return graph;
} // end of new_graph()
void display_graph(Graph *graph) {
int **adj = graph->adj;
int i, j;
for (i = 0; i < graph->V; i++) {
printf("vertex %d: ", i);
for (j = 0; j < graph->V; j++) {
if (*(*(adj + i) + j)) {
printf("%d ", j);
}
} // end of inner for loop
printf("\n");
} // end of outer loop
} // end of display_graph(Graph*)
// ************** Stack starts **************
struct Node {
int data;
struct Node* next;
};
typedef struct Node Node;
Node* new_node() {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = 0;
newNode->next = NULL;
return newNode;
} // end of constructor for struct Node
struct Stack {
struct Node* top;
};
typedef struct Stack Stack;
Stack* new_stack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
} // end of constructor for struct Stack
int is_stack_empty(Stack *stack) { return (stack->top == NULL); }
void push(Stack* stack, int value) {
Node *node = new_node();
node->data = value;
node->next = stack->top;
stack->top = node;
} // end of push(stack*, int)
int peek(Stack* stack) { return (stack->top)->data; }
void pop(Stack* stack) {
if (is_stack_empty(stack)) { return; }
Node *node = stack->top;
stack->top = node->next;
free(node);
} // end of pop(Stack*)
// *************** Stack ends ****************
char get_char(int id) { return (char)(id + 65); }
void dfs_traversal(Graph *graph, int starting_vertex) {
int **adj = graph->adj;
Stack *stack = new_stack();
int is_visited[graph->V];
int i, j;
int vertex;
// initialising the is_visited array
for (i = 0; i <= graph->V; i++) { is_visited [i] = 0; }
push(stack, starting_vertex);
while (!is_stack_empty(stack)) {
vertex = peek(stack);
if (!is_visited[vertex]) {
printf("%c ", get_char(vertex));
is_visited[vertex] = 1;
}
for (j = 0; j < graph->V; j++) {
if (*(*(adj + vertex) + j) && !is_visited[j]) {
push(stack, j);
break;
}
if (j == graph->V - 1) { pop(stack); }
} // end of for loop
} // end of outer while loop
} // end of dfs_traversal(Graph*)
void import_graph(Graph *graph) {
FILE *fp;
fp = fopen("input_new.txt", "r");
if (fp == NULL) {
printf("error: cannot access file\n");
exit(1);
}
int V;
fscanf(fp, "%d", &V);
int **adj = (int**)malloc(sizeof(int*) * V);
int i = 0, j = 0;
int val = 0;
for (; i < V; i++) {
*(adj + i) = (int*)malloc(sizeof(int*) * V);
for (j = 0; j < V; j++) {
fscanf(fp, "%d", &val);
*(*(adj + i) + j) = val;
}
} // end of outer for loop
fclose(fp);
graph->V = V;
graph->adj = adj;
} // end of import_graph(Graph*)
int main() {
Graph *graph = new_graph();
import_graph(graph);
printf("\nGraph :-\n");
display_graph(graph);
int starting_vertex;
printf("\nEnter starting vertex for dfs traversal: ");
scanf("%d", &starting_vertex);
printf("\nDFS traversal: ");
dfs_traversal(graph, starting_vertex);
printf("\n");
return 0;
} // end of main()