当前位置: 首页>>代码示例>>Python>>正文


Python networkx.subgraph函数代码示例

本文整理汇总了Python中networkx.subgraph函数的典型用法代码示例。如果您正苦于以下问题:Python subgraph函数的具体用法?Python subgraph怎么用?Python subgraph使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了subgraph函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: calc_euler_tour

def calc_euler_tour(g, start, end):
    '''Calculates an Euler tour over the graph g from vertex start to vertex end.
    Assumes start and end are odd-degree vertices and that there are no other odd-degree
    vertices.'''
    even_g = nx.subgraph(g, g.nodes())
    if end in even_g.neighbors(start):
        # If start and end are neighbors, remove the edge
        even_g.remove_edge(start, end)
        comps = list(nx.connected_components(even_g))
        # If the graph did not split, just find the euler circuit
        if len(comps) == 1:
            trail = list(nx.eulerian_circuit(even_g, start))
            trail.append((start, end))
        elif len(comps) == 2:
            subg1 = nx.subgraph(even_g, comps[0])
            subg2 = nx.subgraph(even_g, comps[1])
            start_subg, end_subg = (subg1, subg2) if start in subg1.nodes() else (subg2, subg1)
            trail = list(nx.eulerian_circuit(start_subg, start)) + [(start, end)] + list(nx.eulerian_circuit(end_subg, end))
        else:
            raise Exception('Unknown edge case with connected components of size {0}:\n{1}'.format(len(comps), comps))
    else:
        # If they are not neighbors, we add an imaginary edge and calculate the euler circuit
        even_g.add_edge(start, end)
        circ = list(nx.eulerian_circuit(even_g, start))
        try:
            trail_start = circ.index((start, end))
        except:
            trail_start = circ.index((end, start))
        trail = circ[trail_start+1:] + circ[:trail_start]
    return trail
开发者ID:casotto,项目名称:gfl,代码行数:30,代码来源:trails.py

示例2: km_random

def km_random(g,k=5,m=3,start=None):
    """ k nodes of breath first sequence; m add and del number."""
    if start==None:
        start=g.nodes().pop()
    bfList=list(nx.bfs_edges(g,start))
    bfList.reverse()
    bfList.append((start,start))
    tempk=[]
    try:
        while bfList:
            for each in range(k):
                tempk.append(bfList.pop()[1])
            
            tg=nx.subgraph(g,tempk)
            e=del_edge(tg,m)
            g.remove_edges_from(e)

            tg=nx.subgraph(g,tempk)
            e=add_edge(tg,m)
            g.add_edges_from(e)

            tempk=[]

    except IndexError:
        print "pop finishing"
开发者ID:liupenggl,项目名称:dpr,代码行数:25,代码来源:prob.py

示例3: match_story_by_sen_edge

def match_story_by_sen_edge(Gs, stories, target, tau):
    existing = copy.deepcopy(stories['keywords_set'])
    match_target = copy.deepcopy(target['keywords_set'])
    node_cos = match_story(existing, match_target, 0.3)

    subgs1 = []
    for sto in match_target:
        subgs1.append(nx.subgraph(Gs, sto))
    subgs0 = []
    for sto in existing:
        subgs0.append(nx.subgraph(Gs, sto))

    matched = []
    for i in range(len(subgs1)):
        matchingGraph = subgs1[i]
        dis = []
        for cand in subgs0:
            val = compute_distance(matchingGraph, cand)
            dis.append(val)
        total = np.multiply(dis, node_cos[i])
        match_score = np.max(total)

        if match_score < tau:
            stories['keywords_set'].append(target['keywords_set'][i])
            stories['doc_set'].append(target['doc_set'][i])
            continue

        print match_score
        match_ind = np.argmax(total)
        print  match_ind, stories['doc_set'][match_ind], target['doc_set'][i]
# 		match_text = existing[match_ind]
        stories['keywords_set'][match_ind].extend(match_target[i])
        u = stories['doc_set'][match_ind].union(target['doc_set'][i])
        stories['doc_set'][match_ind] = u
开发者ID:ShuaiYAN,项目名称:news_event_evolution,代码行数:34,代码来源:matchstory.py

示例4: test_subgraph_of_subgraph

 def test_subgraph_of_subgraph(self):
     SGv = nx.subgraph(self.G, range(3, 7))
     SDGv = nx.subgraph(self.DG, range(3, 7))
     SMGv = nx.subgraph(self.MG, range(3, 7))
     SMDGv = nx.subgraph(self.MDG, range(3, 7))
     for G in self.graphs + [SGv, SDGv, SMGv, SMDGv]:
         SG = nx.induced_subgraph(G, [4, 5, 6])
         assert_equal(list(SG), [4, 5, 6])
         SSG = SG.subgraph([6, 7])
         assert_equal(list(SSG), [6])
         # subgraph-subgraph chain is short-cut in base class method
         assert_is(SSG._graph, G)
