def test_has_node(node, n, result):
    """Test to check if a node exists in graph."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.has_node(n) == result
Exemple #2
0
    def test_process(self):
        G = Graph({0: [1, 2], 1: [2]})
        fn = FN()
        communities = fn.process(G)
        self.assertEqual(communities, [[0, 1, 2]])
        G = Graph({
            1: {2, 4, 7},
            2: {1, 0, 4, 6},
            4: {1, 2, 10},
            7: {1, 3, 5, 6},
            0: {2, 3, 5},
            6: {2, 7, 11},
            3: {0, 7},
            5: {0, 7, 11},
            10: {4, 11, 14, 13},
            11: {6, 5, 10},
            14: {10, 8, 9},
            13: {10},
            8: {15, 14, 9},
            15: {8},
            9: {8, 14, 12},
            12: {9}
        })
        communities = fn.process(G)
        self.assertEqual(
            communities,
            [[1, 2, 4, 5, 6, 7, 11], [0, 3], [8, 9, 10, 12, 13, 14, 15]])

        G = Graph({0: [], 1: [], 2: []})
        communities = fn.process(G)
        self.assertEqual(communities, [[0], [1], [2]])

        G = Graph({0: {1}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}})
        communities = fn.process(G)
        self.assertEqual(communities, [[0, 1], [2], [3], [4], [5], [6], [7]])
def test_multiple_edges():
    from simple_graph import Graph
    new_graph = Graph()
    for pair in PAIR_LIST:
        new_graph.add_edge(pair[0], pair[1])
    for pair in PAIR_LIST:
        assert {(pair[0], pair[1]): 1} in new_graph.edges()
def test_nodes(node, result):
    """Test to check if all nodes are there."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.nodes() == result
def test_delete_edge_key_error():
    """Test if KeyError exception is raised."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('egg')
    with pytest.raises(KeyError):
        graph.delete_edge('dog', 'egg')
def test_del_nodes(node, result):
    """Test to check the deleted nodes aren't there."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    assert g.del_node(1) == result
def test_add_edge_destination_node_not_exist():
    """Test if a node is added if it is the destination and does not exist."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_edge('chicken', 'egg')
    assert 'egg' in graph.container
    assert 'egg' in graph.container['chicken']
def test_neighbors():
    output = ["person", 'are', 77, 6.66778]
    from simple_graph import Graph
    new_graph = Graph()
    for pair in MAP_LIST:
        new_graph.add_edge(pair[0], pair[1])
    for item in output:
        assert item in new_graph.neighbors(10)
def test_neighbors():
    output = ["person", 'are', 77, 6.66778]
    from simple_graph import Graph
    new_graph = Graph()
    for pair in MAP_LIST:
        new_graph.add_edge(pair[0], pair[1])
    for item in output:
        assert item in new_graph.neighbors(10)
Exemple #10
0
def populated():
    g = Graph()
    g.add_node(5)
    g.add_node(7)
    g.add_node("Hello")
    g.add_edge(5, 7, 30)
    g.add_edge(5, "Hello", 10)
    return g
Exemple #11
0
 def test_add_weight(self):
     G = Graph({0: [1, 2], 1: [2]})
     self.assertEqual(G.edge_weight(1, 2), 1)
     G.add_edge_weight(1, 2, 1)
     self.assertEqual(G.edge_weight(1, 2), 2)
     self.assertEqual(G.vertex_weight(1), 1)
     G.add_vertex_weight(1, 1)
     self.assertEqual(G.vertex_weight(1), 2)
Exemple #12
0
 def test_edge_betweenness(self):
     G = Graph(
         {
             's': {
                 'u': {
                     'weight': 10
                 },
                 'x': {
                     'weight': 5
                 }
             },
             'u': {
                 'v': {
                     'weight': 1
                 },
                 'x': {
                     'weight': 2
                 }
             },
             'v': {
                 'y': {
                     'weight': 4
                 }
             },
             'x': {
                 'u': {
                     'weight': 3
                 },
                 'v': {
                     'weight': 9
                 },
                 'y': {
                     'weight': 2
                 }
             },
             'y': {
                 's': {
                     'weight': 7
                 },
                 'v': {
                     'weight': 6
                 }
             }
         },
         undirected=False)
     self.assertDictEqual(
         G.edge_betweenness(), {
             ('s', 'u'): 0.0,
             ('s', 'x'): 0.4,
             ('u', 'v'): 0.15000000000000002,
             ('u', 'x'): 0.15000000000000002,
             ('v', 'y'): 0.2,
             ('x', 'u'): 0.30000000000000004,
             ('x', 'v'): 0.0,
             ('x', 'y'): 0.25,
             ('y', 's'): 0.4,
             ('y', 'v'): 0.05
         })
def test_delete_edge_in_graph():
    """Test if an edge between two nodes is removed."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    graph.delete_edge('egg', 'chicken')
    assert graph.container['egg'] == {}
