Ejemplo n.º 1
0
def test_biconnected_eppstein():
    # tests from http://www.ics.uci.edu/~eppstein/PADS/Biconnectivity.py
    G1 = nx.Graph({
        0: [1, 2, 5],
        1: [0, 5],
        2: [0, 3, 4],
        3: [2, 4, 5, 6],
        4: [2, 3, 5, 6],
        5: [0, 1, 3, 4],
        6: [3, 4],
    })
    G2 = nx.Graph({
        0: [2, 5],
        1: [3, 8],
        2: [0, 3, 5],
        3: [1, 2, 6, 8],
        4: [7],
        5: [0, 2],
        6: [3, 8],
        7: [4],
        8: [1, 3, 6],
    })
    assert_true(nx.is_biconnected(G1))
    assert_false(nx.is_biconnected(G2))
    answer_G2 = [{1, 3, 6, 8}, {0, 2, 5}, {2, 3}, {4, 7}]
    bcc = list(nx.biconnected_components(G2))
    assert_components_equal(bcc, answer_G2)
Ejemplo n.º 2
0
    def attacks(self):
        for graph in self.graph_lists:
            G_topo = nx.Graph()
            G_topo.add_nodes_from(range(self.node_num))
            G_topo.add_edges_from(graph)
            # graph connectivity measures without attacks 
            self.flowrobust[0]+=self.__flow_robust(G_topo)
            if nx.is_biconnected(G_topo):
                self.biconnect[0]+=1
            self.maxdiam[0]+=self.__max_diam(G_topo)
            self.mindegree[0]+=min(G_topo.degree().values())
            #graph connectivity measures under node failures 
            for fail_rate in [0.1, 0.2, 0.3, 0.4, 0.5]:
                node_to_remove = \
                range(int(self.node_num*fail_rate-self.node_num*0.1),
                int(self.node_num*fail_rate))
                
                for node_index in node_to_remove:
                    G_topo.remove_node(self.attack_seq[node_index])

                self.flowrobust[int(fail_rate*10)] += self.__flow_robust(G_topo)
                if nx.is_biconnected(G_topo):
                    self.biconnect[int(fail_rate*10)]+=1        
                self.maxdiam[int(fail_rate*10)] += self.__max_diam(G_topo)
                self.mindegree[int(fail_rate*10)]+=min(G_topo.degree().values())
Ejemplo n.º 3
0
def planar_cycle_basis_impl(g):
	assert not g.is_directed()
	assert not g.is_multigraph()
	assert nx.is_biconnected(g) # BAND-AID

	# "First" nodes/edges for each cycle are chosen in an order such that the first edge
	#  may never belong to a later cycle.

	# rotate to (hopefully) break any ties or near-ties along the y axis
	rotate_graph(g, random.random() * 2 * math.pi)

	# NOTE: may want to verify that no ties exist after the rotation

	nx.set_edge_attributes(g, 'used_once', {e: False for e in g.edges()})

	# precompute edge angles
	angles = {}
	for s,t in g.edges():
		angles[s,t] = edge_angle(g,s,t) % (2*math.pi)
		angles[t,s] = (angles[s,t] + math.pi) % (2*math.pi)

	# identify and clear away edges which cannot belong to cycles
	for v in g:
		if degree(g,v) == 1:
			remove_filament_from_tip(g, v)

	cycles = []

	# sort ascendingly by y
	for root in sorted(g, key = lambda v: g.node[v]['y']):

		# Check edges in ccw order from the +x axis
		for target in sorted(g[root], key=lambda t: angles[root,t]):

			if not g.has_edge(root, target):
				continue

			discriminator, path = trace_ccw_path(g, root, target, angles)

			if discriminator == PATH_PLANAR_CYCLE:
				assert path[0] == path[-1]
				remove_cycle_edges(g, path)
				cycles.append(path)

			# Both the dead end and the initial edge belong to filaments
			elif discriminator == PATH_DEAD_END:
				remove_filament_from_edge(g, root, target)
				remove_filament_from_tip(g, path[-1])

			# The initial edge must be part of a filament
			# FIXME: Not necessarily true if graph is not biconnected
			elif discriminator == PATH_OTHER_CYCLE:
				remove_filament_from_edge(g, root, target)

			else: assert False # complete switch

			assert not g.has_edge(root, target)

	assert len(g.edges()) == 0
	return cycles
Ejemplo n.º 4
0
 def bases(self):
     base_flowrobust = []
     base_biconnect = []
     for graph in self.graph_lists:
         print graph
         G_topo = nx.Graph()
         G_topo.add_edges_from(graph)
         base_biconnect.append(nx.is_biconnected(G_topo))
         base_flowrobust.append(self.__flow_robust(G_topo))
     return base_flowrobust
Ejemplo n.º 5
0
def _generate_no_biconnected(max_attempts=50):
    attempts = 0
    while True:
        G = nx.fast_gnp_random_graph(100, 0.0575)
        if nx.is_connected(G) and not nx.is_biconnected(G):
            attempts = 0
            yield G
        else:
            if attempts >= max_attempts:
                msg = "Tried %d times: no suitable Graph."
                raise Exception(msg % max_attempts)
            else:
                attempts += 1
Ejemplo n.º 6
0
def _generate_no_biconnected(max_attempts=50):
    attempts = 0
    while True:
        G = nx.fast_gnp_random_graph(100,0.0575)
        if nx.is_connected(G) and not nx.is_biconnected(G):
            attempts = 0
            yield G
        else:
            if attempts >= max_attempts:
                msg = "Tried %d times: no suitable Graph."
                raise Exception(msg % max_attempts)
            else:
                attempts += 1
