-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.h
206 lines (182 loc) · 4.29 KB
/
Graph.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <cstdlib>
#include <iostream>
#include<stack>
using namespace std;
//#define max 1001;
struct edgelist{
int value;
int weight;
struct edgelist *next;
};
class Graph{
public:
edgelist *edges[1000];
int InDegree[1000];
int OutDegree[1000];
int vertex_count ;
int edge_count;
int directed ;
int weighted;
void showGraph();
void addVertex(int n);
void addEdge(int x,int y);
void addEdge(int x,int y,int w);
Graph()
{
// edges[]={};
vertex_count = 0;
edge_count = 0;
directed = 0;
weighted = 0;
}
};
//n is number of new vertices
void Graph::addVertex(int n)
{
for(int i = vertex_count + 1; i <= vertex_count + n; i++)
{
InDegree[i] = 0;
OutDegree[i] = 0;
edges[i] = NULL;
}
vertex_count += n;
};
void Graph::addEdge(int x,int y)
{
struct edgelist *temp1 = (struct edgelist *) malloc(sizeof(struct edgelist));
temp1->value = y;
InDegree[y]++;
OutDegree[x]++;
if(edges[x] == NULL)//IF edgelist of vertex x is empty
{
edges[x] = temp1;
edges[x]->next=NULL;
}
else
{
temp1->next = edges[x];
edges[x] = temp1;
}
edge_count++;
if(directed == 0)
{
struct edgelist *temp2 = (struct edgelist *) malloc(sizeof(struct edgelist *));
temp2->value = x;
InDegree[x]++;
OutDegree[y]++;
if(edges[y] == NULL)
{
edges[y] = temp2;
edges[y]->next=NULL;
}
else
{
temp2->next = edges[y];
edges[y] = temp2;
}
edge_count++;
}
};
//BElow is for a weighted graph
void Graph::addEdge(int x,int y,int w)
{
struct edgelist *temp1 = (struct edgelist *) malloc(sizeof(struct edgelist));
temp1->value = y;
temp1->weight = w;
InDegree[y]++;
OutDegree[x]++;
if(edges[x] == NULL)//IF edgelist of vertex x is empty
{
edges[x] = temp1;
edges[x]->next=NULL;
}
else
{
temp1->next = edges[x];
edges[x] = temp1;
}
edge_count++;
if(directed == 0)
{
struct edgelist *temp2 = (struct edgelist *) malloc(sizeof(struct edgelist *));
temp2->value = x;
temp2->weight = w;
InDegree[x]++;
OutDegree[y]++;
if(edges[y] == NULL)
{
edges[y] = temp2;
edges[y]->next=NULL;
}
else
{
temp2->next = edges[y];
edges[y] = temp2;
}
edge_count++;
}
};
void Graph::showGraph()
{
cout << "Graph is " << endl;
if(weighted == 1)
{
for(int i = 1; i <= vertex_count; i++)
{
struct edgelist *current;
current = edges[i];
while(current != NULL)
{
cout << i << " -> ";
cout << current->value << " === " << current->weight << endl;
current = current->next;
}
cout << endl;
}
}
else
{
for(int i = 1; i <= vertex_count; i++)
{
cout << i << " -> ";
struct edgelist *current;
current = edges[i];
while(current != NULL)
{
cout << current->value << " ";
current = current->next;
}
cout << endl;
}
}
}
void dfs(Graph G)
{
int visit[G.vertex_count+1]={0};
stack<int> stack;
for(int i=1;i<=G.vertex_count;i++)
{
if(visit[i]!=1)
{
stack.push(i);
while(!stack.empty())
{
int s=stack.top();
stack.pop();
if(visit[s]==0)
{
cout<<" "<<s<<" ";
visit[s]=1;
}
struct edgelist *temp=G.edges[s];
while(temp!=NULL)
{
int k=temp->value;
if(visit[k]==0)
stack.push(k);
temp=temp->next;
}
}
}
}
}