algorithm - How to make this sparse matrix and trie work in tandem -


i have sparse matrix has been exported format:

(1, 3) = 4 (0, 5) = 88 (6, 0) = 100 ... 

strings stored trie data structure. numbers in previous exported sparse matrix correspond result of lookup on trie.

lets word "stackoverflow" mapped number '0'. need iterate exported sparse matrix first element equals '0' , find highest value.

for example:

(0, 1) = 4 (0, 3) = 8 (0, 9) = 100 <-- highest value 

(0, 9) going win.

what best implementation store exported sparse matrix? in general, best approach (data structure, algorithm) handle functionality?

absent memory or dynamism constraints, best approach slurp sparse matrix map first number pairs ordered value, e.g.,

matrix_map = {}  # empty map (first_number, second_number, value) in matrix_triples:     if first_number not in matrix_map:         matrix_map[first_number] = []  # empty list     matrix_map[first_number].append((second_number, value)) lst in matrix_map.values():     lst.sort(key=itemgetter(1), reverse=true)  # sort value descending 

given matrix like

(0, 1) = 4 (0, 3) = 8 (0, 5) = 88 (0, 9) = 100 (1, 3) = 4 (6, 0) = 100, 

the finished product looks this:

{0: [(9, 100), (5, 88), (3, 8), (1, 4)],  1: [(3, 4)],  6: [(0, 100)]}. 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -