-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
135 lines (110 loc) · 2.63 KB
/
main.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
// Example for Breadth First Search and Depth First Search
#include <iostream>
#include <map>
#include <set>
#include <list>
using namespace std;
class Tree
{
public:
std::map< int, std::set<int> > elem_conn;
std::map< int, bool > visited; // Let's always use maps (instead of sets)
std::list< int > to_visit;
std::map< int, bool > inserted;
int tree_size;
Tree() {
cout<<"\nTree initialized\n";
tree_size = 0;
};
void add_edge(int ori, int tgt) {
elem_conn[ori].insert(tgt);
cout<<"Added connection: "<< ori <<" -> "<< tgt <<"\n";
}
void DSearch(int start) { // Depth First Search
if (elem_conn.count(start)==0) {
cout<<"Element "<< start <<" doesn't exist!\n";
return;
}
tree_size = 0;
visited.clear();
recursive_search(start);
cout<<"Number of elements: "<< tree_size <<"\n";
}
void recursive_search(int start) {
if (visited.count(start)>0) {
// cout<<"Element "<< start <<" already found...\n";
return;
}
cout<<"Found element: "<< start <<"\n";
tree_size += 1;
visited[start] = true;
for (auto e : elem_conn[start]) {
recursive_search(e);
}
}
void BSearch(int start) { // Breadth First Search
if (elem_conn.count(start)==0) {
cout<<"Element "<< start <<" doesn't exist!\n";
return;
}
tree_size = 0;
visited.clear();
to_visit.clear();
inserted.clear();
to_visit.push_back(start);
inserted[start] = true;
int to_vis;
while (to_visit.empty()==false) {
to_vis = to_visit.front();
to_visit.pop_front();
if (visited.count(to_vis)!=0) cout<<"Already visited!\n";
if (visited.count(to_vis)==0) // Actually superflous
{
cout<<"Found element: "<< to_vis <<"\n";
tree_size += 1;
visited[to_vis] = true;
for (auto el : elem_conn[to_vis]) {
if (inserted.count(el)==0) {
to_visit.push_back(el);
inserted[el] = true;
}
}
}
}
cout<<"Number of elements: "<< tree_size <<"\n";
}
};
int main()
{
Tree mytree;
mytree.add_edge(0, 1);
mytree.add_edge(0, 2);
mytree.add_edge(0, 3);
mytree.add_edge(1, 2);
mytree.add_edge(2, 4);
mytree.add_edge(3, 3);
mytree.add_edge(4, 4);
cout<<"\nDepth first search\n";
mytree.DSearch(5);
cout<<"\nDepth first search\n";
mytree.DSearch(0);
cout<<"\nBreadth first search\n";
mytree.BSearch(0);
Tree T2;
T2.add_edge(2, 1);
T2.add_edge(2, 0);
T2.add_edge(1, 3);
T2.add_edge(1, 4);
T2.add_edge(0, 5);
T2.add_edge(0, 6);
T2.add_edge(0, 7);
T2.add_edge(3, 8);
T2.add_edge(4, 9);
T2.add_edge(5,10);
T2.add_edge(1, 5);
cout<<"\nDepth first search\n";
T2.DSearch(2);
cout<<"\nBreadth first search\n";
T2.BSearch(2);
return(0);
}