Skip to content

Commit 9f78925

Browse files
author
fuli
committed
update Makefile, pretty the kruskal_mst.h
1 parent 2685718 commit 9f78925

File tree

3 files changed

+58
-69
lines changed

3 files changed

+58
-69
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ PROGRAMS = m_based \
5353
random_demo \
5454
k-means_demo \
5555
kmp_demo \
56-
heap_sort_demo
56+
heap_sort_demo \
57+
kruskal_mst_demo
5758

5859
all: $(PROGRAMS)
5960

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@
7777
####贡献者 ( Contributors ) :
7878
Samana : for heavy work of MSVC compatability
7979
wycg1984: for K-Means
80-
xmuliang: for HeapSort
80+
xmuliang: for HeapSort, Kruskal MST

include/kruskal_mst.h

Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
*
1010
* Features:
1111
*
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.
1822
*
1923
* http://en.wikipedia.org/wiki/Kruskal's_algorithm
2024
*
21-
*By Contibutor:xmuliang
25+
* By Contibutor:xmuliang
2226
******************************************************************************/
2327

2428
#ifndef __KRUSKAL_MST_H__
@@ -59,8 +63,7 @@ namespace alg
5963
/**
6064
* construct Kruskal's DataStrcuture by a given graph
6165
*/
62-
Kruskal(const Graph & g)
63-
{
66+
Kruskal(const Graph & g) {
6467
INIT_LIST_HEAD(&m_pg);
6568

6669
Graph::Adjacent * a;
@@ -70,8 +73,7 @@ namespace alg
7073
this->num_vertex=g.vertex_count();
7174
}
7275

73-
~Kruskal()
74-
{
76+
~Kruskal() {
7577
KruskalAdjacent * pa, *pan;
7678
list_for_each_entry_safe(pa, pan, &m_pg, pa_node){
7779
list_del(&pa->pa_node);
@@ -85,8 +87,7 @@ namespace alg
8587
/**
8688
* add an adjacent list to Kruskal's graph
8789
*/
88-
void add_adjacent(const Graph::Adjacent & a)
89-
{
90+
void add_adjacent(const Graph::Adjacent & a) {
9091
KruskalAdjacent * pa = new KruskalAdjacent(a.vertex(), a.num_neigh);
9192
list_add_tail(&pa->pa_node, &m_pg);
9293

@@ -100,8 +101,7 @@ namespace alg
100101
* lookup up a given id
101102
* the related adjacent list is returned.
102103
*/
103-
KruskalAdjacent * lookup(uint32_t id) const
104-
{
104+
KruskalAdjacent * lookup(uint32_t id) const {
105105
KruskalAdjacent * pa;
106106
list_for_each_entry(pa, &m_pg, pa_node){
107107
if (pa->v.id == id) { return pa;}
@@ -123,8 +123,7 @@ namespace alg
123123
*
124124
* Output: Vnew and Enew describe a minimal spanning tree
125125
*/
126-
Graph * run()
127-
{
126+
Graph * run() {
128127
UndirectedGraph * mst = new UndirectedGraph(); // empty Grapph
129128

130129
uint32_t mark[num_vertex];// mark the different set
@@ -137,57 +136,47 @@ namespace alg
137136
uint32_t total_nodes=num_vertex; //nodes of the Kruskal
138137

139138

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;
145143

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+
}
156153

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();
188179
} else break;
189-
190-
191180
}
192181

193182
return mst;
@@ -196,8 +185,7 @@ namespace alg
196185
/**
197186
* print the KruskalGraph
198187
*/
199-
void print()
200-
{
188+
void print() {
201189
struct KruskalAdjacent * pa;
202190
printf("Kruskal Graph: \n");
203191
list_for_each_entry(pa, &m_pg, pa_node){

0 commit comments

Comments
 (0)