Ejemplo n.º 1
0
def compute_mcm( g, estimate = None, pweight = 'weight' ):
    maxmean, arg_cycle = None, None
    for scc in nx.strongly_connected_component_subgraphs( g ):
        root = next( scc.nodes_iter() )
        scc_mcm, cycle = compute_mcm_component( scc, root, estimate, pweight )
        if scc_mcm is None:
            continue

        if maxmean is None or scc_mcm > maxmean:
            maxmean = scc_mcm
            arg_cycle = cycle

    forest = Forest()
    for scc in nx.strongly_connected_component_subgraphs( g, False ):
        if scc.number_of_edges() == 0:
            continue

        for ( v, w, scc_data ) in scc.edges_iter( data = True ):
            data = g.get_edge_data( v, w )

            # negate weight so that we can construct a longest paths tree for the current solution
            scc_data['w'] = maxmean - data.get( 'weight', 0 )

        root = w
        lpp_tree, _ = bfct.find_shortest_paths( scc, root, arg = 'w' )
        forest.add_forest( lpp_tree )

    return maxmean, arg_cycle, forest
Ejemplo n.º 2
0
def remove_all_cycles(g):
	n_scc = [x for x in nx.strongly_connected_component_subgraphs(g) if len(x)>1]
	if len(n_scc)>0:
		print "Found",len(n_scc),"cycles, largest one of size",len(n_scc[0])
	else:
		print "No cycles"
		return g
	# Color them 
	for ccc_idx  in range(len(n_scc)): 
		for node in n_scc[ccc_idx].nodes():
			g.node[node]['in_scc']=ccc_idx

	#sys.exit(0)
	g_p=g
	for i in range(1,1000): #Up to a thousand iteration

		g_n= remove_cycle(g_p)
		if g_n.number_of_edges() == g_p.number_of_edges():
			break
	#	assert("10_4" in g_n)
		g_p=g_n
		n_scc_i = [x for x in nx.strongly_connected_component_subgraphs(g_p) if len(x)>1]
		print "Still",len(n_scc_i),"cycles"

	g_trans=g_p
	# Assert graph is acyclic 
	assert(max([len(x) for x in nx.strongly_connected_components(g_trans)])==1)

	return g_trans
Ejemplo n.º 3
0
def max_cycle_ratio(g, estimate=None):
    maxratio, arg_cycle = None, None
    for scc in nx.strongly_connected_component_subgraphs(g):
        root = next(iter(scc.nodes()))
        scc_mcr, cycle = compute_mcr_component(scc, root, estimate)
        if scc_mcr is None:
            continue

        if maxratio is None or scc_mcr > maxratio:
            maxratio = scc_mcr
            arg_cycle = cycle

    forest = Forest()
    for scc in nx.strongly_connected_component_subgraphs(g, False):
        if scc.number_of_edges() == 0:
            continue

        for (v, w, scc_data) in scc.edges(data=True):
            data = g.get_edge_data(v, w)

            # negate weight so that we can construct a longest paths tree for the current solution
            scc_data['w'] = data.get('weight',
                                     0) - data.get('tokens', 0) * maxratio

        root = w
        parents, _ = longest_distances(scc, root, 'w')
        for child in parents:
            in_edge = parents.get(child)
            if in_edge is not None:
                forest.add_edge(*in_edge)

    return maxratio, arg_cycle, forest
Ejemplo n.º 4
0
    def get_radius(self):
        logging.info("Radius calculations")
        if nx.is_strongly_connected(self.graph) is False:
            logging.info("Graph is not strongly connected")
            sub_graphs = nx.strongly_connected_component_subgraphs(self.graph)

            logging.info("------------------Subgraphs-----------------")

            count = 0
            for sg in sub_graphs:
                count += 1

            logging.info("Graph has :" + str(count) +
                         " strongly connected subgraphs")

            sub_graphs = nx.strongly_connected_component_subgraphs(self.graph)

            for sg in sub_graphs:
                logging.info("Number of nodes in subgraph is: " +
                             str(len(sg.nodes())))
                radius = nx.radius(sg)
                self.sub_graph_radius.append(radius)
        else:
            self.sub_graph_radius.append(nx.radius(self.graph))

        self.graph_radius = max(self.sub_graph_radius)
        print "Max radius: {}".format(self.graph_radius)
Ejemplo n.º 5
0
def compute_mcm(g, estimate=None, pweight='weight'):
    maxmean, arg_cycle = None, None
    for scc in nx.strongly_connected_component_subgraphs(g):
        root = next(scc.nodes_iter())
        scc_mcm, cycle = compute_mcm_component(scc, root, estimate, pweight)
        if scc_mcm is None:
            continue

        if maxmean is None or scc_mcm > maxmean:
            maxmean = scc_mcm
            arg_cycle = cycle

    forest = Forest()
    for scc in nx.strongly_connected_component_subgraphs(g, False):
        if scc.number_of_edges() == 0:
            continue

        for (v, w, scc_data) in scc.edges_iter(data=True):
            data = g.get_edge_data(v, w)

            # negate weight so that we can construct a longest paths tree for the current solution
            scc_data['w'] = maxmean - data.get('weight', 0)

        root = w
        lpp_tree, _ = bfct.find_shortest_paths(scc, root, arg='w')
        forest.add_forest(lpp_tree)

    return maxmean, arg_cycle, forest
Ejemplo n.º 6
0
    def main(self):

        # Load the data
        data = dataLoader.DataLoader()
        medium = data.load_medium()
        large = data.load_large()

        # Send it to the opener
        med = self.opener(medium)
        lg = self.opener(large)

        # Print the results
        print("Q2.4 How many weakly connected components and how many strongly connected components does this network have? How many nodes and links are in the largest strongly connected component of this graph?\n")

        print("Number of weakly connected components Medium: " + str(nx.number_weakly_connected_components(med)))
        print("Number of weakly connected components Large: " + str(nx.number_weakly_connected_components(lg)))
        print("\n")
        print("Number of strongly connected components Medium: " +  str(nx.number_strongly_connected_components(med)))
        print("Number of strongly connected components Large: " + str(nx.number_strongly_connected_components(lg)))
        print("\n")
        print("How many nodes are in the largest strongly connected component?")
        print("Medium Network: " + str(nx.number_of_nodes(max(nx.strongly_connected_component_subgraphs(med), key=len))))
        print("Large Network: " + str(nx.number_of_nodes(max(nx.strongly_connected_component_subgraphs(lg), key=len))))
        print("\n")
        print("How many edges are in the largest strongly connected component?")
        print("Medium Network: " + str(nx.number_of_edges(max(nx.strongly_connected_component_subgraphs(med), key=len))))
        print("Large Network: " + str(nx.number_of_edges(max(nx.strongly_connected_component_subgraphs(lg), key=len))))
