-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotherNew.cpp
84 lines (81 loc) · 1.52 KB
/
motherNew.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
#include <iostream>
using namespace std;
int i , j;
int vcount = 0;
int stepcount = 0;
void dfs(int adj[][10],int r, int v)
{
int visited[r];
for (i = 0; i < r; i++)
{
stepcount++;
visited[i] = 0;
stepcount++;
}
stepcount++;
visited[v] = 1;stepcount++;
vcount++;stepcount++;
int Stack[20],top = -1;stepcount++;
top++;stepcount++;
Stack[top] = v;stepcount++;
while (top != -1)
{
stepcount++;
int w = Stack[top--];stepcount++;
cout << w << " ";stepcount++;
int u = 0;stepcount++;
while (u < r)
{
stepcount++;
stepcount++;
if (adj[w][u] == 1 && visited[u] == 0)
{
stepcount++;
visited[u] = 1;stepcount++;
vcount++;stepcount++;
Stack[++top] = u;stepcount++;
}
u++;stepcount++;
}
stepcount++;
}
}
int main()
{
int mat[10][10],r,src;
cout << "\nEnter the number of vertices in the graph: ";stepcount++;
cin >> r;stepcount++;
cout << "\nEnter the adjacency matrix: \n";stepcount++;
for (i = 0; i < r; i++)
{
stepcount++;
for (j = 0; j < r; j++)
{
stepcount++;
cin >> mat[i][j];
stepcount++;
}
stepcount++;
}stepcount++;
cout << "\nThe mother vertices in the graph are: ";stepcount++;
//dfs for every node in graph
for (i = 0; i < r; i++)
{
stepcount++;
dfs(mat,r,i);stepcount++;
stepcount++;
if (vcount == r-1)
{
stepcount++;
cout << i << " ";
stepcount++;
}
vcount = 0;stepcount++;
}
stepcount++;
cout << endl;
stepcount++;
stepcount++;
cout << "\nThe stepcount for "<<r<<"-vertex graph is: " << ++stepcount;
return 0;
}