Esempio n. 1
0
File: DBCP.py Progetto: hfsun/DBCP
def Bestplacement(graph, colors,avg_weight,max_weight,inter_weight):
    subG = subGraph(graph,colors)              

    controllers = []
    for subg in subG:
        controllerplace = ''
        mintl = -1
        for node in subg:
            lenghts = nx.single_source_shortest_path_length(subg,node)
            lenghts_graph = nx.single_source_shortest_path_length(graph,node)
            mx = -1
            ag = 0.0
            wg = 0.0
            for l in lenghts:
                if lenghts[l]>mx:
                    mx = lenghts[l]
                ag += lenghts[l]
            for l in lenghts_graph:
                wg += lenghts_graph[l]
            tl = tradeoff_function(ag/nx.number_of_nodes(subg),avg_weight,mx,max_weight,wg/nx.number_of_nodes(graph),inter_weight)
            #tl = avg_weight*(ag/nx.number_of_nodes(subg))+max_weight*mx
            if mintl < 0:
                mintl = tl
                controllerplace = node
            elif tl < mintl:
                mintl = tl
                controllerplace = node
        controllers.append(controllerplace)
    return controllers
Esempio n. 2
0
def ego_graph(G,n,radius=1,center=True,undirected=False,distance=None):
    """Returns induced subgraph of neighbors centered at node n within
    a given radius.
    
    Parameters
    ----------
    G : nxgraph
      A NetworkX Graph or DiGraph

    n : node 
      A single node 

    radius : number, optional
      Include all neighbors of distance<=radius from n.
      
    center : bool, optional
      If False, do not include center node in nxgraph

    undirected : bool, optional      
      If True use both in- and out-neighbors of directed graphs.

    distance : key, optional      
      Use specified edge data key as distance.  For example, setting
      distance='weight' will use the edge weight to measure the
      distance from the node n.

    Notes
    -----
    For directed graphs D this produces the "out" neighborhood
    or successors.  If you want the neighborhood of predecessors
    first reverse the nxgraph with D.reverse().  If you want both
    directions use the keyword argument undirected=True.

    Node, edge, and nxgraph attributes are copied to the returned subgraph.
    """
    if undirected:
        if distance is not None:
            sp,_=nx.single_source_dijkstra(G.to_undirected(),
                                           n,cutoff=radius,
                                           weight=distance)
        else:
            sp=nx.single_source_shortest_path_length(G.to_undirected(),
                                                     n,cutoff=radius)
    else:
        if distance is not None:
            sp,_=nx.single_source_dijkstra(G,
                                           n,cutoff=radius,
                                           weight=distance)
        else:
            sp=nx.single_source_shortest_path_length(G,n,cutoff=radius)

    H=G.subgraph(sp).copy()
    if not center:
        H.remove_node(n)
    return  H
Esempio n. 3
0
def add_pseudo_edges(g, dg, threshold):
    """ flawed logic, needs to be fixed """

    if threshold == -1 : return -1

    if len(dg) == 0:
        dg = nx.read_edgelist(sys.argv[2], create_using=dg)

    new_edges = []
    for n in dg.nodes():

        if not g.has_node(n): continue

        fw_count = {}
        n_dists = nx.single_source_shortest_path_length(g,n,4)
        followings = set(dg.successors(n))

        for node, dist in n_dists.iteritems():
            if dist > 2: continue

            for f in dg.successors(node):
                if f not in followings:
                    if f in fw_count:
                        fw_count[f] = fw_count[f] + 1
                    else: fw_count[f] = 1

        for k,v in fw_count.iteritems():
            if v >= threshold and k in n_dists and n_dists[k] <= 4: 
                new_edges.append((n,k))

    for e in new_edges: dg.add_edge(*e)
    print >> sys.stderr, "new edges", len(new_edges)
    return 0
Esempio n. 4
0
    def test_shortest_path(self):
        deg = [3, 2, 2, 1]
        G = nx.generators.havel_hakimi_graph(deg)
        cs1 = nxt.creation_sequence(deg, with_labels=True)
        for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3),
                     (3, 1), (1, 2), (2, 3)]:
            assert_equal(nxt.shortest_path(cs1, n, m),
                         nx.shortest_path(G, n, m))

        spl = nxt.shortest_path_length(cs1, 3)
        spl2 = nxt.shortest_path_length([t for v, t in cs1], 2)
        assert_equal(spl, spl2)

        spld = {}
        for j, pl in enumerate(spl):
            n = cs1[j][0]
            spld[n] = pl
        assert_equal(spld, nx.single_source_shortest_path_length(G, 3))

        assert_equal(nxt.shortest_path(['d', 'd', 'd', 'i', 'd', 'd'], 1, 2), [1, 2])
        assert_equal(nxt.shortest_path([3, 1, 2], 1, 2), [1, 2])
        assert_raises(TypeError, nxt.shortest_path, [3., 1., 2.], 1, 2)
        assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 'a', 2)
        assert_raises(ValueError, nxt.shortest_path, [3, 1, 2], 1, 'b')
        assert_equal(nxt.shortest_path([3, 1, 2], 1, 1), [1])
Esempio n. 5
0
def calc_LDEglob_node(G, xNode):
    '''
    A function to calculate the nodal contribution of path length
    L, diameter D, and global efficiency Eglob from a node xNode.
    
    input parameters:
          G:      A graph in networkX format.
          xNode:  The node where the nodal global efficiency is calculated.
    
    returns:
          L:         The nodal path length at xNode.
          D:         The nodal diameter at xNode.
          EglobSum:  The nodal global efficiency.
    '''
         
    NNodes = len(G.nodes())
    Dx = list(nx.single_source_shortest_path_length(G, xNode).values())
    indZ = np.nonzero(np.array(Dx)==0)[0]
    nzDx = np.delete(Dx, indZ)
    if len(nzDx)>0:
        EglobSum = np.sum(1.0/nzDx)
        L = np.mean(nzDx)
        D = np.max(nzDx)
    else:
        EglobSum = 0
        L = 0
        D = 0
    # returning the nodal global efficiency
    return L, D, EglobSum
Esempio n. 6
0
def savegraph(G, i):

    G = nx.random_geometric_graph(200, 0.125)
    # position is stored as node attribute data for random_geometric_graph
    # pos=nx.get_node_attributes(G,'pos')

    # find node near center (0.5,0.5)
    # dmin=1
    # ncenter=0
    # for n in pos:
    #    x,y=pos[n]
    #    d=(x-0.5)**2+(y-0.5)**2
    #    if d<dmin:
    #        ncenter=n
    #        dmin=d

    ncenter = 0.5
    dmin = 0.5

    # color by path length from node near center
    p = nx.single_source_shortest_path_length(G, ncenter)

    plt.figure(figsize=(8, 8))
    nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
    nx.draw_networkx_nodes(G, pos, nodelist=p.keys(), node_size=80, node_color=p.values(), cmap=plt.cm.Reds_r)

    plt.xlim(-0.05, 1.05)
    plt.ylim(-0.05, 1.05)
    plt.axis("off")
    name = "graph " + str(i) + ".png"
    plt.savefig(name)
