-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion3.py
64 lines (47 loc) · 1.98 KB
/
question3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
from my_helpers import myPrint, \
headerPrint, \
subHeaderPrint # Some simple text rendering functions
#------------------------------ Question #3 ------------------------------------
# Udacity text:
# Given an undirected graph G, find the minimum spanning tree within G.
# A minimum spanning tree connects all vertices in a graph with the smallest
# possible total weight of edges. Your function should take in and return an
# adjacency list.
# Vertices are represented as unique strings. The function definition should
# be question3(G)
# Adjacency list (with additions) that was in Udacity assignment.
G = {'A': [('B', 2), ('C',132)],
'B': [('A', 2), ('C', 8)],
'C': [('B', 5), ('A', 42)]}
def question3(G):
""" Determines Minimum Spanning Tree (MST) by analysing given Graph (G) """
# Prep for Minimum Spanning Tree
MST = {}
# Initialization for `edges`
edges = []
# Extract the graph information into easier to use format
for union in G.keys():
for (vertex, weight) in G[union]:
edges.append((weight, union, vertex))
# Sort `edges` by `weight`
for (weight, union, vertex) in sorted(edges):
# Add `edges` that don't contain `union` to `MST``
if union not in MST.keys() or vertex not in MST.keys():
if union not in MST.keys():
MST[union] = []
if vertex not in MST.keys():
MST[vertex] = []
# Append cleared data
MST[union].append((vertex, weight))
MST[vertex].append((union, weight))
return MST
headerPrint('Question 3 - Finding Minimum Spanning Tree')
subHeaderPrint('This is the full starting graph provided to the program:')
myPrint(G)
myPrint('')
subHeaderPrint('Determining Minimum Spanning Tree (MST)...')
myPrint('Result: ' )
myPrint(question3(G))
print('')
input('Press the enter key to continue...')