Exemple #14
0
 def test_degree(self):
     G = Graph({
         'V': ['a', 'd', 'b', 'c', 'e', 'f'],
         'E': [('a', 'd'), ('b', 'c'), ('c', 'c'), ('c', 'e'), ('d', 'c')]
     })
     self.assertEqual(G.degree('a'), 1)
     self.assertEqual(G.degree('c'), 5)
     self.assertEqual(G.degree('d'), 2)
     self.assertEqual(G.degree('f'), 0)
Exemple #15
0
def negative_graph():
    g = Graph()
    g.add_edge(1, 2, 10)
    g.add_edge(1, 3, -4)
    g.add_edge(2, 4, 6)
    g.add_edge(3, 4, 9)
    g.add_edge(4, 5, 8)
    g.add_edge(2, 5, -6)
    return g
Exemple #16
0
 def test_diameter(self):
     G = Graph({
         "a": ["c"],
         "b": ["c", "e", "f"],
         "c": ["a", "b", "d", "e"],
         "d": ["c"],
         "e": ["b", "c", "f"],
         "f": ["b", "e"]
     })
     self.assertEqual(G.diameter(), 3)
Exemple #17
0
 def test_is_connected(self):
     G = Graph({
         "a": ["d"],
         "b": ["c"],
         "c": ["b", "c", "d", "e"],
         "d": ["a", "c"],
         "e": ["c"],
         "f": []
     })
     self.assertEqual(G.is_connected(), False)
     G = Graph({
         "a": ["d", "f"],
         "b": ["c"],
         "c": ["b", "c", "d", "e"],
         "d": ["a", "c"],
         "e": ["c"],
         "f": ["a"]
     })
     self.assertEqual(G.is_connected(), True)
     G = Graph({
         "a": ["d", "f"],
         "b": ["c", "b"],
         "c": ["b", "c", "d", "e"],
         "d": ["a", "c"],
         "e": ["c"],
         "f": ["a"]
     })
     self.assertEqual(G.is_connected(), True)
Exemple #18
0
def breadth_first(graph, start):
    """Breadth First Traveral using a Deque as a queue."""
    g = Graph(graph)
    visited, d = [], deque([start])
    while d:
        vertex = d.popleft()
        if vertex not in visited:
            visited.append(vertex)
            if visited not in g.neighbors(vertex):
                d.extend(g.neighbors(vertex))
    return visited
Exemple #19
0
def depth_first(graph, start):
    """Depth first traversal using deque as a stack."""
    g = Graph(graph)
    visited, d = [], deque([start])
    while d:
        vertex = d.pop()
        if vertex not in visited:
            visited.append(vertex)
            if visited not in g.neighbors(vertex):
                d.extend(g.neighbors(vertex))
    return(visited)
def test_adjacent_true():
    """Test if node returns true."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert graph.adjacent('egg', 'chicken') is True
def populated():
    g = Graph()
    g.add_node(5)
    g.add_node(7)
    g.add_node("Hello")
    g.add_edge(5, 7, 30)
    g.add_edge(5, "Hello", 10)
    return g
def test_neighbors():
    """Return the list of all nodes connect to node."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert graph.get_neighbors('egg') == {'chicken': 0}
Exemple #23
0
 def test_to_dict(self):
     G = Graph({
         1: {
             1: {
                 'weight': 6
             },
             2: {
                 'weight': 2
             },
             0: {
                 'weight': 2
             }
         },
         2: {
             1: {
                 'weight': 2
             },
             2: {
                 'weight': 6
             },
             0: {
                 'weight': 2
             }
         },
         0: {
             1: {
                 'weight': 2
             },
             2: {
                 'weight': 2
             },
             0: {
                 'weight': 6
             }
         }
     })
     self.assertEqual(
         G.to_dict(), {
             'V': [(1, {}), (2, {}), (0, {})],
             'E': [(1, 1, {
                 'weight': 6
             }), (1, 2, {
                 'weight': 2
             }), (0, 1, {
                 'weight': 2
             }), (0, 2, {
                 'weight': 2
             }), (0, 0, {
                 'weight': 6
             }), (2, 2, {
                 'weight': 6
             })]
         })
