示例#1
0
def read_graph(filename,gt_file):

    with open(gt_file) as f:
        read_dict=json.load(f)
    g = nx.DiGraph()

    with open (filename) as f:
        for lines in f:
            lines1=lines.split()
            g.add_node(lines1[0] + "_" + lines1[2])
            g.add_node(lines1[1] + "_" + lines1[3])
            if lines1[0] in read_dict:
                g.node[lines1[0] + "_" + lines1[2]]['aln_start']=min(read_dict[lines1[0]][0][0],read_dict[lines1[0]][0][1])
                g.node[lines1[0] + "_" + lines1[2]]['aln_end']=max(read_dict[lines1[0]][0][0],read_dict[lines1[0]][0][1])
            else:
                g.node[lines1[0] + "_" + lines1[2]]['aln_start']=0
                g.node[lines1[0] + "_" + lines1[2]]['aln_end']=0
            if lines1[1] in read_dict:
                g.node[lines1[1] + "_" + lines1[3]]['aln_start']=min(read_dict[lines1[1]][0][0],read_dict[lines1[1]][0][1])
                g.node[lines1[1] + "_" + lines1[3]]['aln_end']=max(read_dict[lines1[1]][0][0],read_dict[lines1[1]][0][1])
            else:
                g.node[lines1[1] + "_" + lines1[3]]['aln_start']=0
                g.node[lines1[1] + "_" + lines1[3]]['aln_end']=0

            g.node[lines1[0] + "_" + lines1[2]]['active']=1
            g.node[lines1[1] + "_" + lines1[3]]['active']=int(lines1[4])
            g.add_edge(lines1[0] + "_" + lines1[2], lines1[1] + "_" + lines1[3])
    
    
    nx.write_graphml(g, filename.split('.')[0]+'_hgraph.graphml')
    
    print nx.number_weakly_connected_components(g)
    print nx.number_strongly_connected_components(g)
示例#2
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))))
示例#3
0
def addTracks(artist, tracks, artistGraph):
	for track in tracks:
	# get list of users who have favorited this user's track
		favoriters = client.get('/tracks/' + str(track) + '/favoriters')
		print "Add Favoriters"
		for user in favoriters:
			addWeight(user.id, artist, artistGraph, 'fav_weight')
		try:
			print "Writing out new artists..."
			nx.write_pajek(artistGraph, 'artistGraph.net')
			print "New artists written successfully!"
			print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
			print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
		except IOError:
			print "New artists could not be written..."
	
	# get list of users who have commented on this user's track			
		commenters = client.get('/tracks/' + str(track) + '/comments')
		print "Add Commenters"
		for user in commenters:
			addWeight(user.user_id, artist, artistGraph, 'com_weight')
		try:
			print "Writing out new artists..."
			nx.write_pajek(artistGraph, 'artistGraph.net')
			print "New artists written successfully!"
			print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
			print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
		except IOError:
			print "New artists could not be written..."			
示例#4
0
def generate_oriented(graph, X):
    target_feats = np.empty((X.shape[0], 5))
    source_feats = np.empty((X.shape[0], 5))
    edge_feats = np.empty((X.shape[0], 11))
    l = X.shape[0]
    t1 = time()
    for i, x in enumerate(X):
        t = x[0]
        s = x[1]
        in_d_t, out_d_t, scc_t, wcc_t, sccp_t, n_in_t, n_out_t, n_t, np_t = all_oriented_vertex(
            graph, t)
        in_d_s, out_d_s, scc_s, wcc_s, sccp_s, n_in_s, n_out_s, n_s, np_s = all_oriented_vertex(
            graph, s)
        com_in = len(set(n_in_t).intersection(n_in_s))
        com_on = len(set(n_out_t).intersection(n_out_s))
        trans_ts = len(set(n_out_t).intersection(n_in_s))
        trans_st = len(set(n_out_s).intersection(n_in_t))
        friends_measure_st = 0
        friends_measure_ts = 0
        for ns in n_s:
            for nt in n_t:
                if graph.has_edge(ns, nt):
                    friends_measure_st += 1
                if graph.has_edge(nt, ns):
                    friends_measure_ts += 1
        nh = list(set(n_t).union(n_s))
        nh_plus = list(set(np_t).union(np_s))
        sub_nh = graph.subgraph(nh)
        sub_nh_plus = graph.subgraph(nh_plus)
        scc = nx.number_strongly_connected_components(sub_nh)
        wcc = nx.number_weakly_connected_components(sub_nh)
        scc_plus = nx.number_strongly_connected_components(sub_nh_plus)
        if not nx.has_path(graph, s, t):
            len_path_st = -1
        else:
            len_path_st = nx.shortest_path_length(graph, s, t)
        if not nx.has_path(graph, t, s):
            len_path_ts = -1
        else:
            len_path_ts = nx.shortest_path_length(graph, t, s)
        target_feats[i] = [in_d_t, out_d_t, scc_t, wcc_t, sccp_t]
        source_feats[i] = [in_d_s, out_d_s, scc_s, wcc_s, sccp_s]
        edge_feats[i] = [
            com_in, com_on, trans_ts, trans_st, friends_measure_st,
            friends_measure_ts, scc, wcc, scc_plus, len_path_st, len_path_ts
        ]
        if i % 10000 == 0:
            print(i, l)
            t2 = time()
            print(t2 - t1)
            t1 = t2
    return target_feats, source_feats, edge_feats
示例#5
0
def all_oriented_vertex(graph, v):
    neighbors_in = graph.predecessors(v)
    neighbors_out = graph.successors(v)
    neighbors = list(set(neighbors_in).union(neighbors_out))
    neighbors_plus = neighbors + [v]
    subgraph = graph.subgraph(neighbors)
    subgraph_plus = graph.subgraph(neighbors_plus)
    scc = nx.number_strongly_connected_components(subgraph)
    wcc = nx.number_weakly_connected_components(subgraph)
    scc_plus = nx.number_strongly_connected_components(subgraph_plus)
    #sub_edge_num = count(graph, neighbors)
    #sub_edge_num_plus = count(graph, neighbors_plus)
    return graph.in_degree(v), graph.out_degree(
        v
    ), scc, wcc, scc_plus, neighbors_in, neighbors_out, neighbors, neighbors_plus
示例#6
0
def DNetworkSummary(D, qtr, filename=None):
    """Just outputs and prints a quick table of DIRECTED network statistics."""
    ##  Calculate all the network summary stats
    n = D.number_of_nodes()
    e = D.size()
    e_c = D.size(weight='calls')
    e_min = D.size(weight='min')
    e_sms = D.size(weight='sms')
    e_mms = D.size(weight='mms')
    n_scc = nx.number_strongly_connected_components(D)
    r_scc = relativeLSCCsize(D)
    n_wcc = nx.number_weakly_connected_components(D)
    r_wcc = relativeLWCCsize(D)

    ##  Description vector for printout and output file
    ts = "    "  # just so the output file is a little more readable
    a1 = "Directed Network Statistics -- Quarter "
    a2 = ts + "Number of nodes: "
    a3 = ts + "Number of edges (unweighted): "
    a4 = ts + ts + "Number of edges (weighted by calls): "
    a5 = ts + ts + "Number of edges (weighted by minutes): "
    a6 = ts + ts + "Number of edges (weighted by SMS): "
    a7 = ts + ts + "Number of edges (weighted by MMS): "
    a8 = ts + "Number of Strongly Connected Components (SCC): "
    a9 = ts + ts + "Relative size of largest SCC: "
    a10 = ts + "Number of Weakly Connected Components (WCC): "
    a11 = ts + ts + "Relative size of largest WCC: "
    name = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11]
    result = [qtr, n, e, e_c, e_min, e_sms, e_mms, n_scc, r_scc, n_wcc, r_wcc]
    outputresults = pd.DataFrame(data={'Description': name, 'Result': result})
    if (filename is not None):
        outputresults.to_csv(filename, index=False)
    print outputresults