Ejemplo n.º 7
0
def main():

	
	parser = argparse.ArgumentParser()
	parser.add_argument('path', help = "target file or directory for summarization")
	parser.add_argument("--posseed", help="boosting seed for biased LexRank", default = None)
	parser.add_argument("--negseed", help="blocking seed for biased LexRank", default = None)
	parser.add_argument("--stopfile", help="file containing custom stopwords")
	parser.add_argument("-o", "--output", help = "output file name")
	parser.add_argument("-l", "--length", help = "summary length in lines", default = 10)
	parser.add_argument("--seed_posweight", help = "Weight for positive seed", default = 3)
	parser.add_argument("--seed_negweight", help = "Weight for negative seed", default = .0001)
	parser.add_argument("--ngrams", help = "N-gram number", default = 1)
	#normalization doesn't work due to being inherent within scoring method
	parser.add_argument("-n", "--is_norm", help = "Boolean flag for normalization", default = True)
	args = parser.parse_args()
	
	input_text = args.path
	pos_seed = args.posseed
	neg_seed = args.negseed
	stopfile = args.stopfile
	out_file = args.output
	sum_length = int(args.length)
	norm_flag = args.is_norm
	pos_weight = float(args.seed_posweight)
	neg_weight = float(args.seed_negweight)
	ngram = int(args.ngrams)
	corpus = Corpus(input_text).documents
	
	output_checker(out_file)

	if pos_seed == None and neg_seed == None:
		LR_method = 'unbiased'
		print LR_method
		[term_matrix, normalized] = TDM(corpus, pos_seed, neg_seed, stopfile, norm_flag, ngram).matrix
		pos_seed_vector = []
		neg_seed_vector = []
		
	else:
		LR_method = 'biased'
		if pos_seed == None:
			pos_seed = ''
		if neg_seed == None:
			neg_seed = ''
		
		[term_matrix, normalized, pos_seed_vector, neg_seed_vector] = TDM(corpus, pos_seed, neg_seed, stopfile, norm_flag, ngram).matrix
		corpus = corpus[2:]
		

	[scores,graph] = Graph(normalized, LR_method, pos_seed_vector, neg_seed_vector, pos_weight, neg_weight).sim_scores 
	#embed()
	largest = networkx.strongly_connected_component_subgraphs(graph)[0]
	A = networkx.to_agraph(largest)
	#A.node_attr.update(shape='point')
	A.node_attr.update(overlap='voronoi')
	A.layout(prog='sfdp')
	networkx.write_dot(largest, 'testgraph.gv')
	A.draw('butterflyv2.png')
	
	print_summary(corpus, scores, out_file, sum_length)
Ejemplo n.º 8
0
def load_map(location='Manhattan, New York, USA',
             cache_directory='/tmp',
             force_redownload=False):
    cached_filename = hashlib.md5(location).hexdigest() + '.graphml'
    try:
        if force_redownload:
            raise IOError('Forcing re-download of graph.')
        logging.info('Trying to load map from "%s"',
                     os.path.join(cache_directory, cached_filename))
        graph = ox.load_graphml(cached_filename, folder=cache_directory)
    except IOError:
        logging.info('Downloading map from "%s"', location)
        graph = ox.graph_from_place(location, network_type='drive')
        # Keep the largest strongly connected component.
        logging.info('Finding largest component')
        graph = max(nx.strongly_connected_component_subgraphs(graph), key=len)
        graph = ox.project_graph(graph)
        logging.info('Saving map to "%s"',
                     os.path.join(cache_directory, cached_filename))
        ox.save_graphml(graph,
                        filename=cached_filename,
                        folder=cache_directory)
    # Add dummy speed and length information.
    for u, v, key, data in graph.edges(data=True, keys=True):
        if 'time' not in data:
            time = data['length'] / _DEFAULT_SPEED
            data['time'] = time
            data['speed'] = _DEFAULT_SPEED
        else:
            data['time'] = float(data['time'])
            data['speed'] = float(data['speed'])
        graph.add_edge(u, v, **data)
    return graph
def load_graph(place):
    """
    load graph networkx
    :param place: tuple
        (name [string], (north_lat [float], south_lat [float], east_lon [float], west_lon [float]))
    :return g: networkx graph
    """
    print('     Loading graph ...')
    name, bbox = place
    try:
        with open('./graphs_dir/{0}.pkl'.format(name), 'rb') as f:
            graph = pickle.load(f)
    except FileNotFoundError:
        if bbox is None:
            graph = ox.graph_from_place(name, network_type='drive')
        else:
            graph = ox.graph_from_bbox(bbox[0],
                                       bbox[1],
                                       bbox[3],
                                       bbox[2],
                                       network_type='drive')
        graph = max(nx.strongly_connected_component_subgraphs(graph), key=len)
        try:
            with open('./graphs_dir/{0}.pkl'.format(name), 'wb') as f:
                pickle.dump(graph, f, protocol=4)
        except MemoryError:
            print('         Warning: Dump unsuccessful ...')
    print('         Done!')
    return graph
Ejemplo n.º 10
0
def build_graph(mat):
    G = nx.DiGraph()  #创建空图
    for i in range(0, mat.shape[0]):
        G.add_node(i)  #创造节点
    for i in range(0, mat.shape[0]):
        for j in range(0, mat.shape[1]):
            if mat[i, j] == 1:
                G.add_edge(i, j)  #加一条有向边

    #print nx.in_degree(G,0)
    #print nx.out_degree(G)
    #print nx.degree(G)
    print nx.clustering(G.to_undirected())
    print G.in_degree(1)
    #nx.convert_to_undirected(G)
    #nx.convert_to_undirected()
    print nx.betweenness_centrality(G)
    print nx.closeness_centrality(G)
    #print nx.diameter(G)
    print nx.average_shortest_path_length(G)
    # print nx.average_clustering(G)
    sub_graph = nx.strongly_connected_component_subgraphs(G)
    for line in sub_graph:
        print nx.degree(line)
        #pos =nx.circular_layout(G)
        #plot.title('the orginal graph with pos')
        #nx.draw(G,pos,with_label=True,node_size=300)
        #plot.show()
        nx.draw(line, with_label=True)
        plot.show()
Ejemplo n.º 11
0
 def nontrivial_strongly_connected_components(self):
     ''' iterator over nontrivial strongly connected subgraphs\n
     nontrivial means it has more than one node or it is a node with a self loop '''
     scc = strongly_connected_component_subgraphs(self)
     for g in scc:
         if g.number_of_nodes()>1 or g.number_of_selfloops()>0:
             yield g
Ejemplo n.º 12
0
def answer_six():

    G = answer_one()
    G2 = nx.strongly_connected_component_subgraphs(G)
    G_sc = max(G2, key=len)

    return G_sc
Ejemplo n.º 13
0
def test_dim_error():
    import sys
    authority_dict={}
    graph_file = '/home/michal/SALSA_files/tmp/real_run/middle_graph_authority'
    G_new = gm.read_graph_from_file(graph_file)
    isolates = nx.isolates(G_new)
    print 'num of isolates: '+str(len(isolates)); sys.stdout.flush()
    num_of_not_isolates = G_new.number_of_nodes() - len(isolates)
    authority_dict = {}
    classes = nx.strongly_connected_component_subgraphs(G_new)
    print 'num of classes including isolates: '+str(len(classes)); sys.stdout.flush()
    #remove classes of isolated nodes:   
    classes[:] = [ c for idx,c in enumerate(classes) if c.nodes()[0] not in isolates ]
    
    print 'num of classes NOT including isolates: '+str(len(classes)); sys.stdout.flush()
    for subG in classes:
        #print type(subG)
        out_file = ''.join(['/home/michal/SALSA_files/tmp/real_run/graph_',str(classes.index(subG))])
        gm.write_graph_to_file(subG, out_file)
        tmp_d = salsa.eig_calc(subG, normalize=num_of_not_isolates)    #power_iteration(subG)
    '''    
        for k,v in tmp_d.items():
            authority_dict[G.nodes()[k]] = v
        #print power_iteration(subG, tol=1.0e-10)
    for i in isolates:
        authority_dict[G.nodes()[i]] = 0
    #print authority_dict
    print '\n--- calc_salsa_per_class took: '+str(datetime.now()-startTime); sys.stdout.flush()'''
    return
