Exemplo n.º 1
0
def test_has_node():
    _graph = Graph()
    _node1 = Node(4)
    _node2 = Node(5)
    _graph.add_node(_node1)
    assert _graph.had_node(_node1) == True
    assert _graph.had_node(_node2) == False
Exemplo n.º 2
0
    def test_add_neighbor(self):
        print('Test: test_add_neighbor')
        n, n1 = Node(0), Node(1)

        print('Test: Invalid input')
        self.assertRaises(ValueError, n.add_neighbor, None)
        self.assertRaises(ValueError, n.add_neighbor, n)

        print('Test: general case')
        n.add_neighbor(n1)
        self.assertEqual(n.neighbors[0][0].key, n1.key)
        print('Success: test_add_neighbor')
Exemplo n.º 3
0
def test_adjacent():
    _graph = Graph()
    _node1 = Node(4)
    _node2 = Node(5)
    _node3 = Node(6)
    _node4 = Node(7)
    _graph.add_edges(_node1, _node2, 1)
    _graph.add_edges(_node3, _node4, 1)
    _graph.add_edges(_node2, _node4, 1)
    assert _graph.adjacent(_node1, _node2) == True
    assert _graph.adjacent(_node3, _node4) == True
    assert _graph.adjacent(_node2, _node4) == True
    assert _graph.adjacent(_node1, _node4) == False
Exemplo n.º 4
0
def test_neighbours():
    _graph = Graph()
    _node1 = Node(4)
    _node2 = Node(5)
    _node3 = Node(6)
    _node4 = Node(7)
    _graph.add_edges(_node1, _node2, 1)
    _graph.add_edges(_node3, _node4, 1)
    _graph.add_edges(_node2, _node4, 1)
    _graph.add_edges(_node1, _node2, 2)
    _graph.add_edges(_node3, _node4, 2)
    _graph.add_edges(_node2, _node4, 2)
    assert _graph.neighbours(_node2) == ['n0', 'n3']
Exemplo n.º 5
0
def test_del_node():
    _graph = Graph()
    _node1 = Node(4)
    _node2 = Node(5)
    _graph.add_edges(_node1, _node2, 1)
    _graph.del_node(_node2)
    assert _graph.graph == {'n0': {}}
    _node3 = Node(6)
    _node4 = Node(7)
    _graph.add_edges(_node1, _node2, 1)
    _graph.add_edges(_node3, _node4, 1)
    _graph.add_edges(_node2, _node4, 1)
    _graph.del_node(_node4)
    assert _graph.graph == {'n0': {'n1': 1}, 'n1': {'n0': 1}, 'n3': {}}
Exemplo n.º 6
0
    def test_get_edges(self):
        """
                  A - 1 - B - 2 - C - 1 - D
                  |       |
                  1       5
                  |       |
                  E - 1 - F
                """
        nodes = {}
        for letter in "ABCDEF":
            nodes[letter] = Node(letter)

        def conn_nod(a, b, len):
            connect_both(nodes[a], nodes[b], len)

        conn_nod("A", "B", 1)
        conn_nod("A", "E", 1)
        conn_nod("E", "F", 1)
        conn_nod("F", "B", 5)
        conn_nod("B", "C", 2)
        conn_nod("D", "C", 1)
        graph = Graph([nodes[key] for key in nodes])

        self.assertEqual(graph["A", "B"], 1)
        self.assertEqual(graph["A", "E"], 1)
        self.assertEqual(graph["E", "F"], 1)
        self.assertEqual(graph["F", "B"], 5)
        self.assertEqual(graph["B", "C"], 2)
        self.assertEqual(graph["D", "C"], 1)