def obca(g):
    diameter = nx.diameter(g)
    lb_max = diameter + 1

    # Rank the nodes according to their degree
    results = nx.degree_centrality(g)
    nodes = next(zip(*sorted(results.items(), key=operator.itemgetter(1))))
    results = dict()

    for lb in range(2, lb_max):
        covered_frequency = [0] * len(g.nodes())
        boxes = list()

        for i in range(0, len(nodes)):
            node = nodes[i]

            if covered_frequency[i] > 0:
                continue

            box = list(nx.single_source_shortest_path_length(g, node, lb-1).keys())

            # Verify that all paths within the box have the length less then lb
            index = 0
            while True:
                node = box[index]
                for j in range(index+1, len(box)):
                    neighbor = box[j]

                    if nx.shortest_path_length(g, node, neighbor) >= lb:
                        box.remove(neighbor)

                index += 1
                if index >= len(box):
                    break

            for node in box:
                node_index = nodes.index(node)
                covered_frequency[node_index] += 1

            boxes.append(box)

        for box in boxes:
            redundant_box = True

            for node in box:
                node_index = nodes.index(node)
                if covered_frequency[node_index] == 1:
                    redundant_box = False
                    break

            if redundant_box:
                for node in box:
                    node_index = nodes.index(node)
                    covered_frequency[node_index] -= 1
                boxes.remove(box)

        print("lb: {}, boxes: {}, cf: {}".format(lb, boxes, covered_frequency))
        results[lb] = boxes

    return results
Esempio n. 8
0
    def show_geo_graph(graph):
        node_position = nx.get_node_attributes(graph,'pos')

        # Find node near center (0.5,0.5)
        d_min            = 1
        node_near_center = 0
        for node in node_position:
            x, y = node_position[node]
            distance = (x - 0.5)**2 + (y - 0.5)**2
            if distance < d_min:
                node_near_center = node
                d_min = distance

        # Color by path length from node near center
        color_node        = dict(nx.single_source_shortest_path_length(graph, node_near_center))
        array_color_node  = np.array(list(color_node.values()))

        sns.set_style('darkgrid')
        cmap = sns.cubehelix_palette(start = .5, rot = -.65, dark = .4, light = .6, as_cmap = True)
        plt.figure(figsize = (10, 8))
        nx.draw_networkx_edges(graph, node_position, nodelist=[node_near_center],alpha=0.4)
        nx.draw_networkx_nodes(graph, node_position, nodelist=color_node.keys(),
                               node_size = 80,
                               node_color = array_color_node,
                               cmap = cmap)

        plt.xlim(0,1)
        plt.ylim(0,1)
        plt.axis('off')
        file = str(graph_path) + "/graph.pdf"
        plt.savefig(file, transparent = True)
Esempio n. 9
0
def neighbourhood(G, n, k=1):
    """Get k-neighbourhood of node n"""
    if k == 1:
        return G[n]
    dist = nx.single_source_shortest_path_length(G, n, k)
    del dist[n]
    return dist.keys()
Esempio n. 10
0
def connected_components(G):
    """Return nodes in connected components of graph.

    Parameters
    ----------
    G : NetworkX Graph
       An undirected graph.

    Returns
    -------
    comp : list of lists
       A list of nodes for each component of G.

    See Also       
    --------
    strongly_connected_components

    Notes
    -----
    The list is ordered from largest connected component to smallest.
    For undirected graphs only. 
    """
    if G.is_directed():
        raise nx.NetworkXError("""Not allowed for directed graph G.
              Use UG=G.to_undirected() to create an undirected graph.""")
    seen={}
    components=[]
    for v in G:      
        if v not in seen:
            c=nx.single_source_shortest_path_length(G,v)
            components.append(list(c.keys()))
            seen.update(c)
    components.sort(key=len,reverse=True)            
    return components            
Esempio n. 11
0
def random_graph():
    G=nx.random_geometric_graph(200,0.125)
    # position is stored as node attribute data for random_geometric_graph
    pos=nx.get_node_attributes(G,'pos')

    # find node near center (0.5,0.5)
    dmin=1
    ncenter=0
    for n in pos:
        x,y=pos[n]
        d=(x-0.5)**2+(y-0.5)**2
        if d<dmin:
            ncenter=n
            dmin=d

    # color by path length from node near center
    p=nx.single_source_shortest_path_length(G,ncenter)

    plt.figure(figsize=(8,8))
    nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
    nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),
                           node_size=80,
                           node_color=p.values(),
                           cmap=plt.cm.Reds_r)

    plt.xlim(-0.05,1.05)
    plt.ylim(-0.05,1.05)
    plt.axis('off')
    #plt.savefig('random_geometric_graph.png')
    plt.show()
Esempio n. 12
0
File: bindiff.py Progetto: 0xbc/angr
    def _distances_from_function_exit(function):
        """
        :param function:    A normalized Function object.
        :returns:           A dictionary of basic block addresses and their distance to the exit of the function.
        """
        reverse_graph = function.graph.reverse()
        # we aren't guaranteed to have an exit from the function so explicitly add the node
        reverse_graph.add_node("start")
        found_exits = False
        for n in function.graph.nodes():
            if len(function.graph.successors(n)) == 0:
                reverse_graph.add_edge("start", n)
                found_exits = True

        # if there were no exits (a function with a while 1) let's consider the block with the highest address to
        # be the exit. This isn't the most scientific way, but since this case is pretty rare it should be okay
        if not found_exits:
            last = max(function.graph.nodes(), key=lambda x:x.addr)
            reverse_graph.add_edge("start", last)

        dists = networkx.single_source_shortest_path_length(reverse_graph, "start")

        # remove temp node
        del dists["start"]

        # correct for the added node
        for n in dists:
            dists[n] -= 1

        return dists
Esempio n. 13
0
File: bindiff.py Progetto: 0xbc/angr
 def _distances_from_function_start(function):
     """
     :param function:    A normalized Function object.
     :returns:           A dictionary of basic block addresses and their distance to the start of the function.
     """
     return networkx.single_source_shortest_path_length(function.graph,
                                                        function.startpoint)
Esempio n. 14
0
def graphStats(graph):

    pathlengths = []

    #print("source vertex {target:length, }")
    for v in graph.nodes():
        spl = networkx.single_source_shortest_path_length(graph, v)
        #print('%s %s' % (v,spl))
        for p in spl.values():
            pathlengths.append(p)

    print('')
    print(
        "average shortest path length %s" %
         (sum(pathlengths) / len(pathlengths)))

    # histogram of path lengths
    dist = {}
    for p in pathlengths:
        if p in dist:
            dist[p] += 1
        else:
            dist[p] = 1

    print('')
