Exemplo n.º 1
0
def test_dijkstra_vs_networkx_apsp():
	""" Test Dijkstra: My implementation vs Networkx implementation > All Pairs """
	# NX Version
	G = nx.from_edgelist(edgelist_james) 
	nx.set_edge_attributes(G, 'weight', edgelist_james)
	nx_all_complete_paths = nx.all_pairs_dijkstra_path(G, 'weight')

	# My Version
	d = Dijkstra()
	d.from_edgelist(edgelist_james, directed=False)
	dc_all_lenghts, dc_all_paths = d.all_pairs_shortest_paths()
	dc_all_complete_paths = d.shortest_complete_paths

	assert (nx_all_complete_paths == dc_all_complete_paths)
Exemplo n.º 2
0
def test_dijkstra_vs_networkx_single_source_all_lenghts_and_paths():
	""" Test Dijkstra: Rion's implementation vs Networkx implementation > Single Source """
	# NX Version
	G = nx.from_edgelist(edgelist_james) 
	nx.set_edge_attributes(G, 'weight', edgelist_james)
	nx_lenghts = nx.single_source_dijkstra_path_length(G, source='s', weight='weight')
	nx_paths = nx.single_source_dijkstra_path(G, source='s', weight='weight')
	
	# My Version
	d = Dijkstra()
	d.from_edgelist(edgelist_james, directed=False)
	dc_lenghts, dc_paths = d.single_source_shortest_paths('s', kind='metric')

	assert (nx_lenghts == dc_lenghts)
	assert (nx_paths == dc_paths)
Exemplo n.º 3
0
def compute_metric_backbone(G):
    print('> Computing Dijkstra APSP')
    dict_edges = {(i, j): prox2dist(d['weight'])
                  for i, j, d in G.edges(data=True)}
    dij = Dijkstra.from_edgelist(dict_edges, directed=False, verbose=10)

    print('> Metric')
    poolresults = list(range(len(dij.N)))
    for node in dij.N:
        print('> Dijkstra node {node:d} of {n_nodes:d}'.format(node=node + 1,
                                                               n_nodes=len(
                                                                   dij.N)))
        poolresults[node] = _cy_single_source_shortest_distances(
            node, dij.N, dij.E, dij.neighbors, ('min', 'sum'), verbose=2)
    shortest_distances, local_paths = map(list, zip(*poolresults))
    dij.shortest_distances = dict(zip(dij.N, shortest_distances))
    MSD = dij.get_shortest_distances(format='dict', translate=True)
    #
    dict_edges_backbone = {}
    dict_edges_s_values = {}
    for (i, j), d in dict_edges.items():

        # Backbone = distance == distance-closure
        if d == MSD[i][j]:
            dict_edges_backbone[(i, j)] = True
        else:
            dict_edges_backbone[(i, j)] = False

        # S-Value = distance / distance-closure
        dict_edges_s_values[(i, j)] = d / MSD[i][j]

    return dict_edges_backbone, dict_edges_s_values
Exemplo n.º 4
0
def test_dijkstra_sssp_from_edgelist():
	""" Test Dijkstra: Single Source Shortest Path (SSSP) from Edgelist """
	dij = Dijkstra(verbose=True)
	dij.from_edgelist(edgelist_james)
	dij.single_source_shortest_paths(source='s', kind='metric')
Exemplo n.º 5
0
def test_dijkstra_sssp_from_numpy():
	""" Test Dijkstra: Single Source Shortest Path (SSSP) from Numpy """
	dij = Dijkstra(verbose=True)
	dij.from_numpy_matrix(D)
	dij.single_source_shortest_paths(source=0, kind='metric')
Exemplo n.º 6
0
    # Build Networkx object
    print('--- Building Network ---')
    # C for Correlation network
    G = nx.from_pandas_adjacency(df, create_using=nx.Graph)

    # P for Proximity network (which in this case is a Correlation)
    P = [w for i, j, w in G.edges.data('weight')]

    # Converts (P)roximity to (D)istance using a map.
    D_dict = dict(zip(G.edges(), map(prox2dist, P)))
    # Set the distance value for each edge
    nx.set_edge_attributes(G, name='distance', values=D_dict)
    # Compute closure (Using the Dijkstra Class directly)
    print('--- Computing Dijkstra APSP ---')
    dij = Dijkstra.from_edgelist(D_dict, directed=False, verbose=10)
    # Serial Computation
    poolresults = list(range(len(dij.N)))
    for node in dij.N:
        print('> Dijkstra node %s of %s' % (node + 1, len(dij.N)))
        # Computes the shortest distance and paths between a source node and every other node
        poolresults[node] = _cy_single_source_shortest_distances(
            node, dij.N, dij.E, dij.neighbours, ('min', 'sum'), verbose=10)
    shortest_distances, local_paths = map(list, zip(*poolresults))
    dij.shortest_distances = dict(zip(dij.N, shortest_distances))
    # (S)hortest (D)istances
    SD = dij.get_shortest_distances(format='dict', translate=True)
    print('> Done.')

    print('> Populating (G)raph')
    # Mapping results to triplet (i, j): v -  Convert Dict-of-Dicts to Dict