示例#7
0
 def test_graph_has_isolated_components(self):
     self.setUp()
     scc_G = nx.condensation(self.G)
     scc_G_nodes = nx.number_of_nodes(scc_G)
     scc_G_comp = nx.number_strongly_connected_components(scc_G)
     self.assertEqual(scc_G_comp, scc_G_nodes,
                      'Connected Components are not isolated')
示例#8
0
def gen_graph(n, prob=0.5):
    while True:
        G = nx.binomial_graph(n, prob, None, True)
        if nx.number_strongly_connected_components(G) == 1:
            break

    return G
示例#9
0
    def test_4d(self):
        g = NDTorusTopology(4, [4, 5, 6, 7]).generate()

        assert len(g) == 840
        assert all([d == 8 for d in g.in_degree().values()])
        assert all([d == 8 for d in g.out_degree().values()])
        assert nx.number_strongly_connected_components(g) == 1
示例#10
0
    def test_1d(self):
        g = NDTorusTopology(1, [4]).generate()

        assert len(g) == 4
        assert all([d == 2 for d in g.in_degree().values()])
        assert all([d == 2 for d in g.out_degree().values()])
        assert nx.number_strongly_connected_components(g) == 1
示例#11
0
def compares_run_time_cc_json(file_name: str) -> list:
    """
    Compares the running time of the Connected components method in GraphAlgo  and Networkx
    The graph is created from json file
    :param file_name:
    :return: list , the first place is the results of nx graph and the second is the results of GraphAlgo
    """
    graph_algo = GraphAlgo()
    graph_algo.load_from_json(file_name)  # load the json
    g = nx.Graph()
    nx_graph = g.to_directed()
    for node in graph_algo.get_graph().get_all_v().values(
    ):  # copies all data to nx_graph
        nx_graph.add_node(node.key, pos=node.pos)
    for edges in graph_algo.get_graph().Edges.values():
        for edge in edges.values():
            nx_graph.add_edge(edge.src, edge.dest, weight=edge.weight)
    start_time = datetime.now()  # cc run time of networkx
    nx_results = nx.number_strongly_connected_components(nx_graph)
    end_time = datetime.now()
    run_time_nx = end_time - start_time
    print("run time of networkx graph:", run_time_nx)
    start_time = datetime.now()  # cc run time of GraphAlgo
    my_results = len(graph_algo.connected_components())
    end_time = datetime.now()
    run_time_my = end_time - start_time
    print("run time of GraphAlgo :", run_time_my)
    return [nx_results, my_results]
示例#12
0
        def graph_summary(graph):
            # NaN assortatitvity: https://groups.google.com/forum/#!topic/networkx-discuss/o2zl40LMmqM
            def assortativity(g):
                try:
                    return nx.degree_assortativity_coefficient(g)
                except ValueError:
                    return None

            summary_df = pd.DataFrame(data={
                'no_nodes':
                graph.number_of_nodes(),
                'no_edges':
                graph.number_of_edges(),
                'avg_degree':
                sum([x[1] for x in graph.degree()]) / graph.number_of_nodes(),
                'avg_weighted_degree':
                sum([x[1] for x in graph.degree(weight='weight')]) /
                graph.number_of_nodes(),
                'density':
                nx.density(graph),
                'connected':
                nx.is_weakly_connected(graph),
                'strongly_conn_components':
                nx.number_strongly_connected_components(graph),
                'avg_clustering':
                nx.average_clustering(graph),
                'assortativity':
                assortativity(graph)
            },
                                      index=[0]).round(4)

            return summary_df
示例#13
0
def checkconnectednodes(G):
    try:
        num_strongly_connected_components = nx.number_strongly_connected_components(
            G)
        print("Strongly Connected - ", num_strongly_connected_components)
        comps = utility.findstrongconnection(G)
        component_sizes = []
        for nodes in comps:
            component_sizes.extend([len(nodes)])
        component_sizes.sort(reverse=True)
        print("Biggest connected : ", component_sizes[0])
        indegrees = G.in_degree().values()
        incount = 0
        for degree in indegrees:
            if degree == 0:
                incount += 1
        print("In degree - ", incount)

        outdegrees = G.out_degree().values()
        outcount = 0
        for degree in outdegrees:
            if degree == 0:
                outcount += 1
        print("Outdegree = ", outcount)

    except Exception as e:
        print("Exception: - ", e)
def draw_graph(nodes, edges, graphs_dir, default_lang='all'):
    lang_graph = nx.MultiDiGraph()
    lang_graph.add_nodes_from(nodes)
    for edge in edges:
        if edges[edge] == 0:
            lang_graph.add_edge(edge[0], edge[1])
        else:
            lang_graph.add_edge(edge[0], edge[1], weight=float(edges[edge]), label=str(edges[edge]))

    # print graph info in stdout
    # degree centrality
    print('-----------------\n\n')
    print(default_lang)
    print(nx.info(lang_graph))
    try:
        # When ties are associated to some positive aspects such as friendship or collaboration,
        #  indegree is often interpreted as a form of popularity, and outdegree as gregariousness.
        DC = nx.degree_centrality(lang_graph)
        max_dc = max(DC.values())
        max_dc_list = [item for item in DC.items() if item[1] == max_dc]
    except ZeroDivisionError:
        max_dc_list = []
    # https://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D0%BC%D0%BF%D0%BB%D0%B5%D0%BA%D1%81%D0%BD%D1%8B%D0%B5_%D1%81%D0%B5%D1%82%D0%B8
    print('maxdc', str(max_dc_list), sep=': ')
    # assortativity coef
    AC = nx.degree_assortativity_coefficient(lang_graph)
    print('AC', str(AC), sep=': ')
    # connectivity
    print("Слабо-связный граф: ", nx.is_weakly_connected(lang_graph))
    print("количество слабосвязанных компонент: ", nx.number_weakly_connected_components(lang_graph))
    print("Сильно-связный граф: ", nx.is_strongly_connected(lang_graph))
    print("количество сильносвязанных компонент: ", nx.number_strongly_connected_components(lang_graph))
    print("рекурсивные? компоненты: ", nx.number_attracting_components(lang_graph))
    print("число вершинной связности: ", nx.node_connectivity(lang_graph))
    print("число рёберной связности: ", nx.edge_connectivity(lang_graph))
    # other info
    print("average degree connectivity: ", nx.average_degree_connectivity(lang_graph))
    print("average neighbor degree: ", sorted(nx.average_neighbor_degree(lang_graph).items(),
                                              key=itemgetter(1), reverse=True))
    # best for small graphs, and our graphs are pretty small
    print("pagerank: ", sorted(nx.pagerank_numpy(lang_graph).items(), key=itemgetter(1), reverse=True))

    plt.figure(figsize=(16.0, 9.0), dpi=80)
    plt.axis('off')
    pos = graphviz_layout(lang_graph)
    nx.draw_networkx_edges(lang_graph, pos, alpha=0.5, arrows=True)
    nx.draw_networkx(lang_graph, pos, node_size=1000, font_size=12, with_labels=True, node_color='green')
    nx.draw_networkx_edge_labels(lang_graph, pos, edges)

    # saving file to draw it with dot-graphviz
    # changing overall graph view, default is top-bottom
    lang_graph.graph['graph'] = {'rankdir': 'LR'}
    # marking with blue nodes with maximum degree centrality
    for max_dc_node in max_dc_list:
        lang_graph.node[max_dc_node[0]]['fontcolor'] = 'blue'
    write_dot(lang_graph, os.path.join(graphs_dir, default_lang + '_links.dot'))

    # plt.show()
    plt.savefig(os.path.join(graphs_dir, 'python_' + default_lang + '_graph.png'), dpi=100)
    plt.close()
