Exemplo n.º 1
0
def gen_bonded_tuples(g, num, bond_pair):
    """Generates tuples of different size, based on the graph and input edge.

    Args:
        g: The networkx Graph object.
        num: The length of the tuple.
        bond_pair: The edge which has to be included in all tuples.

    Returns:
        The set of all tuples of defined length from graph `g`.
    """
    b0, b1 = bond_pair
    paths = []
    if num > 3:
        for nb0 in g[b0]:
            paths.extend(nx.single_source_shortest_path(g, nb0, num-1).values())
        for nb1 in g[b1]:
            paths.extend(nx.single_source_shortest_path(g, nb1, num-1).values())

    paths.extend(nx.single_source_shortest_path(g, b0, num-1).values())
    paths.extend(nx.single_source_shortest_path(g, b1, num-1).values())
    output = set()
    for b in paths:
        if len(b) == num and b0 in b and b1 in b:
            if tuple(reversed(b)) not in output:
                output.add(tuple(b))
    return output
Exemplo n.º 2
0
 def test_single_source_shortest_path(self):
     p = nx.single_source_shortest_path(self.directed_cycle, 3)
     assert_equal(p[0], [3, 4, 5, 6, 0])
     p = nx.single_source_shortest_path(self.cycle, 0)
     assert_equal(p[3], [0, 1, 2, 3])
     p = nx.single_source_shortest_path(self.cycle, 0, cutoff=0)
     assert_equal(p, {0: [0]})
Exemplo n.º 3
0
 def test_single_source_shortest_path(self):
     p = nx.single_source_shortest_path(self.directed_cycle, 3)
     assert p[0] == [3, 4, 5, 6, 0]
     p = nx.single_source_shortest_path(self.cycle, 0)
     assert p[3] == [0, 1, 2, 3]
     p = nx.single_source_shortest_path(self.cycle, 0, cutoff=0)
     assert p == {0: [0]}
Exemplo n.º 4
0
 def get_upstream_reaches(self, outlet_reach, broken_at_gages=True):
     """Returns the comIDs of the upstream reaches for a given reach.  
     If no comID is specified, then all reaches in the network are 
     returned."""
     if broken_at_gages == True:
         return single_source_shortest_path(self._g_rev, outlet_reach)
     else:
         return single_source_shortest_path(self._g_unbroken_reverse, outlet_reach)
Exemplo n.º 5
0
    def create_transaction_list(self, numTransactions, transList):

        numNodes = len(self.graph.nodes())
        time = 0
        tList = []

        if (self.model == "LN"):
            lowDegreeTransaction = int(numTransactions / 2)
            midDegreeTransaction = int(numTransactions / 3)
            highDegreeTransaction = int(numTransactions / 9)
        elif (self.model == "XRP"):
            lowDegreeTransaction = int(numTransactions / 3)
            midDegreeTransaction = int(numTransactions / 3)
            highDegreeTransaction = int(numTransactions / 3)

        for x in range(0, lowDegreeTransaction):
            node = int(random.randint(0, len(self.lowDegreeNodeList) - 1))
            hop_count = random.randint(1, 4)
            paths = (nx.single_source_shortest_path(self.graph, 3))
            path_with_hop_count = []
            for key in paths.keys():
                if (hop_count == len(paths[key])):
                    path_with_hop_count.append(int(key))
            destIndex = random.randint(0, len(path_with_hop_count) - 1)
            dest = path_with_hop_count[destIndex]
            tList.append((node, dest, self.create_transaction_amount(0)))

        for x in range(0, midDegreeTransaction):
            node = int(random.randint(0, len(self.midDegreeNodeList) - 1))
            hop_count = random.randint(1, 4)
            paths = (nx.single_source_shortest_path(self.graph, 3))
            path_with_hop_count = []
            for key in paths.keys():
                if (hop_count == len(paths[key])):
                    path_with_hop_count.append(int(key))
            destIndex = random.randint(0, len(path_with_hop_count) - 1)
            dest = path_with_hop_count[destIndex]
            tList.append((node, dest, self.create_transaction_amount(1)))

        for x in range(0, highDegreeTransaction):
            node = int(random.randint(0, len(self.highDegreeNodeList) - 1))
            hop_count = random.randint(1, 4)
            paths = (nx.single_source_shortest_path(self.graph, 3))
            path_with_hop_count = []
            for key in paths.keys():
                if (hop_count == len(paths[key])):
                    path_with_hop_count.append(int(key))
            destIndex = random.randint(0, len(path_with_hop_count) - 1)
            dest = path_with_hop_count[destIndex]
            tList.append((node, dest, self.create_transaction_amount(2)))

        time = 0
        elements = len(tList)
        for x in range(0, elements):
            tranI = random.randint(0, len(tList) - 1)
            transList.append(
                (time, tList[tranI][0], tList[tranI][1], tList[tranI][2]))
            time = time + random.uniform(0, 5)
Exemplo n.º 6
0
def m_reaching_centrality(graph, m=None, direction=None):
    '''
    The network must be connected.
    The local reaching centrality (LRC) of a node is 
    the proportion of the nodes that could be reached from a given node 
    and the number of nodes in the network minus 1.
    The m-LRC is a cutoff after the n-th step from the given node.
    
    Parameters:
    ----------
    graph: networkx object, must be connected
    
    m : cutoff after n step distance
    
    direction: (None|"in"|"out") A node could reach another one only through in- or out-edge,
                or both of them, if the network is undirected.
    
    
    
    Returns:
    -------
    reaches: Dict of nodes with LRC values.

    
    '''
    #    if not nx.is_connected(graph):# it works only on undirected graphs
    #        print('Graph must be connected.')
    #        return {}

    nr_nodes = graph.number_of_nodes()

    if direction == None:
        graph = graph.copy()
        graph = graph.to_undirected()
        reaches = [
            (len(nx.single_source_shortest_path(graph, n, cutoff=m)) - 1) /
            (nr_nodes - 1) for n in graph.nodes()
        ]

    elif direction == 'in':
        graph = graph.copy()
        graph = graph.reverse()
        reaches = [
            (len(nx.single_source_shortest_path(graph, n, cutoff=m)) - 1) /
            (nr_nodes - 1) for n in graph.nodes()
        ]

    elif direction == 'out':
        reaches = [
            (len(nx.single_source_shortest_path(graph, n, cutoff=m)) - 1) /
            (nr_nodes - 1) for n in graph.nodes()
        ]

    else:
        print('Direction format is not correct.')

    return dict(zip(graph.nodes(), reaches))
Exemplo n.º 7
0
 def get_upstream_reaches(self, outlet_reach, broken_at_gages=True):
     """Returns the comIDs of the upstream reaches for a given reach.  
     If no comID is specified, then all reaches in the network are 
     returned."""
     if broken_at_gages == True:
         return single_source_shortest_path(self._g_rev, outlet_reach)
     else:
         return single_source_shortest_path(self._g_unbroken_reverse,
                                            outlet_reach)
Exemplo n.º 8
0
def neighbor_overlap_orderK(G,node1,node2,k):
    nei1=nx.single_source_shortest_path(G,node1,cutoff=k).keys()
    nei2=nx.single_source_shortest_path(G,node2,cutoff=k).keys()

    # return (x1, x2)
    # where x1 = the number of overlapped neighboring nodes
    # and   x2 = the number of overlapped neighboring nodes / the number of union(two neighborhood nodes)

    shared_neighbor=set(nei1).intersection(set(nei2))
    unioned_neighbor=set(nei1).union(set(nei2))

    return (len(shared_neighbor),float(len(shared_neighbor))/float(len(unioned_neighbor)))