Ejemplo n.º 14
0
def linearize(filename):
    graph_name = filename.split('.')[0] + '.graphml'
    g = nx.read_graphml(graph_name)

    print nx.info(g)

    # get first strong connected component

    con = list(nx.strongly_connected_component_subgraphs(g))

    con.sort(key=lambda x: len(x), reverse=True)
    print[len(item) for item in con]

    print nx.info(con[0])

    dfs_edges = list(nx.dfs_edges(con[0]))

    dfs_edges.append((dfs_edges[-1][-1], dfs_edges[0][0]))

    #print dfs_edges

    with open(filename.split('.')[0] + '.linear.edges', 'w') as f:
        for item in dfs_edges:
            f.write(item[0] + ' ' + item[1] + ' ' +
                    str(con[0].edge[item[0]][item[1]]['ew']) + '\n')
Ejemplo n.º 15
0
def do_computations(g):
    print('Bitcoin Alpha Graph:', file=file)
    print('\tNumber of nodes:', g.number_of_nodes(), file=file)
    print('\tNumber of edges:', g.number_of_edges(), file=file)
    print('\tNumber of self-loops:', g.number_of_selfloops(), file=file)

    max_centrality = compute_centrality(
        g)  # Need to set how to calculate centrality inside the function
    print_node_degrees(g, max_centrality)
    print_node_degrees(g,
                       ['2', '3', '4', '7'])  # These are other important nodes

    print('\n\tNumber of triangles:',
          sum(nx.triangles(g.to_undirected()).values()) / 3,
          file=file)
    print('\tAverage clustering:',
          nx.average_clustering(g.to_undirected()),
          file=file)

    print('\n\tStrongly connected components:', file=file)
    connected_comps_strong = list(nx.strongly_connected_component_subgraphs(g))
    strong_giant = compute_component(connected_comps_strong)
    print('\tAverage shortest path length',
          nx.average_shortest_path_length(strong_giant),
          file=file)

    print('\n\tWeakly connected components:', file=file)
    connected_comps_weak = list(nx.weakly_connected_component_subgraphs(g))
    weak_giant = compute_component(connected_comps_weak)
Ejemplo n.º 16
0
def answer_six():
        
    # Your Code Here
    G = answer_one()
    scc_subs = nx.strongly_connected_component_subgraphs(G)
    G_sc = max(scc_subs, key=len)
    return G_sc
def strongly_connected_components():
    conn = sqlite3.connect("zhihu.db")     
    #following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 50000) and user_url in (select user_url from User where agree_num > 50000)', conn)        
    following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 10000) and user_url in (select user_url from User where agree_num > 10000)', conn)        
    conn.close()
    
    G = nx.DiGraph()
    cnt = 0
    for d in following_data.iterrows():
        G.add_edge(d[1][0],d[1][1])
        cnt += 1
    print 'links number:', cnt

    scompgraphs = nx.strongly_connected_component_subgraphs(G)
    scomponents = sorted(nx.strongly_connected_components(G), key=len, reverse=True)
    print 'components nodes distribution:', [len(c) for c in scomponents]
    
    #plot graph of component, calculate saverage_shortest_path_length of components who has over 1 nodes
    index = 0
    print 'average_shortest_path_length of components who has over 1 nodes:'
    for tempg in scompgraphs:
        index += 1
        if len(tempg.nodes()) != 1:
            print nx.average_shortest_path_length(tempg)
            print 'diameter', nx.diameter(tempg)
            print 'radius', nx.radius(tempg)
        pylab.figure(index)
        nx.draw_networkx(tempg)
        pylab.show()

    # Components-as-nodes Graph
    cG = nx.condensation(G)
    pylab.figure('Components-as-nodes Graph')
    nx.draw_networkx(cG)
    pylab.show()    
Ejemplo n.º 18
0
def strongly_connected_components():
    cur.execute(
        'select from_id, to_id from Follows where from_id in (select id from People where fo_num > 5000) and to_id in (select id from People where fo_num > 5000)'
    )
    g = nx.DiGraph(name='net5k')

    for i in cur.fetchall():
        g.add_edge(*i)

    # print((len(list(cur.fetchall()))))
    # print(nx.info(g))

    scompgraphs = nx.strongly_connected_component_subgraphs(g)
    scomponents = sorted(nx.strongly_connected_components(g),
                         key=len,
                         reverse=True)
    print(('components nodes distribution:', [len(c) for c in scomponents]))

    # plot graph of component, calculate saverage_shortest_path_length of
    # components who has over 1 nodes
    index = 0
    print('average_shortest_path_length of components who has over 1 nodes:')
    for tempg in scompgraphs:
        index += 1
        if len(tempg.nodes()) != 1:
            print((nx.average_shortest_path_length(tempg)))
            print('diameter', nx.diameter(tempg))
            print('radius', nx.radius(tempg))
Ejemplo n.º 19
0
def filter_big_scc(g, edges_to_be_removed):
    # Given a graph g and edges to be removed
    # Return a list of big scc subgraphs (# of nodes >= 2)
    g.remove_edges_from(edges_to_be_removed)
    sub_graphs = filter(lambda scc: scc.number_of_nodes() >= 2,
                        nx.strongly_connected_component_subgraphs(g))
    return sub_graphs
Ejemplo n.º 20
0
def alp_pagerank(g, subgraphs, func=max, custom_alpha=0.9, custom_max_iter=10000):
    import operator
    landmarks = []
    for sub in subgraphs.values():
        if g.is_directed():
            g_s = list(nx.strongly_connected_component_subgraphs(g.subgraph(sub)))[0]
        else:
            g_s = list(nx.connected_component_subgraphs(g.subgraph(sub), False))[0]

        n_len = nx.number_of_nodes(g_s)

        if n_len < 3:
            landmarks.append(g_s.nodes()[0])
        else:
            try:
                pr = nx.pagerank(g_s, alpha=custom_alpha, max_iter=custom_max_iter)
            except:
                print g_s.nodes()
                exit(1)

            if func == mode:
                landmarks.append(mode(pr))
            else:
                landmarks.append(func(pr.iteritems(), key=operator.itemgetter(1))[0])

    return landmarks
def answer_five():
        
    G = answer_one()
    largest = max(nx.strongly_connected_component_subgraphs(G),key=len)
    l = largest.nodes()
    
    return len(l)
Ejemplo n.º 22
0
 def detect_deadlock(self):
     """
     Detects whether the system is in a deadlocked state,
     that is, is there a knot. Note that this code is taken
     and adapted from the NetworkX Developer Zone Ticket
     #663 knot.py (09/06/2015)
     """
     knots = []
     for subgraph in nx.strongly_connected_component_subgraphs(
             self.digraph):
         nodes = set(subgraph.nodes())
         if len(nodes) == 1:
             n = nodes.pop()
             nodes.add(n)
             if set(self.digraph.successors(n)) == nodes:
                 knots.append(subgraph)
         else:
             for n in nodes:
                 successors = nx.descendants(self.digraph, n)
                 if successors <= nodes:
                     knots.append(subgraph)
                     break
     if len(knots) > 0:
         return True
     return False