Exemple #24
0
 def test_find_path(self):
     G = Graph({
         "a": ["d"],
         "b": ["c"],
         "c": ["b", "c", "d", "e"],
         "d": ["a", "c"],
         "e": ["c"],
         "f": []
     })
     self.assertEqual(G.find_path('a', 'b'), ['a', 'd', 'c', 'b'])
     self.assertEqual(G.find_path('a', 'f'), None)
     self.assertEqual(G.find_path('c', 'c'), ['c'])
def test_delete_edge_in_graph():
    """Test if an edge between two nodes is removed."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    graph.delete_edge('egg', 'chicken')
    assert graph.container['egg'] == {}
def negative_graph():
    g = Graph()
    g.add_edge(1, 2, 10)
    g.add_edge(1, 3, -4)
    g.add_edge(2, 4, 6)
    g.add_edge(3, 4, 9)
    g.add_edge(4, 5, 8)
    g.add_edge(2, 5, -6)
    return g
Exemple #27
0
 def test_find_all_paths(self):
     G = Graph({
         "a": ["d", "f"],
         "b": ["c"],
         "c": ["b", "c", "d", "e"],
         "d": ["a", "c"],
         "e": ["c"],
         "f": ["d"]
     })
     self.assertEqual(G.find_all_paths('a', 'b'),
                      [['a', 'd', 'c', 'b'], ['a', 'f', 'd', 'c', 'b']])
     self.assertEqual(G.find_all_paths('a', 'f'),
                      [['a', 'd', 'f'], ['a', 'f']])
     self.assertEqual(G.find_all_paths('c', 'c'), [['c']])
Exemple #28
0
def test_add_edge():
    test = Graph()
    test.add_node(5)
    test.add_node(42)
    test.add_edge(5, 42)

    assert 42 in test.graph_dict[5]
def test_bf_three_nodes():
    """Test breadth first with three nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_node('c')
    graph.add_edge('a', 'b')
    graph.add_edge('a', 'c')
    print(graph.container)
    assert graph.breadth_first('a') == ['a', 'b', 'c']
def test_neighbor(node, n, result):
    """Test to check that the correct node have the right edge."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(1, 1)
    g.add_edge(1, 4)
    g.add_edge(4, 2)
    g.add_edge(3, 5)
    assert g.neighbors(n) == result
def test_add_edge():
    """Test if there is an edge between two nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert 'chicken' in graph.container['egg']
def test_delete_node():
    """Delete selected node."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.delete_node('chicken')
    assert 'chicken' not in graph.container
 def rebuild_graph(self):
     '''
     create a new graph to merge communities to vertices
 '''
     graph = Graph()
     for u, v in self.G.edges:
         u_c = self.nc_map[u]
         v_c = self.nc_map[v]
         weight = self.G.edge_weight(u, v)
         edge = graph.edge(u_c, v_c)
         if edge:
             edge.weight += weight
         else:
             graph.add_edge(u_c, v_c, weight=weight)
     self.G = graph
def test_adjacent_true():
    """Test if node returns true."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert graph.adjacent('egg', 'chicken') is True
def test_neighbors():
    """Return the list of all nodes connect to node."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert graph.get_neighbors('egg') == {'chicken': 0}
Exemple #36
0
def test_weighted_graph():
    test_graph = Graph()
    test_graph.add_edge("A", "B", 2)
    test_graph.add_edge("A", "C", 5)
    test_graph.add_edge("B", "C", 2)
    test_graph.add_edge("B", "D", 6)
    test_graph.add_edge("C", "D", 2)

    return test_graph
def non_multi_connected_nodes():
    ''' Define a graph with no multi-connected nodes. '''
    g = Graph()
    g.add_edge("A", "B")
    g.add_edge("A", "C")
    g.add_edge("A", "E")
    g.add_edge("B", "D")
    g.add_edge("B", "F")
    g.add_edge("C", "G")
    return g
def test_adjacent_false():
    """Test if returns false."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    assert graph.adjacent('egg', 'chicken') is False