def relay_placement(input_file_path):
    file_dir = os.path.dirname(input_file_path)
    file = os.path.basename(input_file_path)
    file_name, file_type = file.split('_')
    out_file_path = os.path.join(file_dir, file_name)

    if file_type == 'pickle':
        G = pj.load_pickle(input_file_path)

    subg = make_subgraph(G)

    plt.figure(1)
    pj.draw_graph(subg, out_file_path + "_terminals")
    terminals = subg.nodes()
    #print (terminals)

    print(nx.is_biconnected(subg))
    if nx.is_connected(subg):
        mst = nx.minimum_spanning_tree(subg, weight="weight")
        #print (T1.edges(data=True))
        expand_mst = nx.Graph()
        plt.figure(2)
        pj.draw_graph(mst, out_file_path + "_terminal_mst")
        #pdb.set_trace()

        #expanding the MST
        for e in mst.edges():
            temp = G.subgraph(nx.shortest_path(G, e[0], e[1], weight="weight"))
            expand_mst.add_nodes_from(temp.nodes(data=True))
            expand_mst.add_edges_from(temp.edges(data=True))
        plt.figure(3)
        pj.draw_graph(expand_mst, out_file_path + "_expanded_mst")

        steiner = nx.minimum_spanning_tree(expand_mst)
        while True:
            n = dict(nx.degree(steiner))
            bad_leaf = [
                k for k, v in n.items() if v == 1 and k not in terminals
            ]
            print(bad_leaf)
            if not len(bad_leaf):
                break
            steiner.remove_nodes_from(bad_leaf)
        plt.figure(4)
        pj.draw_graph(steiner, out_file_path + "_RNPC")
        pj.store_pickle(steiner, out_file_path + "RNPC_pickle")
    else:
        print(
            "Terminals of the given graph do not lie in a single sonnected component. Thus it cannot be processed further"
        )
Ejemplo n.º 8
0
def add_non_connected_vertices_to_cliques(graph, cliques, duplicate_articulation = False):
    for v in graph:
        if v not in list( cliques.index ):
            # Add to a clique to see if it connected and add it to clique if it is
            for i in range(1, cliques.max() + 1):
                vertices = cliques.loc[cliques == i].index.tolist()
                Hsub = graph.subgraph(vertices + [v])
                if nx.is_biconnected(Hsub):
                    cliques[v] = i
                    if not duplicate_articulation:
                        break # break if we only want articulation points in one of the cliques
        # If cannot be added to an excisting clique make new clique
        if v not in list( cliques.index ):
            cliques[v] = cliques.max() + 1
    return cliques
Ejemplo n.º 9
0
    def prepare_adhoc_topology(self):

        # Initialize switches and their locations
        for i in xrange(self.total_switches):
            switch_id = "s" + str(i+1)
            s_dict = {"switch_id": switch_id,
                      "x_pos": random.uniform(self.params["min_x"], self.params["max_x"]),
                      "y_pos": random.uniform(self.params["min_y"], self.params["max_y"])
                      }
            self.graph.add_node(switch_id, s=s_dict)

        # Add edges if the distance between switches is within a threshold
        max_dist = math.sqrt(
            (self.params["max_x"] - self.params["min_x"]) ** 2 + (self.params["max_y"] - self.params["min_y"]) ** 2)

        for thresh_frac in [x/1000.0 for x in xrange(1, 1000)]:
            thresh = thresh_frac * max_dist
            self.add_edges_within_threshold(thresh)
            if is_biconnected(self.graph):
                print thresh_frac
                break
Ejemplo n.º 10
0
 def setStats(self):
     """1) go through each of the disjoint graphs
        2) decide if it is one of the following a) single node b) cycle
        c) line d) cyclic tree like structure e) acyclic tree like structure
        3) And set stats for each subgraph
        if edges are still untraced, then enumerate and remove them paths using
         self._branchToBranch
     """
     start = time.time()
     countDisjointGraphs = len(self._disjointGraphs)
     for self._ithDisjointGraph, self._subGraphSkeleton in enumerate(self._disjointGraphs):
         self._findAccessComponentsDisjoint()
         if len(self._nodes) == 1:
             self.typeGraphdict[self._ithDisjointGraph] = SubgraphTypes.singleNode.value
         elif (min(self._degreeList) == max(self._degreeList) and
               nx.is_biconnected(self._subGraphSkeleton) and
               self._cycleCount == 1):
             self._singleCycle(self._cycleList[0])
             self.typeGraphdict[self._ithDisjointGraph] = SubgraphTypes.singleCycle.value
         elif set(self._degreeList) == set((1, 2)) or set(self._degreeList) == {1}:
             self._singleSegment(self._nodes)
             self.typeGraphdict[self._ithDisjointGraph] = SubgraphTypes.singleLine.value
         elif self._cycleCount != 0:
             self._cyclicTree(self._cycleList)
             self.typeGraphdict[self._ithDisjointGraph] = SubgraphTypes.cyclic.value
         else:
             self._tree()
             self.typeGraphdict[self._ithDisjointGraph] = SubgraphTypes.acyclic.value
         # check if any unfinished business in _subGraphSkeleton, untraced edges
         if self._subGraphSkeleton.number_of_edges() != 0:
             self._branchToBranch()
         assert self._subGraphSkeleton.number_of_edges() == 0, ("edges not removed are"
                                                                "%i" % self._subGraphSkeleton.number_of_edges())
         progress = int((100 * self._ithDisjointGraph) / countDisjointGraphs)
         print("finding segment stats in progress {}% \r".format(progress), end="", flush=True)
         if True:
             print()
     self._findAccessComponentsNetworkx()
     print("time taken to calculate segments and their lengths is %0.3f seconds" % (time.time() - start))
Ejemplo n.º 11
0
def print_is_of_type_attrs(graph):
	print("\n====== is of type X? ======")
	print("Directed? ->", "Yes" if nx.is_directed(graph) else "No")
	print("Directed acyclic? ->", "Yes" if nx.is_directed_acyclic_graph(graph) else "No")
	print("Weighted? ->", "Yes" if nx.is_weighted(graph) else "No")

	if nx.is_directed(graph):
		print("Aperiodic? ->", "Yes" if nx.is_aperiodic(graph) else "No")
		print("Arborescence? ->", "Yes" if nx.is_arborescence(graph) else "No")
		print("Weakly Connected? ->", "Yes" if nx.is_weakly_connected(graph) else "No")
		print("Semi Connected? ->", "Yes" if nx.is_semiconnected(graph) else "No")
		print("Strongly Connected? ->", "Yes" if nx.is_strongly_connected(graph) else "No")

	else:
		print("Connected? ->", "Yes" if nx.is_connected(graph) else "No")		
		print("Bi-connected? ->", "Yes" if nx.is_biconnected(graph) else "No")
		if not graph.is_multigraph():
			print("Chordal? -> ", "Yes" if nx.is_chordal(graph) else "No")
			print("Forest? -> ", "Yes" if nx.is_chordal(graph) else "No")

	print("Distance regular? -> ", "Yes" if nx.is_distance_regular(graph) else "No")
	print("Eulerian? -> ", "Yes" if nx.is_eulerian(graph) else "No")
	print("Strongly regular? -> ", "Yes" if nx.is_strongly_regular(graph) else "No")
	print("Tree? -> ", "Yes" if nx.is_tree(graph) else "No")