Esempio n. 15
0
def r2_neighbors(graph, n):
    dict = nx.single_source_shortest_path_length(graph, n, 2)
    dict = gt.invert_dict(dict)
    if 2 in dict:
        return dict[2]
    else:
        return []
Esempio n. 16
0
 def test_single_source_shortest_path_length(self):
     ans = dict(nx.shortest_path_length(self.cycle, 0))
     assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
     assert_equal(ans,
                  dict(nx.single_source_shortest_path_length(self.cycle,
                                                             0)))
     ans = dict(nx.shortest_path_length(self.grid, 1))
     assert_equal(ans[16], 6)
     # now with weights
     ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight'))
     assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
     assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
         self.cycle, 0)))
     ans = dict(nx.shortest_path_length(self.grid, 1, weight='weight'))
     assert_equal(ans[16], 6)
     # weights and method specified
     ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
                                        method='dijkstra'))
     assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
     assert_equal(ans, dict(nx.single_source_dijkstra_path_length(
         self.cycle, 0)))
     ans = dict(nx.shortest_path_length(self.cycle, 0, weight='weight',
                                        method='bellman-ford'))
     assert_equal(ans, {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1})
     assert_equal(ans, dict(nx.single_source_bellman_ford_path_length(
         self.cycle, 0)))
Esempio n. 17
0
def get_neighbors(graph, node, level):
    """Get neighbors of a given node up to a certain level"""

    min_level = int(level)
    if min_level < level:
        max_level = min_level + 1
        percentaje = level - min_level
    else:
        max_level = level
        percentaje = 0

    # All neighbors up to max_level
    all_neighbors = nx.single_source_shortest_path_length(graph, node,
                                                          cutoff=max_level)

    if percentaje > 0:
        neighbors_min_level = [k for (k, v) in all_neighbors.items() if (1 <= v <= min_level)]
        neighbors_max_level = [k for (k, v) in all_neighbors.items() if v == max_level]
        n = np.round(len(neighbors_max_level) * percentaje)
        additional_neighbors = random.sample(neighbors_max_level, int(n))
        neighbors = neighbors_min_level + additional_neighbors
    else:
        neighbors = [k for (k, v) in all_neighbors.items() if (1 <= v <= max_level)]

    return neighbors
Esempio n. 18
0
def depth_crawl(web, depth):
    random.seed(42)
    page_db = aduana.PageDB(tempfile.mkdtemp(prefix='test-', dir='./'))
    backend = aduana.frontera.Backend(
        aduana.BFScheduler.from_settings(
            page_db,
            settings={
                'MAX_CRAWL_DEPTH': depth
            }
        )
    )
    backend.frontier_start()
    backend.add_seeds([FakeLink('https://news.ycombinator.com/', 1.0)])

    crawled = []
    requests = True
    while requests:
        requests = backend.get_next_requests(10)
        for req in requests:
            crawled.append(req.url)
            links = web.crawl(req.url)
            backend.page_crawled(FakeResponse(req.url), map(FakeLink, links))

    dist = nx.single_source_shortest_path_length(
        web.graph,
        'https://news.ycombinator.com/',
        cutoff=depth
    )
    for page in crawled:
        assert dist[page] <= (depth - 1)

    backend.frontier_stop()
Esempio n. 19
0
def node_connected_component(G,n):
    """Return nodes in connected components of graph containing node n.

    Parameters
    ----------
    G : NetworkX Graph
       An undirected graph.

    n : node label       
       A node in G

    Returns
    -------
    comp : lists
       A list of nodes in component of G containing node n.

    See Also       
    --------
    connected_components

    Notes
    -----
    For undirected graphs only. 
    """
    if G.is_directed():
        raise nx.NetworkXError("""Not allowed for directed graph G.
              Use UG=G.to_undirected() to create an undirected graph.""")
    return list(nx.single_source_shortest_path_length(G,n).keys())
Esempio n. 20
0
File: sna.py Progetto: rik0/pynetsym
def approximate_cpl(graph, q=0.5, delta=0.15, eps=0.05):
    """
    Computes the approximate CPL for the specified graph
    :param graph: the graph
    :param q: the q-median to use (default 1/2-median, i.e., median)
    :param delta: used to compute the size of the sample
    :param eps: used to compute the size of the sample
    :return: the median
    :rtype: float
    """
    import networkx
    assert isinstance(graph, networkx.Graph)
    s = _estimate_s(q, delta, eps)
    s = int(math.ceil(s))
    if graph.number_of_nodes() <= s:
        sample = graph.nodes_iter()
    else:
        sample = random.sample(graph.adj.keys(), s)

    averages = []
    for node in sample:
        path_lengths = networkx.single_source_shortest_path_length(graph, node)
        average = sum(path_lengths.values()) / float(len(path_lengths))
        averages.append(average)
    averages.sort()
    median_index = int(len(averages) * q + 1)
    return averages[median_index]
def findNeighbours_of_Nulls(fileName, writer):
   G = nx.Graph()
   
   with open(fileName, 'r') as meterFile:
      distReader = csv.reader(meterFile, delimiter=',')
      next(distReader, None)
      for row in distReader:
        G.add_node(row[0], lat=row[1], lng=row[2])
        G.add_node(row[3], lat=row[4], lng=row[5])
        G.add_edge(row[0],row[3], length= row[-1])
   coord_neighbors = {}     
   nulls = [n for n in G.nodes() if G.node[n]['lat'] == "null" and G.node[n]['lng'] == "null"]
   for node in nulls:
     length = nx.single_source_shortest_path_length(G,node)
     sorted_length = sorted(length.items(), key=operator.itemgetter(1))
     neighCoords = []
     # exclude the firs item of list from the loop which is the node itself with the distance of zero from the node! i.e. ('node',0)
     for l in sorted_length[1:]:
       # check the distance of node from the neigbor and if the neighbor has coordinate
       if 0 < l[1] and G.node[l[0]]['lat'] != "null" and G.node[l[0]]['lng'] != "null":
           # add the neighbor to array
           neighCoords.append( l[1])
       # limit the neighbors to two to have at leat two neighbours with 
       if len(neighCoords) >= 2:
         break
     writer.writerow([node,neighCoords])