Ejemplo n.º 23
0
 def nontrivial_strongly_connected_components(self):
     ''' iterator over nontrivial strongly connected subgraphs\n
     nontrivial means it has more than one node or it is a node with a self loop '''
     scc = strongly_connected_component_subgraphs(self)
     for g in scc:
         if g.number_of_nodes() > 1 or g.number_of_selfloops() > 0:
             yield g
Ejemplo n.º 24
0
def alp_random_ls_non_opt(g,subgraphs):
    #perform the landmark selection within each subgraph
    if g.is_directed():
        landmarks = [random_ls_non_opt(list(nx.strongly_connected_component_subgraphs(g.subgraph(sub)))[0], 1) for sub in subgraphs.values()]
    else:
        landmarks = [random_ls_non_opt(list(nx.connected_component_subgraphs(g.subgraph(sub), False))[0], 1) for sub in subgraphs.values()]
    return [l[0] for l in landmarks if l]
Ejemplo n.º 25
0
def cyclePlot(gexFile):
    DG = nx.DiGraph(nx.read_gexf(gexFile))
    
    #generate networkx friendly position format
    #dictionary keyed by node label with values being a float32 ndarray
    pos = dict()
    for i in range(1, len(DG.node)+1):
        xPos = DG.node[str(i)]['viz']['position']['x']
        yPos = DG.node[str(i)]['viz']['position']['y']
        pos[str(i)] = np.array([xPos,yPos])
    
    #nx.draw_networkx_edges(DG,pos,nodelist=DG.node.keys(),alpha=0.05,
    #                       arrows=True)
    nx.draw_networkx_nodes(DG,pos,nodelist=DG.node.keys(),
                       node_size=30,
                       node_color='grey',
                       alpha=0.4)
    nx.draw_networkx_edges(DG,pos,alpha=0.4,
                               arrows=True,
                               edge_color='k')
    plt.show()
    
    scc=nx.strongly_connected_component_subgraphs(DG)
    CG=scc[0];
    
    #show example
    nx.draw_networkx_nodes(CG,pos,nodelist=CG.node.keys(),
                       node_size=30,
                       node_color='c')
    nx.draw_networkx_edges(CG,pos,alpha=0.5,
                               arrows=True,
                               edge_color='r')
Ejemplo n.º 26
0
def alp_planar(g, subgraphs):
    if not g:
        raise ValueError("Cannot specify graph as null.")
    elif not subgraphs:
        raise ValueError("Cannot specify subgraphs as null.")

    ls = []
    import operator
    for g_p in subgraphs.values():
        if g.is_directed():
            subg = list(nx.strongly_connected_component_subgraphs(g.subgraph(g_p)))[0]
        else:
            subg = list(nx.connected_component_subgraphs(g.subgraph(g_p), False))[0]

        ls.append(nx.periphery(subg)[0])
        '''e = nx.eccentricity(subg)
        #get node with highest eccentricity
        if e:
            ls.append(max(e.iteritems(), key=operator.itemgetter(1))[0])
        else:
            print "Could not find eccentricity"'''
        '''max_e = 0
        max_node = 0
        #identify node with max eccentricity
        for node in g_p:
             if e[node] > max_e:
                max_e = e[node]
                max_node = node

        #add to set
        ls.append(max_node)'''
    return ls
Ejemplo n.º 27
0
def answer_six():
        
    emailG = answer_one()
    
    G_sc = max(nx.strongly_connected_component_subgraphs(emailG), key=len)
    
    return G_sc
Ejemplo n.º 28
0
    def __condensation(self):
        """Produces condensation of cyclic graphs."""
        subgraphs = nx.strongly_connected_component_subgraphs(self.digraph)
        for subgraph in list(subgraphs):
            if subgraph.number_of_nodes() == 1:
                continue  # not a cycle
            pre_edges = []
            suc_edges = []
            for node in subgraph:
                assert node not in self.node2cycle
                assert node in self.digraph  # no accidental copying
                self.node2cycle[node] = subgraph
                for pre_node in self.digraph.predecessors(node):
                    if not subgraph.has_node(pre_node):
                        pre_edges.append((pre_node, node))
                        self.digraph.add_edge(pre_node, subgraph)
                for suc_node in self.digraph.successors(node):
                    if not subgraph.has_node(suc_node):
                        suc_edges.append((node, suc_node))
                        self.digraph.add_edge(subgraph, suc_node)
                self.digraph.remove_node(node)
            assert subgraph not in self.cycles
            self.cycles[subgraph] = (pre_edges, suc_edges)

        cycle_order = lambda x: min(str(u) for u in x)
        for index, cycle in enumerate(sorted(self.cycles, key=cycle_order)):
            self.cycle2index[cycle] = index
Ejemplo n.º 29
0
def scc_nodes_edges(g):
    scc_nodes = set()
    scc_edges = set()
    num_big_sccs = 0
    num_nodes_biggest_scc = 0
    biggest_scc = None
    for sub in nx.strongly_connected_component_subgraphs(g):
        number_nodes = sub.number_of_nodes()
        if number_nodes >= 2:
            scc_nodes.update(sub.nodes())
            scc_edges.update(sub.edges())
            num_big_sccs += 1
            if num_nodes_biggest_scc < number_nodes:
                num_nodes_biggest_scc = number_nodes
                biggest_scc = sub
    nonscc_nodes = set(g.nodes()) - scc_nodes
    nonscc_edges = set(g.edges()) - scc_edges
    print("num nodes biggest scc: %d" % num_nodes_biggest_scc)
    print("num of big sccs: %d" % num_big_sccs)
    if biggest_scc == None:
        return scc_nodes, scc_nodes, nonscc_nodes, nonscc_edges
    print("# nodes in biggest scc: %d, # edges in biggest scc: %d" %
          (biggest_scc.number_of_nodes(), biggest_scc.number_of_edges()))
    print(
        "# nodes,edges in scc: (%d,%d), # nodes, edges in non-scc: (%d,%d) " %
        (len(scc_nodes), len(scc_edges), len(nonscc_nodes), len(nonscc_edges)))
    num_of_nodes = g.number_of_nodes()
    num_of_edges = g.number_of_edges()
    print(
        "# nodes in graph: %d, # of edges in graph: %d, percentage nodes, edges in scc: (%0.4f,%0.4f), percentage nodes, edges in non-scc: (%0.4f,%0.4f)"
        % (num_of_nodes, num_of_edges, len(scc_nodes) * 1.0 / num_of_nodes,
           len(scc_edges) * 1.0 / num_of_edges, len(nonscc_nodes) * 1.0 /
           num_of_nodes, len(nonscc_edges) * 1.0 / num_of_edges))
    return scc_nodes, scc_edges, nonscc_nodes, nonscc_edges
