-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler107.py
23 lines (21 loc) · 861 Bytes
/
euler107.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
edges = []
lines = [line.strip() for line in open('network.txt')]
for i in range(len(lines)):
line = lines[i]
elements = line.split(',')
for j in range(len(elements)):
if elements[j] != '-':
edges.append((i, j, int(elements[j])))
# Perform Prim's algorithm
nodesInMST = [0]
edgesInMST = []
while sorted(nodesInMST) != list(range(len(lines))):
minConnectsToTree = min([q for q in edges if q[0] in nodesInMST and q[
1] not in nodesInMST], key=lambda x: x[2])
edgesInMST.append(minConnectsToTree)
nodesInMST.append(minConnectsToTree[1])
# divide by two because it is a directed graph and edges are added twice
curWeight = sum(x[2] for x in edges) / 2
mstWeight = sum([x[2] for x in edgesInMST])
saving = curWeight - mstWeight
print(("{0} - {1} = {2}".format(curWeight, mstWeight, saving)))