Esempio n. 22
0
def _global_relabeling_heuristic(G, t, labels_dict=None, res_capacity='res_capacity', 
                                 capacity='capacity',preflow='preflow',
                                 distance='distance',excess='excess'):
    """
    The global relabeling heuristic updates the distance function by computing
    shortest path distances in the residual graph from all nodes to the sink.
    """
    res_G = nx.DiGraph()
    for u,v in G.edges_iter():
        if G[u][v][capacity] - G[u][v][preflow] > 0:
            res_G.add_edge(u,v,res_capacity=G[u][v][capacity]-G[u][v][preflow])
        if G[u][v][preflow] > 0:
            res_G.add_edge(v,u,res_capacity=G[u][v][preflow])

    distances = nx.single_source_shortest_path_length(
                               res_G.reverse(copy=False), t)
    for v in distances:
        G.node[v][distance] = distances[v]
            
    if labels_dict is not None:
        for v in distances:
            if G.node[v][excess] > 0 and labels_dict.has_key(G.node[v][distance]):
                labels_dict[G.node[v][distance]].remove(v)
                if labels_dict.has_key(distances[v]):
                    labels_dict[distances[v]].append(v)
                else:
                    labels_dict[distances[v]] = [v]
Esempio n. 23
0
def average_shortest_path_length(G, weight=None):
    r"""Return the average shortest path length.

    The average shortest path length is

    .. math::

       a =\sum_{s,t \in V} \frac{d(s, t)}{n(n-1)}

    where `V` is the set of nodes in `G`,
    `d(s, t)` is the shortest path from `s` to `t`,
    and `n` is the number of nodes in `G`.

    Parameters
    ----------
    G : NetworkX graph

    weight : None or string, optional (default = None)
       If None, every edge has weight/distance/cost 1.
       If a string, use this edge attribute as the edge weight.
       Any edge attribute not present defaults to 1.

    Raises
    ------
    NetworkXError:
       if the graph is not connected.

    Examples
    --------
    >>> G=nx.path_graph(5)
    >>> print(nx.average_shortest_path_length(G))
    2.0

    For disconnected graphs you can compute the average shortest path
    length for each component:
    >>> G=nx.Graph([(1,2),(3,4)])
    >>> for g in nx.connected_component_subgraphs(G):
    ...     print(nx.average_shortest_path_length(g))
    1.0
    1.0

    """
    if G.is_directed():
        if not nx.is_weakly_connected(G):
            raise nx.NetworkXError("Graph is not connected.")
    else:
        if not nx.is_connected(G):
            raise nx.NetworkXError("Graph is not connected.")
    avg=0.0
    if weight is None:
        for node in G:
            path_length=nx.single_source_shortest_path_length(G, node)
            avg += sum(path_length.values())
    else:
        for node in G:
            path_length=nx.single_source_dijkstra_path_length(G, node, weight=weight)
            avg += sum(path_length.values())
    n=len(G)
    return avg/(n*(n-1))
Esempio n. 24
0
def getEdgeDepthMap(og):
    """returns the depth of each edge measured by the first node in the edge"""
    depthMap = nx.single_source_shortest_path_length(og,og.graph['root'])
    edgeDepthMap = []
    for e in og.edges():
        edgeDepthMap.append((depthMap[e[0]],(e[0],e[1])))
    edgeDepthMap.sort()
    return edgeDepthMap
Esempio n. 25
0
    def get_Neib_one(self,ipt,l):
        nei=nx.single_source_shortest_path_length(self.G,ipt,cutoff=l)
        NB={}
        for key,value in nei.iteritems():
            NB.setdefault(value,set()).add(key)
            NB.setdefault('AllNb',set()).add(key)

        return NB
Esempio n. 26
0
def addDistDAttr(dag, d='Dst'):
    dag.reverse(copy=False)
    distD = nx.single_source_shortest_path_length(dag, d)
    for v in dag.nodes():
        dag.add_node(v, distD=distD.get(v, NaN))
        pass
    dag.reverse(copy=False)
    return
Esempio n. 27
0
def efficiency(G):
    avg = 0.0
    n = len(G)
    for node in G:
        path_length = nx.single_source_shortest_path_length(G, node)
        avg += sum(1.0/v for v in path_length.values() if v !=0)
    avg *= 1.0/(n*(n-1))
    return avg
Esempio n. 28
0
def shortest_path_test(vertices):
    """Computes the single-source shortest path using NetworkX."""
    G = nx.DiGraph()
    for u in vertices:
        for v in u.out_vertices:
            G.add_edge(u.id, v.id)
    sp = nx.single_source_shortest_path_length(G, source)
    return sp
Esempio n. 29
0
def eccentricity(G, v=None, sp=None):
    """Return the eccentricity of nodes in G.

    The eccentricity of a node v is the maximum distance from v to
    all other nodes in G.

    Parameters
    ----------
    G : NetworkX graph
       A graph

    v : node, optional
       Return value of specified node       

    sp : dict of dicts, optional       
       All pairs shortest path lengths as a dictionary of dictionaries

    Returns
    -------
    ecc : dictionary
       A dictionary of eccentricity values keyed by node.
    """
#    nodes=
#    nodes=[]
#    if v is None:                # none, use entire graph 
#        nodes=G.nodes()
#    elif v in G:               # is v a single node
#        nodes=[v]
#    else:                      # assume v is a container of nodes
#        nodes=v
    order=G.order()

    e={}
    for n in G.nbunch_iter(v):
        if sp is None:
            length = dict(networkx.single_source_shortest_path_length(G, n))
            L = len(length)
        else:
            try:
                length = sp[n]
                L = len(length)
            except TypeError:
                raise networkx.NetworkXError('Format of "sp" is invalid.')
        if L != order:
            if G.is_directed():
                msg = ('Found infinite path length because the digraph is not'
                       ' strongly connected')
            else:
                msg = ('Found infinite path length because the graph is not'
                       ' connected')
            raise networkx.NetworkXError(msg)

        e[n]=max(length.values())

    if v in G:
        return e[v]  # return single value
    else:
        return e
Esempio n. 30
0
def check_path(g, source, dists, followers):

    nodes = nx.single_source_shortest_path_length(g,source,2)
    count = 0

    for n in nodes.iterkeys():
        if n in followers and n in dists and dists[n] <= 4: count += 1

    return count
Esempio n. 31
0
    def test_shortest_path(self):
        deg = [3, 2, 2, 1]
        G = nx.generators.havel_hakimi_graph(deg)
        cs1 = nxt.creation_sequence(deg, with_labels=True)
        for n, m in [(3, 0), (0, 3), (0, 2), (0, 1), (1, 3), (3, 1), (1, 2),
                     (2, 3)]:
            assert_equal(nxt.shortest_path(cs1, n, m),
                         nx.shortest_path(G, n, m))

        spl = nxt.shortest_path_length(cs1, 3)
        spl2 = nxt.shortest_path_length([t for v, t in cs1], 2)
        assert_equal(spl, spl2)

        spld = {}
        for j, pl in enumerate(spl):
            n = cs1[j][0]
            spld[n] = pl
        assert_equal(spld, nx.single_source_shortest_path_length(G, 3))
