-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prufer Code Generator.py
130 lines (102 loc) · 3.26 KB
/
Prufer Code Generator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from copy import deepcopy
class Tree:
def __init__(self, size):
if size < 2:
print("Size of tree must be 2 or higher")
return
self.numNodes = size
self.numEdges = size - 1
self.matrix = [[0]*size for i in range(size)]
self.workingMatrix = [[0]*size for i in range(size)]
def addEdge(self, v, w):
self.matrix[v-1][w-1] = 1
self.matrix[w-1][v-1] = 1
def printMatrix(self):
print(" ", end="") #offset for labels
for colLabel in range(1, self.numNodes+1):
print(f"{colLabel:4}", end="")
print()
#print each row with a label
for i in range(self.numNodes):
print(f"{i + 1: 4}", end="") #row label
for j in range(self.numNodes):
print(f"{self.matrix[i][j]:4}", end="")
print()
#finding next leaf
def findLeaf(self):
for i in range(self.numNodes):
count = 0
for j in range(self.numNodes):
if self.workingMatrix[i][j]:
count += 1
if count == 1:
return i
return -1 #no leaf found
#clearing all connections from a node, removing it from list
def clearConnections(self, node):
for i in range(self.numNodes):
self.workingMatrix[node][i] = 0
self.workingMatrix[i][node] = 0
#generating prufer code
def generatePrufer(self):
self.workingMatrix = deepcopy(self.matrix)
code = []
while len(code) < self.numNodes - 2:
nextLeaf = self.findLeaf()
for j in range(len(self.workingMatrix[nextLeaf])):
if self.workingMatrix[nextLeaf][j]:
code.append(j+1)
self.clearConnections(nextLeaf)
return code
def generateTreeFromPrufer(code):
code = [i-1 for i in code]
#print(code)
def nextAvailableNode(code, nodes):
for i in nodes:
if i not in code:
return i
size = len(code) + 2
nodes = [i for i in range(size)]
connections = 0
tree = Tree(size)
while connections < tree.numEdges:
if code:
n = nextAvailableNode(code, nodes)
tree.addEdge(n+1, code[0]+1)
if len(code) > 1:
code = code[1:]
else:
code = []
nodes = nodes[0:nodes.index(n)] + nodes[nodes.index(n)+1:]
else:
tree.addEdge(nodes[0]+1, nodes[1]+1)
connections += 1
return tree
#testing
t = Tree(12)
#adding edges
t.addEdge(1, 5)
t.addEdge(1, 8)
t.addEdge(1, 10)
t.addEdge(2, 7)
t.addEdge(2, 12)
t.addEdge(3, 8)
t.addEdge(4, 10)
t.addEdge(5, 12)
t.addEdge(5, 1)
t.addEdge(6, 12)
t.addEdge(7, 2)
t.addEdge(8, 3)
t.addEdge(8, 9)
t.addEdge(8, 1)
t.addEdge(9, 8)
t.addEdge(10, 4)
t.addEdge(10, 1)
t.addEdge(11, 12)
t.addEdge(12, 2)
t.addEdge(12, 5)
t.addEdge(12, 6)
t.addEdge(12, 11)
code = t.generatePrufer()
newt = generateTreeFromPrufer(code)
print(newt.generatePrufer() == t.generatePrufer()) #true, works in reverse