-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_Cycle_In_Undirected_Graph_Using_BFS.cpp
More file actions
85 lines (72 loc) · 1.8 KB
/
Copy pathdetect_Cycle_In_Undirected_Graph_Using_BFS.cpp
File metadata and controls
85 lines (72 loc) · 1.8 KB
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
#include "iostream"
#include "vector"
#include "queue"
#include "set"
#include "map"
using namespace std;
struct Edge {
int source,destination;
};
class Graph{
int V;
vector<vector<int>> adjList;
public:
Graph(vector<Edge> edges,int V){
this->V = V;
adjList.resize(V);
for(auto i : edges){
adjList[i.source].push_back(i.destination);
adjList[i.destination].push_back(i.source);
}
}
void detect_Cycle_In_Undirected_Graph_Using_BFS();
//in bfs we need to find out a cross edge
void printGraph();
};
void Graph :: printGraph()
{
for (int i = 1; i < adjList.size(); i++)
{
cout << i << " -- ";
for (int v : adjList[i])
cout <<"->"<< v << " ";
cout << endl;
}
}
struct Node {
int vertex ,parent ;
};
void Graph ::detect_Cycle_In_Undirected_Graph_Using_BFS() {
vector<bool> discovered(V);
int src = 1;
discovered[src] = true;
queue<Node> q;
q.push({src, -1});
while (!q.empty())
{
Node node = q.front();
q.pop();
for (int u :adjList[node.vertex])
{
if (!discovered[u])
{
discovered[u] = true;
q.push({ u, node.vertex });
}
else if (u != node.parent) {cout << "Cycle found "; return;}
}
}
cout <<"Cycle not found ";
}
int main()
{
vector<Edge> edges ={ {1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 9},
{5, 10}, {4, 7}, {4, 8}, {7, 11}, {7, 12}, {6, 10}
};
set <int > setsize;
for(auto i : edges){ setsize.insert(i.source);setsize.insert(i.destination);}
int V = setsize.size();
Graph graph(edges, V +1 );
graph.detect_Cycle_In_Undirected_Graph_Using_BFS();
return 0;
}