我正在實施本文中的並行DC3,pDC3算法:http://algo2.iti.kit.edu/sanders/papers/KulSan06a.pdf。實現特定的比較功能
見第12行:
"""
Sort S0 U S1 U S2 using the comparison function:
(c, ...) in S1 U S2 <= (d, ...) in S1 U S2 <=> c <= d
(t, t', c', c'', i) in S0 <= (u, u', d', d'', j) in S0 <=> (t, c') <= (u, d')
(t, t', c', c'', i) in S0 <= (d, u, d', j) in S1 <=> (t, c') <= (u, d')
(t, t', c', c'', i) in S0 <= (d, u, u', d'', j) in S2 <=> (t, t', c'') <= (u, u', d'')
"""
我將如何實現在Python這樣的比較?
對不起,我還沒有在這裏給出完整的圖片。但是,讓我回去了幾步,顯示什麼S0-S2的樣子在我的實現:
我的代碼的最後幾行,我計算S0-S2:
s0 = computeS0(indexSortedRankIndexPairs, text, paddedText)
print 'Set0: ' + str(s0)
s1 = computeS1(indexSortedRankIndexPairs, text, paddedText)
print 'Set1: ' + str(s1)
s2 = computeS2(indexSortedRankIndexPairs, text, paddedText)
print 'Set2: ' + str(s2)
這是樣本輸出從我的程序:
Text yabbadabbado
Padded Text yabbadabbado00
Trituples: set([('ada', 4), ('bba', 7), ('abb', 1), ('o00', 11), ('do0', 10), ('bad', 8), ('bba', 2), ('dab', 5)])
Sorted Trituples: [('abb', 1), ('ada', 4), ('bad', 8), ('bba', 7), ('bba', 2), ('dab', 5), ('do0', 10), ('o00', 11)]
Rank Index Pairs: [(1, 1), (2, 4), (3, 8), (4, 7), (4, 2), (5, 5), (6, 10), (7, 11)]
Sorted Rank Index Pairs: [(1, 1), (2, 4), (4, 7), (6, 10), (4, 2), (5, 5), (3, 8), (7, 11)]
Index Sorted Rank Index Pairs: [(1, 1), (4, 2), (2, 4), (5, 5), (4, 7), (3, 8), (6, 10), (7, 11)]
Set0: set([('a', 'd', 6, 7, 9), ('y', 'a', 1, 4, 0), ('a', 'b', 4, 3, 6), ('b', 'a', 2, 5, 3)])
Set1: set([(2, 'a', 5, 4), (1, 'a', 4, 1), (4, 'b', 3, 7), (6, 'd', 7, 10)])
Set2: set([(7, 'o', '0', 0, 11), (3, 'b', 'a', 6, 8), (5, 'd', 'a', 4, 5), (4, 'b', 'b', 2, 2)])
所以,S0,S1和S2基本上是本地Python集(至少現在)。
首先你要告訴我們你是如何代表S0,S1,S2或者,或者我們必須拿出我們自己執行所有前面的代碼,這是(a)更多的工作任何人都想爲你做,並且(b)會迫使你從其他人的實施轉化爲你的人,而不是直接使用它。 – abarnert
請您詳細說明什麼是什麼。 – tMJ
另外,你有閱讀[排序如何](http://docs.python.org/3/howto/sorting.html)?你意識到Python排序函數需要一個'key'函數來轉換兩個值,而不是比較兩個值的比較函數?您可以編寫一個比較函數,然後將其包裝在'functools.cmp_to_key'中,但這通常不是最佳解決方案。 – abarnert