-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEdgeCheck.cpp
More file actions
60 lines (59 loc) · 1.47 KB
/
Copy pathEdgeCheck.cpp
File metadata and controls
60 lines (59 loc) · 1.47 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
class Graph
{
int V,E;
list<int> *adj;
public:
Graph(int v,int e)
{
V=v,
E=e;
adj=new list<int>[V];
}
void addedge(int u,int v)
{
adj[u].push_back(v);
//adj[v].push_back(u);
}
void DFS(int *visited,int *explored,int *parent,int i)
{
explored[i]=1;
for(auto it=adj[i].begin();it!=adj[i].end();it++)
{
cout<<*it<<endl;
if(!visited[*it]&&!explored[*it])
{
cout<<"Tree edge-"<<i<<" "<<*it<<endl;
parent[*it]=i;
DFS(visited,explored,parent,*it);
}
else if(visited[*it]==1)
{
cout<<"Forward Edge-"<<i<<" "<<*it<<endl;
}
else if(explored[*it]==1&&*it!=parent[i])
{
cout<<"Back edge-"<<i<<" "<<*it<<endl;
}
}
visited[i]=1;
}
void EdgeCheck()
{
int *visited=(int *)calloc(V,sizeof(int));
int *explored=(int *)calloc(V,sizeof(int));
int *parent=(int *)calloc(V,sizeof(int));
for(int i=0;i<V;i++)
parent[i]=-1;
for(int i=0;i<V;i++)
{
if(!visited[i]){
parent[i]=i;
DFS(visited,explored,parent,i);
}
}
free(visited);
free(explored);
free(parent);
cout<<endl;
}
};