Exemplo n.º 9
0
def edge_parent_finder(abstract, graph):
    '''
    moved this here since i think that it is forgi specific
    '''
    # find out to which abstract node the edges belong
    # finding out where the edge-nodes belong, because the contractor cant possibly do this
    # draw.graphlearn_draw([abstract,graph],size=10, contract=False,vertex_label='id')

    getabstr = {
        contra: node
        for node, d in abstract.nodes(data=True)
        for contra in d.get('contracted', [])
    }
    # print getabstr
    for n, d in graph.nodes(data=True):
        if 'edge' in d:
            # if we have found an edge node...

            # lets see whos left and right of it:
            # if len is 2 then we hit a basepair, in that case we already have both neighbors
            zomg = graph.neighbors(n)
            if len(zomg) == 1:
                zomg += graph.predecessors(n)

            n1, n2 = zomg

            # case1: ok those belong to the same gang so we most likely also belong there.
            if getabstr[n1] == getabstr[n2]:
                abstract.node[getabstr[n1]]['contracted'].add(n)

            # case2: neighbors belong to different gangs...
            else:
                abstract_intersect = set(abstract.neighbors(getabstr[n1])) & \
                    set(abstract.neighbors(getabstr[n2]))

                # case 3: abstract intersect in radius 1 failed, so lets try radius 2
                if not abstract_intersect:
                    abstract_intersect = set(
                        nx.single_source_shortest_path(
                            abstract, getabstr[n1], 2)) & set(
                                nx.single_source_shortest_path(
                                    abstract, getabstr[n2], 2))
                    if len(abstract_intersect) > 1:
                        print("weired abs intersect...")

                for ai_node in abstract_intersect:
                    if 'contracted' in abstract.node[ai_node]:
                        abstract.node[ai_node]['contracted'].add(n)
                    else:
                        abstract.node[ai_node]['contracted'] = set([n])

    return abstract
Exemplo n.º 10
0
def my_shortest_path(G,
                     source=None,
                     target=None,
                     weight=None,
                     orient=None,
                     stop_pt=None):
    if source is None:
        if target is None:
            ## Find paths between all pairs.
            if weight is None:
                paths = nx.all_pairs_shortest_path(G)
            else:
                paths = nx.all_pairs_dijkstra_path(G, weight=weight)
        else:
            ## Find paths from all nodes co-accessible to the target.
            with nx.utils.reversed(G):
                if weight is None:
                    paths = nx.single_source_shortest_path(G, target)
                else:
                    paths = nx.single_source_dijkstra_path(G,
                                                           target,
                                                           weight=weight)

                # Now flip the paths so they go from a source to the target.
                for target in paths:
                    paths[target] = list(reversed(paths[target]))

    else:
        if target is None:
            ## Find paths to all nodes accessible from the source.
            if weight is None:
                paths = nx.single_source_shortest_path(G, source)
            else:
                paths = nx.single_source_dijkstra_path(G,
                                                       source,
                                                       weight=weight)
        else:
            ## Find shortest source-target path.
            if weight is None:
                paths = nx.bidirectional_shortest_path(G, source, target)
            else:
                if orient is None:
                    paths = nx.dijkstra_path(G, source, target, weight)
                else:
                    paths = my_dijkstra_path(G,
                                             source,
                                             target,
                                             weight,
                                             orient=orient,
                                             stop_pt=stop_pt)

    return paths
Exemplo n.º 11
0
    def _construct_k_neighborhood(self, k: int, G: Graph):
        '''
        Função que constrói todas as k-vizinhanças dos vértices defendidos na
        solução.

            Args:
                k (int): profundidade da vizinhança.
                G (Graph): o grafo de entrada.
        '''

        # Conjunto de todas as k-vizinhanças
        kn = set()

        for d in self.defended:

            kn.update(
                # Menores caminhos de profundidade k (as chaves retornam os
                # destinos)
                v_n for v_n in list(single_source_shortest_path(G, d, k)
                                    .keys())
                # Ignorar vértices defendidos
                if v_n not in self.defended
            )

        self.kn = kn
def nearest(source, bibles_only=True):
    #select graph
    if bibles_only:
        tree = bib_tree
    else:
        tree = full_tree

    #get a directed tree to find the leaves (the other viable ISO codes)
    dG = nx.dfs_tree(tree, 'ROOT')
    leaves = [
        node for node in dG.nodes()
        if dG.in_degree(node) != 0 and dG.out_degree(node) == 0
    ]
    leaves.remove(source)

    #find the distance to all other nodes from the rouce
    spaths = nx.single_source_shortest_path(tree, source)

    #check which of the leaves is the best
    bestd = len(spaths[leaves[0]])
    best = leaves[0]
    for l in leaves:
        ld = len(spaths[l])
        if ld < bestd:
            bestd = ld
            best = l

    return best
Exemplo n.º 13
0
def neighborhood(graph, source, max_length, min_length=1):
    """
    Returns all neighbours of `source` that are less or equal
    to `cutoff` nodes away and more or equal to `start` away
    within a graph excluding the node itself.

    Parameters
    ----------
    graph: :class:`networkx.Graph`
        A networkx graph definintion
    source:
        A node key matching one in the graph
    max_length: :type:`int`
        The maxdistance between the node and its
        neighbours.
    min_length: :type:`int`
        The minimum length of a path. Default
        is zero

    Returns
    --------
    list
       list of all nodes distance away from reference
    """
    paths = nx.single_source_shortest_path(G=graph,
                                           source=source,
                                           cutoff=max_length)
    neighbours = [
        node for node, path in paths.items() if min_length <= len(path)
    ]
    return neighbours
    def take_similarity_of_the_beginning(self, conclusion: dict,
                                         premise: tuple):
        """
        This calculates the score between the conclusion and one premise by the PageRank of the premise.
        This takes the similarity between the conclusion and the fist premise of the argumentation into account.
        This catches up:
            "Even though, in cases of doubt, short ar-guments are preferable, we expect that the mostrelevant arguments
            need some space to lay out theirreasoning. However, to investigate such hypothe-ses, ranking functions are
            required that go beyondthe words in an argument and its context."
        by:
            Wachsmuth, H., Potthast, M., Khatib, K.A., Ajjour, Y., Puschmann, J., Qu, J., Dorsch, J., Morari, V.,
            Bevendorff, J., & Stein, B. (2017). Building an Argument Search Engine for the Web. ArgMining@EMNLP.

        The PageRank is divided by the frequency of the premise since the premise may occur in several arguments.

        :param conclusion: Conclusion which should be taken into account.
        :param premise: Premise which should be taken into account.
        :return: Score between the conclusion and one premise.
        """
        premise_id: int = premise[2]
        paths = nx.single_source_shortest_path(self.G_reversed,
                                               source=premise_id)
        paths = [(key, paths[key]) for key in paths]
        max_path = max(paths, key=lambda x: len(x[1]))
        first_premise = max_path[1][len(max_path[1]) - 1]

        return wordnet_similarity(conclusion['conclusion_text'].lower(),
                                  self.argument_unit_text[first_premise].lower()) * \
               self.page_rank_dict[premise_id] / self.frequency[premise_id]
