本文整理汇总了Python中networkx.is_connected函数的典型用法代码示例。如果您正苦于以下问题:Python is_connected函数的具体用法?Python is_connected怎么用?Python is_connected使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_connected函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_simple_graph
def generate_simple_graph(sfunction, N, avg_degree):
"""generate a simple random graph with sfunction degree sequence"""
graphical_deg_seq = False
is_connected_graph = False
while is_connected_graph == False:
while graphical_deg_seq == False:
seq = sfunction(N, avg_degree, seqtype="simple_degree")
graphical_deg_seq = nx.is_valid_degree_sequence(seq)
G = nx.havel_hakimi_graph(seq)
G.remove_edges_from(G.selfloop_edges())
if not nx.is_connected(G):
try:
connect_simple_graph(G)
is_connected_graph = True
randomize_graph(G)
except (IndexError):
is_connected_graph = False
if not nx.is_connected(G):
try:
connect_simple_graph(G)
is_connected_graph = True
except (IndexError):
is_connected_graph = False
graphical_deg_seq = False
return G
开发者ID:prathasah,项目名称:random-modular-network-generator,代码行数:30,代码来源:random_modular_generator_variable_modules.py
示例2: test_brandes_erlebach_book
def test_brandes_erlebach_book():
# Figure 1 chapter 7: Connectivity
# http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 6), (3, 4),
(3, 6), (4, 6), (4, 7), (5, 7), (6, 8), (6, 9), (7, 8),
(7, 10), (8, 11), (9, 10), (9, 11), (10, 11)])
for flow_func in flow_funcs:
kwargs = dict(flow_func=flow_func)
# edge cutsets
assert_equal(3, len(nx.minimum_edge_cut(G, 1, 11, **kwargs)),
msg=msg.format(flow_func.__name__))
edge_cut = nx.minimum_edge_cut(G, **kwargs)
# Node 5 has only two edges
assert_equal(2, len(edge_cut), msg=msg.format(flow_func.__name__))
H = G.copy()
H.remove_edges_from(edge_cut)
assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
# node cuts
assert_equal(set([6, 7]), minimum_st_node_cut(G, 1, 11, **kwargs),
msg=msg.format(flow_func.__name__))
assert_equal(set([6, 7]), nx.minimum_node_cut(G, 1, 11, **kwargs),
msg=msg.format(flow_func.__name__))
node_cut = nx.minimum_node_cut(G, **kwargs)
assert_equal(2, len(node_cut), msg=msg.format(flow_func.__name__))
H = G.copy()
H.remove_nodes_from(node_cut)
assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
示例3: number_of_3partition
def number_of_3partition(G):
edge_list = G.edges()
count = 0
for n in powerset(range(len(G.nodes()))):
if len(n) == 0:
continue
H1 = G.subgraph(n)
if not nx.is_connected(H1):
continue
nbar1 = []
for i in range(0, len(G.nodes())):
if i not in n:
nbar1.append(i)
for n2 in powerset(nbar1):
if len(n2) == 0:
continue
H2 = G.subgraph(n2)
if not nx.is_connected(H2):
continue
nbar = []
for i in range(0, len(G.nodes())):
if i not in n and i not in n2:
nbar.append(i)
if len(nbar) == 0:
continue
H3 = G.subgraph(nbar)
if not nx.is_connected(H3):
continue
count += 1
return count / 6
示例4: test_solution
def test_solution(sets, G, new_G):
for g in sets.values():
if nx.is_connected(G.subgraph(g)) and \
not nx.is_connected(new_G.subgraph(g)) and len(g)>=2:
print 'Disconnect:',g, G.subgraph(g).edges(), new_G.subgraph(g).edges()
return False
return True
示例5: test_vertex_separator
def test_vertex_separator(self):
sep, part1, part2 = nxmetis.vertex_separator(self.G)
# The two separator nodes must not be present in the
# two bisected chains
nose.tools.ok_(sep[0] not in part1)
nose.tools.ok_(sep[0] not in part2)
nose.tools.ok_(sep[1] not in part1)
nose.tools.ok_(sep[1] not in part2)
# There should be two different separator nodes
nose.tools.assert_equal(len(sep), 2)
nose.tools.assert_not_equal(sep[0], sep[1])
# The lists should be exhaustive with the node list of the Graph
nose.tools.assert_equal(set(sep) | set(part1) | set(part2),
set(self.G))
# The parts must be disjoint sets
nose.tools.assert_equal(set(), set(part1) & set(part2))
# Non-empty set
nose.tools.assert_not_equal(len(part1), 0)
nose.tools.assert_not_equal(len(part2), 0)
# Duplicate-free
nose.tools.assert_equal(len(part1), len(set(part1)))
nose.tools.assert_equal(len(part2), len(set(part2)))
# Connected
nose.tools.ok_(nx.is_connected(self.G.subgraph(part1)))
nose.tools.ok_(nx.is_connected(self.G.subgraph(part2)))
示例6: generateNewExtractionPool
def generateNewExtractionPool(self, network):
#Generate extraction candidates for each key in the extraction map
import time
totalstart = time.time()
self.extractionPool = {}
connected = 0
#***************************************
for element in self.extractionMap:
if nx.is_connected(element[0]):
connected += 1
print "Total number of extracted subgraphs: " + str(len(self.extractionMap))
print "Number of connected subgraphs: " + str(connected)
#***************************************
for element in self.extractionMap:
substart = time.time()
if nx.is_connected(element[0]):
connected += 1
extractionCandidates = self.util.findSubgraphInstances(network, element[0])
print "Subpool size: " + str(len(extractionCandidates))
subelapsed = time.time() - substart
print "Subpool elapsed time: " + str(subelapsed)
self.extractionPool[element[0]] = extractionCandidates
print "Number of connected subgraphs: " + str(connected)
totalelapsed = time.time()- totalstart
print "Total elapsed pool time: " + str(totalelapsed)
import sys
print "Total size of extraction pool in Bytes: " + str(sys.getsizeof(self.extractionPool))
示例7: test_white_harary_paper
def test_white_harary_paper():
# Figure 1b white and harary (2001)
# http://eclectic.ss.uci.edu/~drwhite/sm-w23.PDF
# A graph with high adhesion (edge connectivity) and low cohesion
# (node connectivity)
G = nx.disjoint_union(nx.complete_graph(4), nx.complete_graph(4))
G.remove_node(7)
for i in range(4, 7):
G.add_edge(0, i)
G = nx.disjoint_union(G, nx.complete_graph(4))
G.remove_node(G.order() - 1)
for i in range(7, 10):
G.add_edge(0, i)
for flow_func in flow_funcs:
kwargs = dict(flow_func=flow_func)
# edge cuts
edge_cut = nx.minimum_edge_cut(G, **kwargs)
assert_equal(3, len(edge_cut), msg=msg.format(flow_func.__name__))
H = G.copy()
H.remove_edges_from(edge_cut)
assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
# node cuts
node_cut = nx.minimum_node_cut(G, **kwargs)
assert_equal(set([0]), node_cut, msg=msg.format(flow_func.__name__))
H = G.copy()
H.remove_nodes_from(node_cut)
assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
示例8: test_edge_cutset_random_graphs
def test_edge_cutset_random_graphs():
for i in range(5):
G = nx.fast_gnp_random_graph(50,0.2)
if not nx.is_connected(G):
ccs = iter(nx.connected_components(G))
start = next(ccs)[0]
G.add_edges_from( (start,c[0]) for c in ccs )
cutset = nx.minimum_edge_cut(G)
assert_equal(nx.edge_connectivity(G), len(cutset))
G.remove_edges_from(cutset)
assert_false(nx.is_connected(G))
示例9: main
def main():
LOG = True
#if (len(sys.argv) != 3):
# print "ERROR: genRandomGeorml <nodes> <raio>"
# sys.exit(1)
NMAX = int(sys.argv[1])
RAIO = float(sys.argv[2])
#NMAX=40
#RAIO=0.1
ALCANCE=250
G=nx.random_geometric_graph(NMAX,RAIO,2)
while not nx.is_connected(G):
RAIO=RAIO+.005
G=nx.random_geometric_graph(NMAX,RAIO,2)
if LOG: print "Graph is not full connected"
pos=nx.get_node_attributes(G,'pos')
network(G,pos,1)
#Remove vizinhos que estejam demasiado perto
while nodeNear(G)<1000 :
G.remove_node(nodeNear(G))
if nx.is_connected(G):
pos=nx.get_node_attributes(G,'pos')
network(G,pos,2)
#Remove no que tem mais vizinhos
T=G
if not nodeSolo(T,nodeMaxDegree(T)): T.remove_node(nodeMaxDegree(T))
if nx.is_connected(T):
G=T
pos=nx.get_node_attributes(G,'pos')
network(G,pos,3)
for n in G.neighbors(nodeMaxDegree(G)):
if nx.degree(G,n)== 2 :
degree=nx.degree(G,n)
node=n
print "node=",n
if not nodeSolo(G,n): G.remove_node(n)
break
pos=nx.get_node_attributes(G,'pos')
network(G,pos,4)
else:
if LOG: print "SubGraph is not full connected"
示例10: check_adj_list_connectivity
def check_adj_list_connectivity(adj_graph_file):
g = read_weighted_adj_graph(adj_graph_file)
print 'finished reading in adjacency list file...'
edges = list()
for k, v in g.items():
for val in v[0]:
edges.append((k, val))
print 'finished appending edges'
G = networkx.Graph()
# print len(edges)
G.add_edges_from(edges)
print networkx.is_connected(G)
示例11: simple_query
def simple_query(GLearnt, trials):
i = 1
global Degree_Node
global NodeList
G = nx.Graph(GLearnt)
G = G.subgraph(nx.connected_components(G)[0])
print nx.is_connected(G)
print G.number_of_nodes()
Degree_Node = G.degree()
NodeList = G.nodes()
for i in NodeList:
Degree_Node[i] = [Degree_Node[i], GLearnt.neighbors(i)]
PlainAdamicFullPaths = []
TwoWayAdamicFullPaths = []
djk_time = 0
TwoWayAdamic_time = 0
PlainAdamic_time = 0
count = 0
for i in range(trials):
A = random.choice(NodeList)
B = random.choice(NodeList)
#for A in NodeList:
#for B in NodeList[NodeList.index(A):]:
if A != B :
src = A #raw_input("Enter source name:")
dstn = B #raw_input("Enter destination name:")
start = time.time()
TwoWayAdamicFullPath = TwoWayAdamicWalk(G,src,dstn)
finish = time.time()
TwoWayAdamic_time+=(finish-start)
start = time.time()
PlainAdamicFullPath = OneWayAdamicWalk(G,src,dstn)
finish = time.time()
PlainAdamic_time+=(finish-start)
count += 1
sys.stdout.write(" "*20 + "\b"*50)
sys.stdout.write( "Progress: " + str(float(count)/trials))
print "\n"
print "Plain Adamic Time : ", PlainAdamic_time
print "Two Way Adamic Time : ", TwoWayAdamic_time
return [PlainAdamic_time, TwoWayAdamic_time]
示例12: test_edge_cutset_random_graphs
def test_edge_cutset_random_graphs():
for flow_func in flow_funcs:
for i in range(3):
G = nx.fast_gnp_random_graph(50, 0.25)
if not nx.is_connected(G):
ccs = iter(nx.connected_components(G))
start = arbitrary_element(next(ccs))
G.add_edges_from((start, arbitrary_element(c)) for c in ccs)
cutset = nx.minimum_edge_cut(G, flow_func=flow_func)
assert_equal(nx.edge_connectivity(G), len(cutset), msg=msg.format(flow_func.__name__))
G.remove_edges_from(cutset)
assert_false(nx.is_connected(G), msg=msg.format(flow_func.__name__))
示例13: main
def main():
LOG = True
if (len(sys.argv) != 5):
print "ERROR: genMina3.py <nodes> <radius> <delta> <maxdegree>"
sys.exit(1)
NMAX = int(sys.argv[1])
RAIO = float(sys.argv[2])
delta = float(sys.argv[3])
degree = float(sys.argv[4])
#NMAX=40
#RAIO=0.1
ALCANCE=250
c=0
run=True
first=True
while run:
c+=1
G=nx.random_geometric_graph(NMAX,RAIO,2)
while not nx.is_connected(G):
if first:
RAIO=RAIO+.005
G=nx.random_geometric_graph(NMAX,RAIO,2)
if LOG: print c,"- Radius: Graph is not full connected R=",RAIO
first=False
#Remove vizinhos que estejam demasiado pertoc
candidate=nodeNear(G,delta)
while not candidate==10000 :
G.remove_node(candidate)
candidate=nodeNear(G,delta)
if nx.is_connected(G):
#Remove no que tem mais vizinhos
candidate=nodeMaxDegree(G)
while nx.degree(G,candidate)> degree :
G.remove_node(candidate)
candidate=nodeMaxDegree(G)
if nx.is_connected(G):
run=False
else:
if LOG: print c,"- MaxDegree: Split Graph"
else:
if LOG: print c,"- nodeNear: Split Graph"
pos=nx.get_node_attributes(G,'pos')
network(G,pos,5)
if LOG: print "Raio =",RAIO
if LOG: print "NMAX =",NMAX
if LOG: print "Nodes =",nx.number_of_nodes(G)
示例14: undirected_stats
def undirected_stats(self):
if nx.is_connected(self.nx_graph):
conl = nx.connected_components(self.nx_graph) #needs work-around for unconnected subgraphs
conl = conl.pop()
else:
conl = 'NA - graph is not connected'
result = { #"""returns boolean"""
'con': nx.is_connected(self.nx_graph),
'conn': nx.number_connected_components(self.nx_graph),
'conl': conl,
'Conl': g.subgraph(conl)
}
return result
示例15: test_octahedral_cutset
def test_octahedral_cutset():
G=nx.octahedral_graph()
# edge cuts
edge_cut = nx.minimum_edge_cut(G)
assert_equal(4, len(edge_cut))
H = G.copy()
H.remove_edges_from(edge_cut)
assert_false(nx.is_connected(H))
# node cuts
node_cut = nx.minimum_node_cut(G)
assert_equal(4,len(node_cut))
H = G.copy()
H.remove_nodes_from(node_cut)
assert_false(nx.is_connected(H))