-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.cpp
286 lines (239 loc) · 7.25 KB
/
connect.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
/***********************************************************
Given two arrays: FirstVertex pointing to the EdgeList with destination
node for an undirected graph G(V,E).
The input file begins with number of vertices (V = |V|)
and number of edges (E =|E|) and the list of value for
int FirstVertex[V+1]
int EdgeList[E+1]
The vertices(nodes) are numbered 0,..,|V| -1. The last entry
FirstVertex[V] = E pointing to EdgeList[E] = -1;
represented as a null
You are to compute the number of the connected components.
************************************************************/
// Here is remendary Queue give at end of file.
typedef struct Queue
{
int capacity;
int size;
int front;
int rear;
int *elements;
}Queue;
Queue * createQueue(int maxElements);
void Enqueue(Queue *Q,int element);
int Dequeue(Queue *Q);
int find_connected_components_BFS(int *FirstVertex, int V, int *EdgeList,int E);
void PrintAdjacencyListFormat(int *FirstVertex, int V, int *EdgeList,int E);
// void BFS(Queue * Q,, int *Found, int *FirstVertex, int V, int *EdgeList,int E);
// Stack structure and functions for DFS
typedef struct Stack {
int capacity;
int size;
int top;
int *elements;
} Stack;
Stack* createStack(int maxElements) {
Stack *S = new Stack();
S->elements = new int[maxElements];
S->size = 0;
S->capacity = maxElements;
S->top = -1;
return S;
}
void Push(Stack *S, int element) {
if(S->size == S->capacity) {
cout << "Stack Full" << endl;
} else {
S->elements[++S->top] = element;
S->size++;
}
}
int Pop(Stack *S) {
if(S->size == 0) {
cout << "Stack Empty" << endl;
return -1;
} else {
S->size--;
return S->elements[S->top--];
}
}
int find_connected_components_DFS(int *FirstVertex, int V, int *EdgeList, int E) {
bool *visited = new bool[V]();
Stack *S = createStack(V);
int count = 0;
for(int i = 0; i < V; i++) {
if(!visited[i]) {
count++;
Push(S, i);
visited[i] = true;
while(S->size > 0) {
int v = Pop(S);
for(int j = FirstVertex[v]; j < FirstVertex[v+1]; j++) {
int u = EdgeList[j];
if(!visited[u]) {
visited[u] = true;
Push(S, u);
}
}
}
}
}
delete[] visited;
delete S;
return count;
}
int find_connected_components_BFS(int *FirstVertex, int V, int *EdgeList, int E) {
bool *visited = new bool[V]();
Queue *Q = createQueue(V);
int count = 0;
for(int i = 0; i < V; i++) {
if(!visited[i]) {
count++;
Enqueue(Q, i);
visited[i] = true;
while(Q->size > 0) {
int v = Dequeue(Q);
for(int j = FirstVertex[v]; j < FirstVertex[v+1]; j++) {
int u = EdgeList[j];
if(!visited[u]) {
visited[u] = true;
Enqueue(Q, u);
}
}
}
}
}
delete[] visited;
delete Q;
return count;
}
int main(int argc, char *argv[]){
chrono::time_point<chrono::steady_clock> start, stop;
chrono::duration<double> difference_in_time;
double difference_in_seconds_BFS; // Holds the final run time for BFS
ifstream infile1;
int V;
int E;
int NumberCC_BFS;
infile1.open(argv[1]);
if(!infile1){
cout << "Error opening file " <<endl;
return -1;
}
infile1 >> V;
infile1 >> E;
int *FirstVertex = new int[V+1];
for(int i=0; i< V+1 ; i++)
infile1 >> FirstVertex[i]; // Note: FirstVertex[V] = E fake extra link
int *EdgeList = new int[E+1];
for(int i=0; i< E +1 ; i++)
infile1 >> EdgeList[i]; // Note EdgeList[E] = -1; is null flag
infile1.close();
#if 1 // set to 1 to debug
cout << endl << "A print in Adjacency List form to help with Debugging! " << endl;
PrintAdjacencyListFormat(FirstVertex, V, EdgeList, E);
#endif
/* Find NumberCC_BFS */
start = chrono::steady_clock::now();
NumberCC_BFS=find_connected_components_BFS(FirstVertex, V, EdgeList,E);
stop = chrono::steady_clock::now();
difference_in_time = stop - start;
difference_in_seconds_BFS = double(difference_in_time.count());
//Begin output file : DO NOT CHANGE
ofstream outfile(strcat(argv[1],"_out"));
outfile << difference_in_seconds_BFS << endl;
outfile << NumberCC_BFS << endl;
// outfile << difference_in_seconds_DFS << endl;
// outfile << NumberCC_DFS << endl;
//End output file
return 0;
}
int find_connected_components_BFS(int *FirstVertex, int V, int *EdgeList,int E)
{
int NumberCC_BFS = 0;
int *Found = new int[V];
fill_n(Found, V, -1);
Queue *Q = createQueue(V);
// Write code to interate over BFS to find all connected compoents.
// BFS(Q,Found, FirstVertex, V, EdgeList,int E);
return NumberCC_BFS;
}
/* QUEUE FUCTIONS */
Queue * createQueue(int maxElements)
{
/* Create a Queue */
Queue *Q;
Q = (Queue*)malloc(sizeof(Queue));
/* Initialise its properties */
Q->elements = (int *)malloc(sizeof(int)*maxElements);
Q->size = 0;
Q->capacity = maxElements;
Q->front = 0;
Q->rear = -1;
/* Return the pointer */
return Q;
}
void Enqueue(Queue *Q,int element)
{
/* If the Queue is full, we cannot push an element into it as there is no space for it.*/
if(Q->size == Q->capacity)
{
printf("Queue is Full\n");
}
else
{
Q->size++;
Q->rear = Q->rear + 1;
/* As we fill the queue in circular fashion */
if(Q->rear == Q->capacity)
{
Q->rear = 0;
}
/* Insert the element in its rear side */
Q->elements[Q->rear] = element;
}
return;
}
int Dequeue(Queue *Q)
{
int element = -1;
/* If Queue size is zero then it is empty. So we cannot pop */
if(Q->size==0)
{
// printf("Queue is Empty\n");
return element;
}
/* Removing an element is equivalent to incrementing index of front by one */
else
{ element = Q->elements[Q->front];
Q->size--;
Q->front++;
/* As we fill elements in circular fashion */
if(Q->front==Q->capacity)
{
Q->front=0;
}
}
return element;
}
void PrintAdjacencyListFormat(int *FirstVertex, int V, int *EdgeList,int E)
{
int v;
int e;
cout << "V = " << V << " E = " <<E <<endl;
for( v = 0; v < V; v++)
{
cout << endl;
cout << v << ": ";
for(e = FirstVertex[v]; e < FirstVertex[v+1]; e++)
cout << EdgeList[e]<< "-> ";
cout <<"nil";
}
cout << endl;
cout << v << " : ";
cout << EdgeList[e] << " That's Edge List nil Folks " << endl;
}