Exemplo n.º 15
0
    def analyzeGraph(self, jsonFile, level=10):
        data = []
        nxg = json_graph.load(open(jsonFile))
        for n in nxg.nodes(data=True):
            if nxg.in_degree(n[0]) == 0:
                rootNode = n
                break 
        paths = nx.single_source_shortest_path(nxg,rootNode[0],level)
        nodes = {} # Dictionary to keep track of nodes at length x from root node
        for k,v in paths.items():
            if k == rootNode[0]: continue # exclude root node
            if not nodes.has_key(len(v) - 1):
                nodes[len(v) - 1] = []
            nodes[len(v) - 1].append(k)
                                     
#        cTotal = 0 # cumulative total

        for k in sorted(nodes.keys()):
            bunch = [rootNode[0]]
            for i in range(1,k + 1):
                bunch.extend(nodes[i])
            subgraph = nxg.subgraph(bunch)
            data.append({'name' : rootNode[1]['name'],
                         'level' : k,
                         'node_cnt' : subgraph.number_of_nodes(),
                         'edge_cnt' : subgraph.number_of_edges()})
        return data
Exemplo n.º 16
0
    def calc_star_uptime(self, n, link_fail):
        '''Calc star uptime.

        NOTE: n is the number of nodes.'''
        # Correct for NetworkX, which adds one to n.
        g = nx.star_graph(n - 1)
        # Node 0 is the center of the star.
        edges = g.number_of_edges()
        nodes = g.number_of_nodes()
        paths = nx.single_source_shortest_path(g, list(g.nodes())[1])
        used = flatten(paths)
        sssp_edges = used.number_of_edges()
        if sssp_edges != g.number_of_edges():
            raise Exception("edge not on sssp for star graph")

        # consider those times when a link failed:
        # first, consider failure on outside of graph
        exp_uptime_outer = link_fail * edges * ((float(edges - 1) / edges) * float(edges) / nodes + \
                                                (float(1) / edges) * float(1) / nodes)
        exp_uptime_outer += (1.0 - (link_fail * sssp_edges)) * 1.0

        # consider only the hub as a controller:
        exp_uptime_inner = link_fail * edges * ((float(edges) / edges) * float(edges) / nodes)
        exp_uptime_inner += (1.0 - (link_fail * edges)) * 1.0

        # merge:
        exp_uptime_weighted = float(edges * exp_uptime_outer + 1 * exp_uptime_inner) / nodes
        return exp_uptime_weighted
Exemplo n.º 17
0
def get_cosine_radius(G, u, r):
    real_paths1 = nx.single_source_shortest_path(G, u, r)
    g = G.subgraph(list(real_paths1.keys()))

    sims = cosine(g)

    my_nodes = []
    my_degree = []
    my_sims = []

    for i in sims:
        my_nodes.append(i)
        my_degree.append(G.degree(i))

    for node in my_nodes:
        if node == u:
            my_sims.append(1)
            continue

        if sims[u].get(node) != None:
            my_sims.append(sims[u][node])
        else:
            my_sims.append(0)

    d = {'node_name': my_nodes, 'degree': my_degree, 'cos_sim': my_sims}
    df = pd.DataFrame(d)

    # df['cos_sim'] = (1-df['cos_sim'])
    df['cos_sim'] = normalize_rdd(df, 1, 1000, 'cos_sim')
    df['cos_sim'] = np.log(df['cos_sim'])

    return df
Exemplo n.º 18
0
def ListInsert(graph, metadata):
    '''
    Function takes a list of tuples (all triples comprising shortest path between two NETS nodes) and a list of out
    edges for each NETS node in an edge. The function returns a list of tuples containing all triples except those
    used to retrieve NETS nodes identifiers or labels.
    :param graph: a directed graphical representation where nodes are subjects/objects of the triple and edges are the
    labeled predicates
    :param metadata:list of tuples (all triples comprising shortest path between two NETS nodes)
    :return: a list of tuples sorted in ascending order
    '''
    # finds nodes on path between NETS nodes subclasses
    triple_path = []

    for edge in metadata:
        for part in graph.out_edges(edge[2]):
            preds = [
                x for x in nx.single_source_shortest_path(graph, (
                    part[1])).values() if len(x) != 1
            ]

            if preds:
                triple_path += [
                    tuple([x[0], graph[x[0]][x[1]]['predicate'], x[1]])
                    for x in preds
                ]

            else:
                triple_path.append(
                    tuple([
                        part[0], graph[part[0]][part[1]]['predicate'], part[1]
                    ]))

    return sorted(list(set(metadata + triple_path)))
Exemplo n.º 19
0
def optimize_header_second_pass(header: ir0.Header):
    template_defns_by_name = {
        elem.name: elem
        for elem in header.template_defns
    }

    template_dependency_graph = nx.DiGraph()
    for elem in itertools.chain(header.template_defns,
                                header.toplevel_content):
        if isinstance(elem, ir0.TemplateDefn):
            elem_name = elem.name
        else:
            # We'll use a dummy name for non-template toplevel elems.
            elem_name = ''

        template_dependency_graph.add_node(elem_name)

        if elem_name in header.public_names:
            # We also add an edge from the node '' to all public template defns, so that we can use '' as a source below.
            template_dependency_graph.add_edge('', elem_name)

        for identifier in elem.get_referenced_identifiers():
            if identifier in template_defns_by_name.keys():
                template_dependency_graph.add_edge(elem_name, identifier)

    used_templates = nx.single_source_shortest_path(template_dependency_graph,
                                                    source='').keys()

    return ir0.Header(template_defns=[
        template_defn for template_defn in header.template_defns
        if template_defn.name in used_templates
    ],
                      toplevel_content=header.toplevel_content,
                      public_names=header.public_names)
Exemplo n.º 20
0
def check_loads_connected(model, verbose=True):
    all_sources = []
    all_loads = set()
    load_source_map = {}
    result = True

    for i in model.models:
        if isinstance(i, PowerSource) and i.connecting_element is not None:
            all_sources.append(i)
        elif isinstance(i, PowerSource):
            print(
                'Warning - a PowerSource element has a None connecting element'
            )
        if isinstance(i, Load):
            all_loads.add(i)
            load_source_map[i.name] = []

    if len(all_sources) == 0:
        print('Model does not contain any power source')
        return False

    for source in all_sources:
        ditto_graph = Network()
        ditto_graph.build(model, source.connecting_element)
        ditto_graph.set_attributes(model)
        ditto_graph.remove_open_switches(
            model)  # This deletes the switches inside the networkx graph only
        source_name = source.connecting_element
        all_paths = nx.single_source_shortest_path(ditto_graph.graph,
                                                   source_name)

        for load in all_loads:
            min_dist = float('inf')
            load_connection = load.connecting_element
            if load_connection in all_paths:
                load_source_map[load.name].append(source_name)

    result = True
    sourceless_loads = []
    multi_source_loads = {}
    for load in load_source_map:
        if len(load_source_map[load]) == 0:
            result = False
            sourceless_loads.append(load)
        if len(load_source_map[load]) > 1:
            result = False
            multi_source_loads[load.name] = load_source_map[load]

    if verbose:
        if len(sourceless_loads) > 0:
            print('Loads missing sources:')
            for load in sourceless_loads:
                print(load)

        if len(multi_source_loads) > 0:
            print('Loads with multiple sources:')
            for load in multi_source_loads:
                print(load + ': ' + multi_source_loads[load])

    return result