Exemplo n.º 7
0
    def test_graph_cracov_easy_path_len(self):
        stops_common = [
            "Czerwone Maki", "Chmieleniec", "Kampus UJ", "Ruczaj",
            "Norymberska", "Grota", "Lipinskiego"
        ]
        nodes = {}
        for name in stops_common:
            nodes[name] = Node(name)

        def conn_nod(a, b, len):
            connect_both(nodes[a], nodes[b], len)

        def ass_path_len(a, b, i):
            self.assert_path_len(
                graph, a, b, i,
                "between {0} and {1} should be {2}".format(a, b, i))

        nodes_list = [node for node in nodes.values()]
        conn_nod(stops_common[0], stops_common[1], 5)
        conn_nod(stops_common[1], stops_common[2], 5)
        conn_nod(stops_common[2], stops_common[3], 5)
        conn_nod(stops_common[3], stops_common[4], 5)
        conn_nod(stops_common[4], stops_common[5], 5)
        conn_nod(stops_common[5], stops_common[6], 5)
        graph = Graph(nodes_list)
        q = 0
        for i in range(0, len(stops_common)):
            for q in range(0, len(stops_common)):
                self.assert_path_len(graph, stops_common[i], stops_common[q],
                                     abs(i - q) * 5)
Exemplo n.º 8
0
    def test_graph_cracov_easy_path_next(self):
        stops_common = [
            "Czerwone Maki", "Chmieleniec", "Kampus UJ", "Ruczaj",
            "Norymberska", "Grota", "Lipinskiego"
        ]
        nodes = {}
        for name in stops_common:
            nodes[name] = Node(name)

        def conn_nod(a, b, len):
            connect_both(nodes[a], nodes[b], len)

        nodes_list = [node for node in nodes.values()]
        for i in range(0, len(stops_common) - 1):
            conn_nod(stops_common[i], stops_common[i + 1], 5)

        graph = Graph(nodes_list)
        for stop in stops_common[1:]:
            self.assert_path_next_move(graph, "Czerwone Maki", stop,
                                       "Chmieleniec")
        for stop in stops_common[2:]:
            self.assert_path_next_move(graph, "Chmieleniec", stop, "Kampus UJ")
        for stop in stops_common[3:]:
            self.assert_path_next_move(graph, "Kampus UJ", stop, "Ruczaj")
        for stop in stops_common[:2]:
            self.assert_path_next_move(graph, "Kampus UJ", stop, "Chmieleniec")
        for stop in stops_common[4:]:
            self.assert_path_next_move(graph, "Ruczaj", stop, "Norymberska")
Exemplo n.º 9
0
    def test_remove_neighbor(self):
        print('Test: test_remove_neighbor')
        n, n1 = Node(0), Node(1)

        print('Test: remove a non-existent node')
        self.assertRaises(ValueError, n.remove_neighbor, 1)

        print('Test: general case')
        n.add_neighbor(n1)
        self.assertEqual(n.neighbors[0][0].key, n1.key)
        n.remove_neighbor(1)
        self.assertEqual(len(n.neighbors), 0)
        print('Success: test_remove_neighbor')
Exemplo n.º 10
0
    def test_graph_easy_path_len(self):
        """
          A - 1 - B - 2 - C - 1 - D
          |       |
          1       5
          |       |
          E - 1 - F
        """
        nodes = {}
        for letter in "ABCDEF":
            nodes[letter] = Node(letter)

        def conn_nod(a, b, len):
            connect_both(nodes[a], nodes[b], len)

        def ass_path_len(a, b, i):
            self.assert_path_len(
                graph, a, b, i,
                "between {0} and {1} should be {2}".format(a, b, i))

        conn_nod("A", "B", 1)
        conn_nod("A", "E", 1)
        conn_nod("E", "F", 1)
        conn_nod("F", "B", 5)
        conn_nod("B", "C", 2)
        conn_nod("D", "C", 1)
        graph = Graph([nodes[key] for key in nodes])

        ass_path_len("A", "B", 1)
        ass_path_len("B", "A", 1)
        ass_path_len("A", "E", 1)
        ass_path_len("E", "F", 1)
        ass_path_len("F", "B", 3)
        ass_path_len("B", "F", 3)
        ass_path_len("B", "C", 2)
        ass_path_len("D", "C", 1)
        ass_path_len("A", "F", 2)
Exemplo n.º 11
0
def make_nodes():
    return [Node(i) for i in range(10)]
Exemplo n.º 12
0
def test_add_edge():
    graph = Graph()
    graph.add_edges(Node(4), Node(5), 1)
    assert graph.graph == {'n0': {'n1': 1}, 'n1': {'n0': 1}}
Exemplo n.º 13
0
def test_add_node():
    g = Graph()
    g.add_node(Node(4))
    assert g.graph == {'n0': {}}