Ejemplo n.º 12
0
                    VN[l[0]]=l[1].replace("\n","").replace("\r","")
                    G.add_node(VN[l[0]])
                flag=1
        A = [[0 for x in range(len(V))] for x in range(len(V))]
        flag=0
        for each_line in pl:
            l=each_line.split(',')
            if len(l)==5:
                if flag!=0:
                    #print(l[0],l[1],l[3])
                    A[int(l[0][1:])][int(l[1][1:])]=float(l[3])
                    edge=(VN[l[0]],VN[l[1]])
                    G.add_edge(*edge)
                flag=1
            else:
                continue
        print nx.is_biconnected(G)
        
        comp=list(nx.biconnected_components(G))
        print (len(comp))
                  
                  
        for i in range(len(comp)):
            k=0
            for j in list(comp[i]):
                sheet.write(k, i, j)
                k+=1
        book.save("biconnectedness/"+each_file[:-4]+".xls")
        fp.close()

Ejemplo n.º 13
0
    # print counte,'a'

pos = nx.spring_layout(G, k=0.15, iterations=10)

print str(" ")
print 'BICONNECTEDNESS OF UNDIRECTED GRAPHS'
print str(" ")

G.remove_nodes_from(nx.isolates(G))
lc = sorted(nx.biconnected_components(G), key=len, reverse=True)

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

print 'Is graph G biconnected?', nx.is_biconnected(G)
print 'Is graph G connected?', nx.is_connected(G)
print 'The number of biconnected components of G is:', len(lc)
print 'The number of connected components of G is:', nx.number_connected_components(
    G)
print str(" ")

print 'List of biconnected components:'
print lc
print str(" ")

deg = G.degree()
deg_dic = []
for nd in deg:
    if deg[nd] > 0:
        deg_dic.append(nd)
Ejemplo n.º 14
0
def planar_cycle_basis_impl(g):
    assert not g.is_directed()
    assert not g.is_multigraph()
    assert nx.is_biconnected(g)  # BAND-AID

    # "First" nodes/edges for each cycle are chosen in an order such that the first edge
    #  may never belong to a later cycle.

    # rotate to (hopefully) break any ties or near-ties along the y axis
    rotate_graph(g, random.random() * 2 * math.pi)

    # NOTE: may want to verify that no ties exist after the rotation

    nx.set_edge_attributes(g, 'used_once', {e: False for e in g.edges()})

    # precompute edge angles
    angles = {}
    for s, t in g.edges():
        angles[s, t] = edge_angle(g, s, t) % (2 * math.pi)
        angles[t, s] = (angles[s, t] + math.pi) % (2 * math.pi)

    # identify and clear away edges which cannot belong to cycles
    for v in g:
        if degree(g, v) == 1:
            remove_filament_from_tip(g, v)

    cycles = []

    # sort ascendingly by y
    for root in sorted(g, key=lambda v: g.node[v]['y']):

        # Check edges in ccw order from the +x axis
        for target in sorted(g[root], key=lambda t: angles[root, t]):

            if not g.has_edge(root, target):
                continue

            discriminator, path = trace_ccw_path(g, root, target, angles)

            if discriminator == PATH_PLANAR_CYCLE:
                assert path[0] == path[-1]
                remove_cycle_edges(g, path)
                cycles.append(path)

            # Both the dead end and the initial edge belong to filaments
            elif discriminator == PATH_DEAD_END:
                remove_filament_from_edge(g, root, target)
                remove_filament_from_tip(g, path[-1])

            # The initial edge must be part of a filament
            # FIXME: Not necessarily true if graph is not biconnected
            elif discriminator == PATH_OTHER_CYCLE:
                remove_filament_from_edge(g, root, target)

            else:
                assert False  # complete switch

            assert not g.has_edge(root, target)

    assert len(g.edges()) == 0
    return cycles
# plt.show()

# position = nx.fruchterman_reingold_layout(G)

# nx.draw_networkx_nodes(G,position, nodelist=G.nodes(), node_color="b")
nx.draw_networkx_nodes(G, position, nodelist=CDS, node_color="r")

# nx.draw_networkx_edges(G,position)
# nx.draw_networkx_labels(G,position)

plt.savefig("3_1-conn_3-dom.png")


graphCDS = G.subgraph(CDS)

if not (nx.is_biconnected(graphCDS)):
    print "\n"
    CDS = compute_2_connected_k_dominating_set(G, CDS)
    print "2-connected 3-dominating set"
    print CDS
    print "\n"
else:
    print "Above set is also 2-connected"
    print "\n"

# position = nx.fruchterman_reingold_layout(G)

# nx.draw_networkx_nodes(G,position, nodelist=G.nodes(), node_color="b")
nx.draw_networkx_nodes(G, position, nodelist=CDS, node_color="r")

# nx.draw_networkx_edges(G,position)
def test_null_graph():
    G = nx.Graph()
    assert_false(nx.is_biconnected(G))
    assert_equal(list(nx.biconnected_components(G)), [])
    assert_equal(list(nx.biconnected_component_edges(G)), [])
    assert_equal(list(nx.articulation_points(G)), [])
def test_empty_is_biconnected():
    G = nx.empty_graph(5)
    assert_false(nx.is_biconnected(G))
    G.add_edge(0, 1)
    assert_false(nx.is_biconnected(G))