Esempio n. 32
0
def dF_with_distance(G, src_a=None, src_b=None):
    allnodes = G.nodes()

    shortest_pathlengths = nx.single_source_shortest_path_length(G, src_a)
    shortest_pathlengths_ordered = [
        shortest_pathlengths[node] for node in allnodes
    ]

    source_vector = csr_matrix((G.number_of_nodes(), 1))
    src_a_ind = allnodes.index(src_a)
    source_vector[src_a_ind] = 1

    if src_b:
        src_b_ind = allnodes.index(src_b) if src_b else None
        source_vector[src_b_ind] = -1

    return shortest_pathlengths_ordered, solve(G.loopy_laplacian(),
                                               source_vector)
Esempio n. 33
0
    def display_node_attributes(node):
        """Accepts a node, calculates various attributes, and formats it into a string"""

        text = G[node]["name"] if hasattr(G[node], "name") else "Node: " + str(node)

        if nx.is_directed(G):
            text += ", In Degree: " + str(G.in_degree[node])
            text += ", Out Degree: " + str(G.out_degree[node])
        else:
            text += "Degree: " + str(G.degree[node])

        eccentricity = max(nx.single_source_shortest_path_length(G, node))
        text += ", Ecentricity: " + str(eccentricity)

        if nx.is_directed(G):
            text += ", Reciprocity: {:.2f}".format(reciprocities[node])

        return text
Esempio n. 34
0
    def __compute_depth(self, df):
        self.logger.step_start('Compute depth...')
        df = df.sort_values(by=['cascade_id', 'new_tid'])
        all_depths = []

        for cid in df.cascade_id.unique():
            cascade = df[df['cascade_id'] == cid]
            g = nx.DiGraph()
            g.add_nodes_from(cascade.new_tid.values)
            g.add_edges_from([
                (u, v) for u, v in zip(cascade.new_parent_tid, cascade.new_tid)
            ][1:])
            depths = nx.single_source_shortest_path_length(g, 0)
            all_depths += [v for _, v in sorted(depths.items())]

        df['depth'] = all_depths
        self.logger.step_end()
        return df
Esempio n. 35
0
    def _recalc(self):
        """
        Обходит граф, считая максимальные длины путей в центральную вершину и
        из неё.
        """
        path_down = nx.single_source_shortest_path_length(self.nx_graph, self.pov_id)
        path_up = dict(nx.single_target_shortest_path_length(self.nx_graph, self.pov_id))

        self.levels_down = max([length for length in path_down.values()])
        self.levels_up = max([length for length in path_up.values()])
        # поиск вершин без потомков
        for node in self.nx_graph.node:
            self.nx_graph.node[node]["is_blind"] = (not list(self.nx_graph.successors(node)))
        # поиск циклов, проходящих через точку отсчёта
        cycles = simple_cycles(self.nx_graph)
        cycles = list(filter(lambda cycle: self.pov_id in cycle, cycles))
        for node in set([node for cycle in cycles for node in cycle]):
            self[node]["in_cycle"] = True
    def setup_graph(self):
        color_map = []
        self.base_stations = np.reshape(self.base_stations,
                                        (self.no_of_stations, 2))
        dist = cdist(self.base_stations,
                     self.base_stations,
                     metric="euclidean")
        for i in range(self.no_of_stations):
            self.G.add_node(i, **{'MEC': self.if_mec[i]})
            if self.if_mec[i] == "yes":
                color_map.append("green")
            else:
                color_map.append("red")
        for i in range(self.no_of_stations):
            for j in range(i + 1, self.no_of_stations):
                self.G.add_edge(i, j, weight=dist[i][j])

        self.T = nx.minimum_spanning_tree(self.G)

        for i in range(self.no_of_stations):
            for j in range(self.no_of_stations):
                self.hop_to_hop[i][j] = len(
                    nx.shortest_path(self.T, source=i, target=j)) - 1
        edges = list(self.T.edges(data=True))
        #nx.draw_networkx(self.T,self.pos_loc, node_color=color_map,with_labels=True, node_size = 500)
        self.hop_dist = defaultdict(list)
        for i in range(self.no_of_stations):
            for j in range(self.no_of_stations):
                x = nx.single_source_shortest_path_length(self.T, i, cutoff=j)
                x = [k for k, v in x.items() if v == j]
                self.hop_dist[tuple((i, j))] = x
        X = self.T.copy()
        x = 50

        for i in range(len(self.usercoordinates)):
            if i % 2 == 0:
                X.add_node(x,
                           pos=(self.usercoordinates[i],
                                self.usercoordinates[i + 1]))
                color_map.append("blue")
                x += 1

        pos = nx.get_node_attributes(X, 'pos')
        self.pos_loc.update(pos)
Esempio n. 37
0
def calculate_graph_stats():
    print('Calculating Graph Stats')
    pathlengths.clear()
    loads.clear()
    loadids.clear()
    for v in G.nodes():
        if existing[v]:
            spl = dict(nx.single_source_shortest_path_length(G, v))
            # print('*', v, " = ", end='')
            top = 0
            cnt = 0
            for p in spl:
                top += spl[p]
                cnt += 1
                pathlengths.append(spl[p])
            avgSP = top / cnt
            # print(top, " > ", avgSP)
            loads.append(10000 if avgSP == 0 else avgSP)
            loadids.append(v)
        else:
            loads.append(20000)
            loadids.append(v)

    print('')
    if (len(pathlengths) > 0):
        print("average load %s" % (sum(pathlengths) / len(pathlengths)))
        avgLoad[0] = sum(pathlengths) / len(pathlengths)
        print("minimum load %s" % (min(loads)))

    print("density: %s" % nx.density(G))

    dist.clear()
    for p in pathlengths:
        if p in dist:
            dist[p] += 1
        else:
            dist[p] = 1

    print('')
    if (len(dist) > 0):
        print("length #paths")
        verts = dist.keys()
        for d in sorted(verts):
            print('%s %d' % (d, dist[d]))
Esempio n. 38
0
 def compute_candidate_paths(self, hops, min_pivot, bad_neighbors):
 
     # Get the feasible candidate paths
     # print('hops',hops,'min_pivot',min_pivot)
     all_paths = nx.single_source_shortest_path_length(self.graph, min_pivot, cutoff=hops)
     # print('all paths before',all_paths)
     all_paths = [(k,v) for k,v in all_paths.items() if v == hops]
     
     # print('all paths',all_paths)
     # print('bad neighbors',bad_neighbors)
     
     # Prune out those passing through bad neighbors
     feasible_paths = []
     for candidate in all_paths:
         path = nx.shortest_path(self.graph, candidate[0], min_pivot)
         if path[-2] not in bad_neighbors:
             feasible_paths += [path]
     
     return feasible_paths
