-
Notifications
You must be signed in to change notification settings - Fork 8
/
GraphWithMAP.cpp
174 lines (167 loc) · 4.8 KB
/
GraphWithMAP.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
#include <bits/stdc++.h>
using namespace std;
// class tampleting
template <typename T>
class Giraff
{
public:
unordered_map<T, list<T>> adjList; // most suitable way, can have any differnet nodes
// vector<vector<T>> adjList(n);// when nodes are defiened from 0 to n
void add_edge(T u, T v, bool isDir = 0)
{
adjList[u].push_back(v);
if (isDir == 0)
adjList[v].push_back(u);
}
void display_graph()
{
for (auto i : adjList)
{
cout << endl;
cout << i.first << "->";
for (auto j : i.second)
{
cout << j << ",";
}
}
}
void bfs()
{
// need a map, and a queue
map<T, bool> visited;
queue<T> Q;
// mark all as unvisited on map
for (auto i : adjList)
{
visited[i.first] = false;
}
cout << endl;
// deciding the first node to start with
unordered_map<int, list<int>>::iterator itr; // itrator should have defined typename
itr = adjList.begin(); // itrator to adjList first element, ie pointer to its first element
T first_node = itr->first; // acessing first element of adjList
// 1.Pushing in Q
Q.push(first_node);
visited[first_node] = true;
while (!Q.empty())
{
// taking value of front in Queue
T temp = Q.front();
Q.pop();
cout << temp << " ";
// 2.adding adjecent elements to queue
for (auto i : adjList[temp])
{
if (visited[i] == false)
{
// 3.marking pushed element as visited
visited[i] = true;
Q.push(i);
}
}
}
}
void dfs(int n, map<T, bool> &visited)
{
// printing the element
cout << n << " ";
// 2.calling dfs for its adjecent elemnts (not visited)
for (auto i : adjList[n])
{
if (visited[i] == false)
{
// 3.making visited
visited[i] = true;
dfs(i, visited);
}
}
}
void callDfs()
{
cout << endl;
// 1.map for visited
map<T, bool> visited;
for (auto i : adjList)
{
visited[i.first] = false;
}
// traversing adjList elements, in case if disconnected graph
for (auto i : adjList)
{
if (visited[i.first] == false)
{
// 3.making visited
visited[i.first] = true;
dfs(i.first, visited);
}
}
}
string cycleDetection(vector<vector<int>> &edges, int n, int m)
{
// Write your code here.
// adj list made
for (auto i : edges)
{
adjList[i[0]].push_back(i[1]);
adjList[i[1]].push_back(i[0]);
}
// bfs to detect cycle
/// visited map
map<int, bool> visited;
/// traversal queue
queue<int> Q;
/// parent queue
queue<int> P;
///traversal for disconnected graph
for (auto i : adjList)
{
if (!visited[i.first])
{
///pushing elemnt
Q.push(i.first);
visited[i.first] = true;
//pushing parent
P.push(NULL);
while (!Q.empty())
{
int temp = Q.front();
Q.pop();
///parent of poped element taken
int parent = P.front();
P.pop();
for (auto j : adjList[temp])
{
if (!visited[j])
{
Q.push(j);
P.push(temp);
visited[j] = true;
}
///if encountered *visited* adjecent element is not the preant then there must be cycle
else if (j != parent)
return "Yes";
}
}
}
}
return "No";
}
};
int main()
{
Giraff<int> G1;
cout << "Give the map:";
cout << "\n Number of nodes: ";
int n, u, v;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> u >> v;
G1.add_edge(u, v);
}
G1.display_graph();
// GRaph input;/ representation executed
G1.bfs();
G1.callDfs();
// DFS,BfS executed
}