Example #1
0
    def test_process(self):
        G = Graph({0: [1, 2], 1: [2]})
        em = EM()
        communities = em.process(G, 1)
        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}
            },
            undirected=False)
        communities = em.process(G, 3)

        G = Graph({0: [], 1: [], 2: []})
        communities = em.process(G, 3)

        G = Graph({0: {1}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}})
        communities = em.process(G, 7)
Example #2
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)
    def test_process(self):
        G = Graph({0: [1, 2], 1: [2]})
        gn = GN()
        communities = gn.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 = gn.process(G)
        self.assertEqual(
            communities,
            [[1, 2, 4], [0, 3, 5, 6, 7, 11], [10, 13], [8, 9, 12, 14, 15]])

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

        G = Graph({0: {1}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}})
        communities = gn.process(G)
        self.assertEqual(communities, [[0, 1], [2], [3], [4], [5], [6], [7]])
Example #4
0
 def test_isolate(self):
     G = Graph({
         "a": ["c"],
         "b": ["c", "e"],
         "c": ["a", "b", "d", "e"],
         "d": ["c"],
         "e": ["c", "b"],
         "f": []
     })
     self.assertEqual(G.find_isolated_vertices(), ['f'])
     G = Graph({1: [2, 3], 2: [3]}, undirected=False)
     self.assertEqual(G.find_isolated_vertices(), [])
Example #5
0
 def test_add_edge(self):
     G = Graph()
     self.assertEqual(G.has_edge(1, 2), False)
     G = Graph({0: [1, 2], 1: [2]})
     self.assertEqual(G.has_edge(2, 3), False)
     G.add_edge(2, 3)
     self.assertEqual(G.has_edge(2, 3), True)
     self.assertEqual(G.total_edge_weight(), 8)
     G.add_edge(2, 3)
     self.assertEqual(G.total_edge_weight(), 8)
     G = Graph()
     G.add_edge('a', 'z')
     G.add_edge('x', 'y')
     self.assertEqual(G.has_edge('a', 'z'), True)
     self.assertEqual(G.has_edge('x', 'y'), True)
Example #6
0
 def test_edge_weight(self):
     G = Graph({0: [1, 2], 1: [2]})
     self.assertEqual(G.total_edge_weight(1), 2)
     self.assertEqual(G.total_edge_weight(), 6)
     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.total_edge_weight(), 30)
     self.assertEqual(G.total_edge_weight(1), 10)
     G = Graph(undirected=False)
     G.add_edge(1, 2)
     self.assertEqual(G.total_edge_weight(1), 0)
     self.assertEqual(G.total_edge_weight(), 1)
     self.assertEqual(G.total_edge_weight(2), 1)
     self.assertEqual(G.total_edge_weight(1, 'out'), 1)
     self.assertEqual(G.total_edge_weight(1, 'all'), 1)
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_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_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_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
Example #11
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_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_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
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_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_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_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_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_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']
Example #20
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
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_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_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}
Example #24
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)
Example #25
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
         })
Example #26
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
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'] == {}
Example #28
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)
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
Example #30
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