-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if a given graph is tree or not.cpp
62 lines (52 loc) · 1.29 KB
/
Check if a given graph is tree or not.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
#include<bits/stdc++.h>
using namespace std;
class Graph{
private:
int n;
vector<int> *arr;
bool isCycle(int u, vector<bool> &visited, int parent){
visited[u] = true;
for(int i = 0; i < arr[u].size(); i++)
if(!visited[arr[u][i]]){
if(isCycle(arr[u][i], visited, u))
return true;
}
else if(arr[u][i] != parent)
return true;
return false;
}
public:
Graph(int n){
this->n = n;
arr = new vector<int>[n];
}
void addEdge(int u, int v){
arr[u].push_back(v);
arr[v].push_back(u);
}
bool isTree(){
vector<bool> visited(n, false);
if(isCycle(0, visited, -1))
return false;
for(int i = 0; i < n; i++)
if(visited[i] == false)
return false;
return true;
}
};
int main(){
Graph g1(5);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
g1.addEdge(0, 3);
g1.addEdge(3, 4);
g1.isTree()? cout << "Graph is Tree\n": cout << "Graph is not Tree\n";
Graph g2(5);
g2.addEdge(1, 0);
g2.addEdge(0, 2);
g2.addEdge(2, 1);
g2.addEdge(0, 3);
g2.addEdge(3, 4);
g2.isTree()? cout << "Graph is Tree\n": cout << "Graph is not Tree\n";
return 0;
}