あなたの問題を理解しているように、そのレベルの深さ n とランク r のノードは、ランク r と r+1 のレベル n+1 のノードに接続されます。
ダイレクト パスは確かに、入力として 1 つの DAG を取得する検索機能を使用して、何らかの再帰関係を探すことです。最大ウェイトを探すことから始めることもできます。これが機能する場合、ノードのリストを作成することは大きな問題にはなりません。
このパスに従って動作させました。使用したコードとテスト セットは以下のとおりです...ただし、主題を台無しにしないように、最も興味深い部分を削除しました。必要に応じて、さらにヒントを与えることができます。それはあなたが始めるためだけです。
import unittest
def weight(tdag, path):
return sum([level[p] for p, level in zip(path,tdag)])
def search_max(tdag):
if len(tdag) == 1:
return (0,)
if len(tdag) > 1:
# recursive call to search_max with some new tdag
# when choosing first node at depth 2
path1 = (0,) + search_max(...)
# recursive call to search_max with some new tdag
# when choosing second node at depth 2
# the result path should also be slightly changed
# to get the expected result in path2
path2 = (0,) + ...
if weigth(tdag, path1) > weigth(tdag, path2):
return path1
else:
return path2
class Testweight(unittest.TestCase):
def test1(self):
self.assertEquals(1, weight([[1]],(0,)))
def test2(self):
self.assertEquals(3, weight([[1], [2, 3]],(0, 0)))
def test3(self):
self.assertEquals(4, weight([[1], [2, 3]],(0, 1)))
class TestSearchMax(unittest.TestCase):
def test_max_one_node(self):
self.assertEquals((0,), search_max([[1]]))
def test_max_two_nodes(self):
self.assertEquals((0, 1), search_max([[1], [2, 3]]))
def test_max_two_nodes_alternative(self):
self.assertEquals((0, 0), search_max([[1], [3, 2]]))
def test_max_3_nodes_1(self):
self.assertEquals((0, 0, 0), search_max([[1], [3, 2], [6, 4, 5]]))
def test_max_3_nodes_2(self):
self.assertEquals((0, 0, 1), search_max([[1], [3, 2], [4, 6, 5]]))
def test_max_3_nodes_3(self):
self.assertEquals((0, 1, 1), search_max([[1], [2, 3], [4, 6, 5]]))
def test_max_3_nodes_4(self):
self.assertEquals((0, 1, 2), search_max([[1], [2, 3], [4, 5, 6]]))
if __name__ == '__main__':
unittest.main()