Esempio n. 39
0
    def ComputeLookahead(self):
        """Computes a basic block lookahead map for all bbls."""
        self._ComposeInterProceduralCFG()
        logging.info('Reversing computed Interprocedural CFG ...')
        ipcfg_reverse = self._ipcfg.reverse(copy=True)
        logging.info('Computing shortest paths from each packet send site ...')
        for pkt_send_bbl_entry_node in self._packet_send_bbl_entry_nodes:
            length = nx.single_source_shortest_path_length(
                ipcfg_reverse, pkt_send_bbl_entry_node)
            for reachable_node in length:
                if (not re.match('__BBL_([0-9]+)_ENTRY', reachable_node)
                        or length[reachable_node] < 0):
                    continue

                self._bbl_lookahead[reachable_node] = \
                    min(self._bbl_lookahead[reachable_node],
                        length[reachable_node])
        logging.info('Lookahead computation completed for %d basic blocks ',
                     len(self._bbl_lookahead))
Esempio n. 40
0
def G_features(G, time):
	# section a is closeness centrality and b is betweenes centrality
	g_t  = create_unweighted_G_t(G.train, 0)
	biggest_scc = g_t.subgraph(max(nx.strongly_connected_components(g_t), key=len))
	a_dict = {}
	b_dict = dict.fromkeys(biggest_scc, 0.0)
	size = biggest_scc.number_of_nodes()
	reversed_scc = biggest_scc.reverse()
	for node in  reversed_scc.nodes():
		a_dict[node] = (size - 1) / sum(nx.single_source_shortest_path_length(reversed_scc,node).values())

	for node in biggest_scc.nodes() :
		nodes_queue, predecessors, number_of_shortest_paths = BFS_With_Shortest_Paths(biggest_scc, node)
		b_dict = update_betweenness(b_dict, nodes_queue, predecessors, number_of_shortest_paths, node)
	
	for node in biggest_scc.nodes() :
		b_dict[node] = b_dict[node] * (1 / ((size - 1) * (size -2)))

	return {'a': a_dict, 'b': b_dict }
Esempio n. 41
0
    def __call__(self, data, root_idx=None):
        num_atoms = data.x.size()[0]
        G = graph_data_obj_to_nx(data)

        if root_idx is None:
            if self.center is True:
                root_idx = data.center_node_idx.item()
            else:
                root_idx = random.sample(range(num_atoms), 1)[0]

        # in the PPI case, the subgraph is the entire PPI graph
        data.x_substruct = data.x
        data.edge_attr_substruct = data.edge_attr
        data.edge_index_substruct = data.edge_index
        data.center_substruct_idx = data.center_node_idx

        # Get context that is between l1 and the max diameter of the PPI graph
        l1_node_idxes = nx.single_source_shortest_path_length(G, root_idx, self.l1).keys()
        # l2_node_idxes = nx.single_source_shortest_path_length(G, root_idx,
        #                                                       self.l2).keys()
        l2_node_idxes = range(num_atoms)
        context_node_idxes = set(l1_node_idxes).symmetric_difference(set(l2_node_idxes))
        if len(context_node_idxes) > 0:
            context_G = G.subgraph(context_node_idxes)
            context_G, context_node_map = reset_idxes(context_G)  # need to
            # reset node idx to 0 -> num_nodes - 1, other data obj does not
            # make sense
            context_data = nx_to_graph_data_obj(context_G, 0)  # use a dummy
            # center node idx
            data.x_context = context_data.x
            data.edge_attr_context = context_data.edge_attr
            data.edge_index_context = context_data.edge_index

        # Get indices of overlapping nodes between substruct and context,
        # WRT context ordering
        context_substruct_overlap_idxes = list(context_node_idxes)
        if len(context_substruct_overlap_idxes) > 0:
            context_substruct_overlap_idxes_reorder = [
                context_node_map[old_idx] for old_idx in context_substruct_overlap_idxes
            ]
            data.overlap_context_substruct_idx = torch.tensor(context_substruct_overlap_idxes_reorder)

        return data
Esempio n. 42
0
def get_distance_distribution(network, n_process=4):
    # n_nodes = network.num_vertices()
    n_nodes = len(network)
    # n_edges = network.num_edges()
    n_edges = network.number_of_edges()

    vertices = list(network.nodes())
    l = len(vertices)
    distance_distribution = {}

    # if n_nodes > 10000 or n_edges > 500000:
    #     return None

    for i in range(1, len(vertices)):
        v = vertices[i]
        v_id = int(v)

        # distance_map = topology.shortest_distance(
        #     network,
        #     source=v,
        #     target=None,
        #     directed=False
        # )
        # distance_map = nx.shortest_path(network, source=v, target=None, weight=None)

        distance_map = nx.single_source_shortest_path_length(network, source=v)

        # distance_map = nx.all_pairs_shortest_path_length(network)

        # distance_array = distance_map.get_array()[v_id:]
        # distance_array = distance_map.ToArray()
        # for j in np.nditer(distance_array):
        # for j in distance_map.items():
        for key, value in distance_map.items():
            # distance = int(j)
            distancewithsq = distance_map[key]
            distance = int(distancewithsq)

            if distance not in distance_distribution:
                distance_distribution[distance] = 0
                # distance_distribution.insert(distance, 0)
            distance_distribution[distance] += 1
    return distance_distribution
Esempio n. 43
0
def cool_plot(G_temp):
    G = []
    G = G_temp
    ncenter = 5
    p = dict(nx.single_source_shortest_path_length(G, ncenter))

    plt.figure(figsize=(8, 8))
    nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
    nx.draw_networkx_nodes(G,
                           pos,
                           nodelist=list(p.keys()),
                           node_size=80,
                           node_color=list(p.values()),
                           cmap=plt.cm.Reds_r)

    plt.xlim(-0.05, 1.05)
    plt.ylim(-0.05, 1.05)
    plt.axis('off')
    plt.show()
Esempio n. 44
0
    def _get_hard_negative_path(self, scan_id, path):
        """ Create a negative path that starts along the path then goes to a random neighbor."""
        g, d = self._graphs[scan_id], self._distances[scan_id]
        offset = np.random.randint(1, len(path) - 1)
        source, goal = path[offset], path[-1]

        # get valid neighbors within 4 and 6 hops and greater than 3m from the goal
        max_hops, min_hops, min_distance = 6 - offset, 4 - offset, 3
        neighbors = nx.single_source_shortest_path_length(g,
                                                          source,
                                                          cutoff=max_hops)
        neighbors = [k for k, v in neighbors.items() if v >= min_hops]
        valid = [node for node in neighbors if d[goal][node] > min_distance]
        if len(valid) == 0:
            return

        # return the shortest path to a random negative target viewpoint
        negative = np.random.choice(valid)
        return path[:offset] + nx.dijkstra_path(g, source, negative)