Ejemplo n.º 30
0
def alp_katz(g, subgraphs, func=max):
    import operator
    landmarks = []
    for sub in subgraphs.values():
        if g.is_directed():
            g_s = list(nx.strongly_connected_component_subgraphs(g.subgraph(sub)))[0]
        else:
            g_s = list(nx.connected_component_subgraphs(g.subgraph(sub), False))[0]

        n_len = nx.number_of_nodes(g_s)

        if n_len < 10:
            landmarks.append(g_s.nodes()[0])
        else:
            try:
                bc = nx.katz_centrality(g_s, max_iter=100000, normalized=True)
            except:
                try:
                    print "Running Katz centrality again for tolerance = ", str(.001)
                    bc = nx.katz_centrality(g_s, max_iter=100000, normalized=True, tol=.001)
                except:
                    print "Katz Centrality returned nothing."
                    return []


            if func == mode:
                landmarks.append(mode(bc))
            else:
                landmarks.append(func(bc.iteritems(), key=operator.itemgetter(1))[0])

    return landmarks
Ejemplo n.º 31
0
def alp_load(g, subgraphs, func=max):
    import operator
    landmarks = []
    for sub in subgraphs.values():
        if g.is_directed():
            g_s = list(nx.strongly_connected_component_subgraphs(g.subgraph(sub)))[0]
        else:
            g_s = list(nx.connected_component_subgraphs(g.subgraph(sub), False))[0]

        n_len = nx.number_of_nodes(g_s)

        if n_len < 3:
            landmarks.append(g_s.nodes()[0])
        else:
            try:
                bc = nx.load_centrality(g_s, normalized=True)
            except:
                print "Load Centrality returned nothing."
                return []


            if func == mode:
                landmarks.append(mode(bc))
            else:
                landmarks.append(func(bc.iteritems(), key=operator.itemgetter(1))[0])

    return landmarks
Ejemplo n.º 32
0
 def strategise_enemy_fault_lines(self, world, player):
     possible_sources_and_sinks = []
     territory_graph= nx.DiGraph()
     for a in world.areas:
         for t in world.areas[a].territories:
             for adj in t.adjacent():
                 if not adj.owner.name == player.name and not t.owner.name == player.name:
                     territory_graph.add_edge(t.name, adj.name, capacity=-adj.forces)
                     possible_sources_and_sinks.append(adj)
     possible_sources_and_sinks = compose(list, set)(possible_sources_and_sinks)
     graphs=list(nx.strongly_connected_component_subgraphs(territory_graph))
     fault_lines = []
     for g in graphs:
         sources_and_sinks_in_graph = [p for p in possible_sources_and_sinks if p.name in g.nodes()]
         ordered_pairs = []
         for s in sources_and_sinks_in_graph:
             for d in sources_and_sinks_in_graph:
                 if not s.name == d.name:
                     ordered_pairs.append((s,d))
         for s,d in ordered_pairs:
             flow_value, flow_dict = nx.maximum_flow(g, s.name, d.name)
             fault_lines.append((flow_value, flow_dict))
     paths = []
     for (f,p) in fault_lines:
         paths.append(unwrap_dict_of_dicts(p))
     shortest_fault_line = []
     shortest_fault_line_length = 50
     for p in paths:
         if len(p) < shortest_fault_line_length:
             shortest_fault_line_length = len(p)
             shortest_fault_line = p
     self.fault_line = shortest_fault_line
     self.deceptive_attack.extend(self.fault_line)
Ejemplo n.º 33
0
def get_largest_component(G, strongly=False):
    """
    Return the largest weakly or strongly connected component from a directed graph.
    
    Parameters
    ----------
    G : graph
    strongly : bool, if True, return the largest strongly instead of weakly connected component
    
    Returns
    -------
    G : graph
    """

    start_time = time.time()
    original_len = len(G.nodes())

    if strongly:
        # if the graph is not connected and caller did not request retain_all, retain only the largest strongly connected component
        if not nx.is_strongly_connected(G):
            G = max(nx.strongly_connected_component_subgraphs(G), key=len)
            log('Graph was not connected, retained only the largest strongly connected component ({:,} of {:,} total nodes) in {:.2f} seconds'
                .format(len(G.nodes()), original_len,
                        time.time() - start_time))
    else:
        # if the graph is not connected and caller did not request retain_all, retain only the largest weakly connected component
        if not nx.is_weakly_connected(G):
            G = max(nx.weakly_connected_component_subgraphs(G), key=len)
            log('Graph was not connected, retained only the largest weakly connected component ({:,} of {:,} total nodes) in {:.2f} seconds'
                .format(len(G.nodes()), original_len,
                        time.time() - start_time))

    return G
Ejemplo n.º 34
0
def get_largest_component(G, strongly=False):
    """
    Return the largest weakly or strongly connected component from a directed graph.
    Parameters
    ----------
    G : networkx multidigraph
    strongly : bool
        if True, return the largest strongly instead of weakly connected component
    Returns
    -------
    networkx multidigraph
    """


    original_len = len(list(G.nodes()))

    if strongly:
        # if the graph is not connected and caller did not request retain_all, retain only the largest strongly connected component
        if not nx.is_strongly_connected(G):
            G = max(nx.strongly_connected_component_subgraphs(G), key=len)
            
    else:
        # if the graph is not connected and caller did not request retain_all, retain only the largest weakly connected component
        if not nx.is_weakly_connected(G):
            G = max(nx.weakly_connected_component_subgraphs(G), key=len)
            
    return G
Ejemplo n.º 35
0
def remove_cycles(G):
    while not nx.is_directed_acyclic_graph(G):
        subgraphs = nx.strongly_connected_component_subgraphs(G)
        for subgraph in subgraphs:
            if subgraph.number_of_nodes() > 1:
                edge_index = random.randrange(subgraph.number_of_edges())
                edge = subgraph.edges()[edge_index]
                G.remove_edge(edge[0], edge[1])
Ejemplo n.º 36
0
def remove_cycles(G):
    while not nx.is_directed_acyclic_graph(G):
        subgraphs = nx.strongly_connected_component_subgraphs(G)
        for subgraph in subgraphs:
            if subgraph.number_of_nodes() > 1:
                edge_index = random.randrange(subgraph.number_of_edges())
                edge = subgraph.edges()[edge_index]
                G.remove_edge(edge[0], edge[1])
Ejemplo n.º 37
0
def cycle_induced_subgraph(g):
    ''' Computes the subgraph that is maximally edge-induced by its cycles

    That is, every cycle that is in g is also in cycle_induced_subgraph(g).
    '''

    # gather all edges of SCCs
    return nx.DiGraph( ( edge for scc in nx.strongly_connected_component_subgraphs( g ) for edge in scc.edges_iter() ))
def giant_component(net):
    if 'giant_component' in net.graph['already_computed'] :
        return net.graph['already_computed']['giant_component']
    else :
        if net.is_directed() :
            net.graph['already_computed']['giant_component'] = nx.strongly_connected_component_subgraphs(net)[0]
        else :
            net.graph['already_computed']['giant_component'] = nx.connected_component_subgraphs(net)[0]
        return net.graph['already_computed']['giant_component']
