Пример #1
0
def build_graph(degree_sequence, nodes=None):
    ''' Generates undirected graph of the given degree
        sequence. If nodes are provided, each node will
        have the corresponding data. '''

    if not is_valid_degree_sequence(degree_sequence):
        raise InvalidDegreeSequence('Invalid degree sequence!')

    graph = Graph()
    degree_node = []

    if not nodes:
        nodes = count()

    for item in map(lambda x, y: [x, y], degree_sequence, nodes):
        degree_node.append(item)

    degree_node.sort(reverse=True)
    degree_node = deque(degree_node)

    while degree_node:
        degree, node = degree_node.popleft()

        if degree == 0:
            graph.add_node(node)
            continue

        for other_dn in islice(degree_node, 0, degree):
            graph.add_edge(node, other_dn[1])
            other_dn[0] -= 1

    return graph
Пример #2
0
 def setUp(self):
     self.graph = Graph()
     self.graph.addVertices(["s", "a", "b", "c", "d", "e"])
     self.graph.addEdges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
     self.graph.addEdges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
     self.graph.addEdges([("g", "h"), ("f", "g")])
     self.graph.addEdges([("j", "k"), ("j", "l")])
Пример #3
0
 def test_centers(self):
     self.assertEqual(centers(self.tree_one), {1, 3})
     self.assertEqual(centers(self.tree_two), {1, 2})
     tree = Graph()
     tree.add_edge(1, 2)
     tree.add_edge(2, 3)
     self.assertEqual(centers(tree), {2, })
Пример #4
0
 def test_is_tree(self):
     self.assertTrue(is_tree(self.tree_one))
     self.assertTrue(is_tree(self.tree_two))
     graph = Graph()
     graph.add_edge(1, 1)
     self.assertFalse(is_tree(graph))
     graph.add_edge(1, 2)
     graph.add_edge(2, 3)
     self.assertFalse(is_tree(graph))
Пример #5
0
class test_graph(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        self.graph.addVertices(["s", "a", "b", "c", "d", "e"])
        self.graph.addEdges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
        self.graph.addEdges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])

    def test_initial_setup(self):
        self.assertEqual(self.graph.numberVertices(), 6)
Пример #6
0
def test_all_appropriate_neighbors_can_be_retrieved_from_the_graph():
  node_a = Node('a')
  node_b = Node('b')
  node_c = Node('c')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_node(node_c)
  graph.add_edge(node_a,node_b,1)
  graph.add_edge(node_a,node_c,2)
  assert graph.get_neighbors(node_a) == [{'b': 1}, {'c': 2}]
Пример #7
0
def test_neighbors_are_returned_with_the_weight_between_nodes_included():
  node_a = Node('a')
  node_b = Node('b')
  node_c = Node('c')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_node(node_c)
  graph.add_edge(node_a,node_b,1)
  graph.add_edge(node_a,node_c,2)
  assert graph.get_neighbors(node_a) == [{'b': 1}, {'c': 2}]
Пример #8
0
 def setUp(self):
     self.graph = Graph()
     g = self.graph
     g.add_edge(1, 9)
     g.add_edge(9, 6)
     g.add_edge(6, 1)
     g.add_edge(1, 2)
     g.add_edge(2, 4)
     g.add_edge(4, 3)
     g.add_edge(3, 2)
     g.add_edge(2, 8)
     g.add_edge(1, 8)
     g.add_edge(8, 5)
     g.add_edge(5, 7)
     g.add_edge(7, 8)
     self.digraph = DiGraph()
     dig = self.digraph
     dig.add_edge(1, 2)
     dig.add_edge(2, 5)
     dig.add_edge(2, 3)
     dig.add_edge(5, 3)
     dig.add_edge(3, 5)
     dig.add_edge(5, 6)
     dig.add_edge(6, 3)
     dig.add_edge(3, 4)
     dig.add_edge(4, 2)
     dig.add_edge(3, 7)
     dig.add_edge(7, 8)
     dig.add_edge(8, 1)
Пример #9
0
	def setUp(self):
		self.graph= Graph()
		self.graph.addVertices(["s", "a", "b", "c", "d", "e"])
		self.graph.addEdges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
		self.graph.addEdges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
		self.graph.addEdges([("g", "h"), ("f", "g")])
		self.graph.addEdges([("j", "k"), ("j", "l")])
