9
9
*
10
10
* Features:
11
11
*
12
- * Kruskal's algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph.
13
- * This means it finds a subset of the edges that forms a tree that includes every vertex,
14
- * where the total weight of all the edges in the tree is minimized. If the graph is not connected,
15
- * then it finds a minimum spanning forest (a minimum spanning tree for each connected component).
16
- * This algorithm first appeared in Proceedings of the American Mathematical Society, pp. 48–50 in 1956,
17
- * and was written by Joseph Kruskal.
12
+ * Kruskal's algorithm is a greedy algorithm in graph theory that finds a
13
+ * minimum spanning tree for a connected weighted graph.
14
+ * This means it finds a subset of the edges that forms a tree that includes
15
+ * every vertex, where the total weight of all the edges in the tree is minimized.
16
+ * If the graph is not connected, then it finds a minimum spanning forest
17
+ * (a minimum spanning tree for each connected component).
18
+ *
19
+ * This algorithm first appeared in :
20
+ * Proceedings of the American Mathematical Society, pp. 48–50 in 1956,
21
+ * and was written by Joseph Kruskal.
18
22
*
19
23
* http://en.wikipedia.org/wiki/Kruskal's_algorithm
20
24
*
21
- *By Contibutor:xmuliang
25
+ * By Contibutor:xmuliang
22
26
******************************************************************************/
23
27
24
28
#ifndef __KRUSKAL_MST_H__
@@ -59,8 +63,7 @@ namespace alg
59
63
/* *
60
64
* construct Kruskal's DataStrcuture by a given graph
61
65
*/
62
- Kruskal (const Graph & g)
63
- {
66
+ Kruskal (const Graph & g) {
64
67
INIT_LIST_HEAD (&m_pg);
65
68
66
69
Graph::Adjacent * a;
@@ -70,8 +73,7 @@ namespace alg
70
73
this ->num_vertex =g.vertex_count ();
71
74
}
72
75
73
- ~Kruskal ()
74
- {
76
+ ~Kruskal () {
75
77
KruskalAdjacent * pa, *pan;
76
78
list_for_each_entry_safe (pa, pan, &m_pg, pa_node){
77
79
list_del (&pa->pa_node );
@@ -85,8 +87,7 @@ namespace alg
85
87
/* *
86
88
* add an adjacent list to Kruskal's graph
87
89
*/
88
- void add_adjacent (const Graph::Adjacent & a)
89
- {
90
+ void add_adjacent (const Graph::Adjacent & a) {
90
91
KruskalAdjacent * pa = new KruskalAdjacent (a.vertex (), a.num_neigh );
91
92
list_add_tail (&pa->pa_node , &m_pg);
92
93
@@ -100,8 +101,7 @@ namespace alg
100
101
* lookup up a given id
101
102
* the related adjacent list is returned.
102
103
*/
103
- KruskalAdjacent * lookup (uint32_t id) const
104
- {
104
+ KruskalAdjacent * lookup (uint32_t id) const {
105
105
KruskalAdjacent * pa;
106
106
list_for_each_entry (pa, &m_pg, pa_node){
107
107
if (pa->v .id == id) { return pa;}
@@ -123,8 +123,7 @@ namespace alg
123
123
*
124
124
* Output: Vnew and Enew describe a minimal spanning tree
125
125
*/
126
- Graph * run ()
127
- {
126
+ Graph * run () {
128
127
UndirectedGraph * mst = new UndirectedGraph (); // empty Grapph
129
128
130
129
uint32_t mark[num_vertex];// mark the different set
@@ -137,57 +136,47 @@ namespace alg
137
136
uint32_t total_nodes=num_vertex; // nodes of the Kruskal
138
137
139
138
140
- while (true )
141
- {
142
- int weight = INT_MAX;
143
- uint32_t best_to;
144
- struct KruskalAdjacent * best_from;
139
+ while (true ) {
140
+ int weight = INT_MAX;
141
+ uint32_t best_to;
142
+ struct KruskalAdjacent * best_from;
145
143
146
- // choose the smallest edge from the original graph
147
- list_for_each_entry (pa, &m_pg, pa_node){
148
- if (!pa->heap .is_empty ()&&pa->heap .min_key ()<weight)
149
- {
150
- weight = pa->heap .min_key ();
151
- v = pa->heap .min_value ();
152
- best_to = v->id ;
153
- best_from = pa;
154
- }
155
- }
144
+ // choose the smallest edge from the original graph
145
+ list_for_each_entry (pa, &m_pg, pa_node){
146
+ if (!pa->heap .is_empty ()&&pa->heap .min_key ()<weight) {
147
+ weight = pa->heap .min_key ();
148
+ v = pa->heap .min_value ();
149
+ best_to = v->id ;
150
+ best_from = pa;
151
+ }
152
+ }
156
153
157
- // loop until the chosen edges to total_nodes-1
158
- if (flag<(total_nodes-1 )&&(weight != INT_MAX)) {
159
-
160
- // if the node not been added,construct it
161
- if ((*mst)[best_from->v .id ]==NULL )
162
- {
163
- mst->add_vertex (best_from->v .id );
164
- }
165
-
166
- if ((*mst)[best_to]==NULL )
167
- {
168
- mst->add_vertex (best_to);
169
- }
170
-
171
- // two nodes must belongs to set,to keep uncircle
172
- if (mark[best_from->v .id ]!=mark[best_to])
173
- {
174
-
175
- mst->add_edge (best_from->v .id , best_to, weight);
176
-
177
- uint32_t tmp=mark[best_to];
178
- for (uint32_t i=0 ;i<num_vertex;i++)
179
- {
180
- if (mark[i]==tmp)
181
- mark[i]=mark[best_from->v .id ];
182
- }
183
- flag++;
184
- }
185
-
186
- best_from->heap .delete_min ();
187
- lookup (best_to)->heap .delete_min ();
154
+ // loop until the chosen edges to total_nodes-1
155
+ if (flag<(total_nodes-1 )&&(weight != INT_MAX)) {
156
+ // if the node not been added,construct it
157
+ if ((*mst)[best_from->v .id ]==NULL ) {
158
+ mst->add_vertex (best_from->v .id );
159
+ }
160
+
161
+ if ((*mst)[best_to]==NULL ) {
162
+ mst->add_vertex (best_to);
163
+ }
164
+
165
+ // two nodes must belongs to set,to keep uncircle
166
+ if (mark[best_from->v .id ]!=mark[best_to]) {
167
+ mst->add_edge (best_from->v .id , best_to, weight);
168
+
169
+ uint32_t tmp=mark[best_to];
170
+ for (uint32_t i=0 ;i<num_vertex;i++) {
171
+ if (mark[i]==tmp)
172
+ mark[i]=mark[best_from->v .id ];
173
+ }
174
+ flag++;
175
+ }
176
+
177
+ best_from->heap .delete_min ();
178
+ lookup (best_to)->heap .delete_min ();
188
179
} else break ;
189
-
190
-
191
180
}
192
181
193
182
return mst;
@@ -196,8 +185,7 @@ namespace alg
196
185
/* *
197
186
* print the KruskalGraph
198
187
*/
199
- void print ()
200
- {
188
+ void print () {
201
189
struct KruskalAdjacent * pa;
202
190
printf (" Kruskal Graph: \n " );
203
191
list_for_each_entry (pa, &m_pg, pa_node){
0 commit comments