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 Original file line number Diff line number Diff line change 2
2
import sys
3
3
4
4
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
5
12
13
+ # O(|text|)
6
14
def inverse (self , bwt_text ):
7
15
16
+ # O(|text|)
8
17
last_column = [c for c in bwt_text ]
18
+
19
+ # O(|text|)
9
20
first_column = [c for c in bwt_text ]
10
- first_column .sort ()
21
+ self .count_sort (first_column )
22
+
23
+ # O(|text|)
11
24
last_to_first = self .build_last_to_first (first_column , last_column )
12
25
13
26
origin_text_list = []
14
27
origin_text_list .append (first_column [0 ])
15
28
29
+ # O(|text|)
16
30
i = 0
17
31
while last_to_first [i ] != 0 :
18
32
i = last_to_first [i ]
19
33
origin_text_list .append (first_column [i ])
20
34
35
+ # O(|text|)
21
36
origin_text_list .reverse ()
22
37
38
+ # O(|text|)
23
39
return '' .join (origin_text_list )
24
40
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
+
25
56
def build_last_to_first (self , first_column , last_column ):
26
57
last_to_first = []
27
58
You can’t perform that action at this time.
0 commit comments