示例#1
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
示例#2
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
示例#3
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)
示例#4
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
示例#5
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)
示例#6
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
示例#7
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
示例#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
示例#9
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