Esempio n. 1
0
def build_undirected_graph():
    graph = Graph()
    graph.add_edge('u', 'v', 'undirected')
    graph.add_edge('v', 'y', 'undirected')
    graph.add_edge('y', 'x', 'undirected')
    graph.add_edge('x', 'v', 'undirected')
    graph.add_edge('u', 'x', 'undirected')
    graph.add_edge('w', 'z', 'undirected')
    graph.add_edge('z', 'z', 'undirected')
    return graph, graph.get_vertex('u')
Esempio n. 2
0
def build_directed_cyclic_graph():
    graph = Graph()
    graph.add_edge('u', 'v')
    graph.add_edge('v', 'y')
    graph.add_edge('y', 'x')
    graph.add_edge('x', 'v')
    graph.add_edge('u', 'x')
    graph.add_edge('w', 'y')
    graph.add_edge('w', 'z')
    graph.add_edge('z', 'z')
    return graph, graph.get_vertex('u')
    def test_add_edge(self):
        graph = Graph()
        graph.add_vertex("A")
        graph.add_vertex("B")
        graph.add_vertex("C")

        graph.add_edge("A", "C")
        graph.add_edge("A", "C", 3)

        self.assertEqual(3, len(graph.get_vertices()))
        self.assertEqual(2, len(graph.edge_list))
        graph.add_vertex("E")
        graph.add_vertex("F")
        graph.add_edge("E", "F")

        self.assertEqual(5, graph.num_vertices)
        self.assertEqual(3, graph.num_edges)
        self.assertCountEqual(["A", "B", "C", "E", "F"], graph.get_vertices())
Esempio n. 4
0
def build_undirected_edge_weighted_graph():
    graph = Graph()
    graph.add_edge('a', 'b', cost=4, edge_type='undirected')
    graph.add_edge('b', 'c', cost=8, edge_type='undirected')
    graph.add_edge('c', 'd', cost=7, edge_type='undirected')
    graph.add_edge('d', 'e', cost=9, edge_type='undirected')
    graph.add_edge('e', 'f', cost=10, edge_type='undirected')
    graph.add_edge('f', 'g', cost=2, edge_type='undirected')
    graph.add_edge('g', 'h', cost=1, edge_type='undirected')
    graph.add_edge('a', 'h', cost=8, edge_type='undirected')
    graph.add_edge('b', 'h', cost=11, edge_type='undirected')
    graph.add_edge('h', 'i', cost=7, edge_type='undirected')
    graph.add_edge('i', 'c', cost=2, edge_type='undirected')
    graph.add_edge('i', 'g', cost=6, edge_type='undirected')
    graph.add_edge('c', 'f', cost=4, edge_type='undirected')
    graph.add_edge('d', 'f', cost=14, edge_type='undirected')
    return graph, graph.get_vertex('a')
Esempio n. 5
0
def build_directed_edge_weighted_graph():
    graph = Graph()
    graph.add_edge('s', 't', cost=10)
    graph.add_edge('s', 'y', cost=5)
    graph.add_edge('t', 'y', cost=2)
    graph.add_edge('y', 't', cost=3)
    graph.add_edge('t', 'x', cost=1)
    graph.add_edge('y', 'z', cost=2)
    graph.add_edge('y', 'x', cost=9)
    graph.add_edge('z', 's', cost=7)
    graph.add_edge('z', 'x', cost=6)
    graph.add_edge('x', 'z', cost=4)
    return graph, graph.get_vertex('s')