Esempio n. 45
0
def EfficiencyComputation(InputGraph):
	
	G=InputGraph.copy()
	N=G.order()
	vertexlist=G.nodes()
	
	EfficiencySum=0.0
	for current_vertex in vertexlist:
		print 'current_vertex:',current_vertex
		CurrentVertexDistanceList=list(nx.single_source_shortest_path_length(G,current_vertex).values())
		print 'CurrentVertexDistanceList:',CurrentVertexDistanceList
		InverseDistanceList=ElementwiseInvertList(CurrentVertexDistanceList)
		print 'InverseDistanceList:',InverseDistanceList
		EfficiencySum+=sum(InverseDistanceList)
		print 'EfficiencySum:',EfficiencySum
		print '------------------------'
		
	AverageEfficiency=(1.0*EfficiencySum)/(N*(N-1))
	return AverageEfficiency
Esempio n. 46
0
def path_length_distribution(H):
    """
    Estimates shortest path length distribution between all node pairs. If the graph is not connected it uses the giant component instead.

    Parameters:
        G (NetworkX graph object): graph to estimate on.

    Returns:
        shortest path distribution (dict): keys are mean path length, median path length, std path length, skw path length, kurtosis path length
        
    """
    G = H.copy()

    if not is_connected(H):
        print("graph is not connected, GC will be used instead")
        #use largest connected component
        #G  = sorted(nx.connected_components(G), key=len, reverse=True)
        Gcc = sorted(nx.connected_components(G), key=len, reverse=True)
        G = G.subgraph(Gcc[0])

    pathlengths = []

    for v in G.nodes():

        spl = dict(nx.single_source_shortest_path_length(G, v))
        for p in spl:
            pathlengths.append(spl[p])

    avg_path_length = (sum(pathlengths) / len(pathlengths))
    median_path = statistics.median(pathlengths)

    std_path = statistics.stdev(pathlengths)
    skw_path = skew(pathlengths)
    kurt_path = kurtosis(pathlengths)

    return {
        "mean path length": avg_path_length,
        "median path length": median_path,
        "std path length": std_path,
        "skw path length": skw_path,
        "kurtosis path length": kurt_path
    }
def Verification(G_last_snapshot, G_current_snapshot, degree):
    
    #print "%d edges in G_last_snapshot" %(len(G_last_snapshot.edges()))
    #print "%d edges in G_current_snapshot" %(len(G_current_snapshot.edges()))

    #print "%d nodes in G_last_snapshot" %(len(G_last_snapshot.nodes()))
    #print "%d nodes in G_current_snapshot" %(len(G_current_snapshot.nodes()))

    for node in G_current_snapshot.nodes():
        
        realistic_degree = len(nx.single_source_shortest_path_length(G_current_snapshot, node, 2)) - 1
        record_degree = degree[node]

        if (record_degree != realistic_degree):
            print "now we check node %d, record_degree = %d, realistic_degree = %d" %(node, record_degree, realistic_degree)
            time.sleep(1000)
	    return 0

    print "every thing is OK!"
    return 1
Esempio n. 48
0
def connected_components(G):
    """
    Return a list of lists of nodes in each connected component of G.

    The list is ordered from largest connected component to smallest.
    For undirected graphs only. 
    """
    if G.is_directed():
        raise networkx.NetworkXError,\
              """Not allowed for directed graph G.
              Use UG=G.to_undirected() to create an undirected graph."""
    seen = {}
    components = []
    for v in G:
        if v not in seen:
            c = single_source_shortest_path_length(G, v)
            components.append(c.keys())
            seen.update(c)
    components.sort(lambda x, y: cmp(len(y), len(x)))
    return components
def count_parameters(G):

    graph_parameters = []
    N = nx.number_of_nodes(G)
    # print(N)
    nodes = nx.nodes(G)
    for i in nodes:
        deg = nx.degree(G, i)
        # if Sp=0, then the SP doesn't exist
        SP = nx.single_source_shortest_path_length(G, i)
        sum_SP = 0
        Ecc = 0
        for j in SP:
            if SP[j] > Ecc:
                Ecc = SP[j]
            sum_SP = sum_SP + SP[j]
        param = [i, deg, sum_SP, Ecc]
        graph_parameters.append(param)

    return graph_parameters
Esempio n. 50
0
def average_all_pairs_shortest_path_estimate(G, max_num_sources=100):
    if nx.number_connected_components(G)>1:
        return METRIC_ERROR

    num_sources = min(G.number_of_nodes(), max_num_sources)
    baseline_sources = []
    if hasattr(G, '_musketeer_data'):
        if 'all_pairs_shortest_path_estimate_SOURCES' in G._musketeer_data:
            baseline_sources = G._musketeer_data['all_pairs_shortest_path_estimate_SOURCES']
            baseline_sources = filter(lambda s: s in G, baseline_sources)
    else:
        G._musketeer_data = {}
    sampled_sources = baseline_sources + random.sample(G.nodes(), num_sources - len(baseline_sources))
    G._musketeer_data['all_pairs_shortest_path_estimate_SOURCES'] = sampled_sources
    total_distance = 0.0
    for source in sampled_sources:
        lengths = nx.single_source_shortest_path_length(G, source)
        total_distance += np.average(lengths.values())
    total_distance /= num_sources
    return total_distance
Esempio n. 51
0
 def n_closure(work_author_dict, coauthor_graph, n=2):
     if n < 1:
         raise AttributeError('cannot have `n` < 1')
     if n == 1:
         return work_author_dict
     work_n_closure_dict = {}
     for work, authors in work_author_dict.items():
         n_auths = set()
         for author in authors:
             n_auths.add(author)
             if coauthor_graph.has_node(author):
                 if n == 2:
                     for neighbor in coauthor_graph.neighbors(author):
                         n_auths.add(neighbor)
                 else:
                     for neighbor, l in nx.single_source_shortest_path_length(coauthor_graph, author, cutoff=n).items():
                         if l > 1 and l <= n:
                             n_auths.add(neighbor)
         work_n_closure_dict[work] = list(n_auths)
     return work_n_closure_dict
Esempio n. 52
0
def harm_cent(graph):
    shortest_paths = {
    }  # Dictionary for storing the shortest paths from each vertex
    harmonic_centralities = {
    }  # Dictionary for storing the harmonic centralities
    sum_of_geodesics = 0
    for k in graph.nodes():  # Iterating through each node

        shortest_paths = nx.single_source_shortest_path_length(graph, k)
        # Fidning the SSSP length from each node to all other nodes
        sum_of_geodesics = sum(shortest_paths.values()) / (total_vertices - 1)
        # Summing over all the geodesics for each source vertex
        harmonic_centralities[k] = sum_of_geodesics
        # Storing the harmonic centrality for each node
        shortest_paths = {}

    sorted_harmonics = sorted(harmonic_centralities.items(),
                              key=operator.itemgetter(1))
    # Sorting the harmonic centralities in decreasing order of importance
    return sorted_harmonics