示例#15
0
    def component(self):
        rslt = {}
        if self.directed == 'directed':
            rslt['is_strongly_connected'] = nx.is_strongly_connected(
                self.graph)

            strong = nx.strongly_connected_components(self.graph)
            strong_nodes = []
            for n in strong:
                strong_nodes.append(list(n)[0])
            rslt['strongly_connected'] = strong_nodes

            rslt[
                'number_strongly_connected_components'] = nx.number_strongly_connected_components(
                    self.graph)
            rslt['is_semiconnected'] = nx.is_semiconnected(self.graph)

            weak = nx.weakly_connected_components(self.graph)
            weak_nodes = []
            for n in weak:
                weak_nodes.append(list(n)[0])
            rslt['wealy_connected'] = weak_nodes

            rslt['is_weakly_connected'] = nx.is_weakly_connected(self.graph)
            rslt[
                'number_weakly_connected_components'] = nx.number_weakly_connected_components(
                    self.graph)

        fname_component = self.DIR + '/component.json'
        with open(fname_component, "w") as f:
            json.dump(rslt, f, cls=SetEncoder, indent=2)
        print(fname_component)
示例#16
0
def save_network_statistics(g):
    stats = {}
    stats[
        'num_weakly_connected_components'] = nx.number_weakly_connected_components(
            g)
    stats[
        'num_strongly_connected_components'] = nx.number_strongly_connected_components(
            g)
    stats['num_nodes'] = nx.number_of_nodes(g)
    stats['num_edges'] = nx.number_of_edges(g)
    stats['density'] = nx.density(g)
    try:
        stats['avg_clustering_coef'] = nx.average_clustering(g)
    except:
        stats['avg_clustering_coef'] = None  # not defined for directed graphs
    stats['avg_degree'] = sum(g.degree().values()) / float(stats['num_nodes'])
    stats['transitivity'] = nx.transitivity(g)
    try:
        stats['diameter'] = nx.diameter(g)
    except:
        stats[
            'diameter'] = None  # unconnected --> infinite path length between connected components

    with open('./network-statistics/twitter-combined-statistics.txt',
              'wb') as f:
        for stat_name, stat_value in stats.iteritems():
            f.write(stat_name + ': ' + str(stat_value) + '\n')
示例#17
0
def analyse_strongly_connected_components(G):
    try:
        num_strongly_connected_components = nx.number_strongly_connected_components(
            G)
        print("Strongly Connected Component Count - ",
              num_strongly_connected_components)
        comps = helpers.get_strongly_connected_components(G)
        Gcc = comps[0]
        print("Biggest connected component size: ", len(Gcc))

        # Get in degree
        incount = 0
        for node in G.nodes_iter():
            if node not in Gcc:
                found = False
                for successor in G.successors(node):
                    if successor in Gcc and not found:
                        incount += 1
                        found = True
        print("In degree - ", incount)

        # Get the out degree
        outcount = 0
        for node in Gcc:
            # Count number of connections that are not in teh GCC > 0 increments outcount
            node_out = 0
            for neighbour in G.neighbors(node):
                if neighbour not in Gcc:
                    node_out += 1
            if node_out > 0:
                outcount += 1
        print("Outdegree = ", outcount)

    except Exception as e:
        print("Exception: - ", e)
def graph_structure_info(G, filename, df):
    #degree_out  = pd.DataFrame([d for n,d in G.out_degree()]) # 这里定义了out_degree
    degree_in = pd.DataFrame([d for n, d in G.in_degree()])  # 这里定义了out_degree
    df1 = pd.DataFrame(
        data={
            'date': [filename],
            'nodes_num': [G.number_of_nodes()],
            'edges_num': [G.number_of_edges()],
            'density': [nx.density(G)],
            'number_weakly_connected_components':
            [nx.number_weakly_connected_components(G)],
            'number_strongly_connected_components':
            [nx.number_strongly_connected_components(G)],
            #'size_largest_strongly_connected_components':[len(max(nx.strongly_connected_components(G), key=len))],
            #'size_largest_weakly_connected_components':[len(max(nx.weakly_connected_components(G), key=len))],
            'weights_sum':
            [sum(list((nx.get_edge_attributes(G, 'weight')).values()))],
            #'ave_degree_out':[degree_out.mean()], #入度均值=出度均值
            'ave_degree': [degree_in.mean()]
        })
    #'ave_clustering_coeffient':[nx.average_clustering(G)],
    #'ave_shortest_path_length':[nx.average_shortest_path_length(G)]
    #print df1
    df = df.append(df1)
    print "this year graph_basic_info is done"
    return df
def checker(path_to_input, path_to_output):
    true_output = {}
    user_output = {}

    with open(path_to_input, 'r') as f:
        n, m, t = map(int, f.readline().split())
        if t == 3 or t == 4:
            G = nx.Graph()
        else:
            G = nx.DiGraph()
        for _ in range(m):
            if t == 1:
                i, j = map(int, f.readline().split())
                G.add_edge(i, j)
            elif t == 2:
                i, j, k = map(int, f.readline().split())
                G.add_edge(i, j, weight=k)
            elif t == 3:
                i, j = map(int, f.readline().split())
                G.add_edge(i, j)
            elif t == 4:
                i, j, k = map(int, f.readline().split())
                G.add_edge(i, j, weight=k)
    with open(path_to_output, 'r') as f:
        output_len = int(f.readline())
        user_output['n'] = output_len

    true_output['n'] = nx.number_strongly_connected_components(G)

    return user_output, true_output, user_output['n'] == true_output['n']
示例#20
0
def save_network_statistics(g):
    degrees = g.degree

    nn = [i[1] for i in degrees]

    stats = {}
    stats[
        'num_weakly_connected_components'] = nx.number_weakly_connected_components(
            g)
    stats[
        'num_strongly_connected_components'] = nx.number_strongly_connected_components(
            g)
    stats['num_nodes'] = nx.number_of_nodes(g)
    stats['num_edges'] = nx.number_of_edges(g)
    stats['density'] = nx.density(g)
    try:
        stats['avg_clustering_coef'] = nx.average_clustering(g)
    except:
        stats['avg_clustering_coef'] = None  # not defined for directed graphs
    stats['avg_degree'] = sum(nn) / float(stats['num_nodes'])
    stats['transitivity'] = nx.transitivity(g)
    try:
        stats['diameter'] = nx.diameter(g)
    except:
        stats[
            'diameter'] = None  # unconnected --> infinite path length between connected components

    with open('D:\\fingerprint-statistics-undirected.txt', 'w') as f:
        for stat_name, stat_value in stats.items():
            temp = str(stat_value)
            f.write(stat_name + ': ' + temp + '\n')