开发者ID:ProgVal,项目名称:networkx,代码行数:12,代码来源:test_graphviews.py

示例5: test_subgraph

 def test_subgraph(self):
     assert_equal(self.G.subgraph([0, 1, 2, 4]).adj,
                  nx.subgraph(self.G, [0, 1, 2, 4]).adj)
     assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj,
                  nx.subgraph(self.DG, [0, 1, 2, 4]).adj)
     assert_equal(self.G.subgraph([0, 1, 2, 4]).adj,
                  nx.induced_subgraph(self.G, [0, 1, 2, 4]).adj)
     assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj,
                  nx.induced_subgraph(self.DG, [0, 1, 2, 4]).adj)
     # subgraph-subgraph chain is allowed in function interface
     H = nx.induced_subgraph(self.G.subgraph([0, 1, 2, 4]), [0, 1, 4])
     assert_is_not(H._graph, self.G)
     assert_equal(H.adj, self.G.subgraph([0, 1, 4]).adj)
开发者ID:iaciac,项目名称:networkx,代码行数:13,代码来源:test_function.py

示例6: filterGraphByRecipeID

def filterGraphByRecipeID(G, Grecipes, Gingredients, recipeNodes):
	recipe_to_remove = [ n for n in Grecipes.nodes() if n not in recipeNodes]	
	searchGrecipes = nx.subgraph(Grecipes, recipeNodes)
	searchGrecipes.remove_nodes_from(recipe_to_remove)

	ingrNodes = list(set([b for n in searchGrecipes.nodes() for b in G.neighbors(n)]))
	ingr_to_remove = [ n for n in Gingredients.nodes() if n not in ingrNodes]
	searchGingredients = Gingredients

	searchG = nx.subgraph(G, recipeNodes + ingrNodes)
	searchG.remove_nodes_from(recipe_to_remove)
	searchG.remove_nodes_from(ingr_to_remove)

	return (searchG, searchGrecipes, searchGingredients)
开发者ID:dnanerd,项目名称:Insight,代码行数:14,代码来源:datalivesearch.py

示例7: plot_induced_subgraphs

 def plot_induced_subgraphs(self):
     plt.figure(1)
     partition = self.find_partition()[1]
     communities = [partition[v] for v in partition]
     newGraph=self.G
     for community in communities:
         nx.subgraph(newGraph, [key for key in partition if partition[key]==community])
     node_color=[float(partition[v]) for v in partition]
     labels =  {}
     for node in newGraph.nodes():
         labels[node]= newGraph.node[node].get('name', '')
     nx.draw_spring(newGraph,node_color=node_color, labels=labels)
     plt.show()
     plt.savefig("C:\\Users\\Heschoon\\Dropbox\\ULB\\Current trends of artificial intelligence\\Trends_project\\graphs\\graph_induced.pdf")
开发者ID:HelainSchoonjans,项目名称:FacebookExperiments,代码行数:14,代码来源:socialgraph.py

示例8: core_substitution

def core_substitution(graph, orig_cip_graph, new_cip_graph):
    """
    graph is the whole graph..
    subgraph is the interfaceregrion in that we will transplant
    new_cip_graph which is the interface and the new core
    """
    assert( set(orig_cip_graph.nodes()) - set(graph.nodes()) == set([]) ), 'orig_cip_graph not in graph'

    # select only the interfaces of the cips
    new_graph_interface_nodes = [n for n, d in new_cip_graph.nodes(data=True) if 'core' not in d]
    new_cip_interface_graph = nx.subgraph(new_cip_graph, new_graph_interface_nodes)

    original_graph_interface_nodes = [n for n, d in orig_cip_graph.nodes(data=True) if 'core' not in d]
    original_interface_graph = nx.subgraph(orig_cip_graph, original_graph_interface_nodes)
    # get isomorphism between interfaces, if none is found we return an empty graph

    iso = get_good_isomorphism(graph,
                               orig_cip_graph,
                               new_cip_graph,
                               original_interface_graph,
                               new_cip_interface_graph)

    if len(iso) != len(original_interface_graph):
        # print iso
        # draw.display(orig_cip_graph)
        # draw.display(new_cip_graph)
        #draw.graphlearn([orig_cip_graph, new_cip_graph],size=10)
        logger.log(5,"grammar hash collision, discovered in 'core_substution' ")
        return nx.Graph()

    # ok we got an isomorphism so lets do the merging
    graph = nx.union(graph, new_cip_graph, rename=('', '-'))

    # removing old core
    # original_graph_core_nodes = [n for n, d in orig_cip_graph.nodes(data=True) if 'core' in d]
    original_graph_core_nodes = [n for n, d in orig_cip_graph.nodes(data=True) if 'core' in d]

    for n in original_graph_core_nodes:
        graph.remove_node(str(n))

    # merge interfaces
    for k, v in iso.iteritems():
        graph.node[str(k)][
            'interface'] = True  # i am marking the interface only for the backflow probability calculation in graphlearn, this is probably deleteable because we also do this in merge, also this line is superlong Ooo
        merge(graph, str(k), '-' + str(v))
    # unionizing killed my labels so we need to relabel


    return nx.convert_node_labels_to_integers(graph)
