-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cc
121 lines (97 loc) · 2.3 KB
/
graph.cc
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <stack>
#include <vector>
using namespace std;
struct nodeT {
vector <nodeT *> connect ;
int color ;
int wt ;
};
/*class Graph {
vector <struct node *> vertices ;
int vn ;
char color ;
public:
Graph() {
vn = 0 ;
}
void addnode () ;
void addedge(int ver1, int ver2) ;
void traverse_node () ;
} ;
*/
int vn ;
void addNode (vector <nodeT *> &vertices)
{
struct nodeT * temp ;
temp = new nodeT ;
vertices[vn] = temp ;
vertices[vn]->wt = vn ;
vertices[vn]->color = 0 ;
vn++ ;
// cout << "Node added to the list, Node number is: " << vn-1 <<endl;
// cout << "Color of the node is: "<< vertices[vn-1]->color << endl ;
}
void addEdge(int ver1, int ver2, vector <nodeT *> &vertex) {
cout << "Color: "<< vertex[0]->color << endl ;
cout << "Size: "<<vertex[0]->connect.size()<<endl;
vertex[ver1]->connect.push_back(vertex[ver2]) ;
// cout <<"Hello\n" ;
vertex[ver2]->connect.push_back(vertex[ver1]) ;
cout << "Size: "<<vertex[0]->connect.size()<<endl;
}
void visitvertex(struct nodeT *vert)
{
vert->color = 1;
cout << "In Visit Vertex\n" ;
cout <<"Connect SIze: "<< vert->connect.size() << endl ;
cout << "Visited Vertex is: " << vert->wt << endl ;
for (int j = 0; j < vert->connect.size() ; j++)
{
if ( vert->connect[j]->color == 0) {
visitvertex(vert->connect[j]) ;
}
}
}
void DFS (vector <nodeT *> &vertices)
{
int i = 0 ;
cout << "Size: " << vertices.size() <<endl ;
for (i=0; i<vn; i++)
{
// cout << "Hello\n" ;
vertices[i]->color = 0 ;
}
//cout << "Size: " << vertices.size() <<endl ;
for (i = 0; i<vn; i++)
{
if (vertices[i]->color == 0) {
cout << "Hello\n" ;
visitvertex(vertices[i]) ;
}
}
}
main()
{
int ch ;
int v1, v2 ;
vector <nodeT *> vertices(5) ;
for(;;) {
printf("To add node in the list enter 1, Add edge in the list: 2, Implement DFS: 3\n") ;
cin >> ch ;
switch(ch) {
case 1: addNode(vertices) ;
break ;
case 2: cout << "Enter the vertex between which edge to be added\n";
cin >> v1 >> v2 ;
addEdge(v1, v2, vertices) ;
break ;
case 3: DFS(vertices) ;
break ;
case 4: exit(0) ;
}
}
}