-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule1.py
240 lines (198 loc) · 9.22 KB
/
module1.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import string
#adding debugger
import pudb
class Node:
def __init__(self, suffixLink = None):
self.children = {}
self.start = 0
self.end = 0
self.suffixIndex = 0
if suffixLink is not None:
self.suffixLink = suffixLink
else:
self.suffixLink = self
def __repr__(self):
return "\nChildren: " + str({k:v for k,v in self.children.items() if v is not None}) + " \nStart: " + str(self.start) + " \nEnd: " + str(self.end)
class SuffixTreeNode:
# pu.db
def __init__(self, txt, s1):
# self.start = 0
# self.end = 0
# self.suffixIndex = 0
self.text = txt
self.root = None
self.Node = None
self.lastNewNode = None
self.activeNode = None
self.activeEdge = -1
self.activeLength = 0
self.remainingSuffixCount = 0
self.leafEnd = -1
self.rootEnd = None
self.splitEnd = None
self.size = -1
self.size1 = s1
self.MAXCHAR = 256
# self.maxHeight = 0
# self.substringStartIndex = 0
def __str__(self):
s = ""
for k,v in self.Node.children.iteritems():
s += "\nKeys: " + str(k) + " Value: " + str(v)
return s
#return "\nNode: " + str(self.Node.children.keys()) + "\nValue: " + str(self.Node.children.values()) + "\nStart: " + str(self.start) + "\nEnd: " + str(self.end)
def newNode(self, start, end):
#if self.root == None:
# self.root = Node()
self.Node = Node()
#for i in xrange(self.MAXCHAR):
# self.Node.children[i] = None
#self.Node.children.insert(i, None)
self.Node.suffixLink = self.root
self.start = start
self.end = end
self.SuffixIndex = -1
return self.Node
def edgeLength(self, n):
if n == self.root:
return 0
return n.end - n.start + 1
def walkDown(self, currNode):
if self.activeLength >= self.edgeLength(currNode):
self.activeEdge += self.edgeLength(currNode)
self.activeLength -= self.edgeLength(currNode)
self.activeNode = currNode
print "ActiveLength: " , self.activeLength
return 1
return 0
#not sure about this function being correct
def extendSuffixTree(self, pos):
self.leafEnd = pos
self.remainingSuffixCount += 1
self.lastNewNode = None
while self.remainingSuffixCount > 0:
if self.activeLength == 0:
self.activeEdge = pos
#There is no outgoing edge starting with activeEdge from activeNode
#if self.activeNode.children[ord(self.text[self.activeEdge])] == None:
#if self.text[self.activeEdge] not in self.activeNode.children:
if ord(self.text[self.activeEdge]) not in self.activeNode.children:
self.activeNode.children[ord(self.text[self.activeEdge])] = self.newNode(pos, self.leafEnd)
if self.lastNewNode != None:
self.lastNewNode.suffixLink = self.activeNode
self.lastNewNode = None
else: #There is an outgoing edge starting with activeEdge from activeNode
#next_node = self.activeNode.children[ord(self.text[self.activeEdge])]
next_node = self.activeNode
#if self.walkDown(next_node):
# continue
self.walkDown(next_node)
if self.text[next_node.start + self.activeLength] == self.text[pos]:
if self.lastNewNode != None and self.activeNode != self.root:
self.lastNewNode.suffixLink = self.activeNode
self.lastNewNode = None
self.activeLength += 1
break
self.splitEnd = next_node.start + self.activeLength - 1
#new internal node
split = self.newNode(next_node.start, self.splitEnd)
self.activeNode.children[ord(self.text[self.activeEdge])] = split
#new leaf coming out of new internal node
split.children[ord(self.text[pos])] = self.newNode(pos, self.leafEnd)
next_node.start += self.activeLength
split.children[ord(self.text[next_node.start])] = next_node
if self.lastNewNode != None:
self.lastNewNode.suffixLink = split
self.lastNewNode = split
self.remainingSuffixCount -= 1
if self.activeNode == self.root and self.activeLength > 0:
self.activeLength -= 1
self.activeEdge = pos - self.remainingSuffixCount + 1
elif self.activeNode != self.root:
self.activeNode = self.activeNode.suffixLink
def printf(self, i, j):
for k in xrange(i, j):
if self.text[k] != '#':
print "%c", self.text[k]
if k <= j:
print "#"
def setSuffixIndexByDFS(self, n, labelHeight):
""" Print the suffix tree along with setting suffix index
So tree will be printed in DFS manner
Each edge along with it's suffix index will be printed """
if n == {}:
print "------------------------BASE CASE MET------------------------"
return
if n.start != -1: # a non-root node
#self.printf(n.start, n.end)
#if you flip it it works
self.printf(n.end, n.start)
leaf = 1
#for i in xrange(self.MAXCHAR):
# if n.children[i] != None: #Current node is not a leaf as it has outgoing edges from it
# leaf = 0
# self.setSuffixIndexByDFS(n.children[i], labelHeight + self.edgeLength(n.children[i]))
for k,v in n.children.iteritems():
if n.children[k] is not {}:
leaf = 0
self.setSuffixIndexByDFS(n.children[k], labelHeight + self.edgeLength(n.children[k]))
if leaf == 1:
for i in xrange(n.start, n.end):
if self.text[i] == '#':
n.end = i
n.suffixIndex = self.size - labelHeight
def buildSuffixTree(self):
self.size = len(self.text)
self.rootEnd = -1
self.root = self.newNode(-1, self.rootEnd)
self.activeNode = self.root
for i in xrange(self.size):
self.extendSuffixTree(i)
labelHeight = 0
self.setSuffixIndexByDFS(self.root, labelHeight)
#not sure about this - edited
def doTraversal(self, n, labelHeight, maxHeight, substringStartIndex):
if n == None: return
ret = -1
print n.suffixIndex, self.size1
if n.suffixIndex < 0: #if it is internal node
for i in xrange(self.MAXCHAR):
if n.children[i] != None:
ret = self.doTraversal(n.children[i], labelHeight + self.edgeLength(n.children[i]), maxHeight, substringStartIndex)
#ret = self.doTraversal(n.children[i], labelHeight + self.edgeLength(n.children[i]), self.maxHeight, self.substringStartIndex)
if n.suffixIndex == -1:
n.suffixIndex = ret
elif n.suffixIndex == -2 and ret == -3 | \
n.suffixIndex == -3 and ret == -2 | \
n.suffixIndex == -4:
n.suffixIndex = -4 #Mark node as XY
#keep track of deepest node
if maxHeight < labelHeight:
maxHeight = labelHeight
# if self.maxHeight < labelHeight:
# self.maxHeight = labelHeight
substringStartIndex = n.end - labelHeight + 1
elif n.suffixIndex > -1 and n.suffixIndex < self.size1: #suffix of X
return -2 #mark node as X
elif n.suffixIndex >= self.size1: #suffix of Y
return -3 #mark node as Y
return n.suffixIndex
# edited - not certain about this func as maxHeight is 0 not sure how this var changes
def getLongestCommonSubstring(self):
maxHeight = 0
substringStartIndex = 0
r = self.doTraversal(self.root, 0, maxHeight, substringStartIndex)
print 'traversal returned', r
print "maxheight " , maxHeight
i = 0
for k in xrange(maxHeight):
i = k
print "%c", self.text[k + substringStartIndex]
if i == 0:
print "No common Substring"
else:
print ", of lenght: %d", maxHeight
print "\n"
if __name__ == "__main__":
N = SuffixTreeNode("xabxac#abcabxabcd$", 7)
N.buildSuffixTree()