Ejemplo n.º 18
0
def add_sna_metrics(arradata):

    conn = ad.get_connection()

    sql_query = ("""
            SELECT main_instagrammedia."userId" user_id, photo_id, main_usersinphoto."userId" user_tagged
            FROM main_usersinphoto
            JOIN main_instagrammedia ON main_usersinphoto.photo_id = main_instagrammedia.id
            """)
    all_tag_data = pd.read_sql(sql_query, conn)

    users = list(all_tag_data['user_id'].unique())

    sna_col_names = [
        'largest_clique',
        'density',
        'edge_count',
        'average_clustering',  #'assortativity',
        'clique_count',
        'transitivity',
        'connected',
        'connected_components',
        'biconnected',
        'node_connectivity',
        'edge_connectivity',
        'average_connectivity',
        'radius',
        'diameter',
        'average_shortest_path',
        'isolates',
        'selfies_count',
        'node_count'
    ]

    all_sna_metrics = []

    for u in users:
        tag_data = all_tag_data[all_tag_data['user_id'] == u][[
            'photo_id', 'user_tagged'
        ]]
        selfies_count = sum(tag_data['user_tagged'] == u)
        tag_data = tag_data[tag_data['user_tagged'] != u]
        if tag_data.shape[0] > 0:
            taggers = list(tag_data['user_tagged'].unique())
            nNodes = len(taggers) + 1
            tag_matrix = np.array([0] * nNodes * nNodes).reshape(
                (nNodes, nNodes))
            tag_index = lambda user: taggers.index(user) + 1
            tag_data['tag_index'] = tag_data[[
                'user_tagged'
            ]].applymap(tag_index)['user_tagged']
            photo_list = list(tag_data['photo_id'].unique())
            tag_master = [
                [0] + list(tag_data[tag_data['photo_id'] == p]['tag_index'])
                for p in photo_list
            ]
            for t in tag_master:
                for i in range(len(t)):
                    for j in range(i + 1, len(t)):
                        tag_matrix[t[i], t[j]] = tag_matrix[t[i], t[j]] + 1
            G = nx.from_numpy_matrix(
                np.matrix(tag_matrix, dtype=[('weight', int)]))

            sna_metrics = [
                u,
                nx.graph_clique_number(G),
                nx.density(G),
                nx.number_of_edges(G),
                nx.average_clustering(G),
                #nx.degree_assortativity_coefficient(G),
                nx.graph_number_of_cliques(G),
                nx.transitivity(G),
                nx.is_connected(G),
                nx.number_connected_components(G),
                nx.is_biconnected(G),
                nx.node_connectivity(G),
                nx.edge_connectivity(G),
                nx.average_node_connectivity(G),
                nx.radius(G),
                nx.diameter(G),
                nx.average_shortest_path_length(G),
                len(nx.isolates(G)),
                selfies_count,
                nx.number_of_nodes(G)
            ]

            all_sna_metrics.append(sna_metrics)

    sna_df = pd.DataFrame(all_sna_metrics, columns=['user_id'] + sna_col_names)
    sna_df.index = sna_df['user_id']
    del sna_df.index.name
    sna_df = sna_df.drop('user_id', axis=1)

    #put on social shift columns
    arradata = add_social_shift(arradata, conn, users)

    #sna_df = sna_df.fillna({'assortativity': 0.0})

    arradata = pd.merge(arradata,
                        sna_df,
                        left_index=True,
                        right_index=True,
                        how='left')

    G = nx.Graph()
    G.add_node(1)

    sna_trivial = [
        nx.graph_clique_number(G),
        nx.density(G),
        nx.number_of_edges(G),
        nx.average_clustering(G),
        #0.0, #nx.degree_assortativity_coefficient(G), same as before
        nx.graph_number_of_cliques(G),
        nx.transitivity(G),
        nx.is_connected(G),
        nx.number_connected_components(G),
        nx.is_biconnected(G),
        nx.node_connectivity(G),
        nx.edge_connectivity(G),
        nx.average_node_connectivity(G),
        nx.radius(G),
        nx.diameter(G),
        0,  #nx.average_shortest_path_length(G), although it's formally infinite...
        len(nx.isolates(G)),
        0,  # Selfies count
        nx.number_of_nodes(G)
    ]

    sna_na_dict = {
        sna_col_names[i]: sna_trivial[i]
        for i in range(len(sna_trivial))
    }
    arradata = arradata.fillna(sna_na_dict)

    arradata[
        'selfies_percentage'] = arradata['selfies_count'] / arradata['media']
    arradata = arradata.fillna({'selfies_percentage': 0})

    sna_loc_start = list(arradata.columns).index('largest_clique')
    sna_loc_end = arradata.shape[1]

    trivial_cols_index = []
    for i in range(sna_loc_start, sna_loc_end):
        if len(arradata.ix[:, i].unique()) == 1: trivial_cols_index.append(i)
    trivial_cols = [
        list(arradata.columns)[index] for index in trivial_cols_index
    ]

    ad_cleaned = arradata.drop(trivial_cols, axis=1)

    sna_corr = ad_cleaned.ix[:, sna_loc_start:].corr()

    corrs = []
    for i in range(sna_corr.shape[0]):
        ones = sna_corr.ix[i][sna_corr.ix[i] == 1.0].index.tolist()
        ones_ind = [sna_corr.columns.tolist().index(o) for o in ones]
        if (len(ones_ind) > 1) and ones_ind not in corrs:
            corrs.append(ones_ind)
    corrs_labels = [[sna_corr.columns.tolist()[a] for a in c] for c in corrs]

    for l in corrs_labels:
        ad_cleaned = ad_cleaned.drop(l[1:], axis=1)

    return ad_cleaned
Ejemplo n.º 19
0
                membership[n.organism] = []
            membership[n.organism].append([n.name, i+1])

    print '\nHistogram of component order'
    print 'order\tcount'
    for n in sorted(sg_order_hist):
        print '{0}\t{1}'.format(n, sg_order_hist[n])

