Exemplo n.º 1
0
    def test_euler_undirected(self):
        graph = Graph()
        for i in range(7, -1, -1):
            graph.add_vertex(Vertex(i))
        graph.add_2_edges(0, 1)
        graph.add_2_edges(1, 2)
        graph.add_2_edges(0, 3)
        graph.add_2_edges(1, 3)
        graph.add_2_edges(1, 4)
        graph.add_2_edges(2, 4)
        graph.add_2_edges(3, 5)
        graph.add_2_edges(3, 6)
        graph.add_2_edges(4, 6)
        graph.add_2_edges(4, 7)
        graph.add_2_edges(5, 6)
        graph.add_2_edges(6, 7)
        self.assertSequenceEqual([0, 1, 2, 4, 6, 3, 5, 6, 7, 4, 1, 3, 0],
                                 euler_tour_undirected(graph))

        graph = Graph()
        for i in range(6):
            graph.add_vertex(Vertex(i))
        graph.add_2_edges(0, 1)
        graph.add_2_edges(0, 2)
        graph.add_2_edges(0, 3)
        graph.add_2_edges(0, 4)
        graph.add_2_edges(1, 2)
        graph.add_2_edges(1, 3)
        graph.add_2_edges(1, 4)
        graph.add_2_edges(2, 3)
        graph.add_2_edges(3, 4)
        graph.add_2_edges(2, 5)
        graph.add_2_edges(4, 5)
        self.assertSequenceEqual([0, 1, 2, 3, 4, 5, 2, 0, 3, 1, 4, 0],
                                 euler_tour_undirected(graph))
Exemplo n.º 2
0
 def _graph_from_figure_22_9() -> Graph:
     graph = Graph()
     graph.add_vertex(Vertex('a'))
     graph.add_vertex(Vertex('b'))
     graph.add_vertex(Vertex('c'))
     graph.add_vertex(Vertex('d'))
     graph.add_vertex(Vertex('e'))
     graph.add_vertex(Vertex('f'))
     graph.add_vertex(Vertex('g'))
     graph.add_vertex(Vertex('h'))
     graph.add_edge('a', 'b')
     graph.add_edge('b', 'c')
     graph.add_edge('b', 'e')
     graph.add_edge('b', 'f')
     graph.add_edge('c', 'd')
     graph.add_edge('c', 'g')
     graph.add_edge('d', 'c')
     graph.add_edge('d', 'h')
     graph.add_edge('e', 'a')
     graph.add_edge('e', 'f')
     graph.add_edge('f', 'g')
     graph.add_edge('g', 'f')
     graph.add_edge('g', 'h')
     graph.add_edge('h', 'h')
     return graph
Exemplo n.º 3
0
    def test_bfs(self):
        # Figure 22-3
        graph = Graph()
        graph.add_vertex(Vertex('r'))
        graph.add_vertex(Vertex('s'))
        graph.add_vertex(Vertex('t'))
        graph.add_vertex(Vertex('u'))
        graph.add_vertex(Vertex('v'))
        graph.add_vertex(Vertex('w'))
        graph.add_vertex(Vertex('x'))
        graph.add_vertex(Vertex('y'))
        graph.add_2_edges('r', 's')
        graph.add_2_edges('r', 'v')
        graph.add_2_edges('s', 'w')
        graph.add_2_edges('t', 'u')
        graph.add_2_edges('t', 'w')
        graph.add_2_edges('t', 'x')
        graph.add_2_edges('u', 'x')
        graph.add_2_edges('u', 'y')
        graph.add_2_edges('w', 'x')
        graph.add_2_edges('x', 'y')

        came_from = bfs_with_src(graph, 's')[0]
        self.assertDictEqual({
            'v': 'r',
            'r': 's',
            'w': 's',
            't': 'w',
            'x': 'w',
            'y': 'x',
            'u': 't',
        }, came_from)
Exemplo n.º 4
0
def get_transpose(graph: Graph):
    """Ex 22.1-3."""
    tr = Graph()
    for v in graph.vertex_keys():
        tr.add_vertex(Vertex(v))
    for u in graph.vertex_keys():
        for v, weight in graph.get_vertex(u).successors():
            tr.add_edge(v, u, weight)
    return tr
Exemplo n.º 5
0
def graph_from_adj_matrix(adj_matrix) -> Graph:
    vertex_count = len(adj_matrix)
    graph = Graph()
    for i in range(vertex_count):
        graph.add_vertex(Vertex(i))
    for i in range(vertex_count):
        for j in range(vertex_count):
            if adj_matrix[i][j]:
                graph.add_edge(i, j)
    return graph