示例#21
0
def plot_num_components_directed(G_times, fname):
	max_time = len(G_times)
	t = list(range(0, max_time))
	num_strong = []
	num_weak = []

	for G in G_times:
		num_strong.append(nx.number_strongly_connected_components(G))
		num_weak.append(nx.number_weakly_connected_components(G))

	plt.rcParams.update({'figure.autolayout': True})
	plt.rc('xtick', labelsize='x-small')
	plt.rc('ytick', labelsize='x-small')
	fig = plt.figure(figsize=(4, 2))
	ax = fig.add_subplot(1, 1, 1)
	ax.plot(t, num_strong, marker="P", color='#ffa600', ls='solid', linewidth=0.5, markersize=1, label="strongly")
	ax.plot(t, num_weak, marker="h", color='#003f5c', ls='solid', linewidth=0.5, markersize=1, label="weakly")
	ax.set_xlabel('time', fontsize=8)


	outliers = find_rarity_windowed_outlier(num_weak)		#use weakly 
	outliers.sort()
	for xc in outliers:
		plt.axvline(x=xc,color='k', linestyle=":", linewidth=0.5)
	# ax.set_xscale('log')
	# ax.set_yscale('log')
	ax.set_ylabel('number of connected components', fontsize=8)
	plt.title("number of connected components over time", fontsize='x-small')
	plt.legend(fontsize=5)
	plt.savefig(fname+'components.pdf', pad_inches=0)

	return outliers
示例#22
0
def connected_components(network):
    n_scc = nx.number_strongly_connected_components(network)
    n_wcc = nx.number_weakly_connected_components(network)

    print("# of strongly connected components: " + str(n_scc))
    print("# of weakly connected components: " + str(n_wcc))
    n_scc = nx.strongly_connected_component_subgraphs(network)
    print(max(n_scc, key=len).number_of_edges())
示例#23
0
def connectivity_info(G):
    if nx.is_strongly_connected(G):
        print("A strongly connected graph.")
    elif nx.is_weakly_connected(G):
        print("A weakly connected graph.")
    else:
        num = nx.number_strongly_connected_components(G)
        print("Not a strongly connected graph, have {} component".format(num))
示例#24
0
文件: gfa2sg.py 项目: natir/knot
def print_info(G):
    logging.debug("begin transitiv reduction")
    logging.debug("nb edge : " + str(G.number_of_edges()))
    logging.debug("nb node : " + str(G.number_of_nodes()))
    logging.debug("nb strong components : " +
                  str(nx.number_strongly_connected_components(G)))
    logging.debug("nb weak components : " +
                  str(nx.number_weakly_connected_components(G)))
示例#25
0
def components(graph):
    print("Number of strongly connected components",
          nx.number_strongly_connected_components(graph))
    print("Number of weakly connected components",
          nx.number_weakly_connected_components(graph))

    condensation = nx.condensation(graph)
    nx.write_edgelist(condensation, "Datasets/condenced_graph.edgelist")
示例#26
0
def read_graph(G, gfile):
    print "Reading in artist graph..."
    try:
        G = nx.read_pajek(gfile)
        print "Read successfully!"
        print "The artist graph currently contains " + str(len(G)) + " artists."
        print "The artist graph currently contains " + str(nx.number_strongly_connected_components(G)) + " strongly connected components."
    except IOError:
        print "Could not find artistGraph"
示例#27
0
    def test_host(self):
        g = XGFTTopology(0, [], []).generate()

        assert len(g) == 1
        assert nx.number_strongly_connected_components(g) == 1
        assert nx.diameter(g) == 0

        hosts = [u for u, d in g.nodes(data=True) if d["typ"] == "host"]
        assert len(hosts) == 1
示例#28
0
def write_graph(G, gfile):
    try:
        print "Writing out new artists..."
        nx.write_pajek(G, gfile)
        print "New artists written successfully!"
        print "The artist graph currently contains " + str(len(G)) + " artists."
        print "The artist graph currently contains " + str(nx.number_strongly_connected_components(G)) + " strongly connected components."
    except IOError:
        print "New artists could not be written..."
def prog_27(fname):
    graph = nx.DiGraph()
    f = open(fname)

    ns,es = map(int, f.readline().strip().split())

    graph.add_nodes_from(range(1,ns+1))

    for line in f:
        e1,e2 = map(int, line.strip().split())
        graph.add_edge(e1,e2)

    f.close()

    print nx.is_strongly_connected(graph)
        # print 'xxxxxxxx'

    print nx.number_strongly_connected_components(graph)
示例#30
0
    def component_count(self) -> int:
        if self._component_count is None:
            if self.directed:
                self._component_count = number_strongly_connected_components(
                    self.graph)
            else:
                self._component_count = number_connected_components(self.graph)

        return self._component_count
def write_components_info(G, report_file):
    report_file.write("===COMPONENTS_INFO===\n")
    report_file.write("Number of strongly connected components: {}\n".format(
        nx.number_strongly_connected_components(G)))
    report_file.write("Number of weakly connected components: {}\n".format(
        nx.number_weakly_connected_components(G)))
    report_file.write("Number of attractive components: {}\n".format(
        nx.number_attracting_components(G)))
    report_file.write("Is semiconnected: {}\n".format(nx.is_semiconnected(G)))
示例#32
0
def make_acyclic_scc(G):
    reduced = nx.condensation(G, scc=None)
    SCC_list= list(nx.strongly_connected_components(G))
    Super_nodes = {}
    
    for i in range(int(nx.number_strongly_connected_components(G))):
        Super_nodes[i] = list(SCC_list[i])
    
    return reduced, Super_nodes     
示例#33
0
def get_graph_stats(df):
    edges = list(zip(df["src"],df["trg"]))
    G = nx.DiGraph()
    G.add_edges_from(edges)
    N = G.number_of_nodes()
    M = G.number_of_edges()
    wc_comp = nx.number_weakly_connected_components(G)
    sc_comp = nx.number_strongly_connected_components(G)
    return (N,M,wc_comp,sc_comp)
示例#34
0
def prune_opposing_edge(G, num_edges=1):
    '''Reads in a networkx digraph and prunes the edge opposing the most
    between (i.e. edge on the most shortest path connections).  If this edge
    doesn't have an opposing edge, or if the removal of said edge would
    reduce the connectivity of the space, the next most between edge is pruned
    instead.  Since computing completeness can be expensive, allows multiple
    edges to be pruned before computing the impact of said prunning on
    completeness is computed'''
    #Get the current number of strongly connected components, can't decrease
    #without preventing some paths from being found
    num_components = nx.number_strongly_connected_components(G)
    pruned = 0
    # print 'computing betweeness'
    betweenness = nx.edge_betweenness_centrality(G)
    # print 'betweenness computed'
    while pruned < num_edges:
        max_bet = max(betweenness.values())
        if max_bet <= 0:
            #Set betweeness to -1 if can't prune, set to 0 not between
            return G
        edge = betweenness.keys()[betweenness.values().index(max_bet)]
        if not (edge[1], edge[0]) in G.edges():
            #Already been pruned
            betweenness[edge] = -1
            # print 'no edge'
            continue
        #Test if pruning the edge will break connectivity
        temp_graph = G.copy()
        temp_graph.remove_edge(edge[1], edge[0])
        if num_components == nx.number_strongly_connected_components(
                temp_graph):
            #Can safely prune this edge
            G = temp_graph
            pruned += 1
            betweenness[edge] = -1
            betweenness.pop((edge[1], edge[0]))
            # print 'pruned'
            #Need to prevent further edges from being pruned from this vertex
            for neighbor in G.neighbors(edge[1]):
                betweenness[(edge[1], neighbor)] = -1
        else:
            betweenness[edge] = -1
            # print 'breaks con %s' %(str(edge))
    return G
示例#35
0
def read_graph(filename):

    g = nx.DiGraph()

    with open (filename) as f:
        for lines in f:
            lines1=lines.split()
            g.add_node(lines1[0] + "_" + lines1[2])
            g.add_node(lines1[1] + "_" + lines1[3])

            g.node[lines1[0] + "_" + lines1[2]]['active']=1
            g.node[lines1[1] + "_" + lines1[3]]['active']=int(lines1[4])
            g.add_edge(lines1[0] + "_" + lines1[2], lines1[1] + "_" + lines1[3])
    
    
    nx.write_graphml(g, filename.split('.')[0]+'_hgraph.graphml')
    
    print nx.number_weakly_connected_components(g)
    print nx.number_strongly_connected_components(g)