开发者ID:fabriziocosta,项目名称:GraphLearn,代码行数:49,代码来源:compose.py

示例9: remove_bridges

    def remove_bridges(self, in_file_, start_id, delim_):
        reader = csv.reader(open(in_file_), delimiter=delim_)
        for line in reader:
                self.G.remove_edge(int(line[0]) - start_id,int(line[1]) - start_id)
	
	print "no of components after removing bridges: %d" % nx.number_connected_components(self.G)
	comps = nx.connected_components(self.G)
	for comp in comps:
        	print len(comp)

        bfs = self.BreadthFirstLevels(1,100)
        nbunch = [1]
        for n in bfs:
            #print(n)
            val_ = n.values()
            for set_ in val_:
                nbunch += list(set_)
        #print nbunch
        print "start creating the induced graph!"
        induced_g = nx.subgraph(self.G, nbunch)
        self.G.clear()
#        start_ = 0
#        for n_ in induced_g:
#            self.maps_[n_] = start_
#            start_ += 1
#        for n_1 in induced_g:
#            for n_2 in induced_g:
#                if n_1 in induced_g.neighbors(n_2):
#                    self.G.add_edge(maps_[n_1],maps_[n_2]) 
        self.n = nx.number_of_nodes(induced_g)
        self.G = induced_g
        print "no of node: %d and no of edges: %d in induce graph!" % (self.G.number_of_nodes(), self.G.number_of_edges())
开发者ID:azizur77,项目名称:wcg,代码行数:32,代码来源:WCG.py

示例10: find_football_communities

def find_football_communities():
    """ Finds the communities produced for the football network, uses compare
    methods to graph
    """
    fgraph = CD.football_graph()
    known = CD.football_known_c()
    temp7 = known[7]
    temp8 = known[8]
    temp9 = known[9]
    known[7] = temp8
    known[8] = temp9
    known[9] = temp7

    center_g = nx.Graph()
    center_g.add_nodes_from(range(12))
    centers = nx.circular_layout(center_g, scale = 10)
            
    pos = {}
    subgraphs = [nx.subgraph(fgraph, c) for c in known]
    count = -1
    for g in subgraphs:
        count += 1
        (off_x, off_y) = centers[count]
        pos_local = nx.circular_layout(g, scale=2.)
        for n, place in pos_local.iteritems():
            pos[n] = place + np.array([off_x, off_y])
    
    compare_methods(fgraph,
                    'football_',
                    param=[1., 1., 5./115., 4, 0, .7, 20],
                    known=known,
                    pos=pos,
                    color_map={76:1, 11:2, 7:3, 102:4, 104:5, 47:6, 98:7,
                               96:8, 23:9, 94:10, 27:0},
                    data_path="FootballGames/football_metis")
开发者ID:redheadjune,项目名称:CommunityDetection,代码行数:35,代码来源:community_plots.py

示例11: vis_coauthor_communities

def vis_coauthor_communities(graph, source, i, prefix, options, radius, overlap):
    """ Finds the communities produced by different methods for the astro
    citation network
    """    
    interest = CD.get_ball(graph, options[source][i], radius)
    print "Displaying and computing for a subset of ", len(interest), " nodes."
    sgraph = nx.subgraph(graph, interest)
    
    cleaned = {}
    for key in options.keys():
        """ for generating sub community structure
        """
        if key == source:
            # split the overarching with the substructure
            cleaned[source] = [options[source][i]]
            options['Parallel Subcommunities'] = options[source][:i]
            options['Parallel Subcommunities'].extend(options[source][i+1:])
            key = 'Parallel Subcommunities'
        
        filtered = [filter(lambda n: n in interest, c) for c in options[key]]
        filtered = filter(lambda c: len(c) > 0, filtered)
        cleaned[key] = filtered
        cleaned[key] = CD.clean_of_duplicate_c(cleaned[key], overlap=overlap)

    compare_methods(sgraph, prefix, options=cleaned)
开发者ID:redheadjune,项目名称:CommunityDetection,代码行数:25,代码来源:community_plots.py