Exemplo n.º 6
0
 def test_mohammed_scimitars(self):
     graph = Graph()
     for i in range(11):
         graph.add_vertex(Vertex(i))
     for edge in ((0, 1), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (3, 5),
                  (3, 6), (4, 5), (4, 8), (5, 6), (5, 8), (6, 7), (6, 10),
                  (7, 8), (7, 9), (7, 10), (8, 9)):
         graph.add_2_edges(edge[0], edge[1])
     self.assertSequenceEqual(
         [0, 1, 2, 3, 5, 4, 8, 7, 6, 10, 7, 9, 8, 5, 6, 3, 1, 4, 0],
         euler_tour_undirected(graph))
Exemplo n.º 7
0
    def test_topological_sort_with_loop(self):
        cases = []
        graph = Graph()
        graph.add_vertex(Vertex(0))
        graph.add_edge(0, 0)
        cases.append(graph)

        graph = self._graph_from_figure_22_8()
        graph.add_edge("v", "p")  # Add an edge to form a loop
        cases.append(graph)

        for graph in cases:
            self.assertLess(len(topological_sort_kahn(graph)),
                            graph.vertex_len)
Exemplo n.º 8
0
 def _graph_from_figure_22_7(self):
     graph = Graph()
     graph.add_vertex(Vertex("undershorts"))
     graph.add_vertex(Vertex("socks"))
     graph.add_vertex(Vertex("watch"))
     graph.add_vertex(Vertex("pants"))
     graph.add_vertex(Vertex("shoes"))
     graph.add_vertex(Vertex("belt"))
     graph.add_vertex(Vertex("shirt"))
     graph.add_vertex(Vertex("tie"))
     graph.add_vertex(Vertex("jacket"))
     graph.add_edge("undershorts", "pants")
     graph.add_edge("undershorts", "shoes")
     graph.add_edge("socks", "shoes")
     graph.add_edge("pants", "shoes")
     graph.add_edge("pants", "belt")
     graph.add_edge("shirt", "belt")
     graph.add_edge("shirt", "tie")
     graph.add_edge("tie", "jacket")
     graph.add_edge("belt", "jacket")
     return graph
Exemplo n.º 9
0
def terrain_to_graph(terrain):
    graph = Graph()
    if not terrain or not terrain[0]:
        return graph
    h = len(terrain)
    w = len(terrain[0])
    for x in range(0, w):
        for y in range(0, h):
            graph.add_vertex(Vertex((x, y)))

    for x in range(0, w):
        for y in range(0, h):
            if terrain[y][x] < 0:
                continue
            if x - 1 >= 0 and terrain[y][x - 1] >= 0:
                graph.add_edge((x, y), (x - 1, y), terrain[y][x])
            if y - 1 >= 0 and terrain[y - 1][x] >= 0:
                graph.add_edge((x, y), (x, y - 1), terrain[y][x])
            if x + 1 < w and terrain[y][x + 1] >= 0:
                graph.add_edge((x, y), (x + 1, y), terrain[y][x])
            if y + 1 < h and terrain[y + 1][x] >= 0:
                graph.add_edge((x, y), (x, y + 1), terrain[y][x])
    return graph
Exemplo n.º 10
0
 def test_tree_diameter(self):
     methods = (tree_diameter_recur, tree_diameter_bfs)
     tree = Graph()
     for i in range(0, 16):
         tree.add_vertex(Vertex(i))
     tree.add_2_edges(0, 1)
     tree.add_2_edges(0, 2)
     tree.add_2_edges(0, 3)
     tree.add_2_edges(1, 4)
     tree.add_2_edges(1, 5)
     tree.add_2_edges(2, 6)
     tree.add_2_edges(3, 7)
     tree.add_2_edges(3, 8)
     tree.add_2_edges(4, 9)
     tree.add_2_edges(5, 10)
     tree.add_2_edges(5, 11)
     tree.add_2_edges(6, 12)
     tree.add_2_edges(6, 13)
     tree.add_2_edges(6, 14)
     tree.add_2_edges(14, 15)
     for tree_diameter_method in methods:
         for root in range(0, 16):
             # print('root: %d' % root)
             self.assertEqual(7, tree_diameter_method(tree, root))