Ejemplo n.º 39
0
    def detect_deadlock(self):
        """
        Detects whether the system is in a deadlocked state, that is, is there a knot

        Note that this code is taken and adapted from the NetworkX Developer Zone Ticket #663 knot.py (09/06/2015)

            >>> from import_params import load_parameters
            >>> Q = Simulation(load_parameters('tests/datafortesting/logs_test_for_simulation/'))
            >>> nodes = ['A', 'B', 'C', 'D', 'E']
            >>> connections = [('A', 'D'), ('A', 'B'), ('B', 'E'), ('C', 'B'), ('E', 'C')]
            >>> for nd in nodes:
            ...     Q.digraph.add_node(nd)
            >>> for cnctn in connections:
            ...     Q.digraph.add_edge(cnctn[0], cnctn[1])
            >>> Q.detect_deadlock()
            True

            >>> Q = Simulation(load_parameters('tests/datafortesting/logs_test_for_simulation/'))
            >>> nodes = ['A', 'B', 'C', 'D']
            >>> connections = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
            >>> for nd in nodes:
            ...     Q.digraph.add_node(nd)
            >>> for cnctn in connections:
            ...     Q.digraph.add_edge(cnctn[0], cnctn[1])
            >>> Q.detect_deadlock()
            False

            >>> Q = Simulation(load_parameters('tests/datafortesting/logs_test_for_simulation/'))
            >>> nodes = ['A', 'B']
            >>> for nd in nodes:
            ...     Q.digraph.add_node(nd)
            >>> Q.detect_deadlock()
            False
            >>> connections = [('A', 'A')]
            >>> for cnctn in connections:
            ...     Q.digraph.add_edge(cnctn[0], cnctn[1])
            >>> Q.detect_deadlock()
            True
        """
        knots = []
        for subgraph in nx.strongly_connected_component_subgraphs(self.digraph):
            nodes = set(subgraph.nodes())
            if len(nodes) == 1:
                n = nodes.pop()
                nodes.add(n)
                if set(self.digraph.successors(n)) == nodes:
                    knots.append(subgraph)
            else:
                for n in nodes:
                    successors = nx.descendants(self.digraph, n)
                    if not successors <= nodes:
                        break
                else:
                    knots.append(subgraph)
        if len(knots) > 0:
            return True
        return False
Ejemplo n.º 40
0
def basic_graph_process(_graph):
    # generate SCCs
    sccs = networkx.strongly_connected_component_subgraphs(_graph, copy=True)
    sccs = sorted(sccs, key=len, reverse=True)
    _graph.graph["sccs"] = sccs

    not_dag_edges = []

    for scc in sccs:
        not_dag_edges = not_dag_edges + scc.edges()

    dag_edges = [edge for edge in _graph.edges() if edge not in not_dag_edges]

    # give SCC index to each node, usefull ?????
    for i in range(0, len(sccs)):
        for node in sccs[i].nodes():
            _graph.node[node]["scc_index"] = i

    # find boundary nodes in each SCC
    for scc in sccs:
        scc.graph["inward_nodes"] = set()
        scc.graph["outward_nodes"] = set()

    # add inward and outward nodes
    for edge in dag_edges:
        scc_index = _graph.node[edge[0]]["scc_index"]
        scc = sccs[scc_index]
        scc.node[edge[0]].setdefault("outward_edges", [])
        scc.node[edge[0]]["outward_edges"].append(edge)
        scc.graph["outward_nodes"].add(edge[0])

        scc_index = _graph.node[edge[1]]["scc_index"]
        scc = sccs[scc_index]
        scc.node[edge[1]].setdefault("inward_edges", [])
        scc.node[edge[1]]["inward_edges"].append(edge)
        scc.graph["inward_nodes"].add(edge[1])

    # initial must be add to inward nodes, add final states as outward nodes
    initial_index = _graph.graph["initial"]
    sccs[_graph.node[initial_index]["scc_index"]].graph["inward_nodes"].add(initial_index)
    sccs[_graph.node[initial_index]["scc_index"]].node[initial_index].setdefault("inward_edges", [])

    final_indexes = _graph.graph["finals"]
    for final_index in final_indexes:
        scc_index = _graph.node[final_index]["scc_index"]
        scc = sccs[scc_index]
        scc.graph["outward_nodes"].add(final_index)
        scc.node[final_index].setdefault("outward_edges", [])


    # it's easier to operate on list ???
    for scc in sccs:
        scc.graph["inward_nodes"] = list(scc.graph["inward_nodes"])
        scc.graph["outward_nodes"] = list(scc.graph["outward_nodes"])

    return (sccs, dag_edges)
def get_major_strongly_component(graph):

    major_strongly_component_dict = {'amount_nodes': 0, 'subgraph':None}
    for component in nx.strongly_connected_component_subgraphs(graph):
        amount_nodes = len(component.nodes())
        if amount_nodes > major_strongly_component_dict['amount_nodes']:
            major_strongly_component_dict['amount_nodes'] = amount_nodes
            major_strongly_component_dict['subgraph'] = component

    return major_strongly_component_dict['subgraph']
Ejemplo n.º 42
0
    def __classifyEdges(self, tempG):
        """link classification
        :returns: @todo

        """
        Gd1, Gd2 = self.__turnMatchedGp2Gd(tempG)

        #get all the free nodes in Gp (self.BiG)
        freeNodes = filter(lambda res: res[1]['label']=='free', tempG.nodes(data=True))
        for node in freeNodes:
            nodeName = node[0]
            for Gd in [Gd1,Gd2]:
                flag, pathlist = self.__BFS_classifyEdges(Gd, nodeName)
                if flag:
                    for path in pathlist:
                        for i in range(len(path)-1):
                            Gd.edge[path[i]][path[i+1]]['mark'] = 'used'

        sccs = networkx.strongly_connected_component_subgraphs(Gd1)
        for subgraph in sccs:
            for link in subgraph.edges():
                Gd1.edge[link[0]][link[1]]['mark'] = 'used'
                Gd2.edge[link[1]][link[0]]['mark'] = 'used'

        for link in tempG.edges():
            flag0 = tempG.edge[link[0]][link[1]]['label'] == "matching"

            if link[0].endswith("\t+"):
                source = link[0]
                target = link[1]
            else:
                source = link[1]
                target = link[0]

            if flag0:
                flag1 = Gd1[source][target]['mark']=='unused'
                flag2 = Gd2[target][source]['mark']=='unused'
            else:
                flag1 = Gd1[target][source]['mark']=='unused'
                flag2 = Gd2[source][target]['mark']=='unused'

            # for random network
            source = source.strip("\t+")
            target = target.strip("\t-")

            if flag1 and flag2:
                if flag0:
                    self.DG.edge[source][target]['class'] = 'critical'
                else:
                    self.DG.edge[source][target]['class'] = 'redundant'
            else:
                self.DG.edge[source][target]['class'] = 'intermittent'

        pass
Ejemplo n.º 43
0
def linearize(filename):
    graph_name = filename.split('.')[0]+'.graphml'
    g = nx.read_graphml(graph_name)
    
    print nx.info(g)
    
    # get first strong connected component
    
    con = list(nx.strongly_connected_component_subgraphs(g))
    
    con.sort(key = lambda x:len(x), reverse = True)
    print [len(item) for item in con]
Ejemplo n.º 44
0
def myavgpathlength(G):
    try:
        apl =  nx.average_shortest_path_length(G)
        return [apl]
    except nx.NetworkXError as e:
        #this means graph is not connected
        if isinstance(G,nx.DiGraph):
		    return [nx.average_shortest_path_length(nx.strongly_connected_component_subgraphs(G)[0])]
        else:
            return [nx.average_shortest_path_length(nx.connected_component_subgraphs(G)[0])]
    except ZeroDivisionError as e:
        return [1]     