Exemplo n.º 21
0
def get_forwarding_policy(topo, link_port_map):
    #rules = []

    pol = None
    base_ip = "10.0.%d.1"

    edge_nodes = [n for n in topo.nodes() if topo.node[n]["isHost"]]
    core_nodes = [n for n in topo.nodes() if topo.node[n]["isHost"] == False]

    # print "start"
    for u in edge_nodes: 
        dst_ip = base_ip % (u - 1)       
        
        paths = nx.single_source_shortest_path(topo, u)
        for v in edge_nodes:
            if u != v:
                pass
                # print u, v, paths[v]
        for s in core_nodes:
            next_hop = paths[s][-2]
            #m = match(switch = s, dstip = dst_ip).compile().rules[0].match 
            #act = fwd(link_port_map[s][next_hop]).compile().rules[0].actions
            if pol:
                pol += match(switch = s, dstip = dst_ip) >> fwd(link_port_map[s][next_hop])
            else:
                pol = match(switch = s, dstip = dst_ip) >> fwd(link_port_map[s][next_hop])

            #rules.append(Rule(m, act))
    return pol
Exemplo n.º 22
0
def chiral_order(atoms, chiral_atom, depth=6):
    # Create a list of ordered atoms to be passed back
    ordered = []
    # Do a quick check whether there are multiple hydrogens
    neighbors = atoms.neighbors(chiral_atom)
    hydrogens = [atom for atom in neighbors if atom.element == "H"]
    if len(hydrogens) < 2:
        tree = nx.bfs_tree(atoms, chiral_atom)
        # Generate the list of shortest paths in the molecule, neglecting the trivial path [chiral_atom]
        paths = sorted(nx.single_source_shortest_path(tree, chiral_atom,
                                                      depth).values(),
                       reverse=True)[:-1]
        while paths:
            # Pop the first element (highest priority path) from the list of paths and remove any duplicates.
            path = paths.pop(0)
            paths_no_dups = [
                unpruned for unpruned in paths if unpruned != path
            ]
            # If there are any duplicates, the paths list will be smaller and we can't resolve a highest priority yet.
            if len(paths_no_dups) != len(paths):
                paths = paths_no_dups
            # Otherwise, the path is higher priority than all the other paths, so its second atom is the neighbour with
            # highest priority.
            else:
                ranked_atom = path[1]
                ordered.append(ranked_atom)
                # Drop all the paths containing our ranked atom.
                paths = [
                    unpruned for unpruned in paths
                    if unpruned[1] is not ranked_atom
                ]
    return ordered
Exemplo n.º 23
0
def SampledDiameter(g):
  ns=g.nodes()
  pathLengths=[]
  for n in ns:
    paths=nx.single_source_shortest_path(g, n)
    pathLengths.extend([len(paths[n]) for n in paths.keys()])
  return max(pathLengths)
Exemplo n.º 24
0
    def calc_star_uptime(self, n, link_fail):
        '''Calc star uptime.

        NOTE: n is the number of nodes.'''
        # Correct for NetworkX, which adds one to n.
        g = nx.star_graph(n - 1)
        # Node 0 is the center of the star.
        edges = g.number_of_edges()
        nodes = g.number_of_nodes()
        paths = nx.single_source_shortest_path(g, g.nodes()[1])
        used = flatten(paths)
        sssp_edges = used.number_of_edges()
        if sssp_edges != g.number_of_edges():
            raise Exception("edge not on sssp for star graph")

        # consider those times when a link failed:
        # first, consider failure on outside of graph
        exp_uptime_outer = link_fail * edges * ((float(edges - 1) / edges) * float(edges) / nodes + \
                                                (float(1) / edges) * float(1) / nodes)
        exp_uptime_outer += (1.0 - (link_fail * sssp_edges)) * 1.0

        # consider only the hub as a controller:
        exp_uptime_inner = link_fail * edges * ((float(edges) / edges) * float(edges) / nodes)
        exp_uptime_inner += (1.0 - (link_fail * edges)) * 1.0

        # merge:
        exp_uptime_weighted = float(edges * exp_uptime_outer + 1 * exp_uptime_inner) / nodes
        return exp_uptime_weighted
Exemplo n.º 25
0
    def get_reachability(server, subscribers, topologies):
        """Returns the average probability of reaching any of the subscribers from the
        server in each of the given topologies.  Also includes the result for using
        all topologies at once.
        :returns list: containing reachability for each topology (in order),
        with the last entry representing using all topologies at the same time
        """

        subscribers = set(subscribers)
        subs_reachable_by_tree = []
        all_subscribers_reachable = set()
        for topology in topologies:
            nodes_reachable = set(
                nx.single_source_shortest_path(topology, server))
            # could also use has_path()
            subscribers_reachable = subscribers.intersection(nodes_reachable)
            subs_reachable_by_tree.append(
                len(subscribers_reachable) / float(len(subscribers)))
            all_subscribers_reachable.update(subscribers_reachable)

            log.debug(
                "%s heuristic reached %d subscribers in this topo" %
                (topology.graph['heuristic'], len(subscribers_reachable)))
        log.debug("ALL subscribers reached by these topos: %d" %
                  len(all_subscribers_reachable))
        # Lastly, include all of them reachable
        subs_reachable_by_tree.append(
            float(len(all_subscribers_reachable)) / len(subscribers))
        return subs_reachable_by_tree
Exemplo n.º 26
0
    def _longest_path_in_tree(self, G):
        """ Given a tree graph, compute the longest path and return it
        Given an undirected tree graph, compute the longest path and return it.
        The approach use two shortest path transversals (shortest path in a
        tree is the same as longest path). This could be improve but would
        require implement it:
        https://cs.stackexchange.com/questions/11263/longest-path-in-an-undirected-tree-with-only-one-traversal
        Args:
            G (nx.Graph): Graph which should be an undirected tree graph
        Returns:
            list(int): Returns a list of indexes of the nodes belonging to the
                longest path.
        """
        if not isinstance(G, nx.Graph):
            raise TypeError('G must be a nx.Graph instance')
        if not nx.is_tree(G):
            raise ValueError('Graph G must be a tree (graph without cycles)')

        # Compute the furthest node to the random node v
        v = list(G.nodes())[0]
        distance = nx.single_source_shortest_path_length(G, v)
        vp = max(distance.items(), key=lambda x: x[1])[0]
        # From this furthest point v' find again the longest path from it
        distance = nx.single_source_shortest_path(G, vp)
        longest_path = max(distance.values(), key=len)
        # Return the longest path

        return list(longest_path)
    def test_load_gml_for_debugging(self):
        g: nx.DiGraph = nx.read_gml('real-nodes.gml',
                                    destringizer=self.make_points)
        all_paths = nx.single_source_shortest_path(g, source=Point(0, 0))

        max_distance = 0
        i = 0
        node_count = len(g.nodes)
        greater_than_1000 = 0

        for k in all_paths.keys():
            i += 1

            if len(all_paths[k]) > max_distance:
                max_distance = len(all_paths[k])
            if len(all_paths[k]) >= 1001:
                greater_than_1000 += 1
            print('{}/{}'.format(i, node_count))
        print('max: {}'.format(max_distance - 1))
        print('more than 1000: {}'.format(greater_than_1000))

        for node in g.nodes:
            i += 1
            distance = nx.shortest_path_length(g,
                                               source=Point(0, 0),
                                               target=node)
            if distance > max_distance:
                max_distance = distance
        print(max_distance)