Exemplo n.º 11
0
def dijkstra_basic_test_case():
    graph = Graph()
    vertices = [Vertex(i) for i in range(1, 8)]
    for v in vertices:
        graph.add_vertex(v)
    graph.add_edge(1, 2, 7)
    graph.add_edge(2, 1, 7)
    graph.add_edge(1, 3, 9)
    graph.add_edge(3, 1, 9)
    graph.add_edge(1, 6, 14)
    graph.add_edge(6, 1, 14)
    graph.add_edge(2, 3, 10)
    graph.add_edge(3, 2, 10)
    graph.add_edge(2, 4, 15)
    graph.add_edge(4, 2, 15)
    graph.add_edge(3, 4, 11)
    graph.add_edge(4, 3, 11)
    graph.add_edge(3, 6, 2)
    graph.add_edge(6, 3, 2)
    graph.add_edge(4, 5, 6)
    graph.add_edge(5, 4, 6)
    graph.add_edge(5, 6, 9)
    graph.add_edge(6, 5, 9)

    src = 1

    expected_results = {
        1: (1, ),
        2: (1, 2),
        3: (1, 3),
        4: (1, 3, 4),
        5: (1, 3, 6, 5),
        6: (1, 3, 6),
        7: (),
    }
    return graph, src, expected_results
Exemplo n.º 12
0
    def test_dfs(self):
        graph = Graph()
        graph.add_vertex(Vertex('s'))
        graph.add_vertex(Vertex('t'))
        graph.add_vertex(Vertex('u'))
        graph.add_vertex(Vertex('v'))
        graph.add_vertex(Vertex('w'))
        graph.add_vertex(Vertex('x'))
        graph.add_vertex(Vertex('y'))
        graph.add_vertex(Vertex('z'))
        graph.add_edge('z', 'y')
        graph.add_edge('s', 'z')
        graph.add_edge('y', 'x')
        graph.add_edge('x', 'z')
        graph.add_edge('z', 'w')
        graph.add_edge('s', 'w')
        graph.add_edge('v', 's')
        graph.add_edge('t', 'v')
        graph.add_edge('t', 'u')
        graph.add_edge('u', 't')
        graph.add_edge('w', 'x')
        graph.add_edge('v', 'w')
        graph.add_edge('u', 'v')

        pre_visit_array = []

        def pre_visit(v):
            pre_visit_array.append(v)
            return True

        post_visit_array = []

        def post_visit(v):
            post_visit_array.append(v)
            return True

        result = dfs(graph, pre_visit, post_visit)

        expected_result = DFSResult()
        expected_result.discover_times = {
            's': 1,
            'w': 2,
            'x': 3,
            'z': 4,
            'y': 5,
            't': 11,
            'u': 12,
            'v': 13
        }
        expected_result.finish_times = {
            'y': 6,
            'z': 7,
            'x': 8,
            'w': 9,
            's': 10,
            'v': 14,
            'u': 15,
            't': 16
        }
        expected_result.came_from = {
            's': None,
            'w': 's',
            'x': 'w',
            'z': 'x',
            'y': 'z',
            't': None,
            'u': 't',
            'v': 'u'
        }
        expected_pre_visit_array = ['s', 'w', 'x', 'z', 'y', 't', 'u', 'v']
        expected_post_visit_array = ['y', 'z', 'x', 'w', 's', 'v', 'u', 't']
        self.assertEqual(expected_result, result)
        self.assertSequenceEqual(expected_pre_visit_array, pre_visit_array)
        self.assertSequenceEqual(expected_post_visit_array, post_visit_array)
Exemplo n.º 13
0
 def _graph_from_figure_22_8(self):
     graph = Graph()
     graph.add_vertex(Vertex("m"))
     graph.add_vertex(Vertex("n"))
     graph.add_vertex(Vertex("o"))
     graph.add_vertex(Vertex("p"))
     graph.add_vertex(Vertex("q"))
     graph.add_vertex(Vertex("r"))
     graph.add_vertex(Vertex("s"))
     graph.add_vertex(Vertex("t"))
     graph.add_vertex(Vertex("u"))
     graph.add_vertex(Vertex("v"))
     graph.add_vertex(Vertex("w"))
     graph.add_vertex(Vertex("x"))
     graph.add_vertex(Vertex("y"))
     graph.add_vertex(Vertex("z"))
     graph.add_edge("m", "q")
     graph.add_edge("m", "r")
     graph.add_edge("m", "x")
     graph.add_edge("n", "o")
     graph.add_edge("n", "q")
     graph.add_edge("n", "u")
     graph.add_edge("o", "r")
     graph.add_edge("o", "s")
     graph.add_edge("o", "v")
     graph.add_edge("p", "o")
     graph.add_edge("p", "s")
     graph.add_edge("p", "z")
     graph.add_edge("q", "t")
     graph.add_edge("r", "u")
     graph.add_edge("r", "y")
     graph.add_edge("s", "r")
     graph.add_edge("u", "t")
     graph.add_edge("v", "w")
     graph.add_edge("v", "x")
     graph.add_edge("w", "z")
     graph.add_edge("y", "v")
     return graph