Skip to content

Commit 69f8273

Browse files
committed
Add proof of working for Kruskal's.py and Update Readme
I added the .txt file showing the I/O of the test with a graph of 21 edges and 10 vertex's. And this graph and its MST is shown in the .jpg, both named Kruskal_test. I removed the query for the maximum value of the edge weights As we are not using counting sort. If needed we should find it from the edges that we recive. I updated the README.md with information about the last two scripts that I added to the repo. Signed-off-by: Aditya Prasad <[email protected]>
1 parent aa210cc commit 69f8273

File tree

4 files changed

+278
-4
lines changed

4 files changed

+278
-4
lines changed

Kruskal's.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ def __iter__(self):
125125
# add the edge with this info
126126
the_graph.addEdge(node1_key,node2_key,cost)
127127
the_graph.addEdge(node2_key,node1_key,cost)
128-
128+
"""
129+
AS we wont be using counting sort
129130
print "enter the maximum weight possible for any of edges in the graph"
130131
max_weight=int(raw_input())
131-
132+
"""
132133
# graph DONE - start MST finding
133134

134135
# step 1 : Take all edges and sort them
@@ -300,10 +301,10 @@ def union(self, *objects):
300301
print " \n\nIn the graph with these vertex's"
301302
print the_graph.getVertices()
302303

303-
print "\n With these "+str(len(MST))+" edges between the vertexes given above, we obtain a Minimal Spanning Tree\n"
304+
print "\nWith these "+str(len(MST))+" edges between the vertexes given above, we obtain a Minimal Spanning Tree\n"
304305
print MST
305306

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"
307+
print "\nPlease 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"
307308

308309
# I HAVE TESTED THIS IMPLEMENTATION WITH THE SAMPLE PROBLEM GIVEN IN WIKIPEDIA
309310
# THE IMAGE OF THE GRAPH AND THE ONLY MST IS INCLUDED IN THE REPO, ALONG WITH THE

Kruskal_test.jpg

17.8 KB
Loading

Kruskal_test.txt

