def visit(G, u, t, L): """ Vertex visit procedure for topological sort. Finished vertex is inserted at the head of linked list. The code is almost identical to the `dfs_visit()` procedure with a simple addition of `list_insert()`. Complexity: O(Adj) where `Adj` is number of adjacent vertices :param Graph G: Adjacency list representation of a DAG :param Vertex u: Vertex to visit :param Counter t: Mutable timer (integer counter) :param LinkedList L: Output linked list :return None: Finished vertex is updated and added to the list `L` """ t.tick += 1 u.d = t.tick u.color = GRAY for v in G.Adj(u): if v.color is WHITE: v.p = u visit(G, v, t, L) u.color = BLACK t.tick += 1 u.f = t.tick list_insert(L, Node(u)) # Finished vertex is added onto the front of a linked list
def test(self): l1 = List() list_insert(l1, Node(1)) list_insert(l1, Node(2)) list_insert(l1, Node(3)) list_insert(l1, Node(1)) list_insert(l1, Node(6)) self.assertTrue(solution(l1, 5)) self.assertTrue(solution(l1, 2)) self.assertFalse(solution(l1, 1)) self.assertFalse(solution(l1, 6))
def test(self): l1 = List() list_insert(l1, Node(1)) list_insert(l1, Node(2)) list_insert(l1, Node(2)) list_insert(l1, Node(3)) list_insert(l1, Node(4)) list_insert(l1, Node(4)) list_insert(l1, Node(4)) solution(l1.head) node = l1.head for i in range(4, 0, -1): self.assertEqual(node.key, i) node = node.next