print 'Writing subgraph info table.'
with open(args.output[0] + '.subgraph', 'w') as subinfo_h:
    subinfo_wr = csv.writer(subinfo_h, delimiter='\t')
    subinfo_wr.writerow(['id', 'size', 'order', 'density', 'eularian', 'binconn', 'modularity'])
    for i, sg in enumerate(subgraphs):
        part = com.best_partition(sg)
        subinfo_wr.writerow([i+1, sg.size(), sg.order(), nx.density(sg),
                             nx.is_eulerian(sg), nx.is_biconnected(sg), com.modularity(part, sg)])

if args.writesubs:
    print 'Writing all subgraphs ...'
    for i, sg in enumerate(subgraphs):
        out = os.path.join(args.work_dir[0], 'og{0}.graphml'.format(i+1))
        nx.write_graphml(sg, out)

iso_count = 0
for org in membership:
    print 'There remained {0} isolate genes in {1}'.format(len(isolate_genes[org]), org)
    with open(org + '.memb', 'w') as memb_h:
        memb_wr = csv.writer(memb_h, delimiter='\t')
        memb_wr.writerow(['gene','og'])
        for gn in membership[org]:
            memb_wr.writerow(gn)
Ejemplo n.º 20
0
nx.write_adjlist(G, "test.adjlist")

# find node near center (0.5,0.5)
dmin = 1
ncenter = 0
for n in pos:
    print pos[n]
    x, y = pos[n]
    d = (x - 0.5)**2 + (y - 0.5)**2
    if d < dmin:
        ncenter = n
        dmin = d

print 'is connected: %s' % (nx.is_connected(G))
print 'is bi-connected: %s' % (nx.is_biconnected(G))

nx.draw(G, pos)
plt.show()

# color by path length from node near center
#p=nx.single_source_shortest_path_length(G,ncenter)

#plt.figure(figsize=(8,8))
#nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
#nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),
#node_size=80,
#node_color=p.values(),
#cmap=plt.cm.Reds_r)

#plt.xlim(-10,10)
Ejemplo n.º 21
0
    g = nx.Graph()
    #g = nx.DiGraph()
    g.add_nodes_from(range(len(lines)))
    for row in range(len(lines)):
        for col in range(len(lines)):
            if int(lines[row][col]) == 1:
                g.add_edge(row,col)
    print "Done reading all the files"  
    graphs[item] = g


for g in graphs.keys():
    try:
        print "====================== " , g, "==========================="
        print "number of isolated nodes",'\t\t\t',nx.isolates(graphs[g])
        print "is graph biconnected?",'\t\t\t',nx.is_biconnected(graphs[g])
        print "cut vertices are: ",'\t\t\t',list(nx.articulation_points(graphs[g]))
        print "number of nodes",'\t\t\t',len(graphs[g].nodes())
        print "number of edges",'\t\t\t',len(graphs[g].edges())
        print "degree",'\t\t\t',get_avg(graphs[g].degree())
        print "diameter",'\t\t\t', nx.diameter(graphs[g])
        print "radius",'\t\t\t', nx.radius(graphs[g])
        print "is_bipartite?",'\t\t', bipartite.is_bipartite(graphs[g])
        print "average_shortest_path_length",'\t\t', nx.average_shortest_path_length(graphs[g])
        print "degree_assortativity_coefficient",'\t\t', nx.degree_assortativity_coefficient(graphs[g])
        print "assortativity.average_degree_connectivity",'\t\t', nx.assortativity.average_degree_connectivity(graphs[g])
        #print "degree_pearson_correlation_coefficient",'\t\t', nx.degree_pearson_correlation_coefficient(graphs[g])
        print "node closeness_centrality",'\t\t\t', get_avg(nx.closeness_centrality(graphs[g]))
        print "clustering",'\t\t\t', get_avg(nx.clustering(graphs[g]))
        print "node betweeness",'\t\t\t', get_avg(nx.betweenness_centrality(graphs[g],normalized=False,endpoints=False))
        print "edge betweeness",'\t\t\t', get_avg(nx.edge_betweenness_centrality(graphs[g],normalized=False))
Ejemplo n.º 22
0
def test_null_graph():
    G = nx.Graph()
    assert_false(nx.is_biconnected(G))
    assert_equal(list(nx.biconnected_components(G)), [])
    assert_equal(list(nx.biconnected_component_edges(G)), [])
    assert_equal(list(nx.articulation_points(G)), [])
Ejemplo n.º 23
0
nx.write_adjlist(G, "test.adjlist")

# find node near center (0.5,0.5)
dmin=1
ncenter=0
for n in pos:
    print pos[n]
    x,y=pos[n]
    d=(x-0.5)**2+(y-0.5)**2
    if d<dmin:
        ncenter=n
        dmin=d
        
print 'is connected: %s' % (nx.is_connected(G))
print 'is bi-connected: %s' % (nx.is_biconnected(G))

nx.draw(G,pos)
plt.show()

# color by path length from node near center
#p=nx.single_source_shortest_path_length(G,ncenter)

#plt.figure(figsize=(8,8))
#nx.draw_networkx_edges(G,pos,nodelist=[ncenter],alpha=0.4)
#nx.draw_networkx_nodes(G,pos,nodelist=p.keys(),
                       #node_size=80,
                       #node_color=p.values(),
                       #cmap=plt.cm.Reds_r)

#plt.xlim(-10,10)
Ejemplo n.º 24
0
def min_tree(input_file_path):
    file_dir = os.path.dirname(input_file_path)
    file = os.path.basename(input_file_path)
    file_name, file_type = file.split('_')
    out_file_path = os.path.join(file_dir, file_name)
    test_path = os.path.join(file_dir, 'steps', 'try_')

    if file_type == 'pickle':
        G = pj.load_pickle(input_file_path)

    reduced_graph = remove_relay(G)

    #    plt.figure(1)
    #    pj.draw_graph(reduced_graph, out_file_path+"_terminals")
    fig = 1
    pj.draw_graph(reduced_graph, test_path + "terminal" + str(fig))
    fig += 1
    #terminals = reduced_graph.nodes()
    #print (terminals)

    #finding the sink (Base Station)
    for node in reduced_graph.nodes(data=True):
        if node[1]['type'] == 'BS':
            sink_id = node[1]['id']
            break

    print(nx.is_biconnected(reduced_graph))
    if nx.is_connected(reduced_graph):

        #CMST begins================================================

        #initialize:----------------------------------
        Tree = nx.Graph()

        #getting hop counts and max hop counts
        hop_tuples = []
        for node in reduced_graph.nodes():
            reduced_graph.nodes[node]['hops'] = nx.shortest_path_length(
                reduced_graph, sink_id, node)
            if reduced_graph.nodes[node]['hops'] > 1:
                hop_tuples += [(node, reduced_graph.nodes[node]['hops'])]
        hop_tuples = sorted(hop_tuples, key=lambda x: x[1])
        max_hop_count = hop_tuples[-1][1]

        single_hop = []
        #finding and connecting roots of top sub-trees
        for sink, branch_root in reduced_graph.edges(sink_id):
            single_hop += [branch_root]
            temprary = G.subgraph([sink] +
                                  reduced_graph.edges[sink,
                                                      branch_root]['link'] +
                                  [branch_root])
            Tree.add_nodes_from(temprary.nodes(data=True))
            Tree.add_edges_from(temprary.edges(data=True))
