6
6
# entering too many nodes and edges.The adjacency matrix is a good implementation
7
7
# for a graph when the number of edges is large.So i wont be using that here
8
8
9
+ # for sorting purpose
10
+ import operator
11
+
9
12
# Vertex, which will represent each vertex in the graph.Each Vertex uses a dictionary
10
13
# to keep track of the vertices to which it is connected, and the weight of each edge.
11
14
class Vertex :
@@ -130,6 +133,8 @@ def __iter__(self):
130
133
131
134
# step 1 : Take all edges and sort them
132
135
136
+ '''
137
+ not required as of now
133
138
# Time Complexity of Solution:
134
139
# Best Case O(n+k); Average Case O(n+k); Worst Case O(n+k),
135
140
# where n is the size of the input array and k means the
@@ -151,6 +156,7 @@ def counting_sort(weights,max_weight):
151
156
ndx += 1
152
157
# reset the counter back to the set of zero's
153
158
counter[i] -= 1
159
+ '''
154
160
155
161
# now we have a optimal sorting function in hand, lets sort the list of edges.
156
162
# a dictionary with weights of an edge and the vertexes involved in that edge.
@@ -159,18 +165,17 @@ def counting_sort(weights,max_weight):
159
165
for ver1 in the_graph :
160
166
# take every vertex ver1 is connected to = ver2
161
167
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 ]]
164
172
165
173
print "\n The edges with thier unsorted weights are"
166
174
print vrwght
167
175
168
- temp_weights = vrwght .keys ()
169
- counting_sort (temp_weights ,max_weight )
170
176
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 ))
174
179
175
180
print "\n After sorting"
176
181
print sorted_weights
@@ -265,28 +270,44 @@ def union(self, *objects):
265
270
# execute FIND for all the vertex's in the_graph
266
271
X [the_graph .getVertex (vertex_key )]
267
272
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
+ '''
268
284
# now the UNION.
269
- for weight in sorted_weights :
285
+ for vertex_pair in sorted_weights:
286
+ print vertex_pair
270
287
# 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])]):
272
289
# if both vertices have the same parent (name) then they are in the same set, so ignore this edge
273
290
pass
274
291
else:
275
292
# 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] ]
277
294
# 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
+ '''
279
297
280
298
# thus we have the MST done
281
299
282
300
print " \n \n In the graph with these vertex's"
283
301
print the_graph .getVertices ()
284
302
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 "
286
304
print MST
287
305
288
306
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)
290
311
291
312
292
313
0 commit comments