+257
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
2+
Type "copyright", "credits" or "license()" for more information.
3+
>>> ================================ RESTART ================================
4+
>>>
5+
enter the number of nodes in the graph
6+
10
7+
enter the Node no:1's key
8+
a
9+
enter the Node no:2's key
10+
b
11+
enter the Node no:3's key
12+
c
13+
enter the Node no:4's key
14+
d
15+
enter the Node no:5's key
16+
e
17+
enter the Node no:6's key
18+
f
19+
enter the Node no:7's key
20+
g
21+
enter the Node no:8's key
22+
h
23+
enter the Node no:9's key
24+
i
25+
enter the Node no:10's key
26+
j
27+
enter the number of edges in the graph
28+
21
29+
For the Edge no:1
30+
of the 2 nodes involved in this edge
31+
enter the first Node's key
32+
a
33+
34+
enter the second Node's key
35+
b
36+
37+
enter the cost (or weight) of this edge (or arc) - an integer
38+
9
39+
For the Edge no:2
40+
of the 2 nodes involved in this edge
41+
enter the first Node's key
42+
a
43+
44+
enter the second Node's key
45+
c
46+
47+
enter the cost (or weight) of this edge (or arc) - an integer
48+
9
49+
For the Edge no:3
50+
of the 2 nodes involved in this edge
51+
enter the first Node's key
52+
a
53+
54+
enter the second Node's key
55+
f
56+
57+
enter the cost (or weight) of this edge (or arc) - an integer
58+
8
59+
For the Edge no:4
60+
of the 2 nodes involved in this edge
61+
enter the first Node's key
62+
a
63+
64+
enter the second Node's key
65+
j
66+
67+
enter the cost (or weight) of this edge (or arc) - an integer
68+
18
69+
For the Edge no:5
70+
of the 2 nodes involved in this edge
71+
enter the first Node's key
72+
b
73+
74+
enter the second Node's key
75+
c
76+
77+
enter the cost (or weight) of this edge (or arc) - an integer
78+
3
79+
For the Edge no:6
80+
of the 2 nodes involved in this edge
81+
enter the first Node's key
82+
b
83+
84+
enter the second Node's key
85+
e
86+
87+
enter the cost (or weight) of this edge (or arc) - an integer
88+
6
89+
For the Edge no:7
90+
of the 2 nodes involved in this edge
91+
enter the first Node's key
92+
c
93+
94+
enter the second Node's key
95+
e
96+
97+
enter the cost (or weight) of this edge (or arc) - an integer
98+
4
99+
For the Edge no:8
100+
of the 2 nodes involved in this edge
101+
enter the first Node's key
102+
c
103+
104+
enter the second Node's key
105+
d
106+
107+
enter the cost (or weight) of this edge (or arc) - an integer
108+
2
109+
For the Edge no:9
110+
of the 2 nodes involved in this edge
111+
enter the first Node's key
112+
c
113+
114+
enter the second Node's key
115+
f
116+
117+
enter the cost (or weight) of this edge (or arc) - an integer
118+
9
119+
For the Edge no:10
120+
of the 2 nodes involved in this edge
121+
enter the first Node's key
122+
d
123+
124+
enter the second Node's key
125+
e
126+
127+
enter the cost (or weight) of this edge (or arc) - an integer
128+
2
129+
For the Edge no:11
130+
of the 2 nodes involved in this edge
131+
enter the first Node's key
132+
d
133+
134+
enter the second Node's key
135+
f
136+
137+
enter the cost (or weight) of this edge (or arc) - an integer
138+
8
139+
For the Edge no:12
140+
of the 2 nodes involved in this edge
141+
enter the first Node's key
142+
d
143+
144+
enter the second Node's key
145+
g
146+
147+
enter the cost (or weight) of this edge (or arc) - an integer
148+
9
149+
For the Edge no:13
150+
of the 2 nodes involved in this edge
151+
enter the first Node's key
152+
e
153+
154+
enter the second Node's key
155+
g
156+
157+
enter the cost (or weight) of this edge (or arc) - an integer
158+
9
159+
For the Edge no:14
160+
of the 2 nodes involved in this edge
161+
enter the first Node's key
162+
f
163+
164+
enter the second Node's key
165+
g
166+
167+
enter the cost (or weight) of this edge (or arc) - an integer
168+
7
169+
For the Edge no:15
170+
of the 2 nodes involved in this edge
171+
enter the first Node's key
172+
f
173+
174+
enter the second Node's key
175+
i
176+
177+
enter the cost (or weight) of this edge (or arc) - an integer
178+
9
179+
For the Edge no:16
180+
of the 2 nodes involved in this edge
181+
enter the first Node's key
182+
f
183+
184+
enter the second Node's key
185+
j
186+
187+
enter the cost (or weight) of this edge (or arc) - an integer
188+
10
189+
For the Edge no:17
190+
of the 2 nodes involved in this edge
191+
enter the first Node's key
192+
g
193+
194+
enter the second Node's key
195+
i
196+
197+
enter the cost (or weight) of this edge (or arc) - an integer
198+
5
199+
For the Edge no:18
200+
of the 2 nodes involved in this edge
201+
enter the first Node's key
202+
g
203+
204+
enter the second Node's key
205+
h
206+
207+
enter the cost (or weight) of this edge (or arc) - an integer
208+
4
209+
For the Edge no:19
210+
of the 2 nodes involved in this edge
211+
enter the first Node's key
212+
h
213+
214+
enter the second Node's key
215+
i
216+
217+
enter the cost (or weight) of this edge (or arc) - an integer
218+
1
219+
For the Edge no:20
220+
of the 2 nodes involved in this edge
221+
enter the first Node's key
222+
h
223+
224+
enter the second Node's key
225+
j
226+
227+
enter the cost (or weight) of this edge (or arc) - an integer
228+
4
229+
For the Edge no:21
230+
of the 2 nodes involved in this edge
231+
enter the first Node's key
232+
i
233+
234+
enter the second Node's key
235+
j
236+
237+
enter the cost (or weight) of this edge (or arc) - an integer
238+
3
239+
enter the maximum weight possible for any of edges in the graph
240+
20
241+
242+
The edges with thier unsorted weights are
243+
{('g', 'e'): [9], ('b', 'c'): [3], ('e', 'd'): [2], ('h', 'i'): [1], ('d', 'e'): [2], ('i', 'h'): [1], ('f', 'd'): [8], ('c', 'b'): [3], ('e', 'c'): [4], ('a', 'b'): [9], ('g', 'd'): [9], ('c', 'f'): [9], ('f', 'a'): [8], ('e', 'g'): [9], ('d', 'f'): [8], ('d', 'g'): [9], ('c', 'a'): [9], ('h', 'j'): [4], ('e', 'b'): [6], ('i', 'g'): [5], ('d', 'c'): [2], ('c', 'e'): [4], ('g', 'h'): [4], ('b', 'a'): [9], ('i', 'j'): [3], ('j', 'h'): [4], ('a', 'f'): [8], ('j', 'a'): [18], ('i', 'f'): [9], ('f', 'j'): [10], ('c', 'd'): [2], ('h', 'g'): [4], ('f', 'c'): [9], ('g', 'i'): [5], ('j', 'f'): [10], ('g', 'f'): [7], ('j', 'i'): [3], ('b', 'e'): [6], ('f', 'g'): [7], ('a', 'c'): [9], ('a', 'j'): [18], ('f', 'i'): [9]}
244+
245+
After sorting
246+
[(('h', 'i'), [1]), (('i', 'h'), [1]), (('e', 'd'), [2]), (('d', 'e'), [2]), (('d', 'c'), [2]), (('c', 'd'), [2]), (('b', 'c'), [3]), (('c', 'b'), [3]), (('i', 'j'), [3]), (('j', 'i'), [3]), (('e', 'c'), [4]), (('h', 'j'), [4]), (('c', 'e'), [4]), (('g', 'h'), [4]), (('j', 'h'), [4]), (('h', 'g'), [4]), (('i', 'g'), [5]), (('g', 'i'), [5]), (('e', 'b'), [6]), (('b', 'e'), [6]), (('g', 'f'), [7]), (('f', 'g'), [7]), (('f', 'd'), [8]), (('f', 'a'), [8]), (('d', 'f'), [8]), (('a', 'f'), [8]), (('g', 'e'), [9]), (('a', 'b'), [9]), (('g', 'd'), [9]), (('c', 'f'), [9]), (('e', 'g'), [9]), (('d', 'g'), [9]), (('c', 'a'), [9]), (('b', 'a'), [9]), (('i', 'f'), [9]), (('f', 'c'), [9]), (('a', 'c'), [9]), (('f', 'i'), [9]), (('f', 'j'), [10]), (('j', 'f'), [10]), (('j', 'a'), [18]), (('a', 'j'), [18])]
247+
248+
249+
In the graph with these vertex's
250+
['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j']
251+
252+
With these edges between the vertexes given above, we obtain a Minimal Spanning Tree
253+
254+
{('b', 'c'): [3], ('g', 'f'): [7], ('e', 'd'): [2], ('h', 'i'): [1], ('g', 'h'): [4], ('f', 'd'): [8], ('f', 'a'): [8], ('d', 'c'): [2], ('i', 'j'): [3]}
255+
256+
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
257+
>>>

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@
2222
This was the first real usefull python script I wrote, It was just after I had heard about
2323
BeautifulSoup and I wanted to make a program which downloads manga.
2424

25+
* **Maximum_subarray.py**
26+
This is the solution to the problem of finding the subarray with maximum sum,
27+
It has been solved in this script using both the dynamic programming approach (O(n)) as well
28+
as the divide and conquer approach (O(nlog(n)). I have explained the whole thing in
29+
my [Blog][2]
30+
31+
* **Kruskal's.py**
32+
This is a script to find the [Minimal Spanning Tree][3] of a connected, undirected graph,
33+
using Kruskal's algorithm which is a greedy algorithm. I have also included the I/O of
34+
a test I conducted with a graph and the image of the graph and its only MST.
35+
These are the Kruskal\_test.txt and Kruskal\_test.jpg
36+
I have used part of this code to answer [this question][4]
37+
2538
[1]: https://superuser.com/questions/330297/automate-logging-in-through-sonicwall/785792?noredirect=1#comment1023176_785792
39+
[2]: http://relativetoaditya.blogspot.in/2014/12/maximum-subarray.html
40+
[3]: https://en.wikipedia.org/wiki/Minimum_spanning_tree
41+
[4]: https://stackoverflow.com/questions/14369739/creating-adjacency-lists-from-dicts-in-python/27380835#27380835

0 commit comments

Comments
 (0)