Пример #10
0
    def createGraph(self):
        name = []
        timesLanded = []
        costs = []
        rents = []
        self.removeDataPoints() # Removes All non-properties
        prop = self.db.all()
        for doc in prop:
            name.append(doc['Name'])
            timesLanded.append(doc['Landed'])
            costs.append(doc['Cost'])
            rents.append(doc['Rent'])
        timesLanded = self.convertToPercent(timesLanded)
        _graph = Graph(name, timesLanded, costs, rents)

        #_graph.barChart()
        _graph.lineChart()
Пример #11
0
def test_a_collection_of_all_nodes_can_be_properly_retrieved_from_the_graph():
  node_a = Node('a')
  node_b = Node('b')
  node_c = Node('c')
  node_d = Node('d')
  node_e = Node('e')
  node_f = Node('f')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_node(node_c)
  graph.add_node(node_d)
  graph.add_node(node_e)
  graph.add_node(node_f)
  assert graph.get_nodes() == ['a', 'b', 'c', 'd', 'e', 'f']
Пример #12
0
def test_the_proper_size_is_returned_representing_the_number_of_nodes_in_the_graph():
  node_a = Node('a')
  node_b = Node('b')
  node_c = Node('c')
  node_d = Node('d')
  node_e = Node('e')
  node_f = Node('f')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_node(node_c)
  graph.add_node(node_d)
  graph.add_node(node_e)
  graph.add_node(node_f)
  assert graph.size() == 6
