Skip to content

Commit aa210cc

Browse files
committed
Complete Implementation of Kruskal's
I have finished the implementation completly and tested it on the example graph given in wikipedia, with 10 vertex's and 21 edges. The image of the graph and MST is also being added to the Repo, along with the text file of the I/O during testing. Signed-off-by: Aditya Prasad <[email protected]>
1 parent ae87c5d commit aa210cc

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

Kruskal's.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# entering too many nodes and edges.The adjacency matrix is a good implementation
77
# for a graph when the number of edges is large.So i wont be using that here
88

9+
# for sorting purpose
10+
import operator
11+
912
# Vertex, which will represent each vertex in the graph.Each Vertex uses a dictionary
1013
# to keep track of the vertices to which it is connected, and the weight of each edge.
1114
class Vertex:
@@ -130,6 +133,8 @@ def __iter__(self):
130133

131134
# step 1 : Take all edges and sort them
132135

136+
'''
137+
not required as of now
133138
# Time Complexity of Solution:
134139
# Best Case O(n+k); Average Case O(n+k); Worst Case O(n+k),
135140
# where n is the size of the input array and k means the
@@ -151,6 +156,7 @@ def counting_sort(weights,max_weight):
151156
ndx += 1
152157
# reset the counter back to the set of zero's
153158
counter[i] -= 1
159+
'''
154160

155161
# now we have a optimal sorting function in hand, lets sort the list of edges.
156162
# a dictionary with weights of an edge and the vertexes involved in that edge.
@@ -159,18 +165,17 @@ def counting_sort(weights,max_weight):
159165
for ver1 in the_graph:
160166
# take every vertex ver1 is connected to = ver2
161167
for ver2 in ver1.getConnections():
162-
# make the dictionary with the weights and the 2 vertex's involved with the edge (thier key)
163-
vrwght[ver1.connectedTo[ver2]]=[ver1.getId(),ver2.getId()]
168+
# make the dictionary with the weights and the 2 vertex's involved with the
169+
# edge (thier key) use the pair of vertex's id as the key to avoid uniqueness
170+
# problems in the dictionary, mutliple edges might have the SAME weight
171+
vrwght[ver1.getId(),ver2.getId()]=[ver1.connectedTo[ver2]]
164172

165173
print "\nThe edges with thier unsorted weights are"
166174
print vrwght
167175

168-
temp_weights=vrwght.keys()
169-
counting_sort(temp_weights,max_weight)
170176

171-
sorted_weights={}
172-
for weight in temp_weights:
173-
sorted_weights[weight]=vrwght[weight]
177+
178+
sorted_weights=sorted(vrwght.items(), key=operator.itemgetter(1))
174179

175180
print "\nAfter sorting"
176181
print sorted_weights
@@ -265,28 +270,44 @@ def union(self, *objects):
265270
# execute FIND for all the vertex's in the_graph
266271
X[the_graph.getVertex(vertex_key)]
267272

273+
274+
275+
276+
for i in range(len(sorted_weights)):
277+
if(X[the_graph.getVertex(sorted_weights[i][0][0])]==X[the_graph.getVertex(sorted_weights[i][0][1])]):
278+
pass
279+
else:
280+
MST[sorted_weights[i][0]]=sorted_weights[i][1]
281+
X.union(the_graph.getVertex(sorted_weights[i][0][0]),the_graph.getVertex(sorted_weights[i][0][1]))
282+
283+
'''
268284
# now the UNION.
269-
for weight in sorted_weights:
285+
for vertex_pair in sorted_weights:
286+
print vertex_pair
270287
# here sorted_weights[weight] gives the set of 2 vertex's involved in the that edge
271-
if(X[the_graph.getVertex(sorted_weights[weight][0])]==X[the_graph.getVertex(sorted_weights[weight][1])]):
288+
if(X[the_graph.getVertex(vertex_pair[0][0])]==X[the_graph.getVertex(vertex_pair[0][1])]):
272289
# if both vertices have the same parent (name) then they are in the same set, so ignore this edge
273290
pass
274291
else:
275292
# else as they belong to different sets we can ADD this edge to the MST (MST will be a subset of sorted_weights)
276-
MST[weight]=sorted_weights[weight]
293+
MST[vertex_pair[0]]=sorted_weights[vertex_pair[0]]
277294
# and merge the sets these two vertices belong to thus we call union on them.
278-
X.union(the_graph.getVertex(sorted_weights[weight][0]),the_graph.getVertex(sorted_weights[weight][1]))
295+
X.union(the_graph.getVertex(vertex_pair[0]),the_graph.getVertex(vertex_pair[1]))
296+
'''
279297

280298
# thus we have the MST done
281299

282300
print " \n\nIn the graph with these vertex's"
283301
print the_graph.getVertices()
284302

285-
print "\n With these edges between the vertexes given above, we obtain a Minimal Spanning Tree\n"
303+
print "\n With these "+str(len(MST))+" edges between the vertexes given above, we obtain a Minimal Spanning Tree\n"
286304
print MST
287305

288306
print "\n Please note this is a dictionary with key as the weight of the edge and value as the key's of the two vertex's involved in this edge"
289-
307+
308+
# I HAVE TESTED THIS IMPLEMENTATION WITH THE SAMPLE PROBLEM GIVEN IN WIKIPEDIA
309+
# THE IMAGE OF THE GRAPH AND THE ONLY MST IS INCLUDED IN THE REPO, ALONG WITH THE
310+
# COMMANDLINE I/O OF TESTING, BOTH ARE SAVED AS Kruskal_test (.jpg and .txt respectively)
290311

291312

292313

0 commit comments

Comments
 (0)