#        plt.figure(2)
#        pj.draw_graph(Tree, out_file_path+"_tree")

        pj.draw_graph(Tree, test_path + "tree" + str(fig))
        fig += 1
        #updating reduced tree - removing unrequired edges
        # is this needed?
        for i in list(combinations(single_hop, 2)):
            if reduced_graph.has_edge(i[0], i[1]):
                reduced_graph.remove_edge(i[0], i[1])
#        plt.figure(1)
#        pj.draw_graph(reduced_graph, out_file_path+"_terminals")

        pj.draw_graph(reduced_graph, test_path + "terminal" + str(fig))
        fig += 1
        #UPDATES
        #defining branches, their loads and their components
        branches = {}
        dfs_edges = list(nx.dfs_edges(Tree, source=sink_id))
        for parent, child in dfs_edges:
            if parent == sink_id:
                branch = child
                branches[branch] = {}
                branches[branch]['nodes'] = set([child])
                branches[branch]['load'] = 0
                if Tree.nodes[child]['type'] == 'S':
                    branches[branch]['load'] += 1
            else:
                branches[branch]['nodes'] = set([child
                                                 ]) | branches[branch]['nodes']
                if Tree.nodes[child]['type'] == 'S':
                    branches[branch]['load'] += 1
        #print (dfs_edges)
        #print (branches)

        #UPDATES
        #making growth sets, parent sets and hop sets
        all_sets = {}
        hop_set = {}
        #print(hop_tuples)
        for node, hop in hop_tuples:
            if hop in hop_set:
                hop_set[hop] = set([node]) | hop_set[hop]
            else:
                hop_set[hop] = set([node])

            all_sets[node] = {'growth': set([]), 'parents': set([])}
            for vertex, adj_vert in reduced_graph.edges(node):
                #if parent
                if reduced_graph.nodes[adj_vert]['hops'] < reduced_graph.nodes[
                        vertex]['hops']:
                    all_sets[node]['parents'] = set(
                        [adj_vert]) | all_sets[node]['parents']

                elif reduced_graph.nodes[adj_vert][
                        'hops'] > reduced_graph.nodes[vertex]['hops']:
                    all_sets[node]['growth'] = set(
                        [adj_vert]) | all_sets[node]['growth']

        #print(all_sets)
        #print(hop_set)

        #iteration

        for h in range(2, max_hop_count + 1):
            done = []
            left_tuple = []
            #directly connecting nodes with single parent
            for node in hop_set[h]:
                if len(all_sets[node]['parents']) == 1:
                    done += [node]
                    temp = G.subgraph([node] + reduced_graph.edges[
                        node, all_sets[node]['parents'][0]]['link'] +
                                      all_sets[node]['parents'])
                    Tree.add_nodes_from(temp.nodes(data=True))
                    Tree.add_edges_from(temp.edges(data=True))
                else:
                    left_tuple += [(node, len(all_sets[node]['growth']))]

#            plt.figure(2)
#            pj.draw_graph(Tree, out_file_path+"_tree")

            pj.draw_graph(Tree, test_path + "tree" + str(fig))
            fig += 1
            #updating reduced tree - removing unrequired edges
            # is this needed?
            for i in done:
                for j in hop_set[h]:
                    if reduced_graph.has_edge(i, j):
                        reduced_graph.remove_edge(i, j)