Exemplo n.º 14
0
def test_floyd_warshall(make_weighted_graph):
    graph_obj = Graph()
    graph_obj.name_counter = 1
    n1 = Node('n1', {'n2': 2, 'n3': 4})
    n2 = Node('n2', {'n1': 2, 'n3': 1, 'n4': 5})
    n3 = Node('n3', {'n1': 4, 'n2': 1, 'n4': 3})
    n4 = Node('n4', {'n2': 5, 'n3': 3})

    graph_obj.add_node(n1)
    graph_obj.add_node(n2)
    graph_obj.add_node(n3)
    graph_obj.add_node(n4)
    graph = graph_obj.graph
    inf = float('inf')
    adj_matrix_of_graph = {
        'n1': {
            'n1': 0,
            'n2': 2,
            'n3': 4,
            'n4': inf
        },
        'n2': {
            'n1': 2,
            'n2': 0,
            'n3': 1,
            'n4': 5
        },
        'n3': {
            'n1': 4,
            'n2': 1,
            'n3': 0,
            'n4': 3
        },
        'n4': {
            'n1': inf,
            'n2': 5,
            'n3': 3,
            'n4': 0
        }
    }

    adj_matrix_of_graph_with_shortest_path = {
        'n1': {
            'n1': 0,
            'n2': 2,
            'n3': 3,
            'n4': 6
        },
        'n2': {
            'n1': 2,
            'n2': 0,
            'n3': 1,
            'n4': 4
        },
        'n3': {
            'n1': 3,
            'n2': 1,
            'n3': 0,
            'n4': 3
        },
        'n4': {
            'n1': 6,
            'n2': 4,
            'n3': 3,
            'n4': 0
        }
    }
    t = graph_obj.floyd_warshall(graph)
    assert t[0] == adj_matrix_of_graph
    assert t[1] == adj_matrix_of_graph_with_shortest_path
Exemplo n.º 15
0
    def test_graph_harder_path_len(self):
        """
          A - - 2 - - E
          |  \      / |
          4   1    3  10
          |    \  /   |
          B -2- D -7- F
          |    / \    |
          5   8    4   6
          | /       \ |
          C - - 1 - - G
        """
        nodes = {}
        for letter in "ABCDEFG":
            nodes[letter] = Node(letter)

        def ass_path_len(a, b, i):
            self.assert_path_len(
                graph, a, b, i,
                "between {0} and {1} should be {2}".format(a, b, i))

        def conn_nod(a, b, len):
            connect_both(nodes[a], nodes[b], len)

        connection_list = [("A", "E", 2), ("A", "B", 4), ("A", "D", 1),
                           ("B", "D", 2), ("B", "C", 5), ("C", "D", 8),
                           ("C", "G", 1), ("E", "D", 3), ("E", "F", 10),
                           ("F", "D", 7), ("F", "G", 6), ("G", "D", 4)]
        for conn in connection_list:
            conn_nod(conn[0], conn[1], conn[2])
        graph = Graph([nodes[key] for key in nodes])

        paths_to_check = [
            ("A", "B", 3),
            ("A", "C", 6),
            ("A", "D", 1),
            ("A", "E", 2),
            ("A", "F", 8),
            ("A", "G", 5),
            ("B", "A", 3),
            ("B", "C", 5),
            ("B", "D", 2),
            ("B", "E", 5),
            ("B", "F", 9),
            ("B", "G", 6),
            ("C", "A", 6),
            ("C", "B", 5),
            ("C", "D", 5),
            ("C", "E", 8),
            ("C", "F", 7),
            ("C", "G", 1),
            ("D", "A", 1),
            ("D", "B", 2),
            ("D", "C", 5),
            ("D", "E", 3),
            ("D", "F", 7),
            ("D", "G", 4),
            ("E", "A", 2),
            ("E", "B", 5),
            ("E", "C", 8),
            ("E", "D", 3),
            ("E", "F", 10),
            ("E", "G", 7),
        ]
        for path in paths_to_check:
            ass_path_len(path[0], path[1], path[2])
Exemplo n.º 16
0
 def test_graph_creation(self):
     """Test graph creation"""
     nodes = []
     for letter in "ABCDEF":
         nodes.append(Node(letter))
     graph = Graph(nodes)