示例#1
0
def kernelize(G):
    """
    """
    for H in nx.biconnected_component_subgraphs(G):
        flag = removeSimplicialUniversal(H)
        if H:
            if flag:
                for J in nx.biconnected_component_subgraphs(H):
                    yield J
            else:
                yield H
示例#2
0
def BrandesCutPoint(g):
    n_nodes = len(g.nodes())
    ap = set([x for x in nx.articulation_points(g)])
    bconn = list(nx.biconnected_component_subgraphs(g))

    cp_dict = {}

    for p in ap:
        cp_dict[p] = [b for b in bconn if p in b]

    bc = col.defaultdict(int)

    for p, bconns in cp_dict.items():
        for bcs in bconns:
            h = nx.Graph(g)
            for altrebcs in bconns:
                if(altrebcs != bcs):
                    rimuovi = altrebcs.nodes()
                    rimuovi.remove(p)
                    h.remove_nodes_from(rimuovi)
            b = nx.betweenness_centrality(h, weight='weight', endpoints=False,
                                          normalized=False)[p]
            bc[p] += b
        bc[p] += (n_nodes - 1)
    for n, b in nx.betweenness_centrality(g, weight='weight', endpoints=True,
                                          normalized=False).iteritems():
        if n not in ap:
            bc[n] = b
    return bc
示例#3
0
文件: _planar.py 项目: ExpHP/defect
def planar_cycle_basis_nx(g, xs, ys):
    if g.is_directed():
        raise TypeError('Not implemented for directed graphs')
    if g.is_multigraph():
        raise TypeError('Not implemented for multi-graphs')

    if not (set(g) == set(xs) == set(ys)):
        raise ValueError('g, xs, ys must all share same set of vertices')

    my_g = nx.Graph()
    my_g.add_nodes_from(iter(g))
    my_g.add_edges_from(g.edges())

    nx.set_node_attributes(my_g, 'x', xs)
    nx.set_node_attributes(my_g, 'y', ys)

    # BAND-AID (HACK)
    # This algorithm has an unresolved issue with graphs that have at least
    #   two biconnected components, one of which is "inside" another
    #   (geometrically speaking).  Luckily, it is safe to look at each
    #   biconnected component separately, as all edges in a cycle always
    #   belong to the same biconnected component.
    result = []
    for subg in nx.biconnected_component_subgraphs(my_g):
        if len(subg) >= 3:  # minimum possible size for cycles to exist
            result.extend(planar_cycle_basis_impl(subg))

    # Restore some confidence in the result...
    if len(result) != len(nx.cycle_basis(g)):
        raise RuntimeError(
            'planar_cycle_basis produced a result of incorrect '
            'length on the given graph! (does it have crossing edges?)')

    return result
示例#4
0
def __mod_bc(g):
    n_nodes = len(g.nodes())
    ap = [x for x in nx.articulation_points(g)]
    bconn = list(nx.biconnected_component_subgraphs(g))
    cp_dict = {}
    print len(g)
    for p in ap:
        cp_dict[p] = [b for b in bconn if p in b]
    bc = defaultdict(int)
    for p, bconns in cp_dict.items():
        for bcs in bconns:
            b = nx.betweenness_centrality(bcs,
                                          weight='weight',
                                          endpoints=False,
                                          normalized=False)[p]
            bc[p] += b
        bc[p] += (n_nodes - 1)
    for n, b in nx.betweenness_centrality(g,
                                          weight='weight',
                                          endpoints=True,
                                          normalized=False).iteritems():
        if n not in ap:
            bc[n] = b
        #bc[n] /= n_nodes * (n_nodes - 1)
        # FIXME check that this is the same normalization the other method does
    return bc
示例#5
0
    def __call__(self):
        timeout_at = None if self.timeout is None else time.time(
        ) + self.timeout

        for bcc in networkx.biconnected_component_subgraphs(self.G, False):
            lsp = LongestSimplePath(bcc, self.weight, timeout_at)()
            nodes = set()
            for a, b in lsp:
                nodes.add(a)
                nodes.add(b)
            self.Q.append((nodes, lsp))

        if self.N == 1:
            return np.array([[0]])

        self.nodes, self.C = self.Q.pop()

        while self.Q:
            self.merge()

        result = np.empty((self.N, self.N))
        for i, j in ((i, j) for i in range(self.N) for j in range(i, self.N)):
            result[i, j] = self.C[(i, j)]
            result[j, i] = self.C[(i, j)]

        return result
示例#6
0
文件: _planar.py 项目: ExpHP/defect
def planar_cycle_basis_nx(g, xs, ys):
	if g.is_directed():
		raise TypeError('Not implemented for directed graphs')
	if g.is_multigraph():
		raise TypeError('Not implemented for multi-graphs')

	if not (set(g) == set(xs) == set(ys)):
		raise ValueError('g, xs, ys must all share same set of vertices')

	my_g = nx.Graph()
	my_g.add_nodes_from(iter(g))
	my_g.add_edges_from(g.edges())

	nx.set_node_attributes(my_g, 'x', xs)
	nx.set_node_attributes(my_g, 'y', ys)

	# BAND-AID (HACK)
	# This algorithm has an unresolved issue with graphs that have at least
	#   two biconnected components, one of which is "inside" another
	#   (geometrically speaking).  Luckily, it is safe to look at each
	#   biconnected component separately, as all edges in a cycle always
	#   belong to the same biconnected component.
	result = []
	for subg in nx.biconnected_component_subgraphs(my_g):
		if len(subg) >= 3: # minimum possible size for cycles to exist
			result.extend(planar_cycle_basis_impl(subg))

	# Restore some confidence in the result...
	if len(result) != len(nx.cycle_basis(g)):
		raise RuntimeError(
			'planar_cycle_basis produced a result of incorrect '
			'length on the given graph! (does it have crossing edges?)'
		)

	return result