#            plt.figure(1)
#            pj.draw_graph(reduced_graph, out_file_path+"_terminals")

            pj.draw_graph(reduced_graph, test_path + "terminal" + str(fig))
            fig += 1
            #updating branches, their loads and their components
            dfs_edges = list(nx.dfs_edges(Tree, source=sink_id))
            for parent, child in dfs_edges:
                if parent == sink_id:
                    branch = child
                    branches[branch] = {}
                    branches[branch]['nodes'] = set([child])
                    branches[branch]['load'] = 0
                    if Tree.nodes[child]['type'] == 'S':
                        branches[branch]['load'] += 1
                else:
                    branches[branch]['nodes'] = set(
                        [child]) | branches[branch]['nodes']
                    if Tree.nodes[child]['type'] == 'S':
                        branches[branch]['load'] += 1

            while len(left_tuple) > 0:
                left_tuple = sorted(left_tuple, key=lambda x: x[0])
                left_tuple = sorted(left_tuple, key=lambda x: x[1])
                node = left_tuple[0][0]
                left_tuple_temp = left_tuple[1:]
                left_tuple = []
                metric_tuple = []
                for branch in branches:
                    if len(branches[branch]['nodes']
                           & all_sets[node]['parents']) > 0:
                        #generate the search set if h<max_hop_count
                        for parent in (branches[branch]['nodes']
                                       & all_sets[node]['parents']):
                            ss = set([])
                            relay_set = set([])
                            temp_redgraph = reduced_graph.copy()
                            for i in done:
                                if temp_redgraph.has_edge(node, i):
                                    temp_redgraph.remove_edge(node, i)
                            for p in all_sets[node]['parents']:
                                if temp_redgraph.has_edge(node, p):
                                    temp_redgraph.remove_edge(node, p)
                            for c in all_sets[node]['growth']:
                                flag = 0
                                for cp in all_sets[c]['parents']:
                                    if cp != node and flag == 0:
                                        for path in nx.all_simple_paths(
                                                temp_redgraph,
                                                source=cp,
                                                target=sink_id,
                                                cutoff=h):
                                            if len(
                                                    set(path)
                                                    & branches[branch]['nodes']
                                            ) == 0:
                                                flag = 1
                                                break
                                if flag == 0:
                                    ss = ss | set([c])
                                    relay_set = relay_set | set(
                                        reduced_graph.edges[node, c]['link'])
                            #print ('ss = '+str(ss))
                            #TODO - replace with link weight
                            relay_set = relay_set | set(
                                reduced_graph.edges[node, parent]['link'])
                            relay_count = len(relay_set -
                                              branches[branch]['nodes'])
                            #case1 metric
                            #metric = branches[branch]['load']+1+len(ss)+relay_count
                            #case2 metric
                            metric = branches[branch]['load'] + 1 + len(ss)
                            # checking if branches are fusing
                            relay_flag = 0
                            for b in branches:
                                if b != branch:
                                    if len(relay_set
                                           & branches[b]['nodes']) > 0:
                                        relay_flag = 1
                                        break
                            if (relay_flag == 0):
                                metric_tuple += [(metric, relay_count, branch,
                                                  parent, ss)]
                #TODO - devise how to choose the best parent within a branch
                metric_tuple = sorted(metric_tuple, key=lambda y: y[1])
                metric_tuple = sorted(metric_tuple, key=lambda y: y[0])
                #TODO - add search set addition and remove them from further hop metrix and their links too
                temp = G.subgraph(
                    [node] +
                    reduced_graph.edges[node, metric_tuple[0][3]]['link'] +
                    [metric_tuple[0][3]])
                Tree.add_nodes_from(temp.nodes(data=True))
                Tree.add_edges_from(temp.edges(data=True))
                for n in metric_tuple[0][4]:
                    temp = G.subgraph([n] +
                                      reduced_graph.edges[n, node]['link'] +
                                      [node])
                    Tree.add_nodes_from(temp.nodes(data=True))
                    Tree.add_edges_from(temp.edges(data=True))
#                plt.figure(2)
#                pj.draw_graph(Tree, out_file_path+"_tree")

                pj.draw_graph(Tree, test_path + "tree" + str(fig))
                fig += 1
                #updating reduced tree - removing unrequired edges
                # is this needed?
                for i in hop_set[h]:
                    if reduced_graph.has_edge(node, i):
                        reduced_graph.remove_edge(node, i)
                for p in all_sets[node]['parents']:
                    if p != metric_tuple[0][3]:
                        reduced_graph.remove_edge(node, p)
                for n in metric_tuple[0][4]:
                    hop_set[h + 1] = hop_set[h + 1] - set([n])
                    for i in hop_set[h + 1]:
                        if reduced_graph.has_edge(n, i):
                            reduced_graph.remove_edge(n, i)
                    for p in all_sets[n]['parents']:
                        if p != node:
                            reduced_graph.remove_edge(n, p)
#                plt.figure(1)
#                pj.draw_graph(reduced_graph, out_file_path+"_terminals")

                pj.draw_graph(reduced_graph, test_path + "terminal" + str(fig))
                fig += 1

                done += [node]

                #updating branches, their loads and their components
                dfs_edges = list(nx.dfs_edges(Tree, source=sink_id))
                for parent, child in dfs_edges:
                    if parent == sink_id:
                        branch = child
                        branches[branch] = {}
                        branches[branch]['nodes'] = set([child])
                        branches[branch]['load'] = 0
                        if Tree.nodes[child]['type'] == 'S':
                            branches[branch]['load'] += 1
                    else:
                        branches[branch]['nodes'] = set(
                            [child]) | branches[branch]['nodes']
                        if Tree.nodes[child]['type'] == 'S':
                            branches[branch]['load'] += 1
                #TODO - may need to change these for higher hop count nodes as well
                #updating growth sets and parent sets
                for n, a in left_tuple_temp:
                    all_sets[n] = {'growth': set([]), 'parents': set([])}
                    for vertex, adj_vert in reduced_graph.edges(n):
                        #if parent
                        if reduced_graph.nodes[adj_vert][
                                'hops'] < reduced_graph.nodes[vertex]['hops']:
                            all_sets[n]['parents'] = set(
                                [adj_vert]) | all_sets[n]['parents']

                        elif reduced_graph.nodes[adj_vert][
                                'hops'] > reduced_graph.nodes[vertex]['hops']:
                            all_sets[n]['growth'] = set(
                                [adj_vert]) | all_sets[n]['growth']

                    left_tuple += [(n, len(all_sets[n]['growth']))]

        plt.figure(fig)
        pj.draw_graph(reduced_graph, out_file_path + "_skeletontree")
        plt.figure(fig + 1)
        pj.draw_graph(Tree, out_file_path + "_fulltree")
        pj.store_pickle(Tree, out_file_path + "balancedtree_pickle")
    else:
        print(
            "Terminals of the given graph do not lie in a single sonnected component. Thus it cannot be processed further"
        )
Ejemplo n.º 25
0
def test_is_biconnected():
    G=nx.cycle_graph(3)
    assert_true(nx.is_biconnected(G))
    nx.add_cycle(G, [1, 3, 4])
    assert_false(nx.is_biconnected(G))
