Exemplo n.º 1
0
 def test_add_edge(self):
     graph = Graph("my graph")
     graph.add_vertex("A")
     graph.add_vertex("B")
     graph.add_edge("A", "B")
     self.assertEqual(1, len(graph.edges()))
     graph.add_edge("X", "Y")
     self.assertEqual(2, len(graph.edges()))
     self.assertEqual(4, len(graph.vertices()))
Exemplo n.º 2
0
 def test_edges(self):
     json_graph = {"label": "my graph", "graph": {"A": ["B"], "B": []}}
     graph = Graph(input_graph=json.dumps(json_graph))
     self.assertEqual(json_graph['label'], graph.get_label())
     self.assertEqual(False, graph.is_directed())
     self.assertEqual(json_graph['graph'], graph.get_graph())
     self.assertEqual([Edge('A', 'B')], graph.edges())
Exemplo n.º 3
0
def density(graph: Graph) -> float:
    """
    The graph density is defined as the ratio of the number of edges of a given
    graph, and the total number of edges, the graph could have.
    A dense graph is a graph G = (V, E) in which |E| = Θ(|V|^2)
    :param graph:
    :return:
    """
    V = len(graph.vertices())
    E = len(graph.edges())
    return 2.0 * (E / (V**2 - V))
Exemplo n.º 4
0
def is_complete(graph: Graph) -> bool:
    """
    Checks that each vertex has V(V-1) / 2 edges and that each vertex is
    connected to V - 1 others.

    runtime: O(n^2)
    :param graph:
    :return: true or false
    """
    V = len(graph.vertices())
    max_edges = (V**2 - V)
    if not graph.is_directed():
        max_edges //= 2

    E = len(graph.edges())
    if E != max_edges:
        return False

    for vertex in graph:
        if len(graph[vertex]) != V - 1:
            return False
    return True
Exemplo n.º 5
0
 def test_set_from_adjacency_matrix(self):
     array_graph = np.array([[0, 1], [1, 0]], dtype=object)
     graph = Graph(input_array=array_graph)
     self.assertEqual(2, len(graph.vertices()))
     self.assertEqual(1, len(graph.edges()))