This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFT.cpp
227 lines (208 loc) · 4.14 KB
/
DFT.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Graph Traversal-DFS
#include <iostream>
using namespace std;
struct ga;
struct gv // Graph Vertex
{
char data;
bool instack, processed;
ga *firstarc;
gv *nextvertex;
};
struct ga // Graph Arc
{
gv *content;
ga *nextarc;
};
struct node // Node for stack
{
gv *vertex;
node *link;
};
class stack // Stack for DFT
{
private:
node *top;
public:
int count;
stack() : count(0) { top = NULL; }
void push(gv *v)
{
node *temp = new node;
temp->vertex = v;
temp->link = NULL;
if (top == NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
count++;
}
node *Topstack()
{
return top;
}
gv *pop()
{
node *temp = top;
gv *v = temp->vertex;
top = top->link;
delete temp;
count--;
return v;
}
};
class graph
{
private:
gv *first;
int count;
public:
graph() : count(0) { first = NULL; }
void insertvertex(char x)
{
gv *temp = new gv;
temp->data = x;
temp->instack = temp->processed = false;
temp->firstarc = NULL;
temp->nextvertex = NULL;
// Insertion
if (first == NULL)
first = temp;
else
{
gv *t = first;
while (t->nextvertex != NULL)
t = t->nextvertex;
t->nextvertex = temp;
}
count++;
cout << "Vertex inserted!!" << endl;
}
bool insertarc(char x, char y)
{
gv *xtemp = NULL, *ytemp = NULL;
gv *xt = first, *yt = first;
while (xt != NULL && xt->data != x)
xt = xt->nextvertex;
while (yt != NULL && yt->data != y)
yt = yt->nextvertex;
if (xt == NULL || yt == NULL) // Vertex not found
return false;
else
{
xtemp = xt;
ytemp = yt;
}
// Insertion at x arc
ga *xarc = new ga;
xarc->content = ytemp;
xarc->nextarc = NULL;
if (xtemp->firstarc == NULL)
xtemp->firstarc = xarc;
else
{
ga *xa = xtemp->firstarc;
while (xa->nextarc != NULL)
xa = xa->nextarc;
xa->nextarc = xarc;
}
// Insertion at y arc
ga *yarc = new ga;
yarc->content = xtemp;
yarc->nextarc = NULL;
if (ytemp->firstarc == NULL)
ytemp->firstarc = yarc;
else
{
ga *ya = ytemp->firstarc;
while (ya->nextarc != NULL)
ya = ya->nextarc;
ya->nextarc = yarc;
}
};
void dfs()
{
stack s;
int visitedvertex = 0;
gv *vertex = first;
s.push(vertex);
vertex->processed = true;
vertex->instack = true;
visitedvertex++;
cout << vertex->data << "\t";
while (count != visitedvertex)
{
ga *arc = s.Topstack()->vertex->firstarc;
while (arc != NULL && arc->content->processed == true)
{
arc = arc->nextarc;
}
if (arc != NULL)
{
s.push(arc->content);
arc->content->processed = true;
arc->content->instack = true;
visitedvertex++;
cout << arc->content->data << "\t";
}
else
{
vertex = s.pop();
vertex->instack = false;
if (s.Topstack() == NULL)
{
vertex = first->nextvertex;
while (vertex != NULL && vertex->processed == true)
vertex = vertex->nextvertex;
if (vertex != NULL)
{
s.push(vertex);
vertex->processed = true;
vertex->instack = true;
visitedvertex++;
cout << vertex->data << "\t";
}
}
}
}
cout << "\n";
}
};
int main()
{
graph g;
int ch;
char x, y;
do
{
cout << "\n1-Insert Vertex, 2-Insert Edge, 3-Depth first search,0-Exit\n";
cout << "Enter choice: ";
cin >> ch;
switch (ch)
{
case 1:
cout << "Enter character to insert: ";
cin >> x;
g.insertvertex(x);
break;
case 2:
cout << "Enter characters to add edge: ";
cin >> x >> y;
if (g.insertarc(x, y))
cout << "Edge inserted between " << x << " and " << y << "." << endl;
else
cout << "Either one or both vertices not present in graph.\n";
break;
case 3:
g.dfs();
break;
case 0:
break;
default:
cout << "Invalid choice!";
}
} while (ch != 0);
}