-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpanningTree_Prims.py
66 lines (45 loc) · 1.16 KB
/
SpanningTree_Prims.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
64
65
66
def printDebug(what, msg):
# print(what)
# print(msg)
# print('-------')
pass
f = open('edges.txt', 'r')
nNodes, nEdges = f.readline().split(' ')
#printDebug(nNodes)
#printDebug(nEdges)
def parseData():
g = {}
for l in f:
u,v,c = [int(item) for item in l.split(' ')]
if u not in g: g[u] = [(v,c)]
else: g[u].append((v,c))
if v not in g: g[v] = [(u,c)]
else: g[v].append((u,c))
return g
def get_cheapes(g, x):
vertexes = [(item, g[item]) for item in x]
printDebug('x', x)
printDebug('vertexes', vertexes)
rU, rV = vertexes[0]
rV, rC = rV[0]
rU, rV, rC = (0,0, 9999999999)
for (u, adj) in vertexes:
for (v,c) in adj:
if rC > c and v not in x:
rU, rV, rC = u,v,c
ret = rU,rV,rC
printDebug('ret', ret)
return ret
def prims(g):
nodes = list(g.keys())
x = [nodes[0]]
t = []
while len(x) != len(nodes):
u,v,c = get_cheapes(g,x)
if (u,v,c) == (0,0,0): break
t.append((u,v,c))
x.append(v)
print(t)
print(sum([item[2] for item in t]))
g = parseData()
prims(g)