示例#36
0
 def test_connectedness(self):
     ntests = 50
     nsuccess = 0
     for test in range(ntests):
         numStates = npr.randint(100, 5000)
         numActions = npr.randint(2, 20)
         G = graphs.make_strongly_connected(graphs.rand_graph_uniform_degree(numStates, numActions))
         if nx.number_strongly_connected_components(G) == 1:
             nsuccess += 1
     self.assertEqual(nsuccess, ntests)
def output_conectivity_info (graph, path):
    """Output connectivity information about the graph.
       graph : (networkx.Graph)
       path: (String) contains the path to the output file
    """
    with open(path, 'w') as out:
        out.write('***Conectivity***\n')
        out.write('Is weakly connected: %s\n' % nx.is_weakly_connected(graph))
        out.write('Number of weakly connected components: %d\n' % nx.number_weakly_connected_components(graph))
        out.write('Is strongly connected: %s\n' % nx.is_strongly_connected(graph))
        out.write('Number of strongly connected components: %d' % nx.number_strongly_connected_components(graph))
示例#38
0
def addComments(artist, comments, artistGraph):
	print "Add Comments"
	for user in comments:
		addWeight(artist, user, artistGraph, 'com_weight')
	try:
		print "Writing out new artists..."
		nx.write_pajek(artistGraph, 'artistGraph.net')
		print "New artists written successfully!"
		print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
		print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
	except IOError:
		print "New artists could not be written..."	
示例#39
0
def choose_random_nodes(G, ntr = 1, n_edges = 1):
    '''
    Returns a random set of absorbing nodes, if the other nodes in the
    graph form a connected component after removing the absorbing nodes.
    Otherwise it returns None
    Parameters
    ----------
    G : Networkx graph
        The graph from which the team will be selected.
    ntr : the number of absorbing nodes
    Returns
    -------
    
    nodes_to_remove : 
        The list of nodes in t the graph to be made absorbing.
    is_viable:
        Boolean indicating whether the graph will stay connected after making
        the nodes absorbing, meaning wheather the partition is viable
    '''
    prng = RandomState()
    order = array(G.nodes())
    nodes_to_remove = list(prng.choice(order, ntr, replace=False ))
    
    H = G.copy()
    H.remove_nodes_from(nodes_to_remove)
    if G.is_directed():
        n_components = nx.number_strongly_connected_components(H)
    else:
        n_components =  nx.number_connected_components(H)
        
    if n_components == 1:
        is_viable = True
        if G.is_directed():
            for node in nodes_to_remove:
                if (H.number_of_nodes() - len(set(G.predecessors(node)) 
                                           - set(nodes_to_remove))) < n_edges:
                    is_viable = False
                    break  
        else:
            for node in nodes_to_remove:
                if (H.number_of_nodes() - len(set(G.neighbors(node)) 
                                           - set(nodes_to_remove))) < n_edges:
                    is_viable = False
                    break   
        return nodes_to_remove, is_viable
        
    else:
        is_viable = False
        return nodes_to_remove, is_viable
示例#40
0
def analyse_graph(graph):
    print "graph"
    print "{0} nodes, {1} edges in macrogenetic graph.".format(graph.number_of_nodes(),
                                                                     graph.number_of_edges())
    absolutely_dated_notes_sorted = macrogenesis.absolutely_dated_nodes_sorted(graph)
    print "{0} nodes with absolute datings.".format(len(absolutely_dated_notes_sorted))
    num_equal_absolute_datings = macrogenesis.insert_minimal_edges_from_absolute_datings(graph)
    print "{0} equal absolute datings.".format(num_equal_absolute_datings)

    print "{0} strongly connected components (conflicts).".format(networkx.number_strongly_connected_components(graph))

    transitive_closure = networkx.transitive_closure(graph)
    print "transitive closure"
    print "{0} nodes, {1} edges in transtive closure.".format(transitive_closure.number_of_nodes(),
                                                                     transitive_closure.number_of_edges())
def save_network_statistics(g):
    stats = {}
    stats['num_weakly_connected_components'] = nx.number_weakly_connected_components(g)
    stats['num_strongly_connected_components'] = nx.number_strongly_connected_components(g)
    stats['num_nodes'] = nx.number_of_nodes(g)
    stats['num_edges'] = nx.number_of_edges(g)
    stats['density'] = nx.density(g)
    try:
        stats['avg_clustering_coef'] = nx.average_clustering(g)
    except:
        stats['avg_clustering_coef'] = None # not defined for directed graphs
    stats['avg_degree'] = sum(g.degree().values()) / float(stats['num_nodes'])
    stats['transitivity'] = nx.transitivity(g)
    try:
        stats['diameter'] = nx.diameter(g)
    except:
        stats['diameter'] = None # unconnected --> infinite path length between connected components

    with open('./network-statistics/twitter-combined-statistics.txt', 'wb') as f:
        for stat_name, stat_value in stats.iteritems():
            f.write(stat_name + ': ' + str(stat_value) + '\n')
示例#42
0
def graph_statistics(macrogenetic_graph):
    """Print statistics about the macrogenenesis graph"""

    output_dir = faust.config.get("macrogenesis", "output-dir")
    statistics_file_name = 'statistics.txt'
    with open(os.path.join(output_dir, statistics_file_name), mode='w') as statistics_file:
        statistics_file.write("graph\n")
        statistics_file.write("{0} nodes, {1} edges in macrogenetic graph.\n".format(macrogenetic_graph.number_of_nodes(),
                                                                                     macrogenetic_graph.number_of_edges()))
        absolutely_dated_notes_sorted = graph._absolutely_dated_nodes_sorted(macrogenetic_graph)
        statistics_file.write("{0} nodes with absolute datings.\n".format(len(absolutely_dated_notes_sorted)))
        num_equal_absolute_datings = graph.insert_minimal_edges_from_absolute_datings(macrogenetic_graph)
        statistics_file.write("{0} equal absolute datings.\n".format(num_equal_absolute_datings))

        statistics_file.write("{0} strongly connected components (conflicts).\n".format(
            networkx.number_strongly_connected_components(macrogenetic_graph)))

        transitive_closure = networkx.transitive_closure(macrogenetic_graph)
        statistics_file.write("\ntransitive closure\n")
        statistics_file.write("{0} nodes, {1} edges in transitive closure.\n".format(transitive_closure.number_of_nodes(),
                                                                      transitive_closure.number_of_edges()))
    return [("Graph statistics", statistics_file_name)]
