-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem107.py
60 lines (54 loc) · 1.66 KB
/
problem107.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
import math
def prims(adjMatrix):
vertices = len(adjMatrix)
tree = [0]
weights = []
edges = [[0 for x in range(vertices)] for x in range(vertices)]
while len(tree) < vertices:
minIndex = 0
minEdge = math.inf
minRow = 0
# for every node in the tree
for i in range(len(tree)):
row = adjMatrix[tree[i]]
# for every edge with this node
for j in range(len(row)):
edge = row[j]
if edge < minEdge and j not in tree:
minEdge = edge
minRow = i
minIndex = j
tree.append(minIndex)
weights.append(minEdge)
edges[minRow][minIndex] = minEdge
return(sum(weights))
def readGraph():
file = open("p107_network.txt")
lines = file.readlines()
numVertices = len(lines)
edges = [[math.inf for x in range(numVertices)] for x in range(numVertices)]
for i in range(len(lines)):
parts = lines[i].split(",")
for j in range(len(parts)):
if not parts[j].strip() == '-':
edges[i][j] = int(parts[j].strip())
edges[j][i] = int(parts[j].strip())
file.close()
return edges
def sumGraph(graph):
total = 0
for i in range(len(graph)):
row = graph[i]
for j in row:
if not j == math.inf:
total += j
return total/2
# graph edges with weights
# diagram of graph is shown above
graph = readGraph()
oldWeight = sumGraph(graph)
# pass the # of vertices and the graph to run prims algorithm
newWeight = prims(graph)
print(oldWeight)
print(newWeight)
print(oldWeight-newWeight)