コード例 #1
0
def random_graph(n, p):
    """Crea un grafo aleatorio de 'n' vertices con una probabilidad
    de conexion 'p'.

    Args:
        n (int): Cantidad de vertices del grafo.
        p (float): Probabilidad de que dos vertices del grafo esten conectados.

    Returns:
        Graph: Nuevo grafo.
    """
    import random
    import numpy as np
    graph = np.zeros((n + 1, n + 1))
    for i in range(1, n + 1):
        for j in range(i + 1, n + 1):
            if random.random() <= p:
                graph[i, j] = graph[j, i] = 1
    graph_dict = {}
    for i in range(1, n + 1):
        neighbours_i = []
        for j in range(1, n + 1):
            if graph[i, j]:
                neighbours_i.append(str(j))
        graph_dict[str(i)] = neighbours_i
    return Graph(graph_dict)
コード例 #2
0
def test_for_hd_graph(save_file=True):
    print("_________________________________")
    print("---------------------------------")
    print('Test for HD graph with TSC')
    graph = Graph(HD)
    global algorithms
    statistics = tsc_test(algorithms, graph, 11, 100, 11)
    if save_file is True:
        with open('test_for_real_case_hd.json', 'w') as handle:
            handle.write(json.dumps(statistics))
コード例 #3
0
                n_colored += 1
                # actualizamos los valores de las memorias
                self._update_values(vertex, color, semi_coloring)
                # agregamos cada vecino del vertice a la lista de
                # la componente conexa en caso de que no haya sido visitado
                for neighbour in self._graph.neighbours(vertex):
                    if visit[neighbour] is 0:
                        visit[neighbour] = 1
                        bfs_vertices.append(neighbour)
        return self.threshold(semi_coloring), semi_coloring


if __name__ == "__main__":

    g = {"a": ["b", "c"], "b": ["a", "c"], "c": ["a", "b", "d"], "d": ["c"]}
    graph = Graph(g)
    S = ["1", "2", "3", "4"]
    W = {
        "1": {
            "1": 1,
            "2": .5,
            "3": .25,
            "4": .125
        },
        "2": {
            "1": .5,
            "2": 1,
            "3": .5,
            "4": .25
        },
        "3": {
コード例 #4
0
# Test de CSC general para grafos
def csc_test(algorithms, graph, iters, t, w_function=empiric_dist):
    rtest = GraphTester()
    rtest.make_spectrum(len(graph.vertices()))
    rtest.make_w(w_function)
    return rtest.run_test2(graph, t, algorithms, iters, TSC_OR_CSC='CSC')


simple_graph = {
    "a": ["b", "c"],
    "b": ["a", "c"],
    "c": ["a", "b", "d"],
    "d": ["c"]
}
simple_graph = Graph(simple_graph)


def simple_tsc_test():
    print("_________________________________")
    print("---------------------------------")
    print('Simple TSC test')
    global algorithms
    tsc_test(algorithms, simple_graph, 4, 10, 4)


def simple_csc_test():
    print("_________________________________")
    print("---------------------------------")
    print('Simple CSC test')
    global algorithms