示例#43
0
def gatherAllNGramsFromArticles(listOfArticles, pathToArticles):

    articleInvarints = []
    articlesProcessed = 0

    for currentFileName in listOfArticles:
        articlesProcessed+=1
        if articlesProcessed%100 == 0:
            print "Ngrams gathered from: ", articlesProcessed


        ngram_index = 0
        ngramsDictionary = dict()

        with open(pathToArticles + currentFileName) as currentFile:

            content = currentFile.read()
            ngramGraph = nx.DiGraph()
            ngramVector = []

            contentCleared = ""
            for word in content.split():
                cleanedWord = cleaningOfWord(word)
                if cleanedWord is not None:
                    contentCleared+=cleanedWord + " "

            text = contentCleared.split(" ")
            lastNode = None
            for k in range (0, len(text)-NGRAM_SIZE+1):
                window = ""
                start = k
                end = k + NGRAM_SIZE
                for i in range (start, end):
                    if window=="":
                        window = text[i]
                    else:
                        window = window + " " + text[i]


                if ngramsDictionary.has_key(window):
                    node = ngramsDictionary[window]
                else:
                    node = ngram_index
                    ngramsDictionary[window] = ngram_index
                    ngram_index += 1

                if lastNode:
                    ngramGraph.add_edge(lastNode, node)

                ngramVector.append(node)
                lastNode = node


            undirectedGraph = nx.Graph(ngramGraph)

            numberOfNodes = ngramGraph.number_of_nodes()
            numberOfEdges = ngramGraph.number_of_edges()
            connectedComponents = nx.number_strongly_connected_components(ngramGraph)
            try:
                averageClustering = nx.average_clustering(undirectedGraph)
            except:
                averageClustering  = 0
                print "Error with ", currentFileName, " nodes: ", numberOfNodes, " edges ", numberOfEdges

            kurt = kurtosis(ngramVector)
            skewness = skew(ngramVector)

            articleInvarints.append((
                numberOfNodes,
                numberOfEdges,
                connectedComponents,
                averageClustering,
                kurt,
                skewness
            ))


    return articleInvarints
示例#44
0
print "Here are their connections."

for artist in artistGraph.nodes():
    if artist:
        username = scac.id2username(artist)
        followings = artistGraph.successors(artist)
        followers = artistGraph.predecessors(artist)
        print "\t", username + " has " + str(len(followings)) + " followings"
        print "\t", username + " follows " + ", ".join(map(lambda x: scac.id2username(x), followings))
        print "\t", username + " has " + str(len(followers)) + " followers"
        print "\t", username + " is followed by " + ", ".join(map(lambda x: scac.id2username(x), followers))
        print "-" * 40

print "The artist graph currently contains " + str(
    nx.number_strongly_connected_components(artistGraph)
) + " strongly connected components."

nx.write_graphml(artistGraph, "artistGraph.graphml")

# Go through the graph and compute each PR until it converges.
iterations = 10
print "Computing PageRank on our artistGraph..."
computePR(artistGraph, 0.85, iterations)

prList = []

for artist in artistGraph.nodes():
    prList.append((artist, artistGraph.node[artist]["currPR"]))

prList.sort(key=lambda tup: tup[1])  # Sort the list in place
示例#45
0
def scc_test(G):
    return digraph_scc_count(G), nx.number_strongly_connected_components(G)
示例#46
0
文件: gquota.py 项目: huxiaoqian/case
def compute_quota(G, gg, date, windowsize, topic, all_uid_pr, network_type):
    prekey = _utf8_unicode(topic)+'_'+str(date)+'_'+str(windowsize)
    #print 'prekey:', prekey.encode('utf-8')
    #print 'G_nodes:',len(G.nodes())
    #print 'gg_nodes:', len(gg.nodes())
    #无向图的最大连通子图
    

    G_edges = G.edges()
    print 'G_edges:',len(G_edges)
    '''
    nodes_list = G.nodes()
    l = len(nodes_list)
    print 'l:', l
    r = random.randint(0,l-1)
    print 'r:', r
    bfs_edges = list(nx.bfs_edges(gg,nodes_list[r]))
    print 'bfs_edges:', bfs_edges
    print 'len(bfs_edges):', len(bfs_edges)
    '''
    degree = G.degree()
    print 'degree_counter'
    degree_test = get_counter(degree)

    indegree = G.in_degree()
    #print 'indegree:', indegree
    indegree_histogram = get_counter(indegree)
    save_quota(prekey+'_indegree_histogram_'+str(network_type), json.dumps(indegree_histogram))
    outdegree = G.out_degree()
    #print 'outdegree:', outdegree
    outdegree_histogram = get_counter(outdegree)
    save_quota(prekey+'_outdegree_histogram_'+str(network_type), json.dumps(outdegree_histogram))
    
    HH = nx.connected_component_subgraphs(gg)
    maxhn = 0
    for h in HH:
        if maxhn < len(h.nodes()):
            maxhn = len(h.nodes())
            H = h
    #print 'H_nodes:', len(H.nodes())
    
    #ndegree = G.degree()
    # 节点度,dict{nodes:value}
    #get_key_user('node_degree', topic, date, windowsize, ndegree)
    #根据节点度排序,获取节点度层面的关键用户
    
    dCentrality = nx.degree_centrality(G)
    # 度中心性 dict{nodes:value} 度量重要性
    
    avedc = get_ave(dCentrality)
    #平均度中心性 float
    save_quota(prekey+'_ave_degree_centrality_'+str(network_type), avedc)
    
    maxwcc = nx.weakly_connected_component_subgraphs(G).next()
    #print 'maxwcc_G:', len(maxwcc)
    
    bCentrality = nx.betweenness_centrality(G)
    # 介数中心 dict{nodes:value},度量其对网络流程的重要性
    
    avebc = get_ave(bCentrality)
    # 平均介数中心性 float
    save_quota(prekey+'_ave_betweenness_centrality_'+str(network_type), avebc)
    
    cCentrality = nx.closeness_centrality(G)
    # 紧密中心性 dict{nodes:value},度量感知整个网络流程事件的位置
    avecc = get_ave(cCentrality)
    # 平均紧密中心性 float
    save_quota(prekey+'_ave_closeness_centrality_'+str(network_type), avecc)
    
    # get_key_user module
    print 'get_user'
    get_key_user(topic, date, windowsize, dCentrality, bCentrality, cCentrality, network_type)
    ''' 
    eCentrality = nx.eigenvector_centrality_numpy(G)
    # 特征向量中心性
    #get_key_user('eigenvector_centrality', topic, date, windowsize, eCentrality)
    # 获取特征向量中心性层面的关键用户
    aveec = get_ave(eCentrality)
    # 平均特征向量中心性 float
    save_quota(prekey+'_eigenvector_centrality_'+str(network_type), aveec)
    '''
    spl_histogram = get_spl_histogram(H)
    save_quota(prekey + '_shortest_path_length_histogram_'+str(network_type), json.dumps(spl_histogram))
       
    avespl = nx.average_shortest_path_length(H) # !!!!
    # 平均最短路径长度 float--only for connected gragh
    save_quota(prekey+'_average_shortest_path_length_'+str(network_type), avespl)
   
    dhistogram = nx.degree_histogram(G)
    # 节点度分布(从一到最大度的出现频次)
    save_quota(prekey+'_degree_histogram_'+str(network_type), dhistogram)


    '''
    #Hdhistogram = nx.degree_histogram(G) # !!!!
    # histogram of H-----max connected graph
    #save_quota(prekey + '_H_degree_histogram', Hdhistogram)
    '''
    gamma = get_powerlaw(dhistogram, prekey)
    # 幂律分布系数
    save_quota(prekey+'_power_law_distribution_'+str(network_type), gamma)
    
    
    nnodes = len(G.nodes())
    # the number of nodes in G
    save_quota(prekey+'_number_nodes_'+str(network_type), nnodes)
    
    Hnnodes = len(H.nodes())
    # the number o nodes in H
    ratio_H2G = float(Hnnodes) / float(nnodes)
    print '!!!!!ratio_H2G!!!!!:',ratio_H2G
    #save_quota(prekey + '_ratio_H2G', ratio_H2G)
    
    alldegree = sum(dhistogram)
    ave_degree = float(alldegree) / float(nnodes)
    # ave_degree 平均节点度
    save_quota(prekey+'_ave_degree_'+str(network_type), ave_degree)

    
    nedges = len(G.edges())
    # the number of edged in G
    save_quota(prekey+'_number_edges_'+str(network_type), nedges)
    
    gdiameter = nx.diameter(H) # !!!
    # The diameter is the maximum eccentricity   int-n
    save_quota(prekey+'_diameter_'+str(network_type), gdiameter)

    geccentricity = nx.eccentricity(H) # !!!
    # the eccentricity of nodes in gg
    avegec = get_ave(geccentricity)
    save_quota(prekey+'_ave_eccentricity_'+str(network_type), avegec)

    
    sconnectedn = nx.number_strongly_connected_components(G)
    # 强连通子图数量  int-n
    save_quota(prekey+'_number_strongly_connected_components_'+str(network_type), sconnectedn)
    #maxscc = nx.strongly_connected_component_subgraphs(G).next()
    #print 'maxwcc:', len(maxwcc)
    wconnectesn = nx.number_weakly_connected_components(G)
    # 弱连通子图数量 int-n
    
    save_quota(prekey+'_number_weakly_connected_components_'+str(network_type), wconnectesn)
    maxwcc = nx.weakly_connected_component_subgraphs(G).next()
    print 'maxwcc_G:', len(maxwcc.nodes())
    print '!!!!ratio_maxwcc_G!!!:', float(len(maxwcc.nodes()))/float(nnodes)
    
    aveclustering = nx.average_clustering(gg) # !!!!
    # 平均聚类系数
    save_quota(prekey+'_average_clustering_'+str(network_type), aveclustering)

    dassortativity_coefficient = nx.degree_assortativity_coefficient(G)
    # 同配性系数
    save_quota(prekey + '_degree_assortativity_coefficient_'+str(network_type), dassortativity_coefficient)
    
    #print 'G_edges:', len(G.edges())
    #print 'G_edges:', len(G.selfloop_edges())
    #GG = G
    #GG.remove_edges_from(GG.selfloop_edges())
    #print 'test_edges:',len(GG.edges())
    kcore = nx.core_number(G)
    #print 'kcore:', kcore
    # k_score k核数
    #avekc = get_ave(kcore)
    
    maxkc = get_max(kcore)
    save_quota(prekey + '_max_k_core_'+str(network_type), maxkc)