Ejemplo n.º 45
0
def diGraph_netX_stats(tmpDig):
	realSccs = 0
	scc = nx.strongly_connected_components(tmpDig)
	sccsg = nx.strongly_connected_component_subgraphs(tmpDig)
	actualScc = []
	for i in scc: 
		if len(i) > 1: actualScc.append(i)
	sccN= len(actualScc)
	selfLoops = tmpDig.number_of_selfloops()
	selfLoopsEgdes = tmpDig.selfloop_edges()
	realSccs = selfLoops + sccN 
	return actualScc, sccN, selfLoops, selfLoopsEgdes, realSccs, sccsg
Ejemplo n.º 46
0
	def filter_graph_for_strongly_connected_components(self, min_nodes=2): 
		"""
		Get strongly connected components in graph. 
		min_nodes : int 
			Return only connected components with a minimal number of min_nodes
		"""
		edges = [] 
		for g in nx.strongly_connected_component_subgraphs(self.graph): 
			if len(g.nodes(data=True)) >= min_nodes: 
				for e in g.edges(data=True): 
					edges.append(e)

		self.graph = nx.DiGraph(edges)
    def __init__(self, point_cloud, lines):
        # Simulates random order of lines
        random.shuffle(point_cloud)
        self.lines = lines
        self.point_cloud = point_cloud

        before = len(point_cloud)
        point_cloud = list(set(point_cloud))
        after = len(point_cloud)

        #assert before == after
        #assert len(point_cloud)/2 == len(lines)

        line_end_points = np.array(point_cloud)
        num_lines = len(line_end_points) / 2

        Y = cdist(np.array(line_end_points), np.array(line_end_points), 'euclidean')
        plt.figure(3)
        c = plt.imshow(Y, interpolation='none')
        plt.colorbar(c)
        plt.show()

        new = np.zeros(Y.shape)

        for i in range(len(Y)):
            row = Y[i]
            h = enumerate(row)
            h = sorted(h, key=lambda x: x[1])
            idx = [e[0] for e in h[:3]]
            for idx_i in idx:
                new[i][idx_i] = 1


        self.groups = []

        plt.figure(4)
        G = nx.from_numpy_matrix(new, create_using=nx.DiGraph())
        cols = itertools.cycle(["ro", "yo", "go", "mo", "bo", "co"])
        for subg in nx.strongly_connected_component_subgraphs(G):
            v = np.array([line_end_points[e] for e in subg.nodes()])
            print "Group", v.mean(), v.std()

            self.groups.append(v)


            c = cols.next()
            for element in subg.nodes():
                a, b = line_end_points[element]
                plt.plot(a, b, c)
        plt.show()
Ejemplo n.º 48
0
def calc_salsa_per_class_OLD(G, rank_type):
    '''
    # G is the original graph
    # rank_type = 'authority' or 'hub' 
    '''
    import scipy.sparse
    print '\t\t~~~~~~ calc_salsa_per_class ~~~~~~'; startTime = datetime.now(); sys.stdout.flush()

    # Authority:
    A=get_matrix(G,mat_type=rank_type,sparse=True)
    G_new = nx.DiGraph(A)
    '''DEBUG:
    out_file = ''.join(['/home/michal/SALSA_files/tmp/real_run/middle_graph_',str(rank_type)])
    gm.write_graph_to_file(G_new, out_file)'''
    #x=scipy.ones((n,1))/n  # initial guess
    isolates = nx.isolates(G_new)   # isolate node is a node with in_deg=out_deg=0
    print '--- calc_salsa_per_class: num of isolates- ',len(isolates),', out of- ',G_new.number_of_nodes(),' nodes (',float(len(isolates))/G_new.number_of_nodes(),'%)'; sys.stdout.flush()
    num_of_not_isolates = G_new.number_of_nodes() - len(isolates)
    scores_dict = {}
    tmpTime = datetime.now()
    classes = nx.strongly_connected_component_subgraphs(G_new)
    #print '--- calc_salsa_per_class: separate to classes took- '+str(datetime.now()-tmpTime); sys.stdout.flush(); tmpTime = datetime.now()
    #remove classes of isolated nodes:   
    classes[:] = [ c for idx,c in enumerate(classes) if c.nodes()[0] not in isolates ]
    #print '--- calc_salsa_per_class: clean classes from isolates took- ',datetime.now()-tmpTime; sys.stdout.flush(); 
    
    num_of_classes = 0
    domain_class_dict = {}
    for subG in classes:
        num_of_classes += 1
        '''DEBUG: 
        out_file = ''.join(['/home/michal/SALSA_files/tmp/real_run/graph_',str(classes.index(subG))])
        gm.write_graph_to_file(subG, out_file)'''
        tmp_d = eig_calc(subG, normalize=num_of_not_isolates)   
        #tmp_d = power_iteration(subG,max_iter=100,tol=1.0e-8,normalize=num_of_not_isolates,nstart=None)
        #tmp_d = power_iteration(subG, normalize=num_of_not_isolates, nstart=[v[G.n_attr.risk] for v in subG.nodes(data=True)])
        for k,v in tmp_d.items():
            d = G.nodes()[k]
            scores_dict[d] = v
            domain_class_dict[d] = num_of_classes
    print '--- calc_salsa_per_class: num of classes (NOT including isolates)- ',num_of_classes
    for i in isolates:
        d = G.nodes()[i]
        scores_dict[d] = 0
        domain_class_dict[d] = 0 # class zero represents the isolates
    #print authority_dict
    print '--- calc_salsa_per_class took: ',datetime.now()-startTime; sys.stdout.flush()

    return scores_dict, domain_class_dict
Ejemplo n.º 49
0
def simple_cycles2(G):
    comps = nx.strongly_connected_component_subgraphs(G)
    print 'found %s strongly-connected component subgraphs' % len(comps)
    all_cycles = []
    do_pdb = True
    for c in comps:
        if len(c) == 1:
            continue
        else:
            for cycle in nx.simple_cycles(c):
                print 'found cycle, length: %s' % len(cycle)
                all_cycles.append(cycle)
                if len(all_cycles) % 10 == 0 and do_pdb:
                    import pdb;pdb.set_trace()
    return all_cycles
Ejemplo n.º 50
0
    def _parse_loops_from_graph(self, graph):
        """
        Return all Loop instances that can be extracted from a graph.

        :param graph:   The graph to analyze.

        :return:        A list of all the Loop instances that were found in the graph.
        """
        out = []
        for subg in networkx.strongly_connected_component_subgraphs(graph):
            if len(subg.nodes()) == 1:
                if len(subg.successors(subg.nodes()[0])) == 0:
                    continue
            out += self._parse_loop_graph(subg, graph)
        return out