示例12: compute_global_utility

def compute_global_utility(graph):
    """
    Return an index that quantifies how big the size of adopter
    clusters is in the entire population of consumers. We call this
    index 'Global utility' in our article.

    This index computes the cluster-size-weighted average of adopter
    clusters divided by the total number of consumers

    So it goes from 0 to 1 and it's always increasing.
    """
    N = len(graph.nodes())
    adopters = get_adopters(graph)
    
    clusters = nx.subgraph(graph, adopters)
    cluster_sizes = [len(c) for c in nx.connected_components(clusters) if len(c) > 1]
    if cluster_sizes:
        # The weight of each cluster depends on its size
        weights = np.array(cluster_sizes) / N
        # Compute the weighted average
        weigthed_average = np.average(cluster_sizes, weights=weights)
        # Since the index needs to go between 0 and 1, we need to divide between N
        # again
        utility = weigthed_average / N
        return utility
    else:
        return 0
开发者ID:ccordoba12,项目名称:models,代码行数:27,代码来源:utilities.py

示例13: dyad_census

def dyad_census(pg, debug=0, debuglog=0):
    """
    dyad_census() calculates the number of null, asymmetric, and
    mutual edges between all pairs of nodes in a directed graph.
    """
    if not networkx.is_directed_acyclic_graph(pg):
        logging.error('pyp_network.dyad_census() requires a directed graph as input!')
        return 0
    else:
        census = {}
        census['null'] = 0
        census['asymmetric'] = 0
        census['mutual'] = 0
        tg = networkx.subgraph(pg, pg.nodes())
        for u in pg.nodes_iter():
            tg.delete_node(u)
            for v in tg.nodes_iter():
                if not pg.has_neighbor(u,v):
                    census['null'] = census['null'] + 1
                elif u in pg.predecessors(v) and v in pg.successors(u):
                    census['mutual'] = census['mutual'] + 1
                    if debug:
                        print 'Nodes %s and %s link to one another!' % ( u, v )
                    if debuglog:
                        logging.error('Nodes %s and %s link to one another!',u, v)
                elif u in pg.predecessors(v) and v not in pg.successors(u):
                    census['asymmetric'] = census['asymmetric'] + 1
                elif u not in pg.predecessors(v) and v in pg.successors(u):
                    census['asymmetric'] = census['asymmetric'] + 1
                else:
                    pass
        del(tg)
        return census
开发者ID:sam-m888,项目名称:pypedal,代码行数:33,代码来源:pyp_network.py

示例14: mean_geodesic

def mean_geodesic(pg, debug=0):
    """
    mean_geodesic() calculates the mean geodesic (shortest) distance
    between two vertices in a network.
    """
    length_sum = 0
    if networkx.is_directed_acyclic_graph(pg):
        n_pairs_with_paths = 0
    else:
        n_pairs_with_paths = ( pg.order() * ( pg.order() + 1 ) ) / 2
    tg = networkx.subgraph(pg, pg.nodes())
    for u in pg.nodes_iter():
        tg.delete_node(u)
        for v in tg.nodes_iter():
            try:
                length = networkx.shortest_path_length(pg,u,v)
                if length > 0:
                    length_sum = length_sum + length
                    if networkx.is_directed_acyclic_graph(pg):
                        n_pairs_with_paths = n_pairs_with_paths + 1
            except networkx.exception.NetworkXError:
                pass
    try:
        geodesic = float(length_sum) / float(n_pairs_with_paths)
    except:
        geodesic = -999.
    if debug:
        print 'length_sum:\t', length_sum
        print 'n_pairs_with_paths:\t', n_pairs_with_paths
    return geodesic
开发者ID:sam-m888,项目名称:pypedal,代码行数:30,代码来源:pyp_network.py

示例15: bound_branch

def bound_branch(G, k ,q_nodes, is_use_cores=False, select_method='rand'):
    '''
    wrapper of branch and bound method
    '''
    ts = time.time()
    global optimal
    optimal = set()
    k_neighbors = k_hop_nbrs_n(G, k, q_nodes)
    sub = set(q_nodes)
    sub.update(k_neighbors)
    g = nx.subgraph(G, sub)

    if is_use_cores:
        cores = nx.core_number(g)
    else:
        cores = None

    # print('subgraph ', g.nodes())
    print('minimum degree of subgraph', minimum_degree(g))
    print('k neighbors', len(k_neighbors))
    BB(g, k, q_nodes, set(), cores, select_method)
    print('the solution is', optimal)

    te = time.time()

    texe = round(te-ts, 2) # the execution time

    return texe
开发者ID:KeithYue,项目名称:KplexSearch,代码行数:28,代码来源:bound_and_branch.py


注:本文中的networkx.subgraph函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。