-
Notifications
You must be signed in to change notification settings - Fork 0
/
connected-components-in-graph.c
166 lines (134 loc) · 4.14 KB
/
connected-components-in-graph.c
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
/*
To find connected components in a graph use this code with uncommented int main().
This code is basically base for finding the spanning trees in a graph which you can find in spanning-trees-of-a-graph.c. For that
we need to comment the int main() in this code so that we can include this file in spanning-trees-of-a-graph.c
The following code explanation is at following link
http://www.martinbroadhurst.com/connected-components-of-a-graph-in-c.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <D:\wamp\www\Concepts\Datastructure\cirque_implementation.c>
typedef struct
{
unsigned int first;
unsigned int second;
} edge;
//USING DEPTH FIRST SEARCH METHOD
void connected_components_recursive(const edge *edges, unsigned int e, int vertix, int component, int *components)
{
components[vertix] = component;
for (int i = 0; i < e; i++)
{
if (edges[i].first == vertix || edges[i].second == vertix)
{
const unsigned int *neighbour = vertix == edges[i].first ? &edges[i].second : &edges[i].first;
//not visited
if (components[*neighbour] == -1)
{
connected_components_recursive(edges, e, *neighbour, component, components);
}
}
}
}
//USING BREADTH FIRST SEARCH METHOD
void connected_components_internal(const edge *edges, unsigned int e, int vertix, int component, int *components)
{
components[vertix] = component;
cirque *queue = cirque_create();
if (queue == NULL)
{
cirque_delete(queue);
return;
}
cirque_insert(queue, &vertix);
while (cirque_get_count(queue))
{
int *current = cirque_remove(queue);
for (int i = 0; i < e; i++)
{
if (edges[i].first == *current || edges[i].second == *current)
{
const unsigned int *neighbour = *current == edges[i].first ? &edges[i].second : &edges[i].first;
//not visited
if (components[*neighbour] == -1)
{
components[*neighbour] = component;
cirque_insert(queue, (void*)neighbour);
}
}
}
}
cirque_delete(queue);
}
unsigned int connected_components(const edge *edges, unsigned int e, unsigned int v, int **components)
{
int component = 0;
*components = malloc(v * sizeof(int));
if (*components == NULL)
return 0;
for (int i = 0; i < v; i++)
(*components)[i] = -1;
for (int j = 0; j < v; j++)
{
if ((*components)[j] == -1)
{
//by breadth first method
connected_components_internal(edges, e, j, component, *components);
//by depth first method
// connected_components_recursive(edges, e, j, component, *components);
component++;
}
}
return component;
}
void printComponents(int *components, int v)
{
for (int i = 0; i < v; i++)
{
printf("%d vertix is in %d component \n", i, components[i]);
}
}
// int main()
// {
// int *components;
// edge *edges;
// //no of vertices
// unsigned const int v = 9;
// //no of edges
// unsigned const int e = v - 1;
// edges = malloc(e * sizeof(edge));
// if (edges == NULL)
// return 1;
// //Square component
// edges[0].first = 0;
// edges[0].second = 1;
// edges[1].first = 1;
// edges[1].second = 2;
// edges[2].first = 2;
// edges[2].second = 3;
// edges[3].first = 3;
// edges[3].second = 0;
// //Triangle component
// edges[4].first = 4;
// edges[4].second = 5;
// edges[5].first = 5;
// edges[5].second = 6;
// edges[6].first = 6;
// edges[6].second = 4;
// //Line component
// edges[7].first = 7;
// edges[7].second = 8;
// edges[8].first = 8;
// edges[8].second = 7;
// int c = connected_components(edges, e, v, &components);
// if (components == NULL)
// {
// free(edges);
// return 1;
// }
// printf("There are %d components \n", c);
// printComponents(components, v);
// free(edges);
// free(components);
// return 0;
// }