Repository navigation
Expand file tree
/
Copy pathOldImplementation.py
More file actions
211 lines (158 loc) · 5.63 KB
/
Copy pathOldImplementation.py
File metadata and controls
211 lines (158 loc) · 5.63 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class Node():
def __init__(self, name, neighbours, type):
self.name = name
self.neighbours = neighbours
self.type = ""
self.coordinate = (0,0)
self.colour = (0,0,0)
def __str__(self):
return self.name
def __name__(self):
return self.name
class Edge():
def __init__(self, u, v):
self.u = u
self.v = v
self.name = u.name + v.name
self.type = ""
self.direction = None
def GetAB(u,v,Set_):
Set_.append(u)
for node in u.neighbours:
if node not in Set_ and node is not v:
Set_ = GetAB(node,u,Set_)
return Set_
def CheckABx(A,B,Leaf):
for i in A:
if i in Leaves:
for x1 in B:
for x2 in B:
if x1 != x2 and x1 in Leaves and x2 in Leaves:
if ({i.name,Leaf.name},{x1.name,x2.name}) not in Splits and ({x1.name,x2.name},{i.name,Leaf.name}) not in Splits:
return False
return True
def StrongEdgeStem(Edge):
Edge.type = "StrongEdge"
def DirectedEdge(Edge, Direction):
Edge.type = "Directed"
Edge.direction = Direction
def WeakStemEdge(Edge):
Edge.type = "WeakEdge"
def FindStem(Leaf):
for Edge in NetworkEdges:
A = GetAB(Edge.u,Edge.v,[])
B = GetAB(Edge.v,Edge.u,[])
Ac = CheckABx(A,B,Leaf)
Bc = CheckABx(B,A,Leaf)
if Ac and Bc:
StrongEdgeStem(Edge)
elif Ac and not Bc:
DirectedEdge(Edge, Edge.u)
elif Bc and not Ac:
DirectedEdge(Edge, Edge.v)
elif not Ac and not Bc:
WeakStemEdge(Edge)
def GetNextEdge(edge):
for i in NetworkEdges:
if i.u is edge.direction or i.v is edge.direction:
if i.direction is not edge.direction:
return i
return edge.direction
def WeakEdgeConstructor(Edges, newLeaf):
TempLeaves = []
global InternalNodes
for i in Edges:
if i.u not in TempLeaves:
TempLeaves.append(i.u)
if i.v not in TempLeaves:
TempLeaves.append(i.v)
TempNeighbours = []
for i in TempLeaves:
for j in i.neighbours:
if j not in TempLeaves and j not in TempNeighbours:
TempNeighbours.append(j)
AdjacentEdges = []
for i in NetworkEdges:
if (i.u in TempLeaves) ^ (i.v in TempLeaves):
AdjacentEdges.append(i)
InternalNodes += 1
InternalNode = Node("InternalNode" + str(InternalNodes), TempNeighbours, "Internal")
NetworkLeaves.append(InternalNode)
for i in TempNeighbours:
for j in i.neighbours:
if j in TempLeaves:
i.neighbours.remove(j)
i.neighbours.append(InternalNode)
for i in AdjacentEdges:
if i.u in TempLeaves:
i.u = InternalNode
if i.v in TempLeaves:
i.v = InternalNode
for i in TempLeaves:
NetworkLeaves.remove(i)
for i in Edges:
NetworkEdges.remove(i)
StemVertexConstructor(InternalNode, newLeaf)
def StemVertexConstructor(node, newLeaf):
node.neighbours.append(newLeaf)
NetworkLeaves.append(newLeaf)
NetworkEdges.append(Edge(node, newLeaf))
newLeaf.neighbours.append(node)
def ConstructNetwork(NewLeaf):
weakEdges = []
for edge in NetworkEdges:
if edge.type == "StrongEdge":
print("Strong", NewLeaf.name, edge.name)
NetworkEdges.remove(edge)
global InternalNodes
InternalNodes += 1
InternalNode = Node("InternalNode"+str(InternalNodes), [edge.u,edge.v,NewLeaf],"Internal")
NetworkLeaves.append(InternalNode)
NetworkLeaves.append(NewLeaf)
NetworkEdges.append(Edge(edge.u,InternalNode))
NetworkEdges.append(Edge(edge.v,InternalNode))
NetworkEdges.append(Edge(NewLeaf,InternalNode))
edge.u.neighbours.remove(edge.v)
edge.v.neighbours.remove(edge.u)
edge.u.neighbours.append(InternalNode)
edge.v.neighbours.append(InternalNode)
NewLeaf.neighbours.append(InternalNode)
return
if edge.type == "WeakEdge":
print('weak', NewLeaf.name, edge.name)
weakEdges.append(edge)
if len(weakEdges) != 0:
WeakEdgeConstructor(weakEdges, NewLeaf)
return
constructing = True
edge = NetworkEdges[0]
while constructing:
edge = GetNextEdge(edge)
if type(edge) is not Edge:
constructing = False
StemVertexConstructor(edge, NewLeaf)
NetworkLeaves = []
NetworkEdges = []
Leaves = []
Splits = []
InternalNodes = 0
def ConstructTree(file_):
#Reading the file and constructing a set of leaves and a set of the splits of the network
with open(file_) as f:
lines = f.readlines()
TempLeaves = lines[0].split("(")[1].split(")")[0].split(",")
for i in TempLeaves:
Leaves.append(Node(i,[],"Leaf"))
for i in range(1,len(lines)):
line = lines[i]
Splits.append(({line[1],line[3]},{line[5],line[7]}))
#Step 1: Creating the first edge of the network
Leaves[0].neighbours.append(Leaves[1]), NetworkLeaves.append(Leaves[0])
Leaves[1].neighbours.append(Leaves[0]), NetworkLeaves.append(Leaves[1])
NetworkEdges.append(Edge(Leaves[0],Leaves[1]))
#Step 2: Find stem for next leaf
length = len(Leaves) - 1
for i in range(1,length):
FindStem(Leaves[i+1])
ConstructNetwork(Leaves[i+1])
return NetworkLeaves, NetworkEdges