Ejemplo n.º 51
0
def get_SMTCode(mdg, mp_prop, opts):
    # input mdg
    # output SMTCode list, result per 
    results = {'SCCs':[], 'AccSCCs':[]}
    if opts.tgba:
        all_acceptlist = mdg.graph['acccond']
    SCCs = nx.strongly_connected_component_subgraphs(mdg)
    for scc in SCCs:
        results['SCCs'].append({'edges':nx.number_of_edges(scc), 'nodes':nx.number_of_nodes(scc)})
        if scc.edges() == []:
            pass
        else:
            # nx.draw_circular(scc)
            accept = 0
            if opts.tgba:
                a = reduce(lambda x, y : x | y , [set(edge[2]['acc']) for edge in scc.edges(data=True)])
                print a
                acceptlist = list(a)
                print acceptlist
                """for edge in scc.edges(data=True):
                    for acc in edge[2]['acc']:
                        if not acc in acceptlist:
                            acceptlist.append(acc)
                    if opts.debug:
                        print acceptlist
                        """
                if all_acceptlist.sort() == acceptlist.sort():
                    if opts.pdebug:
                        print acceptlist,' ==? ',all_acceptlist
                    print '  TGBA-Accept'
                    accept = 1
            else:
                for node in scc.nodes():
                    if 'accept' in node:
                        accept = 1
                        break

            if accept == 1:
                if opts.debug:
                    print '\t found BA/TGBA-Accept SCC'
                code_time = runsmt.gen_smtcodelist_time(scc, mp_prop, opts)
                results['AccSCCs'].append({'SMTCode':code_time['code'],
                                           'edges':nx.number_of_edges(scc), 'nodes':nx.number_of_nodes(scc),
                                           'gen_time':code_time['time'], 'sat_time':0})
            else:
                if opts.pdebug:
                    print ' non accept SCC'
    return results
Ejemplo n.º 52
0
def make_DAG(digraph, key_node=None):
    '''Make out a DAG. 
    Only one node in each cycle is kept in the original graph. That node is used as the key of cycle subgraphs. 
    key_node!=None indicates selecting the minimal one among all nodes of the cycle per key_node.
    Otherwise which one being selected is an implementation specific behavior.
    Note: Selfloop edges will be stripped silently.
    '''
    # output_graph(digraph)
    cycles = dict()
    dict_node2cycle = dict()
    for node in digraph.nodes_iter():
        dict_node2cycle[node] = None
    # Strip all selfloop edges silently.
    digraph.remove_edges_from(digraph.selfloop_edges())
    subgraphs = sorted(nx.strongly_connected_component_subgraphs(digraph), key=len, reverse=True)
    for ind in range(len(subgraphs)-1, -1, -1):
        subgraph = subgraphs[ind]
        if(subgraph.number_of_nodes()==1):
            # Selfloop edges have been stripped.
            assert(subgraph.number_of_edges()==0)
            del subgraphs[ind]
        else:
            nodes = subgraph.nodes()
            if(key_node):
                min_node = min(nodes, key=key_node)
            else:
                min_node = nodes[0]
            cycles[min_node] = subgraph
            for node in nodes:
                dict_node2cycle[node] = min_node
    for min_node in cycles:
        nodes = cycles[min_node].nodes()
        nodes.remove(min_node)
        # print 'min_node: %s, other nodes: ' % str(min_node), ' '.join(map(str, nodes))
        for node in nodes:
            pre_nodes = digraph.predecessors(node)
            suc_nodes = digraph.successors(node)
            for pre_node in pre_nodes:
                if(pre_node==min_node or (pre_node in nodes) or digraph.has_edge(pre_node, min_node)):
                    continue
                digraph.add_edge(pre_node, min_node)
            for suc_node in suc_nodes:
                if(suc_node==min_node or (suc_node in nodes) or digraph.has_edge(min_node, suc_node)):
                    continue
                digraph.add_edge(min_node, suc_node)
            # All edges assiciated with a node will also be removed when removing the node from the graph.
            digraph.remove_node(node)
    return(cycles, dict_node2cycle)
Ejemplo n.º 53
0
 def _sort_chunks_by_build_order(self, graph):
     order = reversed(sorted(graph.nodes()))
     try:
         return networkx.topological_sort(graph, nbunch=order)
     except networkx.NetworkXUnfeasible:
         # Cycle detected!
         loop_subgraphs = networkx.strongly_connected_component_subgraphs(
             graph, copy=False)
         all_loops_str = []
         for graph in loop_subgraphs:
             if graph.number_of_nodes() > 1:
                 loops_str = '->'.join(str(node) for node in graph.nodes())
                 all_loops_str.append(loops_str)
         raise cliapp.AppException(
             'One or more cycles detected in build graph: %s' %
             (', '.join(all_loops_str)))
Ejemplo n.º 54
0
def compute_subgraphs(graph = None, graph_file = None, save_base = None):
	if graph is None:
		G = nx.read_graphml(graph)
	else:
		G = graph 

	subgraphs = nx.strongly_connected_component_subgraphs(G)
	subgraph_list = []
	for subgraph in subgraphs:
		subgraph_list.append(subgraph)
	subgraphs = subgraph_list
	if save_base is not None:
		for i in range(0,len(subgraphs)):
			subgraph = subgraphs[i]
			graph_file = "%s%d.graphml" %(save_base, i)
			nx.write_graphml(subgraph, graph_file)
	return subgraphs
Ejemplo n.º 55
0
def meanVVdistance(g):
    'Mean Vertex-Vertex distance'

    giant = nx.strongly_connected_component_subgraphs(g)[0]
    mean = 0
    #spd = nx.shortest_path_length
    spd = nx.dijkstra_path_length
    nodes = giant.nodes()
    print " * max cluster #nodes: %d" % len(nodes)
    for i in range(len(nodes)):
        if i % 100 == 0:
            print "Step", i
        source = nodes[i]
        for j in range(len(nodes)):
            dest = nodes[j]
            mean += spd(g, source, dest)

    return (mean*1. / len(giant) / (len(giant)-1))
Ejemplo n.º 56
0
    def __trim(self, G):
        """trim unnecessary edges which are included in no cycle before generating a subproblem
        Cycle can be found by strongly connected component decomposition

        :G: @todo
        :returns: @todo

        """
        sccs = networkx.strongly_connected_component_subgraphs(G)

        edgesRemain = set()
        for subgraph in sccs:
            edgesRemain = set.union(edgesRemain, set(subgraph.edges()))

        for link in G.edges():
            if (link[0], link[1]) not in edgesRemain:
                G.remove_edge(link[0], link[1])

        return G
Ejemplo n.º 57
0
def find_SCCs(mdp, Sneg):
    #----simply find strongly connected components----
    print 'Remaining states size', len(Sneg)
    SCC  = set()
    simple_digraph = DiGraph()
    A = dict()
    for s in mdp.nodes():
        A[s] = mdp.node[s]['act'].copy()
    for s_f in Sneg:
        if s_f not in simple_digraph:
            simple_digraph.add_node(s_f)
        for s_t in mdp.successors_iter(s_f):
            if s_t in Sneg:
                simple_digraph.add_edge(s_f,s_t)
    print "SubGraph of one Sf: %s states and %s edges" %(str(len(simple_digraph.nodes())), str(len(simple_digraph.edges())))
    sccs = strongly_connected_component_subgraphs(simple_digraph)
    for scc in sccs:
        SCC.add(frozenset(scc.nodes()))    
    return SCC, A
Ejemplo n.º 58
0
    def _parse_loops_from_graph(self, graph):
        """
        Return all Loop instances that can be extracted from a graph.

        :param graph:   The graph to analyze.

        :return:        A list of all the Loop instances that were found in the graph.
        """
        outtop = []
        outall = []
        for subg in networkx.strongly_connected_component_subgraphs(graph):
            if len(subg.nodes()) == 1:
                if len(subg.successors(subg.nodes()[0])) == 0:
                    continue
            thisloop, allloops = self._parse_loop_graph(subg, graph)
            if thisloop is not None:
                outall += allloops
                outtop.append(thisloop)
        return outtop, outall