本文整理汇总了Python中igraph.Graph.is_connected方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.is_connected方法的具体用法?Python Graph.is_connected怎么用?Python Graph.is_connected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.is_connected方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __findNegativeCut
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import is_connected [as 别名]
def __findNegativeCut(self,debug=False):
"""Best negative cut heuristic.
Heuristic to find the best cut value to construct the Gamma Model (RMgamma).
Args:
debug (bool,optional): Show debug information.
Returns:
A Heuristic object that contains all the relevant info about the heuristic.
"""
time_total = time.time()
# Graph and unique set construction
time_graph_construction = time.time()
graph_negative = Graph()
graph_negative.add_vertices(self.__n)
unique_negative_weights = set()
for i in range(self.__n):
for j in range (i+1,self.__n):
if self.__S[i][j] <= 0:
graph_negative.add_edge(i,j,weight=self.__S[i][j])
unique_negative_weights.add(self.__S[i][j])
time_graph_construction = time.time() - time_graph_construction
# Sort unique weights and start heuristic to find the best cut value
time_find_best_cut = time.time()
unique_negative_weights = sorted(unique_negative_weights)
# Test different cuts and check connected
best_negative_cut = 0
for newCut in unique_negative_weights:
edges_to_delete = graph_negative.es.select(weight_lt=newCut)
graph_negative.delete_edges(edges_to_delete)
if graph_negative.is_connected():
best_negative_cut = newCut
else:
break
time_find_best_cut = time.time() - time_find_best_cut
time_total = time.time() - time_total
if debug==True:
print ("Time Graph Construction: %f" %(time_graph_construction))
print ("Time Heuristic to find best cut: %f" %(time_find_best_cut))
print ("Total Time: %f" %(time_total))
print ("NEW (Best cut-): %d" %(best_negative_cut))
heuristic={}
heuristic['cut'] = best_negative_cut
heuristic['time_total']=time_total
heuristic['time_graph_construction']=time_graph_construction
heuristic['time_find_best_cut']=time_find_best_cut
return heuristic
示例2: get_igraph_graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import is_connected [as 别名]
def get_igraph_graph(network):
print 'load %s users into igraph' % len(network)
g = Graph(directed=True)
keys_set = set(network.keys())
g.add_vertices(network.keys())
print 'iterative load into igraph'
edges = []
for source in network:
for target in network[source].intersection(keys_set):
edges.append((source, target))
g.add_edges(edges)
g = g.simplify()
print 'make sure graph is connected'
connected_clusters = g.clusters()
connected_cluster_lengths = [len(x) for x in connected_clusters]
connected_cluster_max_idx = connected_cluster_lengths.index(max(connected_cluster_lengths))
g = connected_clusters.subgraph(connected_cluster_max_idx)
if g.is_connected():
print 'graph is connected'
else:
print 'graph is not connected'
return g
示例3: Graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import is_connected [as 别名]
from igraph import Graph
foster = Graph().LCF(90, [17, -9, 37, -37, 9, -17], 15)
foster.to_directed()
print "Is Directed? " + str(foster.is_directed())
for start in range(0, 90):
for end in range(0, 90):
# Don't delete this. Delete opposite direction edge
if start + 1 == end:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != 1:
foster.delete_edges([opposite_ID])
else:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != -1:
current_ID = foster.get_eid(start, end, True, False)
if current_ID != -1:
foster.delete_edges([current_ID])
print "Number of Edges: " + str(len(foster.get_edgelist()))
print (foster.is_connected())
foster_list = foster.get_adjacency()
for sublist in foster_list:
sublist = map(str, sublist)
sublist_string = " ".join(sublist)
print (sublist_string)
示例4: reduce_and_save_communities
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import is_connected [as 别名]
def reduce_and_save_communities(root_user, distance=10, return_graph_for_inspection=False):
print 'starting reduce_and_save_communities'
print 'root_user: %s, following_in_our_db: %s, distance: %s' % (
root_user.screen_name, len(root_user.following), distance)
network = TwitterUser.get_rooted_network(root_user, postgres_handle, distance=distance)
print 'load %s users into igraph' % len(network)
g = Graph(directed=True)
keys_set = set(network.keys())
g.add_vertices(network.keys())
g.vs["id"] = network.keys() #need this for pajek format
print 'iterative load into igraph'
edges = []
for source in network:
for target in network[source].intersection(keys_set):
edges.append((source, target))
g.add_edges(edges)
g = g.simplify()
print 'make sure graph is connected'
connected_clusters = g.clusters()
connected_cluster_lengths = [len(x) for x in connected_clusters]
connected_cluster_max_idx = connected_cluster_lengths.index(max(connected_cluster_lengths))
g = connected_clusters.subgraph(connected_cluster_max_idx)
if g.is_connected():
print 'graph is connected'
else:
print 'graph is not connected'
if return_graph_for_inspection:
return g
print 'write to pajek format'
root_file_name = root_user.screen_name
f = open('io/%s.net' % root_file_name, 'w')
g.write(f, format='pajek')
print 'run infomap'
#infomap_command = 'infomap_dir/infomap 345234 io/%s.net 10'
#infomap_command = 'conf-infomap_dir/conf-infomap 344 io/%s.net 10 10 0.50'
infomap_command = 'infohiermap_dir/infohiermap 345234 io/%s.net 30'
os.system(infomap_command % root_file_name)
print 'read into memory'
f = open('io/%s.smap' % root_file_name)
section_header = ''
communities = defaultdict(lambda: ([], [], []))
for line in f:
if line.startswith('*Modules'):
section_header = 'Modules'
continue
if line.startswith('*Insignificants'):
section_header = 'Insignificants'
continue
if line.startswith('*Nodes'):
section_header = 'Nodes'
continue
if line.startswith('*Links'):
section_header = 'Links'
continue
if section_header == 'Modules':
#looks like this:
#1 "26000689,..." 0.130147 0.0308866
#The names under *Modules are derived from the node with the highest
#flow volume within the module, and 0.25 0.0395432 represent, respectively,
#the aggregated flow volume of all nodes within the module and the per
#step exit flow from the module.
continue
if section_header == 'Nodes':
#looks like this:
#1:10 "2335431" 0.00365772
#or w/ a semicolon instead, semicolon means not significant
#see http://www.tp.umu.se/~rosvall/code.html
if ';' in line:
continue
community_idx = line.split(':')[0]
node_id = line.split('"')[1]
final_volume = float(line.split(' ')[2])
communities[community_idx][1].append(node_id)
communities[community_idx][2].append(final_volume)
if section_header == 'Links':
#community_edges
#looks like this:
#1 4 0.0395432
community_idx = line.split(' ')[0]
target_community_idx = line.split(' ')[1]
edge_weight = line.split(' ')[2]
communities[community_idx][0].append('%s:%s' % (target_community_idx, edge_weight))