Ejemplo n.º 26
0
 def case_split(self):
     node = self.bd.graph['root']
     leaf = dict()
     for e in self.bd.edges(node):
         if (self.bd.degree(e[1]) != 1):
             leaf[e] = self.bd.edge[e[0]][e[1]]['label'].sort()
         else:
             v = e[1]
             mid = self.bd.edge[e[0]][e[1]]['label']
     one_list = list()
     for e in leaf.keys():
         if set(leaf[e]).issubset(mid):
             one_list.append(e)
     while one_list:
         curr_edge = one_list.pop()
         curr_node = int(self.bd.number_of_nodes())
         self.bd.add_node(curr_node, nodetype=int, label=list())
         lab = set
         for e in leaf.keys():
             if e[1] != curr_edge[1] and e[1] != v:
                 self.bd.add_edge(curr_node, e[1], label=leaf[e])
                 lab |= set(leaf[e])
                 self.bd.remove_edge(e[1], node)
         self.bd.add_edge(node, curr_node, label=list(lab))
         v = node
         node = curr_node
         mid = list(lab)
         self.bd.graph['root'] = curr_node
     if nx.is_biconnected(self.g):
         ga = nx.Graph()
         nodes = set()
         for e in self.bd.edges(node):
             if self.bd.degree(e[1]) == 1:
                 nodes |= set(self.bd.edge[e[0]][e[1]]['label'])
                 nodes |= {
                     self.edge.keys()[self.edge.values().index(
                         self.bd.edge[e[0]][e[1]]['label'].sort())]
                 }
         for i in range(len(nodes)):
             nodes[i] = nodes[1] - 1
         ga = self.g.subgraph(list(nodes))
         for n in ga.nodes():
             if ga.degree(n) == 1 and n + 1 not in mid:
                 e = ga.edges(n)
                 alert = 0
                 curr_edge1 = list()
                 curr_edge1.append(n)
                 for e1 in ga.edges(e[1]):
                     curr_edge1.append(e1[1])
                     if e1[1] in mid:
                         alert = 1
                 if alert != 1:
                     curr_edge = leaf.keys()[leaf.values().index(
                         (curr_edge1.sort()))]
                     curr_node = int(self.bd.number_of_nodes())
                     self.bd.add_node(curr_node, nodetype=int, label=list())
                     lab = set
                     for e in leaf.keys():
                         if e[1] != curr_edge[1] and e[1] != v:
                             self.bd.add_edge(curr_node,
                                              e[1],
                                              label=leaf[e])
                             lab |= set(leaf[e])
                             self.bd.remove_edge(e[1], node)
                     self.bd.add_edge(node, curr_node, label=list(lab))
                     v = node
                     node = curr_node
                     mid = list(lab)
                     self.bd.graph['root'] = curr_node
     ga = nx.Graph()
     nodes = set()
     for e in self.bd.edges(node):
         if self.bd.degree(e[1]) == 1:
             nodes |= set(self.bd.edge[e[0]][e[1]]['label'])
             nodes |= {
                 self.edge.keys()[self.edge.values().index(
                     self.bd.edge[e[0]][e[1]]['label'].sort())]
             }
     for i in range(len(nodes)):
         nodes[i] = nodes[1] - 1
     ga = self.g.subgraph(list(nodes))
     if nx.is_connected(ga):
         return False
     else:
         comp = nx.connected_components(ga)
Ejemplo n.º 27
0
def test_empty_is_biconnected():
    G=nx.empty_graph(5)
    assert_false(nx.is_biconnected(G))
    G.add_edge(0, 1)
    assert_false(nx.is_biconnected(G))
Ejemplo n.º 28
0
def test_null_graph():
    G = nx.Graph()
    assert not nx.is_biconnected(G)
    assert list(nx.biconnected_components(G)) == []
    assert list(nx.biconnected_component_edges(G)) == []
    assert list(nx.articulation_points(G)) == []
def test_is_biconnected():
    G = nx.cycle_graph(3)
    assert_true(nx.is_biconnected(G))
    nx.add_cycle(G, [1, 3, 4])
    assert_false(nx.is_biconnected(G))
Ejemplo n.º 30
0
def test_is_biconnected():
    G = nx.cycle_graph(3)
    assert nx.is_biconnected(G)
    nx.add_cycle(G, [1, 3, 4])
    assert not nx.is_biconnected(G)
Ejemplo n.º 31
0

pos=nx.spring_layout(G,k=0.15,iterations=10)

print str(" ")
print 'BICONNECTEDNESS OF UNDIRECTED GRAPHS'
print str(" ")

G.remove_nodes_from(nx.isolates(G))
lc=sorted(nx.biconnected_components(G), key = len, reverse=True)

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

print 'Is graph G biconnected?', nx.is_biconnected(G)
print 'Is graph G connected?', nx.is_connected(G)
print 'The number of biconnected components of G is:', len(lc)
print 'The number of connected components of G is:', nx.number_connected_components(G)
print str(" ")

print 'List of biconnected 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)
Ejemplo n.º 32
0
print 'Writing subgraph info table.'
with open(args.output[0] + '.subgraph', 'w') as subinfo_h:
    subinfo_wr = csv.writer(subinfo_h, delimiter='\t')
    subinfo_wr.writerow([
        'id', 'size', 'order', 'density', 'eularian', 'binconn', 'modularity'
    ])
    for i, sg in enumerate(subgraphs):
        part = com.best_partition(sg)
        subinfo_wr.writerow([
            i + 1,
            sg.size(),
            sg.order(),
            nx.density(sg),
            nx.is_eulerian(sg),
            nx.is_biconnected(sg),
            com.modularity(part, sg)
        ])

if args.writesubs:
    print 'Writing all subgraphs ...'
    for i, sg in enumerate(subgraphs):
        out = os.path.join(args.work_dir[0], 'og{0}.graphml'.format(i + 1))
        nx.write_graphml(sg, out)

iso_count = 0
for org in membership:
    print 'There remained {0} isolate genes in {1}'.format(
        len(isolate_genes[org]), org)
    with open(org + '.memb', 'w') as memb_h:
        memb_wr = csv.writer(memb_h, delimiter='\t')
Ejemplo n.º 33
0
 def calculate(graph):
     return nx.is_biconnected(graph)
Ejemplo n.º 34
0
print("seed matrix size: ")
print(_V_)
print("iterations: ")
print(k)

G = StochasticKroneckerGenerator.generateStochasticKroneckerGraph(
    initiator, k, True)

print("=== GRAPH GENERATION COMPLETE ===")

bipartite = nx.is_bipartite(G)

print("Is Bipartite: ")
print(bipartite)

connected = nx.is_biconnected(G)

print("Is Connected: ")
print(connected)

nx.draw_networkx(G,
                 node_color='red',
                 pos=nx.spring_layout(G),
                 node_size=10,
                 with_labels=False)
plt.show()

print("Generating statistics...")
statistics = generateStatistics(G)
print(statistics)