示例#47
0
            newTracks = scac.getTracks(profile)
            print "New Tracks: " + ", ".join([scac.id2username(track, 'tracks') if isinstance(scac.id2username(track, 'tracks'), str) else str(track) for track in newTracks])
            scac.addTracks(profile, newTracks, profileGraph)
            profiles_to_enqueue.extend(newTracks)

        else:
            print "\t", "Artist ID %s is not query-able" % profile
            unavailable_profiles.append(profile)

print "The profile graph currently contains " + str(len(profileGraph.nodes())) + " profiles."

print "Here are their connections."

print_graph(profileGraph)

print "The profile graph currently contains " + str(nx.number_strongly_connected_components(profileGraph)) + " strongly connected components."

# Go through the graph and compute each PR until it converges.
iterations = 10
print "Computing PageRank on our profileGraph..."
computePR(profileGraph, 0.85, iterations)

prList = []

for profile in profileGraph.nodes():
    prList.append((profile, profileGraph.node[profile]['currPR']))

prList.sort(key = lambda tup: tup[1]) # Sort the list in place

prList.reverse() # order by descending PR
示例#48
0
文件: graphAttrib.py 项目: jkcc/MuNS
        
            binNum = 10
            
            # compute in/out degree distribution
            lOutDeg = list(gGraph.out_degree().values())
            lInDeg = list(gGraph.in_degree().values())
            lOutDegHist = np.histogram(lOutDeg, bins=binNum)
            lInDegHist = np.histogram(lInDeg, bins=binNum)
            
            
            # compute vert/edge ratio
            vertEdgeRatio = float(gGraph.number_of_nodes()) / gGraph.number_of_edges()
            
            # compute # of connected components
            ccNum = nx.number_connected_components(gGraph)
            strongCCNum = nx.number_strongly_connected_components(gGraph)
            
            # compute distribution of vert/edge ratio
            lVertInOutDeg = [float(gGraph.in_degree(v)) / gGraph.out_degree(v) for v in gGraph.nodes_iter()]
            lVertIODegHist = np.histogram(lVertInOutDeg, bins=binNum)
            
            # compute clustering coefficient
            avgClusCoeff = nx.average_clustering(gGraph)
            
            # degree assoritivity
            degAssort = nx.degree_assortativity_coefficient(gGraph)
            
            print vertEdgeRatio, ccNum, strongCCNum, avgClusCoeff, degAssort, lOutDegHist, lInDegHist, lVertIODegHist
    

#############################################################3
示例#49
0
import networkx as nx

G = nx.Graph()
G.add_edge(5, 1)
G.add_edge(1, 0)
G.add_edge(0, 7)
G.add_edge(2, 4)
G.add_edge(7, 2)
G.add_edge(0, 6)
G.add_edge(6, 8)
G.add_edge(6, 3)
G.add_edge(1, 9)

for item in nx.number_strongly_connected_components(G):
    print(item.edges())

# for elem in nx.connected_component_subgraphs(G):
#     print(elem.edges())
# Gc = max(nx.connected_component_subgraphs(G), key=len)

# import matplotlib.pyplot as plt
# pos = nx.graphviz_layout(G)
# nx.draw(G, nodecolor='r', edge_color='b')
# plt.draw()  # pyplot draw()
# plt.savefig('sample_graph.png')
                g.add_edge(source, target, weight=tweetCount)
                #print 'Added node ' + target + '. Edge added from ' + source + ' to ' + target
        else:
            g.add_node(source)
            if target in nodes_array:
                g.add_edge(source, target, weight=tweetCount)
                #print 'Edge added from ' + source + ' to ' + target
            else:
                g.add_node(target)      # We must add the target node first
                g.add_edge(source, target, weight=tweetCount)
                #print 'Added node ' + target + '. Edge added from ' + source + ' to ' + target


print 'Graph contains ' + str(g.number_of_nodes()) + ' nodes.'
print 'Graph contains ' + str(g.number_of_edges()) + ' edges.'
print 'Graph contains ' + str(nx.number_strongly_connected_components(g)) + ' strongly connected components.'


# Determine which node has the greatest out degree
out_degree_dictionary = g.out_degree(g.nodes())     #Put all of the nodes in with the key=node name and value=out degree
max_id_node_name = out_degree_dictionary.keys()[0] 
max_out_degree = out_degree_dictionary.values()[0]
for key, value in out_degree_dictionary.iteritems():
    #print key + " " +  str(value)
    if value > max_out_degree:
        max_od_node_name = key
        max_out_degree = value
   
print 'Node with highest out degree: ' + max_od_node_name + ' (' + str(max_out_degree) + ')'

# Determine which node has the highest in degree
示例#51
0
文件: scc.py 项目: ajiehust/rosalind
def scc(G):
    return nx.number_strongly_connected_components(G)
示例#52
0
def graph_summary(graph, outfile):
    print("Metric,Value", file=outfile)
    print("Order (Nodes),{}".format(graph.order()), file=outfile)
    print("Size (Edges),{}".format(graph.size()), file=outfile)
    print("Size (Edges),{}".format(graph.size()), file=outfile)
    print("Components,{}".format(nx.number_strongly_connected_components(graph)), file=outfile)
示例#53
0
pos=nx.spring_layout(G,k=0.15,iterations=10)
# pos=nx.graphviz_layout(G)
# pos=layout(G)

G.remove_nodes_from(nx.isolates(G))

print str(" ")
print 'STRONG CONNECTEDNESS OF DIRECTED GRAPHS'
print str(" ")

print str(" ")
print G.name
print str(" ")

print 'Is graph G strongly connected?', nx.is_strongly_connected(G)
print 'The number of strongly connected components of G is:', nx.number_strongly_connected_components(G)
print str(" ")