def test_adjacent(key, value):
    from simple_graph import Graph
    new_graph = Graph()
    node1 = new_graph.add_node(key)
    node2 = new_graph.add_node(value)
    new_graph.add_edge(node1, node2)
    assert new_graph.adjacent(node1, node2) is True
def test_df_two_nodes():
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b')
    assert graph.depth_first('a') == ['a', 'b']
def test_bf_three_nodes():
    """Test breadth first with three nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_node('c')
    graph.add_edge('a', 'b')
    graph.add_edge('a', 'c')
    print(graph.container)
    assert graph.breadth_first('a') == ['a', 'b', 'c']
def test_add_edge():
    """Test if there is an edge between two nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.add_edge('egg', 'chicken')
    assert 'chicken' in graph.container['egg']
def test_bf_two_nodes():
    """Test breadth first with two nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b')
    assert graph.breadth_first('a') == ['a', 'b']
def test_delete_node():
    """Delete selected node."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    graph.delete_node('chicken')
    assert 'chicken' not in graph.container
def test_del_edge(node, result):
    """Test to check the deleted edges aren't there."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(2, 3)
    g.add_edge(1, 4)
    assert g.del_edge(1, 4) == result
def test_adjacent_false():
    """Test if returns false."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('chicken')
    graph.add_node('egg')
    assert graph.adjacent('egg', 'chicken') is False
def test_df_two_nodes():
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b')
    assert graph.depth_first('a') == ['a', 'b']
def test_adjacent(key, value):
    from simple_graph import Graph
    new_graph = Graph()
    node1 = new_graph.add_node(key)
    node2 = new_graph.add_node(value)
    new_graph.add_edge(node1, node2)
    assert new_graph.adjacent(node1, node2) is True
def test_bf_two_nodes():
    """Test breadth first with two nodes."""
    from simple_graph import Graph
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b')
    assert graph.breadth_first('a') == ['a', 'b']
def test_edge(node, result):
    """Test to check if all edges are there."""
    from simple_graph import Graph
    g = Graph()
    for idx in node:
        g.add_node(idx)
    g.add_edge(2, 3)
    g.add_edge(1, 4)
    assert g.edges() == result
def full_graph():
    g = Graph()
    g.add_node(3)
    g.add_node(10)
    g.add_node(15)
    g.add_node(7)
    g.add_node(14)
    g.add_edge(3, 7, 1)
    g.add_edge(15, 10, 2)
    g.add_edge(14, 3, 3)
    g.add_edge(3, 10, 4)
    g.add_edge(7, 15, 5)
    return g
def test_add_edge():
    g = Graph()
    g.add_node(5)
    g.add_node(10)
    g.add_edge(5, 10, 6)
    assert g.gdict[5] == {10: 6}
def test_add_node():
    g = Graph()
    g.add_node(5)
    assert g.gdict == {5: {}}
def cyclic_graph():
    g = Graph()
    g.add_node(3)
    g.add_node(10)
    g.add_node(5)
    g.add_node(2)
    g.add_node(7)
    g.add_edge(3, 10, 1)
    g.add_edge(3, 5, 2)
    g.add_edge(10, 5, 3)
    g.add_edge(5, 2, 4)
    g.add_edge(2, 7, 5)
    g.add_edge(5, 3, 5)
    return g
def test_add_node():
    g = Graph()
    g.add_node(70)
    assert 70 in g.graph
def test_depth_based_path(graph, start, finish, bool):
    from simple_graph import Graph
    from graphtraverse import depth_based_path
    new_graph = Graph()
    new_graph.node_map = graph
    assert depth_based_path(new_graph, start, finish) == bool
def cyclic():
    g = Graph()
    g.add_node(1)
    g.add_node(2)
    g.add_node(3)
    g.add_node(4)
    g.add_node(5)
    g.add_node(6)
    g.add_node(7)
    g.add_node(8)
    g.add_node(9)
    g.add_edge(1, 2, 1)
    g.add_edge(1, 3, 3)
    g.add_edge(1, 6, 3)
    g.add_edge(5, 1, 10)
    g.add_edge(2, 8, 1000)
    g.add_edge(2, 9, 1)
    g.add_edge(2, 4, 4)
    g.add_edge(2, 3, 50)
    g.add_edge(3, 7, 4)
    g.add_edge(7, 1, 5)
    g.add_edge(7, 6, 10)
    return g