Skip to content

Commit 494e7b4

Browse files
author
Hamid Gasmi
committed
#162 is optimized with a Count_Sort
1 parent 931e442 commit 494e7b4

File tree

1 file changed

+32
-1
lines changed
  • 5-string-processing-and-pattern-matching-algorithms/2-burrows-wheeler-suffix-arrays

1 file changed

+32
-1
lines changed

5-string-processing-and-pattern-matching-algorithms/2-burrows-wheeler-suffix-arrays/bwtinverse.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,57 @@
22
import sys
33

44
class Burrows_Wheeler_Inverse:
5+
def __init__(self):
6+
self.alphabet = dict()
7+
self.alphabet["$"] = 0
8+
self.alphabet["A"] = 1
9+
self.alphabet["C"] = 2
10+
self.alphabet["G"] = 3
11+
self.alphabet["T"] = 4
512

13+
# O(|text|)
614
def inverse(self, bwt_text):
715

16+
# O(|text|)
817
last_column = [c for c in bwt_text]
18+
19+
# O(|text|)
920
first_column = [c for c in bwt_text]
10-
first_column.sort()
21+
self.count_sort(first_column)
22+
23+
# O(|text|)
1124
last_to_first = self.build_last_to_first(first_column, last_column)
1225

1326
origin_text_list = []
1427
origin_text_list.append(first_column[0])
1528

29+
# O(|text|)
1630
i = 0
1731
while last_to_first[i] != 0:
1832
i = last_to_first[i]
1933
origin_text_list.append(first_column[i])
2034

35+
# O(|text|)
2136
origin_text_list.reverse()
2237

38+
# O(|text|)
2339
return ''.join(origin_text_list)
2440

41+
# O(Text)
42+
def count_sort(self, char_list):
43+
count = [0 for _ in range(len(self.alphabet))]
44+
45+
for c in char_list:
46+
i = self.alphabet[c]
47+
count[i] += 1
48+
49+
i = 0
50+
for c in self.alphabet:
51+
index = self.alphabet[c]
52+
for _ in range(count[index]):
53+
char_list[i] = c
54+
i += 1
55+
2556
def build_last_to_first(self, first_column, last_column):
2657
last_to_first = []
2758

0 commit comments

Comments
 (0)