示例#7
0
def BC_CutPoint_Interf(g, nodiespansicut, NodiEspansi):
    n_nodes = len(g.nodes())
    bconn = list(nx.biconnected_component_subgraphs(g))

    cp_dict = dict.fromkeys(nodiespansicut, [])

    for p in nodiespansicut:
        cp_dict[p] = [b for b in bconn if p in b]

    bc = col.defaultdict(int)

    for p, bconns in cp_dict.items():
        for bcs in bconns:
            h = nx.Graph(g)
            for altrebcs in bconns:
                if(altrebcs != bcs):
                    rimuovi = altrebcs.nodes()
                    rimuovi.remove(p)
                    h.remove_nodes_from(rimuovi)
            if (AltriNodiDentroBiconnesso(bcs, p, NodiEspansi) == True):
                continue
            else:
                b = nx.betweenness_centrality(h, weight='weight', endpoints=False,
                                              normalized=False)[p]
                bc[p] += b

    return bc
示例#8
0
def getGraphs(G, fileToSave):
    N = G.number_of_nodes()
    numToRemove = int(N * 0.05)

    order = createOrder(G)

    counter = 0

    for i in range(18):
        print(i)

        nodesList = list(G.nodes())

        conn = list(nx.connected_component_subgraphs(G))
        biconn = list(nx.biconnected_component_subgraphs(G))

        GC = max(conn, key=len)
        GBC = max(biconn, key=len)

        GCnodes = list(GC.nodes())
        GBCnodes = list(GBC.nodes())

        try:
            fig, ax = ox.plot_graph(G, fig_height=30, fig_width=30)
        except:
            break

        for n in GCnodes:
            dictInfo = G.node[n]
            ax.scatter(dictInfo['x'], dictInfo['y'], c='green')

        GCName = fileToSave + "_Deg_GCPlot_%d.png" % (i)
        GBCName = fileToSave + "_Deg_GBCPlot_%d.png" % (i)

        plt.savefig(GCName)
        plt.clf()

        try:
            fig, ax = ox.plot_graph(G, fig_height=30, fig_width=30)
        except:
            break

        for n in GBCnodes:
            dictInfo = G.node[n]
            ax.scatter(dictInfo['x'], dictInfo['y'], c='red')

        plt.savefig(GBCName)
        plt.clf()

        startIndex = counter * numToRemove
        endIndex = startIndex + numToRemove

        nodesToRemove = order[startIndex:endIndex]

        G.remove_nodes_from(nodesToRemove)

        counter = counter + 1
 def __init__(self, G, weight="weight"):
     self.N = G.number_of_nodes()
     self.Q = []
     for bcc in networkx.biconnected_component_subgraphs(G, False):
         lsp = LongestSimplePath(bcc, weight)()
         nodes = set()
         for a, b in lsp:
             nodes.add(a)
             nodes.add(b)
         self.Q.append((nodes, lsp))
示例#10
0
 def __init__(self, G, weight="weight"):
     self.N = G.number_of_nodes()
     self.Q = []
     for bcc in networkx.biconnected_component_subgraphs(G, False):
         lsp = LongestSimplePath(bcc, weight)()
         nodes = set()
         for a, b in lsp:
             nodes.add(a)
             nodes.add(b)
         self.Q.append((nodes, lsp))
示例#11
0
def test_biconnected_component_subgraphs_cycle():
    G=nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4, 5])
    Gc = set(nx.biconnected_component_subgraphs(G))
    assert_equal(len(Gc), 2)
    g1, g2=Gc
    if 0 in g1:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(0,1),(0,2),(1,2)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(1,3),(1,5),(3,4),(4,5)])))
    else:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(1,3),(1,5),(3,4),(4,5)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(0,1),(0,2),(1,2)])))
示例#12
0
def test_non_repeated_cuts():
    # The algorithm was repeating the cut {0, 1} for the giant biconnected
    # component of the Karate club graph.
    K = nx.karate_club_graph()
    G = max(list(nx.biconnected_component_subgraphs(K)), key=len)
    solution = [{32, 33}, {2, 33}, {0, 3}, {0, 1}, {29, 33}]
    cuts = list(nx.all_node_cuts(G))
    if len(solution) != len(cuts):
        print(nx.info(G))
        print("Solution: {}".format(solution))
        print("Result: {}".format(cuts))
    assert_true(len(solution) == len(cuts))
    for cut in cuts:
        assert_true(cut in solution)
def test_biconnected_component_subgraphs_cycle():
    G = nx.cycle_graph(3)
    nx.add_cycle(G, [1, 3, 4, 5])
    Gc = set(nx.biconnected_component_subgraphs(G))
    assert_equal(len(Gc), 2)
    g1, g2 = Gc
    if 0 in g1:
        assert_true(nx.is_isomorphic(g1, nx.Graph([(0, 1), (0, 2), (1, 2)])))
        assert_true(
            nx.is_isomorphic(g2, nx.Graph([(1, 3), (1, 5), (3, 4), (4, 5)])))
    else:
        assert_true(
            nx.is_isomorphic(g1, nx.Graph([(1, 3), (1, 5), (3, 4), (4, 5)])))
        assert_true(nx.is_isomorphic(g2, nx.Graph([(0, 1), (0, 2), (1, 2)])))
