def test_paths_undirected(): g = Graph(7, directed=False) g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(0, 3) g.add_edge(0, 4) g.add_edge(1, 5) g.add_edge(2, 5) g.add_edge(3, 5) g.add_edge(5, 6) g.add_edge(4, 6) ''' 1 ⤢ ⤡ 0 ⇿ 2 ⇿ 5 ⇿ 6 ↕⤡ ⤢ ⤢ ↕ 3 ⤢ ⤡ ⤢ 4 ⇿ ⤢ ''' def aggregate_list(path): if not hasattr(aggregate_list, "l"): setattr(aggregate_list, "l", []) aggregate_list.l.append(path) return aggregate_list.l g.paths(0, 6, aggregate_list) assert aggregate_list.l == [[0, 1, 5, 6], [0, 2, 5, 6], [0, 3, 5, 6], [0, 4, 6]] aggregate_list.l = [] g.paths(1, 6, aggregate_list) assert aggregate_list.l == [[1, 0, 2, 5, 6], [1, 0, 3, 5, 6], [1, 0, 4, 6], [1, 5, 2, 0, 4, 6], [1, 5, 3, 0, 4, 6], [1, 5, 6]] aggregate_list.l = [] g.paths(1, 1, aggregate_list) assert aggregate_list.l == [[1]] # Try BFS-based all paths extraction aggregate_list.l = [] g.paths_2(0, 6, aggregate_list) assert aggregate_list.l == [[0, 4, 6], [0, 1, 5, 6], [0, 2, 5, 6], [0, 3, 5, 6]] aggregate_list.l = [] g.paths_2(1, 6, aggregate_list) assert aggregate_list.l == sorted( [[1, 0, 2, 5, 6], [1, 0, 3, 5, 6], [1, 0, 4, 6], [1, 5, 2, 0, 4, 6], [1, 5, 3, 0, 4, 6], [1, 5, 6]], cmp=lambda l1, l2: cmp(len(l1), len(l2))) aggregate_list.l = [] g.paths_2(1, 1, aggregate_list) assert aggregate_list.l == [[1]] # All shortest paths by length aggregate_list.l = [] g.all_shortest_paths_by_length(0, 6, aggregate_list) assert aggregate_list.l == [[0, 4, 6]] aggregate_list.l = [] g.all_shortest_paths_by_length(1, 6, aggregate_list) assert aggregate_list.l == [[1, 5, 6]] aggregate_list.l = [] g.all_shortest_paths_by_length(0, 5, aggregate_list) assert aggregate_list.l == [[0, 1, 5], [0, 2, 5], [0, 3, 5]] # shortest path by length assert g.shortest_path_by_length(0, 6) == [0, 4, 6] assert g.shortest_path_by_length(1, 6) == [1, 5, 6] assert g.shortest_path_by_length(0, 5) == [0, 1, 5]
def test_paths_directed(): g = Graph(7, directed=True) g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(0, 3) g.add_edge(0, 4) g.add_edge(1, 5) g.add_edge(2, 5) g.add_edge(3, 5) g.add_edge(5, 6) g.add_edge(4, 6) ''' 1 ↗ ↘ 0 → 2 → 5 → 6 ↓↘ ↗ ↑ ↓ 3 ↗ ↘ ↗ 4 → ↗ ''' def aggregate_list(path): if not hasattr(aggregate_list, "l"): setattr(aggregate_list, "l", []) aggregate_list.l.append(path) return aggregate_list.l g.paths(0, 6, aggregate_list) assert aggregate_list.l == [[0, 1, 5, 6], [0, 2, 5, 6], [0, 3, 5, 6], [0, 4, 6]] aggregate_list.l = [] g.paths(1, 6, aggregate_list) assert aggregate_list.l == [[1, 5, 6]] aggregate_list.l = [] g.paths(6, 1, aggregate_list) assert aggregate_list.l == [] # Try BFS-based all paths extraction aggregate_list.l = [] g.paths_2(0, 6, aggregate_list) assert aggregate_list.l == [[0, 4, 6], [0, 1, 5, 6], [0, 2, 5, 6], [0, 3, 5, 6]] aggregate_list.l = [] g.paths_2(1, 6, aggregate_list) assert aggregate_list.l == [[1, 5, 6]] aggregate_list.l = [] g.paths_2(6, 1, aggregate_list) assert aggregate_list.l == [] # All shortest paths by length aggregate_list.l = [] g.all_shortest_paths_by_length(0, 6, aggregate_list) assert aggregate_list.l == [[0, 4, 6]] aggregate_list.l = [] g.all_shortest_paths_by_length(0, 5, aggregate_list) assert aggregate_list.l == [[0, 1, 5], [0, 2, 5], [0, 3, 5]] # shortest path by length assert g.shortest_path_by_length(0, 6) == [0, 4, 6] assert g.shortest_path_by_length(0, 5) == [0, 1, 5] assert g.shortest_path_by_length(6, 1) == []