#include<bits/stdc++.h>
#define White 1
#define Gray 2
#define Black 3
#define s Start_Time
#define f Finishing_Time
#define t Time
#define c Color
using namespace std;
int a[100][100], c[100], s[100], f[100], t = 1, node, edge;
void dfsvisit(int x)
{
c[x] = Gray;
s[x] = t;
t++;
int i;
for(i=0;i<node;i++)
{
if(a[x][i] == 1)
{
if(c[i] == White)
{
dfsvisit(i);
}
}
}
c[x] = Black;
f[x] = t;
t++;
}
void dfs()
{
int i;
for(i=0;i<node;i++)
{
c[i] = White;
}
for(i=0;i<node;i++)
{
if(c[i] == White)
{
dfsvisit(i);
}
}
}
int main()
{
int i, j, n1, n2;
cout << "Enter Node Number :: ";
cin >> node;
cout << "Enter Edge Number :: ";
cin >> edge;
cout << "Enter Adjacent Node :: " << endl;
for(i=0;i<edge;i++)
{
cin >> n1 >> n2;
a[n1][n2] = 1;
}
dfs();
for(i=0;i<node;i++)
{
cout << i << " Node Start Time " << s[i] << " && " << "Finishing Time " << f[i] << endl;
}
return 0;
}
#define White 1
#define Gray 2
#define Black 3
#define s Start_Time
#define f Finishing_Time
#define t Time
#define c Color
using namespace std;
int a[100][100], c[100], s[100], f[100], t = 1, node, edge;
void dfsvisit(int x)
{
c[x] = Gray;
s[x] = t;
t++;
int i;
for(i=0;i<node;i++)
{
if(a[x][i] == 1)
{
if(c[i] == White)
{
dfsvisit(i);
}
}
}
c[x] = Black;
f[x] = t;
t++;
}
void dfs()
{
int i;
for(i=0;i<node;i++)
{
c[i] = White;
}
for(i=0;i<node;i++)
{
if(c[i] == White)
{
dfsvisit(i);
}
}
}
int main()
{
int i, j, n1, n2;
cout << "Enter Node Number :: ";
cin >> node;
cout << "Enter Edge Number :: ";
cin >> edge;
cout << "Enter Adjacent Node :: " << endl;
for(i=0;i<edge;i++)
{
cin >> n1 >> n2;
a[n1][n2] = 1;
}
dfs();
for(i=0;i<node;i++)
{
cout << i << " Node Start Time " << s[i] << " && " << "Finishing Time " << f[i] << endl;
}
return 0;
}
Comments
Post a Comment