示例#14
0
def test_non_repeated_cuts():
    # The algorithm was repeating the cut {0, 1} for the giant biconnected
    # component of the Karate club graph.
    K = nx.karate_club_graph()
    G = max(list(nx.biconnected_component_subgraphs(K)), key=len)
    solution = [{32, 33}, {2, 33}, {0, 3}, {0, 1}, {29, 33}]
    cuts = list(nx.all_node_cuts(G))
    if len(solution) != len(cuts):
        print(nx.info(G))
        print("Solution: {}".format(solution))
        print("Result: {}".format(cuts))
    assert_true(len(solution) == len(cuts))
    for cut in cuts:
        assert_true(cut in solution)
示例#15
0
def get_connected_component_subgraphs(C):
    sub_graph = {}
    sub_graph['biconnected_components'] = []

    edge_list = []
    # print(C.edges.data())
    for e in C.edges.data():
        edge_list.append((e[0], e[1]))

    sub_graph['edges'] = edge_list

    for bc in nx.biconnected_component_subgraphs(C):
        sub_graph['biconnected_components'].append(get_all_edges(bc))

    return sub_graph
示例#16
0
def getGraphs(G, fileToSave):
	N = G.number_of_nodes()
	numToRemove = int(N * 0.1)

	for i in range(10):
		print(i)

		nodesList = list(G.nodes())

		conn = list(nx.connected_component_subgraphs(G))
		biconn = list(nx.biconnected_component_subgraphs(G))

		GC = max(conn, key=len)
		GBC = max(biconn, key=len)

		GCnodes = list(GC.nodes())
		GBCnodes = list(GBC.nodes())

		try:
			fig, ax = ox.plot_graph(G,fig_height=30, fig_width=30)
		except:
			break

		for n in GCnodes:
			dictInfo = G.node[n]
			ax.scatter(dictInfo['x'], dictInfo['y'], c='green')

		GCName = fileToSave + "_GCPlot_%d.png" % (i) 
		GBCName = fileToSave + "_GBCPlot_%d.png" % (i) 

		plt.savefig(GCName)
		plt.clf()

		try:
			fig, ax = ox.plot_graph(G,fig_height=30, fig_width=30)
		except:
			break

		for n in GBCnodes:
			dictInfo = G.node[n]
			ax.scatter(dictInfo['x'], dictInfo['y'], c='red')

		plt.savefig(GBCName)
		plt.clf()

		nodesToRemove = random.sample(nodesList,numToRemove)

		G.remove_nodes_from(nodesToRemove)
示例#17
0
def computeLevel(N):
    """

    :param N:
    :param :
    :return:
    """
    level = 0
    for BN in networkx.biconnected_component_subgraphs(N.to_undirected()):
        # compute the level of each biconnected component
        m = BN.number_of_edges()
        n = BN.number_of_nodes()
        if m-n+1 > level:
            level = m-n+1
            #print "Biconnected component "+str(i)+": "+str(m)+" edges, "+str(n)+" nodes, cyclomatic number "+str(m-n+1)
    return level
示例#18
0
def read_edges(file_name):
        try:
                file = open(file_name, 'r')
        except IOError:
                sys.exit('Could not open file: {}'.format(file_name))
        edges = []
        for line in file.readlines():
                cols = line.split(' ')
                edges.append((cols[0], cols[1].rstrip()))
        file.close()

        g = networkx.Graph()
        g.add_edges_from(edges)
	g = max(networkx.biconnected_component_subgraphs(g), key=len)
        g.remove_edges_from(g.selfloop_edges())
        return g
示例#19
0
def countMetrics(links):
    g = nx.Graph(links)
    dg = nx.DiGraph(links)
    row = [g.number_of_nodes(), len(links)]

    sub_graphs = nx.connected_component_subgraphs(g)
    row += getStats(sub_graphs)

    sub_graphs = nx.biconnected_component_subgraphs(g)
    row += getStats(sub_graphs)

    sub_graphs = nx.strongly_connected_component_subgraphs(dg)
    row += getStats(sub_graphs)

    sub_graphs = nx.find_cliques(g)
    row += getCliqueStats(list(sub_graphs), 3, 8)
    return row
示例#20
0
def findArt(G):

	listOfNodes = G.nodes()

	biconnSizeList = [] 

	for i in listOfNodes:

		newG = G.copy()

		newG.remove_node(i)

		newBiconn = list(nx.biconnected_component_subgraphs(newG))

		largestBiconn = 0

		if len(newBiconn) > 0: 

			largestBiconn = len(max(newBiconn, key = len))

		biconnSizeList.append((largestBiconn,i))

	return biconnSizeList
示例#21
0
import networkx as nx
import matplotlib.pyplot as plt
import numpy

G = nx.read_gml('test1.gml')
# k_components = nx.k_components(G)
# print(k_components)

a = list(nx.articulation_points(G))
# print(a)
G.add_edge(3, 7)
k_components = nx.k_components(G)
# print(k_components)
a = list(nx.articulation_points(G))
# print(a)
A = numpy.matrix([[0, 1, 1, 0, 0], [1, 0, 1, 0, 0], [1, 1, 0, 1, 1],
                  [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]])
H = nx.from_numpy_matrix(A)
k_components = nx.k_components(H)
print(k_components)
bicomponents = list(nx.biconnected_component_subgraphs(H))
print(bicomponents[0].nodes())