Exemplo n.º 28
0
def day7():
    """Counting matryoshka bags."""
    x = open("7.txt").read().strip().split("\n")

    g = nx.DiGraph()
    for i in x:
        i, outs = re.match("^(.*?) bags contain (.*).$", i).groups()
        outs = filter(None, re.findall("(?:[0-9]+ (.*?)|no other) bags?",
                                       outs))
        for j in outs:
            g.add_edge(j, i)
    res1 = len(nx.single_source_shortest_path(g, "shiny gold")) - 1

    g = nx.DiGraph()
    for i in x:
        i, outs = re.match("^(.*?) bags contain (.*).$", i).groups()
        outs = re.findall(r"\b([0-9]+|no) (.*?) bags?", outs)
        for k, j in outs:
            if (k, j) != ("no", "other"):
                g.add_edge(i, j, weight=int(k))

    @lru_cache(maxsize=len(g))
    def get_sum(i):
        return 1 + sum(get_sum(j) * k["weight"] for j, k in g[i].items())

    res2 = sum(get_sum(j) * k["weight"] for j, k in g["shiny gold"].items())

    return res1, res2
Exemplo n.º 29
0
 def reset(self, config):
     self.n_node = config.n_node
     self.k_ring = config.k_ring
     self.p_rewiring = config.p_rewiring
     self.num_actions = config.num_actions
     self.path_len_limit = config.path_len_limit
     self.ndigits = config.ndigits
     self.nway = config.nway
     self.num_step = 0
     self.reset_graph()
     while 1:
         self.target_state = np.random.randint(0, self.n_node)
         self.done = False
         self.path = nx.single_source_shortest_path(self.graph,
                                                    self.target_state)
         self.src_prob = np.zeros(len(self.path))
         for i in self.path:
             self.path[i] = list(reversed(self.path[i]))
             self.src_prob[i] = len(self.path[i]) > 2 and (
                 len(self.path[i]) - 1) <= self.path_len_limit
         if self.src_prob.sum() > 0:
             self.src_prob /= self.src_prob.sum()
             break
     self.src_state = np.random.choice(self.n_node, p=self.src_prob)
     self.current_state = self.src_state
     return self.current_state
def _make_join_graph(join_clauses, root):
    """Constructs nx.Graph and nx.DiGraph representations of the specified join.

    Constructs an undirected graph in which vertices are tables, and edges are
    joins.

    Also returns an arborescence (directed tree) in which edges originate from
    the join root to every joined table.
    """
    clauses = []
    for line in join_clauses:
        groups = match_join_clause_or_fail(line).groups()
        clauses.append(groups)
    g = nx.Graph()
    for t1, c1, t2, c2 in clauses:
        assert not g.has_edge(t1, t2)
        g.add_edge(t1, t2, join_keys={t1: c1, t2: c2})
    assert nx.is_tree(g), g.edges

    paths = nx.single_source_shortest_path(g, root)
    dg = nx.DiGraph()
    for path in paths.values():
        prev = None
        for t in path:
            if prev is not None:
                dg.add_edge(prev, t)
            prev = t
    assert set(g.nodes) == set(dg.nodes)
    return g, dg
Exemplo n.º 31
0
def shortest_path_subgraph(kg_graph,
                           prev_graph,
                           nodes,
                           path_len=2,
                           add_all_path=False):
    # Get non-neighbor nodes: nodes without edges between them
    new_graph = kg_graph.subgraph(nodes).copy()
    new_graph.remove_edges_from(nx.selfloop_edges(new_graph))
    world_graph = nx.compose(prev_graph, new_graph)
    if path_len < 2:
        return world_graph
    triplets = []
    sg_nodes = list(nx.isolates(world_graph))
    for source in sg_nodes:
        nb = nx.single_source_shortest_path(kg_graph, source, cutoff=path_len)
        if add_all_path:
            triplets.extend([[source, target, ' '.join(nb[target][1:-1])]
                             for target in nodes
                             if source != target and target in nb
                             and len(nb[target]) == path_len + 1])
        else:
            for target in nodes:  # Just add one link
                if source != target and target in nb and len(
                        nb[target]) == path_len + 1:
                    triplets.append(
                        [source, target, ' '.join(nb[target][1:-1])])
                    break
    add_triplets_to_graph(world_graph, triplets)

    return world_graph
Exemplo n.º 32
0
    def get_permission_details(self, name):
        """ Get a permission and what groups it's assigned to. """

        with self.lock:
            data = {
                "groups": {},
            }

            # Get all mapped versions of the permission. This is only direct relationships.
            direct_groups = set()
            for groupname, permissions in self.permission_metadata.iteritems():
                for permission in permissions:
                    if permission.permission == name:
                        data["groups"][groupname] = self.get_group_details(
                            groupname, show_permission=name)
                        direct_groups.add(groupname)

            # Now find all members of these groups going down the tree.
            checked_groups = set()
            for groupname in direct_groups:
                group = ("Group", groupname)
                paths = single_source_shortest_path(self._graph, group, None)
                for member, path in paths.iteritems():
                    if member == group:
                        continue
                    member_type, member_name = member
                    if member_type != 'Group':
                        continue
                    if member_name in checked_groups:
                        continue
                    checked_groups.add(member_name)
                    data["groups"][member_name] = self.get_group_details(
                        member_name, show_permission=name)

            return data
Exemplo n.º 33
0
    def to_rule_cpd(self):
        """
        Returns a RuleCPD object which represents the TreeCPD

        Examples
        --------
        >>> from pgmpy.factors import TreeCPD, Factor
        >>> tree = TreeCPD([('B', factors(['A'], [2], [0.8, 0.2]), '0'),
        ...                 ('B', 'C', '1'),
        ...                 ('C', factors(['A'], [2], [0.1, 0.9]), '0'),
        ...                 ('C', 'D', '1'),
        ...                 ('D', factors(['A'], [2], [0.9, 0.1]), '0'),
        ...                 ('D', factors(['A'], [2], [0.4, 0.6]), '1')])
        >>> tree.to_rule_cpd()

        """
        # TODO: This method assumes that factors class has a get_variable method. Check this after merging navin's PR.
        root = [node for node, in_degree in self.in_degree().items() if in_degree == 0][0]
        paths_root_to_factors = {target: path for target, path in nx.single_source_shortest_path(self, root).items() if
                                 isinstance(target, Factor)}
        for node in self.nodes_iter():
            if isinstance(node, Factor):
                rule_cpd = RuleCPD(node.scope()[0])

        for factor, path in paths_root_to_factors.items():
            rule_key = []
            for node_index in range(len(path) - 1):
                rule_key.append(path[node_index] + '_' + self.edge[path[node_index]][path[node_index + 1]]['label'])
            for value_index in range(len(factor.values)):
                rule_key.append(factor.get_variables()[0] + '_' + str(value_index))
                rule_cpd.add_rules({tuple(sorted(rule_key)): factor.values[value_index]})
        return rule_cpd
