forked from ArsalanKhairani/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimMST.py
48 lines (37 loc) · 1.47 KB
/
PrimMST.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
################################################################################
## Copyright(c) Arsalan Khairani, 2014 ##
## Recursive Depth First Search ##
## ##
## Depth first graph searching algorithm. Built-in List is used as stack ##
################################################################################file = open("graph3.txt")
def getscore(a, b):
return a / b
def main():
file = open("edges.txt")
info = file.readline().split(" ")
nodes = int(info[0])
edges = int(info[1])
graph = []
allVertices = set() # set
for line in file:
info = line.split(" ")
v1 = int(info[0])
if v1 not in allVertices:
allVertices.add(v1)
v2 = int (info[1])
if v2 not in allVertices:
allVertices.add(v2)
l = int (info[2])
graph.append((l, v1, v2))
g = sorted(graph)
X = set({graph[0][1]}) # Just intialization
T = []
while X != allVertices:
allEdges = [edge for edge in g if ((edge[1] in X and edge[2] not in X) or (edge[2] in X and edge[1] not in X))]
sortedEdges = sorted(allEdges)
e = sortedEdges[0]
T.append(e[0]) # add span to T
X.add(e[1] if e[1] not in X else e[2]) # Add a vertex to X
print (sum(T))
if __name__ == "__main__":
main()