plt.show()
示例#22
0
def main ():
    global all_in_whole_names
    global all_in_resp_names
    name = sys.argv[1]


    g = nx.read_graphml(name).to_undirected(reciprocal=False)
    g = nx.Graph(g)
    g.remove_edges_from(g.selfloop_edges())
    hirisk = g.subgraph([x for x in g.nodes() if g.node[x]["hasrisk"] != "0"])
    lorisk = g.subgraph([x for x in g.nodes() if g.node[x]["hasrisk"] == "0"])

    oldsize = len(hirisk)

    lcc = list()
    deg_med = list()
    triangle_med = list()
    clustering_med = list()
    core_number_med = list()
    sze = list()
    clique = list()
    bicc = list ()
    path_len = list()
    for r in range(1001):
    
        try: 
	    

		    allccs = [g] + [hirisk] + [lorisk]
		    #allccs = [x.to_undirected() for x in allccs]
		    #allcss = [nx.Graph(x) for x in allccs]

		    # ============
		    # CONNECTIVITY
		    # ------------

		    #print "Size of largest connected components"
		    # for i in range(len(allccs)):
		    #    print "%s & %d \\\\ \hline" % (names[i],len(allccs[i].node))
		    l = dict()

		    cchi = sorted(nx.connected_component_subgraphs(hirisk.to_undirected()), key=lambda x: -len(x))[0]
		    cclow = sorted(nx.connected_component_subgraphs(lorisk.to_undirected()), key=lambda x: -len(x))[0]
		    #pdb.set_trace()
		    lcc.append({"hirisk" : len(cchi.node), "lorisk": len(cclow.node)})
		    sze.append({"hirisk": hirisk.node, "lorisk":lorisk.node})
		    #l.append(("overlap.hirisk", len(np.intersect1d(hirisk_original["hashed_id"], hirisk.node.keys()))))
		    #l.append(("overlap.lorisk", len(np.intersect1d(lorisk_original["hashed_id"], lorisk.node.keys()))))
		    clique.append({"hirisk":nx.graph_clique_number(hirisk.to_undirected()), "lorisk": nx.graph_clique_number(lorisk.to_undirected())})
		    bicc.append({"hirisk" : len(sorted(nx.biconnected_component_subgraphs(hirisk.to_undirected()), key=lambda x: -len(x))[0].node), "lorisk" :len(sorted(nx.biconnected_component_subgraphs(lorisk.to_undirected()), key=lambda x: -len(x))[0].node)})
		    
		    path_len.append({"hirisk":nx.average_shortest_path_length(cchi), "lorisk": nx.average_shortest_path_length(cclow)})
		    
		    if r == 0:
		        degs = [dict([(node, len(cc.neighbors(node))) for node in cc]) for cc in allccs]
		    else:
		        for l in [1,2]:
		            degs[l] = dict([(node, len(allccs[l].neighbors(node))) for node in allccs[l]])


		    deg_med.append(graphfeatures (degs, "degs", allccs, all_in_whole_names))
		    '''
		    if r > 0:
		        if deg_med[-1][all_in_whole_names[1]] - deg_med[-1][all_in_whole_names[0]] > deg_med[0][all_in_whole_names[1]] - deg_med[0][all_in_whole_names[0]]:
		            for node in allccs[3].node.keys():
		                d = {"feature" : "deg_number", "graphnum" : graphnum, "node": node}
		                funnygraphs.append(d)
		            graphnum += 1
		    '''

		    # ============
		    # TRANSITIVITY
		    # ------------
		    print "Computing transitivity"
		    # transitivity = [nx.transitivity(g) for g in allccs]
		    #print "transitivity"
		    #for i in range(len(allccs)):
		    #    print "%s & %f \\\\ \hline" % (names[i],transitivity[i])



		    # =========
		    # TRIANGLES
		    # ---------

		    if r == 0:
		        triangles = [nx.triangles(g) for g in allccs]
		    else:
		        for l in [1,2]:
		            triangles[l] = nx.triangles(allccs[l])
		    triangle_med.append(graphfeatures (triangles, "triangles", allccs, all_in_whole_names))
		    '''
		    if r > 0:
		        if triangle_med[-1][all_in_whole_names[2]] - triangle_med[-1][all_in_whole_names[1]] > triangle_med[0][all_in_whole_names[2]] - triangle_med[0][all_in_whole_names[1]]:
		            for node in allccs[3].node.keys():
		                d = {"feature" : "triangle", "graphnum" : graphnum, "node": node}
		                funnygraphs.append(d)
		            graphnum += 1
		    '''

		    # ============
		    # CLUSTERING
		    # ------------
		    print "Clustering..."
		    if r == 0:
		        clustering = [nx.clustering(g) for g in allccs]
		    else:
		        for l in [1,2]:
		            clustering[l] = nx.clustering(allccs[l])
		    clustering_med.append(graphfeatures (clustering, "clustering", allccs, all_in_whole_names))
		    '''
		    if r > 0:
		        if clustering_med[-1][all_in_whole_names[2]] - clustering_med[-1][all_in_whole_names[1]] < clustering_med[0][all_in_whole_names[2]] - clustering_med[0][all_in_whole_names[1]]:
		            for node in allccs[3].node.keys():
		                d = {"feature" : "clustering", "graphnum" : graphnum, "node": node}
		                funnygraphs.append(d)
		            graphnum += 1
		    '''

		    #    transitivity = [nx.transitivity(g) for g in allccs]
		    #print "Finding clique numbers"
		    #for i in range(4, len(allccs)):
		    #    print "%s & %d \\\\ \hline" % (names[i],nx.graph_clique_number(allccs[i]))

		    # ============
		    # CORE_NUMBER
		    # ------------
		    print "Core_number..."
		    #core_number = [nx.core_number(g) for g in allccs]

		    if r == 0:
		        core_number = [nx.core_number(g) for g in allccs]
		    else:
		        for l in [1,2]:
		            core_number[l] = nx.core_number(allccs[l])

		    core_number_med.append(graphfeatures (core_number, "core_number", allccs, all_in_whole_names))
		    '''
		    if r > 0:
		        if core_number_med[-1][all_in_whole_names[2]] - core_number_med[-1][all_in_whole_names[1]] > core_number_med[0][all_in_whole_names[2]] - core_number_med[0][all_in_whole_names[1]] :
		            for node in allccs[3].node.keys():
		                d = {"feature" : "core_number", "graphnum" : graphnum, "node": node}
		                funnygraphs.append(d)
		            graphnum += 1
		    '''

		    

        except IndexError:
            #ex.printStackTrace(sys.stdout)
            traceback.print_exc(file=sys.stdout)
            print (set(hirisk.nodes()))

        hirisk = g.subgraph(random.sample(g.nodes(), oldsize))
        lorisk = g.subgraph(set(g.nodes()) - set(hirisk.nodes()))

    for (name, data) in [('ave_path_len', path_len), ('core_number', core_number_med), ('triangles', triangle_med), ('clustering', clustering_med), ('lccs', lcc), ('degrees', deg_med),  ("clique", clique), ("biconnected size", bicc)]:
        f = open(name + "_med.json", "wo")
        # ("size", size),
        json.dump(data, f)
        f.close()

        print "Computing " + name
        nums = [x["lorisk"] - x["hirisk"] for x in data]
        plt.hist(nums, bins=20)
        plt.title(name)
        plt.xlabel("Value") 
        plt.ylabel("Frequency")
        plt.axvline(x=nums[0], color='r', linestyle='dashed', linewidth=10)
        plt.savefig(name + "_med.pdf")
        #fig = plt.gcf()

        #plot_url = py.plot_mpl(fig, filename='mpl-basic-histogram')
        plt.clf()