Exemplo n.º 34
0
def networkx_Call(M, source):

    print('Format conversion ... ')
    M = M.tocsr()
    if M is None:
        raise TypeError('Could not read the input graph')
    if M.shape[0] != M.shape[1]:
        raise TypeError('Shape is not square')

    # should be autosorted, but check just to make sure
    if not M.has_sorted_indices:
        print('sort_indices ... ')
        M.sort_indices()

    # Directed NetworkX graph
    Gnx = nx.DiGraph(M)

    print('NX Solving... ')
    t1 = time.time()

    path = nx.single_source_shortest_path(Gnx, source)

    t2 = time.time() - t1

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

    return path
Exemplo n.º 35
0
    def get_permission_details(self, name):
        """ Get a permission and what groups it's assigned to. """

        with self.lock:
            data = {
                "groups": {},
            }

            # Get all mapped versions of the permission. This is only direct relationships.
            direct_groups = set()
            for groupname, permissions in self.permission_metadata.iteritems():
                for permission in permissions:
                    if permission.permission == name:
                        data["groups"][groupname] = self.get_group_details(
                            groupname, show_permission=name)
                        direct_groups.add(groupname)

            # Now find all members of these groups going down the tree.
            checked_groups = set()
            for groupname in direct_groups:
                group = ("Group", groupname)
                paths = single_source_shortest_path(self._graph, group, None)
                for member, path in paths.iteritems():
                    if member == group:
                        continue
                    member_type, member_name = member
                    if member_type != 'Group':
                        continue
                    if member_name in checked_groups:
                        continue
                    checked_groups.add(member_name)
                    data["groups"][member_name] = self.get_group_details(
                        member_name, show_permission=name)

            return data
Exemplo n.º 36
0
def mst_of_g(g,terminals,verbose=False,weighted=True,cutoff=7,return_gL=False,bidir=False):
	STARTTIME=time.time()
	if verbose:
		logger.info("Starting MST construction")
		sys.stdout.flush()

	STARTTIME=time.time()
	gLedges=[]
	shortest_network=model.AnnotatedGraph()

	for i in range(len(terminals)):
		src=terminals[i]
		if src not in g:
			if verbose:
				logger.info("Node %s not in g"%(src))
			continue
		if weighted:
			costs,paths=nx.single_source_dijkstra(g, src, weight='weight',cutoff=cutoff)
		else:
			paths=nx.single_source_shortest_path(g,src,cutoff=cutoff)
			costs=dict([(k,len(v)) for k,v in paths.items()])

		if bidir:
			span=range(len(terminals))
		else:
			span=range(i+1,len(terminals))
		for j in span:
			if j==i:
				continue
			tgt=terminals[j]
			if tgt not in paths:
				if verbose:
					logger.info("no paths between %s and %s"%(src,tgt))
				continue
			shortest_network.add_path(paths[tgt])
			gLedges.append((src,tgt,{'weight':costs[tgt],'path':paths[tgt]}))
		if verbose:
			logger.info("Done %s. Still %d to go"%(src,len(terminals)-i))
			sys.stdout.flush()			
	if verbose:
		logger.info("Computed Metric closure in %f seconds"%(time.time() - STARTTIME))
		STARTTIME=time.time()
		sys.stdout.flush()			
	gL=nx.Graph()
	gL.add_edges_from(gLedges)
	# Min spanning Tree
	tL=nx.minimum_spanning_tree(gL)
	if verbose:
		logger.info("Computed Min spanning tree in %f seconds"%(time.time() - STARTTIME))
		STARTTIME=time.time()
		sys.stdout.flush()	

	mst=model.AnnotatedGraph()
	for e in tL.edges(data=True):
		mst.add_path(e[2]["path"])
	copy_attributes_from_g(mst,g)
	if return_gL:
		return mst,gL,shortest_network
	else:
		return mst
Exemplo n.º 37
0
def chiral_order(atoms, chiral_atom, depth=6):
  # Create a list of ordered atoms to be passed back
  ordered = []
  # Do a quick check whether there are multiple hydrogens
  neighbors = atoms.neighbors(chiral_atom)
  hydrogens = [atom for atom in neighbors if atom.element == "H"]
  if len(hydrogens) < 2:
    tree = nx.bfs_tree(atoms, chiral_atom)
    # Generate the list of shortest paths in the molecule, neglecting the trivial path [chiral_atom]
    paths = sorted(nx.single_source_shortest_path(tree, chiral_atom, depth).values(), reverse = True)[:-1]
    while paths:
      # Pop the first element (highest priority path) from the list of paths and remove any duplicates.
      path = paths.pop(0)
      paths_no_dups = [unpruned for unpruned in paths if unpruned != path]
      # If there are any duplicates, the paths list will be smaller and we can't resolve a highest priority yet.
      if len(paths_no_dups) != len(paths):
        paths = paths_no_dups
      # Otherwise, the path is higher priority than all the other paths, so its second atom is the neighbour with
      # highest priority.
      else:
        ranked_atom = path[1]
        ordered.append(ranked_atom)
        # Drop all the paths containing our ranked atom.
        paths = [unpruned for unpruned in paths if unpruned[1] is not ranked_atom]
  return ordered
Exemplo n.º 38
0
def betweenness(g):  #Function to find the betweenness in the graph
    count = 0
    res = []
    mat = []
    for i in xrange(len(
            g.nodes())):  #Making the matrix to store the betweenness value.
        mat.append([])
        for j in xrange(len(g.nodes())):
            mat[i].append(0.0)  #Initialising it with 0.
    for i in range(len(g.nodes())
                   ):  #Finding the shortest path between every pair of node.
        path = nx.single_source_shortest_path(
            g, i)  #Function to get the shortest path.
        for p in range(len(path)):
            for k in range(len(path[p]) - 1):
                mat[path[p][k]][path[p][k + 1]] += 1.0
        count = count + len(path)
    for i in range(len(g.nodes())):  #finding the betweenness.
        for j in range(len(g.nodes())):
            mat[i][j] = mat[i][j] / count
    """for i in range(len(g.nodes())):
		for j in range(i+1,len(g.nodes())):
			list1 = nx.shortest_path(g,source=i,target=j)
			for k in range(len(list1)):
							
			res.append(list1)"""
    return mat  #returning the matrix.
Exemplo n.º 39
0
    def make_graph_from(self, minima, cutoff=1):
        """rebuild the graph using only the passed minima and those in self.from_minima"""
        #        cutoff += 1
        self.from_minima.update(minima)
        minima = self.from_minima
        nodes = set()
        # make a graph from the minima in self.minima and nearest neighbors
        outer_layer = set()
        for m in minima:
            nodesdir = nx.single_source_shortest_path(self.full_graph,
                                                      m,
                                                      cutoff=cutoff)
            for n, path in nodesdir.items():
                d = len(path) - 1
                if d < cutoff:
                    # n is close to m, remove it from outer layer
                    outer_layer.discard(n)
                elif d == cutoff:
                    if n not in nodes:
                        # n is in the outer layer of m and not near any other nodes.
                        outer_layer.add(n)
            nodes.update(nodesdir)

        self.boundary_nodes = outer_layer
        self.graph = self.full_graph.subgraph(nodes)

        # remove nodes not in the graph from the dictionary positions
        difference = set(self.positions.keys())
        difference.difference_update(self.graph.nodes())
        for m in difference:
            self.positions.pop(m)

        print("boundary nodes", len(self.boundary_nodes),
              self.graph.number_of_nodes())
