This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfsanddfs.h
160 lines (149 loc) · 5.33 KB
/
bfsanddfs.h
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
#ifndef bfsanddfs_h
#define bfsanddfs_h
#include<iostream>
#include<list>
#include<string>
using namespace std;
template<class T>
class Graph
{
int no_of_vertices;
list<int> *adj;
public:
Graph(int v)
{
no_of_vertices = v;
adj = new list<int>[no_of_vertices];
}
int addEdge(T v1, T v2)
{
if(isalpha(v1))
{
if(v1 >= 65 && v1 <= 90)
{
int v3 = (int)v1-65;
int v4 = (int)v2-65;
adj[v3].push_back(v4);
}
else if(v1 >= 97 && v1 <= 122)
{
int v3 = (int)v1-97;
int v4 = (int)v2-97;
adj[v3].push_back(v4);
}
}
else adj[v1].push_back(v2); // Nodes are numbers
}
int BFS(T source)
{
int flag = 0;
int i,vertex, source1;
list<int> Q;
list<int> :: iterator it;
bool *visited = new bool[no_of_vertices];
for(i=0;i<no_of_vertices;i++)
{
visited[i] = false; //Initially none visited
}
if(isalpha(source))
{
if(source>=65 && source<=90)
{
flag=1;
source1= (int)source-65;
}
else if(source>=97 && source<=122)
{
flag=2;
source1= (int)source-97;
}
visited[source1] = true;
Q.push_back(source1);
}
else
{
visited[source] = true; //At source
Q.push_back(source);
}
while(!Q.empty()) // Popping from Queue
{
vertex = Q.front();
Q.pop_front();
if(flag==1)
{
cout<<" "<<char(vertex+65)<<" ";
}
else if(flag==2)
{
cout<<" "<<char(vertex+97)<<" ";
}
else cout<<" "<<vertex<<" ";
for(it = adj[vertex].begin();it!=adj[vertex].end();it++)
{
if(!visited[*it])
{
visited[*it] = true;
Q.push_back(*it);
}
}
}
return 0;
}
int DFS(T source)
{
int flag,source1;
int i,vertex;
bool *visited = new bool[no_of_vertices];
list<int> Stack;
list<int> :: iterator it;
flag = 0;
for(i=0;i<no_of_vertices;i++)
{
visited[i] = false; //Initially none visited
}
if(isalpha(source))
{
if(source>=65 && source<=90)
{
flag = 1;
source1= (int)source-65;
}
else if(source>=97 && source<=122)
{
flag = 2;
source1= (int)source-97;
}
visited[source1] = true;
Stack.push_back(source1);
}
else
{
visited[source] = true; //At source
Stack.push_back(source);
}
while(!Stack.empty()) // Popping from Queue
{
vertex = Stack.back();
Stack.pop_back();
if(flag==1)
{
cout<<" "<<char(vertex+65)<<" ";
}
else if(flag==2)
{
cout<<" "<<char(vertex+97)<<" ";
}
else cout<<" "<<vertex<<" ";
for(it = adj[vertex].begin();it!=adj[vertex].end();it++)
{
if(!visited[*it])
{
visited[*it] = true;
Stack.push_back(*it);
}
}
}
return 0;
}
};
#endif