示例#23
0
import csv
import matplotlib.pyplot as plt
import networkx as nx

from sys import argv
if __name__ == '__main__':
    G = nx.Graph()
    with open(argv[1], mode='rb') as f:
        csv = csv.reader(f)
        for line in list(csv)[1:]:
            if (line[2] is not '0'):
                G.add_edge(line[0], line[1], weight=line[3])
    components = sorted(nx.biconnected_component_subgraphs(G),
                        key=len,
                        reverse=True)
    for i in components:
        print len(i)
    nx.draw(components[0])
    nx.write_graphml(components[0], argv[1][:-4] + ".graphml")
示例#24
0
def k_components(G, flow_func=None):
    r"""Returns the k-component structure of a graph G.

    A `k`-component is a maximal subgraph of a graph G that has, at least,
    node connectivity `k`: we need to remove at least `k` nodes to break it
    into more components. `k`-components have an inherent hierarchical
    structure because they are nested in terms of connectivity: a connected
    graph can contain several 2-components, each of which can contain
    one or more 3-components, and so forth.

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

    flow_func : function
        Function to perform the underlying flow computations. Default value
        :meth:`edmonds_karp`. This function performs better in sparse graphs with
        right tailed degree distributions. :meth:`shortest_augmenting_path` will
        perform better in denser graphs.

    Returns
    -------
    k_components : dict
        Dictionary with all connectivity levels `k` in the input Graph as keys
        and a list of sets of nodes that form a k-component of level `k` as
        values.

    Raises
    ------
    NetworkXNotImplemented:
        If the input graph is directed.

    Examples
    --------
    >>> # Petersen graph has 10 nodes and it is triconnected, thus all
    >>> # nodes are in a single component on all three connectivity levels
    >>> G = nx.petersen_graph()
    >>> k_components = nx.k_components(G)

    Notes
    -----
    Moody and White [1]_ (appendix A) provide an algorithm for identifying
    k-components in a graph, which is based on Kanevsky's algorithm [2]_
    for finding all minimum-size node cut-sets of a graph (implemented in
    :meth:`all_node_cuts` function):

        1. Compute node connectivity, k, of the input graph G.

        2. Identify all k-cutsets at the current level of connectivity using
           Kanevsky's algorithm.

        3. Generate new graph components based on the removal of
           these cutsets. Nodes in a cutset belong to both sides
           of the induced cut.

        4. If the graph is neither complete nor trivial, return to 1;
           else end.

    This implementation also uses some heuristics (see [3]_ for details)
    to speed up the computation.

    See also
    --------
    node_connectivity
    all_node_cuts
    biconnected_components : special case of this function when k=2
    k_edge_components : similar to this function, but uses edge-connectivity
        instead of node-connectivity

    References
    ----------
    .. [1]  Moody, J. and D. White (2003). Social cohesion and embeddedness:
            A hierarchical conception of social groups.
            American Sociological Review 68(1), 103--28.
            http://www2.asanet.org/journals/ASRFeb03MoodyWhite.pdf

    .. [2]  Kanevsky, A. (1993). Finding all minimum-size separating vertex
            sets in a graph. Networks 23(6), 533--541.
            http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract

    .. [3]  Torrents, J. and F. Ferraro (2015). Structural Cohesion:
            Visualization and Heuristics for Fast Computation.
            https://arxiv.org/pdf/1503.04476v1

    """
    # Dictionary with connectivity level (k) as keys and a list of
    # sets of nodes that form a k-component as values. Note that
    # k-compoents can overlap (but only k - 1 nodes).
    k_components = defaultdict(list)
    # Define default flow function
    if flow_func is None:
        flow_func = default_flow_func
    # Bicomponents as a base to check for higher order k-components
    for component in nx.connected_components(G):
        # isolated nodes have connectivity 0
        comp = set(component)
        if len(comp) > 1:
            k_components[1].append(comp)
    bicomponents = list(nx.biconnected_component_subgraphs(G))
    for bicomponent in bicomponents:
        bicomp = set(bicomponent)
        # avoid considering dyads as bicomponents
        if len(bicomp) > 2:
            k_components[2].append(bicomp)
    for B in bicomponents:
        if len(B) <= 2:
            continue
        k = nx.node_connectivity(B, flow_func=flow_func)
        if k > 2:
            k_components[k].append(set(B.nodes()))
        # Perform cuts in a DFS like order.
        cuts = list(nx.all_node_cuts(B, k=k, flow_func=flow_func))
        stack = [(k, _generate_partition(B, cuts, k))]
        while stack:
            (parent_k, partition) = stack[-1]
            try:
                nodes = next(partition)
                C = B.subgraph(nodes)
                this_k = nx.node_connectivity(C, flow_func=flow_func)
                if this_k > parent_k and this_k > 2:
                    k_components[this_k].append(set(C.nodes()))
                cuts = list(nx.all_node_cuts(C, k=this_k, flow_func=flow_func))
                if cuts:
                    stack.append((this_k, _generate_partition(C, cuts, this_k)))
            except StopIteration:
                stack.pop()

    # This is necessary because k-components may only be reported at their
    # maximum k level. But we want to return a dictionary in which keys are
    # connectivity levels and values list of sets of components, without
    # skipping any connectivity level. Also, it's possible that subsets of
    # an already detected k-component appear at a level k. Checking for this
    # in the while loop above penalizes the common case. Thus we also have to
    # _consolidate all connectivity levels in _reconstruct_k_components.
    return _reconstruct_k_components(k_components)