Exemplo n.º 40
0
    def make_graph_from(self, minima, cutoff=1):
        """rebuild the graph using only the passed minima and those in self.from_minima"""
#        cutoff += 1
        self.from_minima.update(minima)
        minima = self.from_minima
        nodes = set()
        # make a graph from the minima in self.minima and nearest neighbors
        outer_layer = set()
        for m in minima:
            nodesdir = nx.single_source_shortest_path(self.full_graph, m, cutoff=cutoff)
            for n, path in nodesdir.iteritems():
                d = len(path) - 1
                if d < cutoff:
                    # n is close to m, remove it from outer layer
                    outer_layer.discard(n)
                elif d == cutoff:
                    if n not in nodes:
                        # n is in the outer layer of m and not near any other nodes.
                        outer_layer.add(n)
            nodes.update(nodesdir)

        self.boundary_nodes = outer_layer
        self.graph = self.full_graph.subgraph(nodes)

        # remove nodes not in the graph from the dictionary positions
        difference = set(self.positions.viewkeys())
        difference.difference_update(self.graph.nodes())
        for m in difference:
            self.positions.pop(m)

        print "boundary nodes", len(self.boundary_nodes), self.graph.number_of_nodes()
Exemplo n.º 41
0
def simrank_radius(G, u, r):

    real_paths1 = nx.single_source_shortest_path(G, u, r)

    g = G.subgraph(list(real_paths1.keys()))

    sim = nx.simrank_similarity(g, u)

    sim_list = []
    node_list = []
    degree_list = []

    for n in sim:
        sim_list.append(sim[n])
        node_list.append(n)
        degree_list.append(G.degree(n))

    d = {'node_name': node_list, 'degree': degree_list, 'simrank': sim_list}
    df = pd.DataFrame(d)

    # print(df)

    df['simrank'] = normalize_rdd(df, 1, 1000, 'simrank')
    df['simrank'] = np.log10(df['simrank'])

    # print(df)

    return df
Exemplo n.º 42
0
def get_forwarding_policy(topo, link_port_map):
    #rules = []

    pol = None
    base_ip = "10.0.%d.1"

    edge_nodes = [n for n in topo.nodes() if topo.node[n]["isHost"]]
    core_nodes = [n for n in topo.nodes() if topo.node[n]["isHost"] == False]

    # print "start"
    for u in edge_nodes:
        dst_ip = base_ip % (u - 1)

        paths = nx.single_source_shortest_path(topo, u)
        for v in edge_nodes:
            if u != v:
                pass
                # print u, v, paths[v]
        for s in core_nodes:
            next_hop = paths[s][-2]
            #m = match(switch = s, dstip = dst_ip).compile().rules[0].match
            #act = fwd(link_port_map[s][next_hop]).compile().rules[0].actions
            if pol:
                pol += match(switch=s, dstip=dst_ip) >> fwd(
                    link_port_map[s][next_hop])
            else:
                pol = match(switch=s, dstip=dst_ip) >> fwd(
                    link_port_map[s][next_hop])

            #rules.append(Rule(m, act))
    return pol
def connect_shortest_v3(weigthed_graph,nodes,weighted=True,cutoff=None,verbose=False):
	STARTTIME=time.time()
	if verbose:
		print "Starting SHOV3 construction"
		sys.stdout.flush()

	STARTTIME=time.time()

	res=nx.Graph()
	for i in range(len(nodes)):
		src=nodes[i]
		if src not in weigthed_graph:
			continue
		if weighted:
			costs,spaths=nx.single_source_dijkstra(weigthed_graph, src, weight='weight',cutoff=cutoff)
		else:
			spaths=nx.single_source_shortest_path(weigthed_graph, src,cutoff=cutoff)
		for j in range(i+1, len(nodes)):
			t=nodes[j]
			if t not in spaths:
				continue
			if cutoff and (len(spaths[t])>cutoff):
				continue
			res.add_path(spaths[t])
		if verbose:
			print "Done",src,"to go:",len(nodes)-i
			sys.stdout.flush()			
	if verbose:
		print "Computed SHOV3,",time.time() - STARTTIME,"seconds"
		STARTTIME=time.time()
		sys.stdout.flush()	
	return res
Exemplo n.º 44
0
def get_bw_cost(g, source, hops):
    counter = 0
    paths = nx.single_source_shortest_path(g, source, hops)
    no_friends = len(paths) - 1
    for k, v in paths.iteritems():
      if (len(v) <= hops):
          counter += g.degree(k)
    return no_friends, counter - no_friends
Exemplo n.º 45
0
 def neighbors_genotyped_selective(self, node, depth=1):
     '''Return all neighbors of a node in the entire pedigree (if genotyped = False) or
     all genotyped neighbors (if genotyped = True) of depth <= depth. Selects only neighbors
     that have genotyped neighbors on the path between the neighbor and node.'''
     return list(reduce(set.union,
                        (v for (k, v) in 
                         nx.single_source_shortest_path(self._graph_undirected, node, cutoff=depth).iteritems() 
                         if self.is_genotyped(k)), set([])))
Exemplo n.º 46
0
 def neighbors(self, node, depth=1, genotyped=False, add_parents=False):
     '''Return all neighbors of a node in the entire pedigree (if genotyped = False) or
     all genotyped neighbors (if genotyped = True) of depth <= depth.
     If add_parents=False, adds all parents of all neighbors to complete the pedigree.'''
     nbhrs = [x for x in nx.single_source_shortest_path(self._graph_undirected, node, cutoff=depth).iterkeys() 
              if (self.is_genotyped(x) if genotyped else True)]
     if add_parents: nbhrs = list(set(nbhrs + [parent for x in nbhrs for parent in self.parents(x).itervalues()])) 
     return nbhrs
Exemplo n.º 47
0
def edge_parent_finder(abstract, graph):
    '''
    moved this here since i think that it is forgi specific
    '''
    # find out to which abstract node the edges belong
    # finding out where the edge-nodes belong, because the contractor cant possibly do this
    #draw.graphlearn_draw([abstract,graph],size=10, contract=False,vertex_label='id')

    getabstr = {contra: node for node, d in abstract.nodes(data=True) for contra in d.get('contracted', [])}
    # print getabstr
    for n, d in graph.nodes(data=True):
        if 'edge' in d:
            # if we have found an edge node...

            # lets see whos left and right of it:
            # if len is 2 then we hit a basepair, in that case we already have both neighbors
            zomg = graph.neighbors(n)
            if len(zomg)==1:
                zomg+=graph.predecessors(n)

            n1, n2 = zomg


            # case1: ok those belong to the same gang so we most likely also belong there.
            if getabstr[n1] == getabstr[n2]:
                abstract.node[getabstr[n1]]['contracted'].add(n)

            # case2: neighbors belong to different gangs...
            else:
                abstract_intersect = set(abstract.neighbors(getabstr[n1])) & set(abstract.neighbors(getabstr[n2]))

                # case 3: abstract intersect in radius 1 failed, so lets try radius 2
                if not abstract_intersect:
                    abstract_intersect = set(nx.single_source_shortest_path(abstract, getabstr[n1], 2)) & set(
                        nx.single_source_shortest_path(abstract, getabstr[n2], 2))
                    if len(abstract_intersect) > 1:
                        print "weired abs intersect..."

                for ai_node in abstract_intersect:
                    if 'contracted' in abstract.node[ai_node]:
                        abstract.node[ai_node]['contracted'].add(n)
                    else:
                        abstract.node[ai_node]['contracted'] = set([n])

    return abstract