Пример #13
0
class MaxPathTest(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        self.graph.add_edge(1, 2)
        self.digraph = DiGraph()
        g = self.digraph
        g.add_edge(1, 3, w=3)
        g.add_edge(2, 3, w=5)
        g.add_edge(3, 5, w=11)
        g.add_edge(3, 4, w=8)
        g.add_edge(5, 6, w=12)
        g.add_edge(4, 7, w=16)
        g.add_edge(5, 7, w=14)
        g.add_edge(4, 6, w=18)
        g.add_edge(6, 8, w=21)
        g.add_edge(4, 7, w=16)
        g.add_edge(7, 8, w=16)
        g.add_edge(8, 10, w=7)
        g.add_edge(8, 9, w=5)

    def test_max_path(self):
        pred, distance = max_path(self.digraph, weight_attribute='w')
        self.assertEqual(59, max(distance.values()))
        self.assertEqual(pred,
                         {3: 2, 4: 3, 5: 3, 6: 4,
                          7: 5, 8: 6, 9: 8, 10: 8})

    def test_max_path_graph(self):
        with self.assertRaises(NotDAG):
            max_path(self.graph)

    def test_max_path_digraph_with_cycle(self):
        self.digraph.add_edge(10, 8, w=4)
        with self.assertRaises(NotDAG):
            max_path(self.digraph)
        self.digraph.remove_edge(10, 8)

    def test_max_path_negative_edge(self):
        self.digraph.add_edge(1, 1000, w=-1)
        with self.assertRaises(NegativeEdgeWeight):
            max_path(self.digraph, weight_attribute='w')
        self.digraph.remove_edge(1, 1000)
 def setUp(self):
     self.graph = Graph()
     g = self.graph
     g.add_edge(1, 2)
     g.add_edge(1, 4)
     g.add_edge(1, 6)
     g.add_edge(3, 2)
     g.add_edge(3, 4)
     g.add_edge(3, 6)
     g.add_edge(5, 2)
     g.add_edge(5, 4)
     g.add_edge(5, 6)
Пример #15
0
def test_edge_can_be_successfully_added_to_the_graph():
  node_a = Node('a')
  node_b = Node('b')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_edge(node_a,node_b,1)
  assert graph._adjacency_list[node_a][0].node == node_b
Пример #16
0
 def setUp(self):
     self.graph = Graph()
     self.graph.add_edge(1, 2)
     self.digraph = DiGraph()
     g = self.digraph
     g.add_edge(1, 3, w=3)
     g.add_edge(2, 3, w=5)
     g.add_edge(3, 5, w=11)
     g.add_edge(3, 4, w=8)
     g.add_edge(5, 6, w=12)
     g.add_edge(4, 7, w=16)
     g.add_edge(5, 7, w=14)
     g.add_edge(4, 6, w=18)
     g.add_edge(6, 8, w=21)
     g.add_edge(4, 7, w=16)
     g.add_edge(7, 8, w=16)
     g.add_edge(8, 10, w=7)
     g.add_edge(8, 9, w=5)
    def setUp(self):
        self.positive_graph = Graph()
        pg = self.positive_graph
        pg.add_edge(1, 2, w=5)
        pg.add_edge(1, 3, w=29)
        pg.add_edge(1, 6, w=70)
        pg.add_edge(2, 3, w=10)
        pg.add_edge(2, 4, w=15)
        pg.add_edge(3, 4, w=51)
        pg.add_edge(4, 5, w=6)
        pg.add_edge(3, 6, w=10)

        self.positive_digraph = DiGraph()
        pdig = self.positive_digraph
        pdig.add_edge(1, 2, w=5)
        pdig.add_edge(1, 3, w=29)
        pdig.add_edge(1, 6, w=70)
        pdig.add_edge(2, 3, w=10)
        pdig.add_edge(2, 4, w=15)
        pdig.add_edge(3, 4, w=51)
        pdig.add_edge(4, 5, w=6)
        pdig.add_edge(3, 6, w=10)
Пример #18
0
class BFS_test(unittest.TestCase):
	def setUp(self):
		self.graph= Graph()
		self.graph.addVertices(["s", "a", "b", "c", "d", "e"])
		self.graph.addEdges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
		self.graph.addEdges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
		self.graph.addEdges([("g", "h"), ("f", "g")])
		self.graph.addEdges([("j", "k"), ("j", "l")])

	def test_BFS_method(self):
		self.assertEqual(len(BFS(self.graph,"s")),6)
		self.assertEqual(len(BFS(self.graph,"j")),3)
		self.assertEqual(len(BFS(self.graph,"g")),3)

	def test_DFS_method(self):
		self.assertEqual(len(DFS(self.graph,"s")),6)
		self.assertEqual(len(DFS(self.graph,"j")),3)
		self.assertEqual(len(DFS(self.graph,"g")),3)
Пример #19
0
class BFS_test(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        self.graph.addVertices(["s", "a", "b", "c", "d", "e"])
        self.graph.addEdges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")])
        self.graph.addEdges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")])
        self.graph.addEdges([("g", "h"), ("f", "g")])
        self.graph.addEdges([("j", "k"), ("j", "l")])

    def test_BFS_method(self):
        self.assertEqual(len(BFS(self.graph, "s")), 6)
        self.assertEqual(len(BFS(self.graph, "j")), 3)
        self.assertEqual(len(BFS(self.graph, "g")), 3)

    def test_DFS_method(self):
        self.assertEqual(len(DFS(self.graph, "s")), 6)
        self.assertEqual(len(DFS(self.graph, "j")), 3)
        self.assertEqual(len(DFS(self.graph, "g")), 3)
Пример #20
0
 def test_with_one_and_only(self):
     t = Graph()
     t.add_node(1, p=1256)
     self.assertEqual(max_independent_set(t, 1, weight_attribute="p"), (1256, {1}))
Пример #21
0
class EulerianTest(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        g = self.graph
        g.add_edge(1, 9)
        g.add_edge(9, 6)
        g.add_edge(6, 1)
        g.add_edge(1, 2)
        g.add_edge(2, 4)
        g.add_edge(4, 3)
        g.add_edge(3, 2)
        g.add_edge(2, 8)
        g.add_edge(1, 8)
        g.add_edge(8, 5)
        g.add_edge(5, 7)
        g.add_edge(7, 8)
        self.digraph = DiGraph()
        dig = self.digraph
        dig.add_edge(1, 2)
        dig.add_edge(2, 5)
        dig.add_edge(2, 3)
        dig.add_edge(5, 3)
        dig.add_edge(3, 5)
        dig.add_edge(5, 6)
        dig.add_edge(6, 3)
        dig.add_edge(3, 4)
        dig.add_edge(4, 2)
        dig.add_edge(3, 7)
        dig.add_edge(7, 8)
        dig.add_edge(8, 1)

    def tearDown(self):
        del self.digraph
        del self.graph

    def test_is_eulerain_graph(self):
        self.assertTrue(is_eulerian(self.graph))

    def test_is_eulerian_digraph(self):
        self.assertTrue(is_eulerian(self.digraph))

    def test_find_eulerian_cycle_graph(self):
        self.assertEqual(find_eulerian_cycle(self.graph, 1),
                         [1, 8, 5, 7, 8, 2, 3, 4, 2, 1, 9, 6, 1])

    def test_find_eulerian_cycle_graph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            find_eulerian_cycle(self.graph, 999)

    def test_find_eulerian_cycle_not_eulerian_graph(self):
        self.graph.add_edge(101, 102)
        self.assertEqual(find_eulerian_cycle(self.graph, 1), None)
        self.graph.remove_edge(101, 102)

    def test_find_eulerian_cycle_digraph(self):
        self.assertEqual(find_eulerian_cycle(self.digraph, 1),
                         [1, 2, 3, 4, 2, 5, 3, 5, 6, 3, 7, 8, 1])

    def test_find_eulerian_cycle_digraph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            find_eulerian_cycle(self.digraph, 999)

    def test_find_eulerian_cycle_not_eulerian_digraph(self):
        self.digraph.add_edge(101, 102)
        self.assertEqual(find_eulerian_cycle(self.digraph, 1), None)
        self.digraph.remove_edge(101, 102)
Пример #22
0
def test_an_empty_graph_properly_returns_null():
  graph = Graph()
  assert graph._adjacency_list == {}
Пример #23
0
def test_bussiness_trip():
  node_pandora = Node('pandora')
  node_arendelle = Node('arendelle')
  node_metroville = Node('metroville')
  node_monstropolis = Node('monstropolis')
  node_naboo = Node('naboo')
  node_narnia = Node('narnia')

  trip = Graph()

  trip.add_node(node_pandora)
  trip.add_node(node_arendelle)
  trip.add_node(node_metroville)
  trip.add_node(node_monstropolis)
  trip.add_node(node_naboo)
  trip.add_node(node_narnia)

  trip.add_edge(node_pandora,node_arendelle,150)
  trip.add_edge(node_pandora,node_metroville,82)
  trip.add_edge(node_arendelle,node_pandora,150)
  trip.add_edge(node_arendelle,node_metroville,99)
  trip.add_edge(node_arendelle,node_monstropolis,42)
  trip.add_edge(node_metroville,node_pandora,82)
  trip.add_edge(node_metroville,node_arendelle,99)
  trip.add_edge(node_metroville,node_monstropolis,105)
  trip.add_edge(node_metroville,node_naboo,26)
  trip.add_edge(node_metroville,node_narnia,37)
  trip.add_edge(node_monstropolis,node_arendelle,42)
  trip.add_edge(node_monstropolis,node_metroville,105)
  trip.add_edge(node_monstropolis,node_naboo,73)
  trip.add_edge(node_naboo,node_monstropolis,73)
  trip.add_edge(node_naboo,node_metroville,26)
  trip.add_edge(node_naboo,node_narnia,250)
  trip.add_edge(node_narnia,node_metroville,37)
  trip.add_edge(node_narnia,node_naboo,250)

  assert business_trip(trip,['Metroville', 'Pandora' ]) == 82
  assert business_trip(trip,['Arendelle', 'Monstropolis', 'Naboo']) == 115
  assert business_trip(trip,['Naboo', 'Pandora']) == None
  assert business_trip(trip,['Narnia', 'Arendelle', 'Naboo']) == None
class BipartitenessTest(unittest.TestCase):
    def setUp(self):
        self.graph = Graph()
        g = self.graph
        g.add_edge(1, 2)
        g.add_edge(1, 4)
        g.add_edge(1, 6)
        g.add_edge(3, 2)
        g.add_edge(3, 4)
        g.add_edge(3, 6)
        g.add_edge(5, 2)
        g.add_edge(5, 4)
        g.add_edge(5, 6)

    def tearDown(self):
        del self.graph

    def test_is_bipartite_graph(self):
        self.assertEqual(is_bipartite(self.graph),
                         ({1, 3, 5}, {2, 4, 6}))

    def test_is_bipartite_not_bipartite_graph(self):
        self.graph.add_edge(1, 3)
        self.assertEqual(is_bipartite(self.graph), None)
        self.graph.remove_edge(1, 3)

    def test_is_balanced_bipartite(self):
        self.assertTrue(is_balanced_bipartite(self.graph))

    def test_is_balanced_bipartite_not_balanced(self):
        self.graph.add_edge(1, 100)
        self.assertFalse(is_balanced_bipartite(self.graph))
        self.graph.remove_edge(1, 100)

    def test_is_biregular(self):
        self.assertTrue(is_biregular(self.graph))

    def test_is_biregular_not_biregular_graph(self):
        self.graph.add_edge(1, 100)
        self.assertFalse(is_biregular(self.graph))
        self.graph.remove_edge(1, 100)
Пример #25
0
def test_a_graph_with_only_one_node_and_edge_can_be_properly_returned():
  node_a = Node('a')
  graph = Graph()
  graph.add_node(node_a)
  graph.add_edge(node_a,node_a,1)
  assert graph._adjacency_list
class ShortestPathsTest(unittest.TestCase):
    def setUp(self):
        self.positive_graph = Graph()
        pg = self.positive_graph
        pg.add_edge(1, 2, w=5)
        pg.add_edge(1, 3, w=29)
        pg.add_edge(1, 6, w=70)
        pg.add_edge(2, 3, w=10)
        pg.add_edge(2, 4, w=15)
        pg.add_edge(3, 4, w=51)
        pg.add_edge(4, 5, w=6)
        pg.add_edge(3, 6, w=10)

        self.positive_digraph = DiGraph()
        pdig = self.positive_digraph
        pdig.add_edge(1, 2, w=5)
        pdig.add_edge(1, 3, w=29)
        pdig.add_edge(1, 6, w=70)
        pdig.add_edge(2, 3, w=10)
        pdig.add_edge(2, 4, w=15)
        pdig.add_edge(3, 4, w=51)
        pdig.add_edge(4, 5, w=6)
        pdig.add_edge(3, 6, w=10)

    def tearDown(self):
        del self.positive_digraph
        del self.positive_graph

    def test_unweighted_shortest_paths_graph(self):
        self.assertEqual(
            unweighted_shortest_paths(self.positive_graph, 1, 2),
            ({1: None, 2: 1, 3: 1, 4: 3, 5: 4, 6: 1},
             {1: 0, 2: 2, 3: 2, 4: 4, 5: 6, 6: 2}))

    def test_unweighted_shortest_paths_graph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            unweighted_shortest_paths(self.positive_graph, 999)

    def test_unweighted_shortest_paths_digraph(self):
        self.assertEqual(
            unweighted_shortest_paths(self.positive_digraph, 1),
            ({1: None, 2: 1, 3: 1, 4: 3, 5: 4, 6: 1},
             {1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 1}))

    def test_unweighted_shortest_paths_digraph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            unweighted_shortest_paths(self.positive_digraph, 999)

    def test_bellman_ford_graph(self):
        self.assertEqual(
            shortest_paths_from(self.positive_graph, 1, weight_attribute='w'),
            ({1: None, 2: 1, 3: 2, 4: 2, 5: 4, 6: 3},
             {1: 0, 2: 5, 3: 15, 4: 20, 5: 26, 6: 25}))

    def test_bellman_ford_graph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            shortest_paths_from(self.positive_graph, 999)

    def test_bellman_ford_graph_negative_edges(self):
        self.positive_graph.add_edge(1, 100, w=-1)
        with self.assertRaises(NegativeCycle):
            shortest_paths_from(self.positive_graph, 1, weight_attribute='w')
        self.positive_graph.remove_edge(1, 100)

    def test_bellman_ford_digraph(self):
        self.assertEqual(
            shortest_paths_from(
                self.positive_digraph, 1, weight_attribute='w'),
            ({1: None, 2: 1, 3: 2, 4: 2, 5: 4, 6: 3},
             {1: 0, 2: 5, 3: 15, 4: 20, 5: 26, 6: 25}))

    def test_bellman_ford_digraph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            shortest_paths_from(self.positive_digraph, 999)

    def test_bellman_ford_digraph_negative_edges(self):
        g = DiGraph()
        g.add_edge(1, 2, w=7)
        g.add_edge(1, 4, w=6)
        g.add_edge(4, 5, w=5)
        g.add_edge(5, 4, w=-2)
        g.add_edge(2, 3, w=9)
        g.add_edge(2, 5, w=-3)
        g.add_edge(4, 2, w=8)
        g.add_edge(4, 3, w=-4)
        g.add_edge(3, 1, w=7)
        g.add_edge(3, 5, w=7)
        self.assertEqual(shortest_paths_from(g, 1, weight_attribute='w'),
                         ({1: None, 2: 1, 3: 4, 4: 5, 5: 2},
                          {1: 0, 2: 7, 3: -2, 4: 2, 5: 4}))

    def test_bellman_ford_digraph_negative_cycle(self):
        self.positive_digraph.add_edge(2, 1, w=-6)
        with self.assertRaises(NegativeCycle):
            shortest_paths_from(
                self.positive_digraph, 1, weight_attribute='w')
        self.positive_digraph.remove_edge(2, 1)

    def test_dijkstra_graph(self):
        self.assertEqual(
            dijkstra(self.positive_graph, 1, weight_attribute='w'),
            ({1: None, 2: 1, 3: 2, 4: 2, 5: 4, 6: 3},
             {1: 0, 2: 5, 3: 15, 4: 20, 5: 26, 6: 25}))

    def test_dijkstra_graph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            dijkstra(self.positive_graph, 999)

    def test_dijkstra_digraph(self):
        self.assertEqual(
            dijkstra(self.positive_digraph, 1, weight_attribute='w'),
            ({1: None, 2: 1, 3: 2, 4: 2, 5: 4, 6: 3},
             {1: 0, 2: 5, 3: 15, 4: 20, 5: 26, 6: 25}))

    def test_dijkstra_digraph_missing_node(self):
        with self.assertRaises(NodeNotFound):
            dijkstra(self.positive_digraph, 999)
Пример #27
0
 def test_centers_one_node_tree(self):
     tree = Graph()
     tree.add_node(1)
     self.assertEqual(centers(tree), {1, })
Пример #28
0
def test_node_can_be_successfully_added_to_the_graph():
  node_a = Node('a')
  graph = Graph()
  graph.add_node(node_a)
  assert node_a in graph._adjacency_list
  assert graph._adjacency_list[node_a] == []
Пример #29
0
 def setUp(self):
     tree = Graph()
     tree.add_edge(1, 2)
     tree.add_edge(1, 3)
     tree.add_edge(1, 4)
     tree.add_edge(2, 5)
     tree.add_edge(2, 6)
     tree.add_edge(5, 7)
     tree.add_edge(3, 8)
     tree.add_edge(4, 9)
     tree.add_edge(9, 10)
     self.tree = tree
Пример #30
0
def test_depth_first():
  node_a = Node('a')
  node_b = Node('b')
  node_c = Node('c')
  node_d = Node('d')
  node_e = Node('e')
  node_f = Node('f')
  node_g = Node('g')
  node_h = Node('h')

  graph = Graph()

  graph.add_node(node_a)
  graph.add_node(node_b)
  graph.add_node(node_c)
  graph.add_node(node_d)
  graph.add_node(node_e)
  graph.add_node(node_f)
  graph.add_node(node_g)
  graph.add_node(node_h)

  graph.add_edge(node_a,node_b)
  graph.add_edge(node_a,node_d)
  graph.add_edge(node_b,node_a)
  graph.add_edge(node_b,node_c)
  graph.add_edge(node_b,node_d)
  graph.add_edge(node_c,node_b)
  graph.add_edge(node_c,node_g)
  graph.add_edge(node_d,node_a)
  graph.add_edge(node_d,node_b)
  graph.add_edge(node_d,node_e)
  graph.add_edge(node_d,node_f)
  graph.add_edge(node_d,node_h)
  graph.add_edge(node_e,node_d)
  graph.add_edge(node_f,node_d)
  graph.add_edge(node_f,node_h)
  graph.add_edge(node_g,node_c)
  graph.add_edge(node_h,node_d)
  graph.add_edge(node_h,node_f)

  assert(graph.depth_first(node_a)) == ['a', 'b', 'c', 'g', 'd', 'e', 'f', 'h']