示例#25
0
import osmnx as ox
import networkx as nx

Belgrade = ox.graph_from_place('Belgrade, Serbia')

NY = ox.graph_from_place('New York City, New York, USA')

Beijing = ox.graph_from_place('Beijing, China', which_result=2, network_type='drive')

biconn_Belgrade = list(nx.biconnected_component_subgraphs(Belgrade)) 

biconn_NY = list(nx.biconnected_component_subgraphs(NY)) 

biconn_Beijing = list(nx.biconnected_component_subgraphs(Beijing)) 

GBC_Belgrade = max(biconn_Belgrade, key=len)

GBC_NY = max(biconn_NY, key=len)

GBC_Beijing = max(biconn_Beijing, key=len)

nx.write_gpickle(GBC_Belgrade,"GBC_Belgrade.gpickle")

nx.write_gpickle(GBC_NY,"GBC_NY.gpickle")

nx.write_gpickle(GBC_Beijing,"GBC_Beijing.gpickle")
示例#26
0
deg=G.degree()
deg_dic=[]
for nd in deg:
    if deg[nd]>0:
        deg_dic.append(nd)
node0 = random.choice(deg_dic)

for i in range(len(lc)):
    if node0 in lc[i] :
        print 'The nodes in the biconnected component of graph containing the randomly chosen node ' + str(node0) + ' are: \n', lc[i]
        # break
print str(" ")

colors_list=['c','b','g','y','k','m']
colors_to_select=list(colors_list)
graphs =sorted(nx.biconnected_component_subgraphs(G), key = len, reverse=True)
biconnected_component_subgraphs_edges=[]
biconnected_component_subgraphs_nodes=[]
nodes_color_alpha=[]
edges_color_alpha=[]
colors_of_edges=[]
edge_width_l=[]
for gr in range(len(graphs)):
    graph='G' + str(gr+1)
    # print 'Nodes of biconnected component', graph+':', graphs[gr].nodes()
    # print 'Edges of biconnected component', graph+':', graphs[gr].edges()
    if len(graphs[gr].nodes()) >1 and len(graphs[gr].edges())>0:
        biconnected_component_subgraphs_edges.append(graphs[gr].edges())
        biconnected_component_subgraphs_nodes.append(graphs[gr].nodes())
        if len(colors_to_select)==0:
            colors_to_select=list(colors_list)
    allccs = gccs + resp
    all_in_whole = resp
    all_in_resp = []

    # ============
    # CONNECTIVITY
    # ------------

    l = list()
    l.append(("cc", len(nx.connected_component_subgraphs(resp[0])[0].node)))
    l.append(("size", len(resp[0].node)))
    l.append(("overlap", len(np.intersect1d(resp_original, resp[0].node))))
    l.append(("clique", nx.graph_clique_number(resp[0])))
    l.append(("bicc",
              len(list(nx.biconnected_component_subgraphs(resp[0]))[0].node)))
    lcc.append(dict(l))

    if r == 0:
        degs = [
            dict([(node, len(cc.neighbors(node))) for node in cc])
            for cc in allccs
        ]
    else:
        for l in [1]:
            degs[l] = dict([(node, len(allccs[l].neighbors(node)))
                            for node in allccs[l]])

    deg_med.append(
        graphfeatures(degs, "degs", allccs, names, all_in_whole,
                      all_in_whole_names, all_in_resp, all_in_resp_names))