Exemplo n.º 48
0
 def _solve_graph(self):
     sols = []
     j = self.state.to_json()
     d = hashlib.md5(j).hexdigest()
     for a,p in nx.single_source_shortest_path(self.gr,d).items():
         if a in self.gr.graph['finals']:
             sols.append(p)
     sols = sorted(sols, key=lambda sol: len(sol))
     return sols
Exemplo n.º 49
0
def get_shortest_paths(topo):
    edge_nodes = [n for n in topo.nodes() if topo.node[n]["isHost"]]
    shortest_paths = []
    for u in edge_nodes:
        paths = nx.single_source_shortest_path(topo, u)
        for v in edge_nodes:
            if u != v:
                shortest_paths.append(paths[v])
    return shortest_paths
Exemplo n.º 50
0
def is_connected(G):
    """Return True if G is connected.
    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."""
    return len(single_source_shortest_path(G, G.nodes_iter().next()))==len(G)
Exemplo n.º 51
0
def sum_edges(g, hops):

    for source in g.nodes_iter():
        count = 0
        paths = nx.single_source_shortest_path(g, source, hops-1)

        for node in paths.iterkeys():
            count += g.degree(node)

        print >> out_file, count
 def wiener(self , last_previous_level_vertex_number , previous_level_wiener):
     sum=0
     n=self.level_number
     for i in range(last_previous_level_vertex_number , n):
         path=nx.single_source_shortest_path(self.g2,i)
         for j in range(0,last_previous_level_vertex_number):
             sum+= (len(path[j]) -1)*(len(path[j]) -1)*(len(path[j]) - 1)
         for j in range(i+1,n):
             sum += (len(path[j]) - 1)*(len(path[j]) - 1)*(len(path[j]) - 1)
     return sum+previous_level_wiener
Exemplo n.º 53
0
def random_shortest_path(graphs):
    mu, sigma = cutoff_params
    cutoff = int(random.gauss(mu, sigma))
    graph = graphs[random.randint(0,len(graphs)-1)]
    source = randnode(graph)
    paths = nx.single_source_shortest_path(graph, source, cutoff)
    targets = paths.keys()
    target = targets[random.randint(0,len(targets)-1)]
    path = nx.shortest_path(graph, source, target, 'weight')
    return tuple(path)
Exemplo n.º 54
0
 def get_riaa_path(self, label_id):
     shortest = []
     paths = nx.single_source_shortest_path(self.db, label_id)
     for path in paths.values():
         if self.is_riaa(path[-1]):
             if len(shortest) == 0:
                 shortest = path
             elif len(path) < len(shortest):
                 shortest = path
     return shortest
def generate_SP(G, N = 100, seeds_num = 3):
    Gcc = sorted(nx.connected_component_subgraphs(G.to_undirected()), key = len, reverse = True)
    nodes = set()
    i = 0
    seeds = set()
    leaf = []
    infection_order = []
    infection_track = {}

    for i in xrange(len(Gcc)):
        nodes.update(set(Gcc[i].nodes()))
        seeds.add(Gcc[i].nodes()[np.random.choice(range(Gcc[i].number_of_nodes()))])
        if len(nodes) >= N:
            break
    G = G.subgraph(nodes).to_undirected()
    #if len(seeds) < seeds_num:
    rest_seeds_num = max(0, seeds_num - len(seeds))

    nodes = list(nodes.difference(seeds))


    chosen = [nodes[j] for j in np.random.choice(range(len(nodes)), size = N)]
    seeds.update(chosen[:rest_seeds_num])
    to_infect = chosen[rest_seeds_num:]

    #seeds_num = len(seeds)

    SSSP = {}
    for s in seeds:
        SPs = nx.single_source_shortest_path(G, s)
        SSSP[s] = SPs

    inf_paths = []
    maxpath = -sys.maxint

    for i in to_infect:
        src = -1
        for s in SSSP.keys():
            if (src == -1 and i in SSSP[s]) or (i in SSSP[s] and len(SSSP[s][i]) < len(SSSP[src][i])):
                src = s

        infection_track[src] = infection_track.get(src, []) + [i]
        if len(SSSP[src][i]) == 1:
            inf_paths.append([(src, src)])
        else:
            inf_paths.append([tuple(SSSP[src][i][ind:ind+2]) for ind in range(len(SSSP[src][i])-1)])
        if len(SSSP[src][i]) > maxpath:
            maxpath = len(SSSP[src][i])-1

    for ind in xrange(maxpath):
        for path in inf_paths:
            if ind < len(path) and path[ind] not in infection_order:
                infection_order.append(path[ind])

    return infection_order, infection_track, seeds, to_infect
Exemplo n.º 56
0
 def test_single_source_shortest_path(self):
     p = nx.shortest_path(self.cycle, 0)
     assert_equal(p[3], [0, 1, 2, 3])
     assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
     p = nx.shortest_path(self.grid, 1)
     validate_grid_path(4, 4, 1, 12, p[12])
     # now with weights
     p = nx.shortest_path(self.cycle, 0, weight='weight')
     assert_equal(p[3], [0, 1, 2, 3])
     assert_equal(p, nx.single_source_dijkstra_path(self.cycle, 0))
     p = nx.shortest_path(self.grid, 1, weight='weight')
     validate_grid_path(4, 4, 1, 12, p[12])
     # weights and method specified
     p = nx.shortest_path(self.cycle, 0, method='dijkstra', weight='weight')
     assert_equal(p[3], [0, 1, 2, 3])
     assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
     p = nx.shortest_path(self.cycle, 0, method='bellman-ford',
                          weight='weight')
     assert_equal(p[3], [0, 1, 2, 3])
     assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
Exemplo n.º 57
0
def sum_edges(g, source, hops):

    """ Sum edges that are hops away in undirected graph """

    edges = 0

    paths = nx.single_source_shortest_path(g, source, hops)
    for node in paths.iterkeys():
        edges += len(g.neighbors(node))

    return edges
Exemplo n.º 58
0
 def test_single_source_shortest_path(self):
     p=nx.shortest_path(self.cycle,0)
     assert_equal(p[3],[0,1,2,3])
     assert_equal(p,nx.single_source_shortest_path(self.cycle,0))
     p=nx.shortest_path(self.grid,1)
     assert_equal(p[12],[1, 2, 3, 4, 8, 12])
     # now with weights
     p=nx.shortest_path(self.cycle,0,weighted=True)
     assert_equal(p[3],[0,1,2,3])
     assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0))
     p=nx.shortest_path(self.grid,1,weighted=True)
     assert_equal(p[12],[1, 2, 3, 4, 8, 12])
Exemplo n.º 59
0
 def test_single_source_shortest_path(self):
     p=nx.shortest_path(self.cycle,0)
     assert_equal(p[3],[0,1,2,3])
     assert_equal(p,nx.single_source_shortest_path(self.cycle,0))
     p=nx.shortest_path(self.grid,1)
     validate_grid_path(4, 4, 1, 12, p[12])
     # now with weights
     p=nx.shortest_path(self.cycle,0,weight='weight')
     assert_equal(p[3],[0,1,2,3])
     assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0))
     p=nx.shortest_path(self.grid,1,weight='weight')
     validate_grid_path(4, 4, 1, 12, p[12])