lc=sorted(nx.strongly_connected_components(G), key = len, reverse=True)
print 'List of strongly connected components:'
print lc
print str(" ")


deg=G.degree()
deg_dic=[]
for nd in deg:
    if deg[nd]>0:
        deg_dic.append(nd)
node0 = random.choice(deg_dic)
示例#54
0
def run(filename, gt_file, n_iter):
    
    
    f=open(filename)
    line1=f.readline()
    print line1
    f.close()
    if len(line1.split()) !=2:
	   g=input1(filename)
    else:
	   g=input2(filename)
    
    print str(len(g.nodes())) + " vertices in graph to begin with."

    connected_components=[x for x in nx.weakly_connected_components(g)]
    for component in connected_components:
        if len(component) < 10:
            g.remove_nodes_from(component)

    print str(len(g.nodes())) + " vertices in graph after removing components of at most "+str(LENGTH_THRESHOLD)+ " nodes."

    read_to_chr_map={}

    if gt_file.split('.')[-1]=='json':
        with open(gt_file,'r') as f:
            tmp_map=json.load(f)
        for read in tmp_map:
            readid=int(read.strip("'"))
            read_to_chr_map[readid] = int(tmp_map[read][0][2])
    else:
        with open(gt_file,'r') as f:
            for num, line in enumerate(f.readlines()):
                m = map(int, line.strip().split())
                read_to_chr_map[m[0]]=m[1]   
    
    nodes_seen=set([x.split("_")[0] for x in g.nodes()])

    for node in nodes_seen:
        read_to_chr_map.setdefault(int(node),-1)

    #print nx.info(g)
    print "Num reads read : "+str(len(read_to_chr_map))
    
    for node in g.nodes():
        nodeid=int(node.split('_')[0])

        g.node[node]['count'] = 1
        g.node[node]['read'] = node
        #print str(nodeid), node,g.node[node]['chr']
        
        
    degree_sequence=sorted(g.degree().values(),reverse=True)
    print Counter(degree_sequence)
    for i in range(n_iter):
        for node in g.nodes():
            if g.in_degree(node) == 0:
                g.remove_node(node)
    
        print nx.info(g)
        degree_sequence=sorted(nx.degree(g).values(),reverse=True)
        print Counter(degree_sequence)
    
    degree_sequence=sorted(nx.degree(g).values(),reverse=True)
    print Counter(degree_sequence)
    
    
    g.graph['aval'] = 1000000000
    
    for i in range(5):
        merge_simple_path(g)
        degree_sequence=sorted(nx.degree(g).values(),reverse=True)
        print Counter(degree_sequence)
    
    h=nx.DiGraph()
    h.add_nodes_from(g)
    h.add_edges_from(g.edges())
    for node in g.nodes():
        reads_in_node=[int(x.split('_')[0]) for x in g.node[node]['read'].split(':')]
        try:
            chr_in_node=map(lambda x: read_to_chr_map[x], reads_in_node)
        except:
            print reads_in_node,g.node[node]['read']
            return
        chr_in_node_set=set(chr_in_node)
        if len(chr_in_node_set) ==1:
            h.node[node]['chr']=chr_in_node[0]
        else:
            h.node[node]['chr']=':'.join(map(str,chr_in_node))

        h.node[node]['count']=g.node[node]['count']
        try:
            h.node[node]['read']=g.node[node]['read']
        except:
            pass

    
    nx.write_graphml(h, filename.split('.')[0]+'_condensed_annotated.graphml')
    
    print nx.number_weakly_connected_components(h)
    print nx.number_strongly_connected_components(h)
示例#55
0
                                try:
                                    if to_node.is_white:
                                        G.add_node(to_node_position, side="WHITE")
                                    elif to_node.is_black:
                                        G.add_node(to_node_position, side="BLACK")
                                except AttributeError:
                                    G.add_node(to_node_position, side="UNDEFINED")
                                if to_node == None:
                                    G.add_edge(from_node_position, to_node_position, weight=0.0)
                                elif i.is_opponent(to_node):
                                    G.add_edge(from_node_position, to_node_position, weight=-1.0)
                                else:
                                    G.add_edge(from_node_position, to_node_position, weight=1.0)                        
            fout.write(move + '\t')                
            fout.write(str(side) + '\t') 
            fout.write(str(nx.number_strongly_connected_components(G)) + '\t')
            fout.write(str(nx.number_weakly_connected_components(G)) + '\t')
            try:
                fout.write(str(nx.average_shortest_path_length(G)) + '\t')
            except:
                fout.write("Not connected" + '\t')
            fout.write(str(side == winner) + '\n')
            '''
            edefensive=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >= 0.0]
            #eoffensive=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <= 0.0]

            pos=nx.circular_layout(G) # positions for all nodes

            # nodes
            if side == 1:
                nx.draw_networkx_nodes(G,pos,nodelist=[node for (node,d) in G.nodes(data=True) if d['side'] == "BLACK"], node_color='blue',node_size=300)
示例#56
0
import multiprocessing as mp 
from cc_mp_classes import Consumer, Task, bookTasks

# A global artist graph used to iterate through the various algorithms.
# Each node is artist id, with edges weighted by activity between then.
artistGraph = nx.MultiDiGraph()

tasks = mp.Queue()
results = mp.Queue()

try:
	print "Reading in artist graph..."
	artistGraph = nx.read_pajek('artistGraph.net')
	print "Read successfully!"
	print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
	print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
except IOError:
	print "Could not find artistGraph"

def my_shuffle(A):
	random.shuffle(A)
	return A	

rartists = my_shuffle(artistGraph.nodes())	

for artist_id in rartists:
	
	# initialize the task queue
	artists_to_enqueue = [artist_id]

	depth = 3
示例#57
0
    for g in testy:
        if len(g)>=size_of_component:
            uu+=1
    if uu>=number_of_components:
        break
    else:
        counte+=1
        continue

print str(" ")
print G.name
print str(" ")

G.remove_nodes_from(nx.isolates(G))

print 'Number of strongly connected components:', nx.number_strongly_connected_components(G)
print 'Number of weakly connected components:', nx.number_weakly_connected_components(G)
print str(" ")

print 'Number of unilaterally connected components:', len(ucc)
print str(" ")

print 'Unilaterally connected components (UCC):'
for i in range(len(ucc)):
    print 'UCC', str(i+1)+':', ucc[i]
print str(" ")

print 'Edges in unilaterally connected components:'
for i in range(len(ucce)):
    print 'Edges in UCC', str(i+1)+':', ucce[i]
print str(" ")
Institution (directed edge means at least one email was sent \
October 2003 and March 2005')
G = nx.read_edgelist('Email-EuAll.txt', create_using=nx.DiGraph(),
                     nodetype=int)
print('')

print("Properti jaringan")
print("-----------------")
print('')
N, K = G.order(), G.size()
avg_deg = float(K)/N
print('Jumlah node: %d' % N)
print('Jumlah edge: %d' % K)
print('Rata-rata edge: %.3f' % avg_deg)
print('Jumlah strongly connected component: %d' %
      nx.number_strongly_connected_components(G))
print('Jumlah weakly connected component: %d' %
      nx.number_weakly_connected_components(G))
print('')

print('Distribusi degree')
print('-----------------')
print('')
in_degrees = G.in_degree()
in_values = sorted(set(in_degrees.values()))
in_hist = [in_degrees.values().count(x) for x in in_values]
out_degrees = G.out_degree()
out_values = sorted(set(out_degrees.values()))
out_hist = [out_degrees.values().count(x) for x in out_values]

print('Pembuatan grafik...')