示例#28
0
         resp = [gcc.subgraph(samp) for gcc in gccs]

    allccs = gccs + resp 
    all_in_whole = resp
    all_in_resp = []

    # ============
    # CONNECTIVITY
    # ------------

    l = list()
    l.append(("cc", len(nx.connected_component_subgraphs(resp[0])[0].node)))
    l.append(("size", len(resp[0].node)))
    l.append(("overlap", len(np.intersect1d(resp_original, resp[0].node))))
    #l.append(("clique", nx.graph_clique_number(resp[0])))
    l.append(("bicc", len(list(nx.biconnected_component_subgraphs(resp[0]))[0].node)))
    lcc.append(dict(l))
 
    if r == 0:
        degs = [dict([(node, len(cc.neighbors(node))) for node in cc]) for cc in allccs]
    else:
        for l in [1]:
            degs[l] = dict([(node, len(allccs[l].neighbors(node))) for node in allccs[l]])


    deg_med.append(graphfeatures (degs, "degs", allccs, names, all_in_whole, all_in_whole_names, all_in_resp, all_in_resp_names))
"""
    # ============
    # TRANSITIVITY
    # ------------
    print "Computing transitivity"
示例#29
0
    while G.number_of_nodes() > 0:

        #assert G.number_of_nodes() == (N - counter*int(step_size*N))

        conn = list(nx.connected_component_subgraphs(G))

        if len(conn) == 0:
            GC = nx.empty_graph(0)
            GCLen = 0
            avgConn = 0
        else:
            GC = max(conn, key=len)
            GCLen = len(GC)
            avgConn = getAvg(conn)

        biConn = list(nx.biconnected_component_subgraphs(G))

        N_0 = getNO(conn)
        N_1 = getN1(conn)

        NB_1 = getBN_1(biConn)
        NB_2 = getBN_2(biConn)

        #NB_0 = getNOBiconn(biConn)

        if len(biConn) == 0:
            GBCLen = 0
            avgBiconn = 0
        else:
            GBCLen = len(max(biConn, key=len))
            avgBiconn = getAvg(biConn)
示例#30
0
    cchi = len(nx.connected_component_subgraphs(high[0])[0].node)
    cclow = len(nx.connected_component_subgraphs(low[0])[0].node)
    l.append(("cc.high", cchi))
    l.append(("cc.low", cclow))
    l.append(("size.high", len(high[0].node)))
    l.append(("size.low", len(low[0].node)))
    l.append(
        ("overlap.high",
         len(np.intersect1d(high_original["hashed_id"], high[0].node.keys()))))
    l.append(("overlap.low",
              len(np.intersect1d(low_original["hashed_id"],
                                 low[0].node.keys()))))
    l.append(("clique.high", nx.graph_clique_number(high[0])))
    l.append(("clique.low", nx.graph_clique_number(low[0])))
    l.append(("bicc.high",
              len(list(nx.biconnected_component_subgraphs(high[0]))[0].node)))
    l.append(("bicc.low",
              len(list(nx.biconnected_component_subgraphs(low[0]))[0].node)))
    lcc.append(dict(l))

    if r == 0:
        degs = [
            dict([(node, len(cc.neighbors(node))) for node in cc])
            for cc in allccs
        ]
    else:
        for l in [2, 3]:
            degs[l] = dict([(node, len(allccs[l].neighbors(node)))
                            for node in allccs[l]])

    deg_med.append(
示例#31
0
def getBiCompInConnComp(G):
    counter = 0
    for item in G:
        biConn = list(nx.biconnected_component_subgraphs(item))
        counter = counter + len(biConn)
    return counter
示例#32
0
def reduce_graph(g):
    g1 = list(nx.biconnected_component_subgraphs(g))
    for f1 in g1:
        # TODO: might not work due to hiding in the function reduction
        reduction(f1)
    return g1
示例#33
0
		largestBiconn = 0

		if len(newBiconn) > 0: 

			largestBiconn = len(max(newBiconn, key = len))

		biconnSizeList.append((largestBiconn,i))

	return biconnSizeList




GER = nx.erdos_renyi_graph(N, p, seed = SEED)

biconn = list(nx.biconnected_component_subgraphs(GER))

biconnToCall = max(biconn, key = len)

for i in range(10):

	a = findArt(biconnToCall)

	print(len(biconnToCall))

	a.sort()

	#print(a)

	b = a[:numNodesToRemove]
示例#34
0
deg_dic = []
for nd in deg:
    if deg[nd] > 0:
        deg_dic.append(nd)
node0 = random.choice(deg_dic)

for i in range(len(lc)):
    if node0 in lc[i]:
        print 'The nodes in the biconnected component of graph containing the randomly chosen node ' + str(
            node0) + ' are: \n', lc[i]
        # break
print str(" ")

colors_list = ['c', 'b', 'g', 'y', 'k', 'm']
colors_to_select = list(colors_list)
graphs = sorted(nx.biconnected_component_subgraphs(G), key=len, reverse=True)
biconnected_component_subgraphs_edges = []
biconnected_component_subgraphs_nodes = []
nodes_color_alpha = []
edges_color_alpha = []
colors_of_edges = []
edge_width_l = []
for gr in range(len(graphs)):
    graph = 'G' + str(gr + 1)
    # print 'Nodes of biconnected component', graph+':', graphs[gr].nodes()
    # print 'Edges of biconnected component', graph+':', graphs[gr].edges()
    if len(graphs[gr].nodes()) > 1 and len(graphs[gr].edges()) > 0:
        biconnected_component_subgraphs_edges.append(graphs[gr].edges())
        biconnected_component_subgraphs_nodes.append(graphs[gr].nodes())
        if len(colors_to_select) == 0:
            colors_to_select = list(colors_list)
示例#35
0
def k_components(G, flow_func=None):
    r"""Returns the k-component structure of a graph G.

    A `k`-component is a maximal subgraph of a graph G that has, at least,
    node connectivity `k`: we need to remove at least `k` nodes to break it
    into more components. `k`-components have an inherent hierarchical
    structure because they are nested in terms of connectivity: a connected
    graph can contain several 2-components, each of which can contain
    one or more 3-components, and so forth.

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

    flow_func : function
        Function to perform the underlying flow computations. Default value
        :meth:`edmonds_karp`. This function performs better in sparse graphs with
        right tailed degree distributions. :meth:`shortest_augmenting_path` will
        perform better in denser graphs.

    Returns
    -------
    k_components : dict
        Dictionary with all connectivity levels `k` in the input Graph as keys
        and a list of sets of nodes that form a k-component of level `k` as
        values.

    Raises
    ------
    NetworkXNotImplemented:
        If the input graph is directed.

    Examples
    --------
    >>> # Petersen graph has 10 nodes and it is triconnected, thus all
    >>> # nodes are in a single component on all three connectivity levels
    >>> G = nx.petersen_graph()
    >>> k_components = nx.k_components(G)

    Notes
    -----
    Moody and White [1]_ (appendix A) provide an algorithm for identifying
    k-components in a graph, which is based on Kanevsky's algorithm [2]_
    for finding all minimum-size node cut-sets of a graph (implemented in
    :meth:`all_node_cuts` function):

        1. Compute node connectivity, k, of the input graph G.

        2. Identify all k-cutsets at the current level of connectivity using
           Kanevsky's algorithm.

        3. Generate new graph components based on the removal of
           these cutsets. Nodes in a cutset belong to both sides
           of the induced cut.

        4. If the graph is neither complete nor trivial, return to 1;
           else end.

    This implementation also uses some heuristics (see [3]_ for details)
    to speed up the computation.

    See also
    --------
    node_connectivity
    all_node_cuts
    biconnected_components : special case of this function when k=2
    k_edge_components : similar to this function, but uses edge-connectivity
        instead of node-connectivity

    References
    ----------
    .. [1]  Moody, J. and D. White (2003). Social cohesion and embeddedness:
            A hierarchical conception of social groups.
            American Sociological Review 68(1), 103--28.
            http://www2.asanet.org/journals/ASRFeb03MoodyWhite.pdf

    .. [2]  Kanevsky, A. (1993). Finding all minimum-size separating vertex
            sets in a graph. Networks 23(6), 533--541.
            http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract

    .. [3]  Torrents, J. and F. Ferraro (2015). Structural Cohesion:
            Visualization and Heuristics for Fast Computation.
            http://arxiv.org/pdf/1503.04476v1

    """
    # Dictionary with connectivity level (k) as keys and a list of
    # sets of nodes that form a k-component as values. Note that
    # k-compoents can overlap (but only k - 1 nodes).
    k_components = defaultdict(list)
    # Define default flow function
    if flow_func is None:
        flow_func = default_flow_func
    # Bicomponents as a base to check for higher order k-components
    for component in nx.connected_components(G):
        # isolated nodes have connectivity 0
        comp = set(component)
        if len(comp) > 1:
            k_components[1].append(comp)
    bicomponents = list(nx.biconnected_component_subgraphs(G))
    for bicomponent in bicomponents:
        bicomp = set(bicomponent)
        # avoid considering dyads as bicomponents
        if len(bicomp) > 2:
            k_components[2].append(bicomp)
    for B in bicomponents:
        if len(B) <= 2:
            continue
        k = nx.node_connectivity(B, flow_func=flow_func)
        if k > 2:
            k_components[k].append(set(B.nodes()))
        # Perform cuts in a DFS like order.
        cuts = list(nx.all_node_cuts(B, k=k, flow_func=flow_func))
        stack = [(k, _generate_partition(B, cuts, k))]
        while stack:
            (parent_k, partition) = stack[-1]
            try:
                nodes = next(partition)
                C = B.subgraph(nodes)
                this_k = nx.node_connectivity(C, flow_func=flow_func)
                if this_k > parent_k and this_k > 2:
                    k_components[this_k].append(set(C.nodes()))
                cuts = list(nx.all_node_cuts(C, k=this_k, flow_func=flow_func))
                if cuts:
                    stack.append((this_k, _generate_partition(C, cuts,
                                                              this_k)))
            except StopIteration:
                stack.pop()

    # This is necessary because k-components may only be reported at their
    # maximum k level. But we want to return a dictionary in which keys are
    # connectivity levels and values list of sets of components, without
    # skipping any connectivity level. Also, it's possible that subsets of
    # an already detected k-component appear at a level k. Checking for this
    # in the while loop above penalizes the common case. Thus we also have to
    # _consolidate all connectivity levels in _reconstruct_k_components.
    return _reconstruct_k_components(k_components)
示例#36
0
data2 = data[PHQ9]
data_high = data[PHQ9_high]
data_low = data[PHQ9_low]
#data_high = list(data[:,PHQ9_high])
#data_low = list(data[:,PHQ9_low])

phqs = list(data2["PHQ9_score"])

degs = [len(u.neighbors(node)) for node in u]

med_degs = np.median(degs)

giant_cc = len(nx.connected_component_subgraphs(u)[0].node)

giant_bicc = len(list(nx.biconnected_component_subgraphs(u))[0].node)

triangles = nx.triangles(u)

np.median(triangles.values())

clustering = nx.clustering(u)
the_ids = list(u.node)

core_number = nx.core_number(u)
resp = u.subgraph(data2['hashed_id'])

for i in range(5000):
    samp = set(random.sample(the_ids, 185))
    samp_feats = [
        (abs(np.median([len(u.neighbors(node)) for node in samp]) - 66)) / 66,