Esempio n. 53
0
    def _rigid_mode(self, mode):
        """Compute an eigenmode for the system given a rigid-body constraint"""
        # Fill mode with available values from the selected eigenvector of
        # the Hessian
        arr = np.zeros((self._top.n_atoms, 3))
        arr[self._ind, :] = np.reshape(self.eigenvectors_[:, mode],
                                       (self.n_atoms_, 3))

        # Initialize graph of bonded atoms
        G = nx.Graph([(i.index, j.index) for i, j in self._top.bonds])

        # For each non-selected atom search the graph for the nearest bonded
        # atom that has been selected and have the query atom inherit its
        # gradient
        for i in (set(range(self._top.n_atoms)) - set(self._ind)):
            path_lengths = nx.single_source_shortest_path_length(G, i)
            d = np.array(list(path_lengths.values()))[self._ind]
            arr[i, :] = arr[np.argmin(d), :]

        return arr
Esempio n. 54
0
    def show_geo_graph(graph):
        node_position = nx.get_node_attributes(graph, 'pos')

        # Find node near center (0.5,0.5)
        d_min = 1
        node_near_center = 0
        for node in node_position:
            x, y = node_position[node]
            distance = (x - 0.5)**2 + (y - 0.5)**2
            if distance < d_min:
                node_near_center = node
                d_min = distance

        # Color by path length from node near center
        color_node = dict(
            nx.single_source_shortest_path_length(graph, node_near_center))
        array_color_node = np.array(list(color_node.values()))

        sns.set_style('darkgrid')
        cmap = sns.cubehelix_palette(start=.5,
                                     rot=-.65,
                                     dark=.4,
                                     light=.6,
                                     as_cmap=True)
        plt.figure(figsize=(10, 8))
        nx.draw_networkx_edges(graph,
                               node_position,
                               nodelist=[node_near_center],
                               alpha=0.4)
        nx.draw_networkx_nodes(graph,
                               node_position,
                               nodelist=color_node.keys(),
                               node_size=80,
                               node_color=array_color_node,
                               cmap=cmap)

        plt.xlim(0, 1)
        plt.ylim(0, 1)
        plt.axis('off')
        file = str(graph_path) + "/graph.pdf"
        plt.savefig(file, transparent=True)
def main():
    read_files()

    print(sp.artist((get_artist_id(ARTISTS[0]))).keys())
    for artist_name in ARTISTS:
        artist_id = get_artist_id(artist_name)
        artist = sp.artist(artist_id)
        print(artist["name"], artist["popularity"], artist["genres"])

    dash_size = 50
    print("=" * dash_size)
    time(construct_graph)
    print("=" * dash_size)

    print(len(G), "rappers included.\n" + "=" * dash_size)

    yt = "Young Thug"
    for comp in nx.connected_components(G):
        if len(comp) == 1:
            continue

        s = G.subgraph(comp)
        print("eccentricity:\n", nx.eccentricity(s))

        c = nx.center(s)
        print("center:\n", c)

        dists = nx.single_source_shortest_path_length(G, yt if yt in c else c[0])

        print("distributions of distance:")
        freq = {}
        for _, d in dists.items():
            if d not in freq:
                freq[d] = 0
            freq[d] += 1
        print(freq)

        print("distance from center:\n", dists)
        print("-" * dash_size)

    vis_graph()
Esempio n. 56
0
def average_shortest_path_len(G):
	#return nx.average_shortest_path_length(G)
	
	if G.is_directed():
		if not nx.is_weakly_connected(G):
			raise nx.NetworkXError("Graph is not connected.")
	else:
		if not nx.is_connected(G):
			raise nx.NetworkXError("Graph is not connected.")
		
	avg = 0.0
	for node in G:
		path_length = nx.single_source_shortest_path_length(G, node)
		avg += sum(path_length.values())
		
		#if node % 100 == 0:
			#print(node)
	
	n = len(G)
	
	return avg/(n*(n-1))
Esempio n. 57
0
def plot_nests(nests):
    g = nx.DiGraph(nests)
    root = [n for n in g.nodes if g.in_degree(n) == 0][0]
    lengths = nx.single_source_shortest_path_length(g, root)
    pos = {}
    levels = [0] * (max(lengths.values()) + 1)
    for key, x in lengths.items():
        pos[key] = [levels[x], -x]
        levels[x] += 1
    plot = nx.draw(g,
                   pos=pos,
                   node_color='white',
                   alpha=1,
                   node_size=1000,
                   arrows=False,
                   edge_color='green',
                   font_size=15,
                   font_weight='normal',
                   labels={k: k
                           for k in g.nodes})
    return plot
Esempio n. 58
0
    def get_subgraph (self, search_term, radius):
        """
        use BFS to label nodes as part of a 'neighborhood' subgraph
        """
        subgraph = set([])
        paths = {}
        the_node_id = None

        for node_id, label in self.labels.items():
            if label == search_term:
                the_node_id = node_id
                r = nx.bfs_edges(self.nxg, source=node_id, depth_limit=radius)
                subgraph = set([node_id])

                for _, neighbor in r:
                    subgraph.add(neighbor)

                paths = nx.single_source_shortest_path_length(self.nxg, node_id, cutoff=radius)
                break

        return subgraph, paths, str(the_node_id)
Esempio n. 59
0
    def __init__(self, map: dict, should_display: bool = True):
        nx.Graph.__init__(self)
        for site in map["sites"]:  # populate sites in main graph
            self.add_node(site["id"])
            self.node[site["id"]]["site"] = Site(site["id"], site["x"],
                                                 site["y"])
        self.add_edges_from([
            (river["source"], river["target"]) for river in map["rivers"]
        ])  # populate edges in main graph
        self.mines = map["mines"]  # get mines in self.mines
        for mine in self.mines:  # add mines to main graph
            self.node[mine]["site"].isMine = True

        for mine in self.mines:  # precalculate score for all site and all mines as it is static
            list = nx.single_source_shortest_path_length(
                self, mine)  # get the path to the mine indexed by node
            nx.set_node_attributes(self, "pathForMine_" + str(mine),
                                   list)  # set the attributes
        self.should_display = should_display
        if self.should_display:
            self.fig = plt.figure(figsize=(10, 30))  # initialize graphics
Esempio n. 60
0
def networkx_call(M, source, edgevals=False):

    # Directed NetworkX graph
    Gnx = nx.from_pandas_edgelist(M,
                                  source='0',
                                  target='1',
                                  edge_attr='weight',
                                  create_using=nx.DiGraph())
    print('NX Solving... ')
    t1 = time.time()

    if edgevals is False:
        path = nx.single_source_shortest_path_length(Gnx, source)
    else:
        path = nx.single_source_dijkstra_path_length(Gnx, source)

    t2 = time.time() - t1

    print('NX Time : ' + str(t2))

    return path, Gnx