本文整理汇总了Python中networkx.minimum_edge_cut函数的典型用法代码示例。如果您正苦于以下问题:Python minimum_edge_cut函数的具体用法?Python minimum_edge_cut怎么用?Python minimum_edge_cut使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了minimum_edge_cut函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: 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__))
示例2: __cut
def __cut(graph):
''' param:
graph:a nx.DiGraph obj
return:
cs : edge cut set of the graph
g1 , g2 : subgraphs induced by cs
'''
assert isinstance(graph, nx.DiGraph), "graph class: %s " % graph.__class__
assert graph.number_of_nodes() > 1, "Number of nodes: %d" % graph.number_of_nodes()
unigraph = nx.Graph( graph )
cs = nx.minimum_edge_cut( unigraph )
if not cs:
raise Exception,"Cut Set of this graph is Empty"
#CS中的边,可能不存在于原来的有向图中,所以需要将这种边的方向颠倒
#将所有real edge,存到RCS中
rcs = []
for eachEdge in cs:
if not graph.has_edge( eachEdge[0], eachEdge[1] ):
eachEdge = (eachEdge[1], eachEdge[0]) #调换方向
rcs.append(eachEdge)
graph.remove_edges_from(rcs)
glist = []
for eachCntComp in nx.weakly_connected_component_subgraphs(graph, copy = False):
glist.append(eachCntComp)
assert len(glist) == 2
return rcs, glist[0], glist[1]
示例3: getMinCut
def getMinCut(self):
graph = self.getGraph()
try:
min_cut = nx.minimum_edge_cut(graph)
except: # not connected
return -1
return len(min_cut)
示例4: 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)
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))
示例5: 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__))
示例6: RunProbabilityTest
def RunProbabilityTest(G):
'''
Description:
Finds the probability of running Karger's on the
same graph 10*n**2 times with n = 20 and finding
the min cut correctly.
Args:
G networkx graph of 20 nodes
Returns:
probability of running Karger's algo
on the same graph and finding the min cut
'''
min_cuts_found = 0.0
min_edge_cut = len(nx.minimum_edge_cut(G))
for i in range(1, (10*20**2)+1):
H = G.copy()
H = RunKarger(H)
# See if karger's returns the correct min cut
if H.number_of_edges() == min_edge_cut:
min_cuts_found += 1
# For every n node sized graph find the probability
# of getting the min cut each time karger's is run
# for a total of 10*n**2 runs
print min_cuts_found, i
return float('{0:.3f}'.format(min_cuts_found/i))
示例7: cut_edges_detection
def cut_edges_detection(self, segments, feature):
#T = self.iterdiff(feature, segments)
G, n_nodes, T = self.build_nodes_edges(segments, feature)
hyp = Timeline()
hypothesis = Timeline()
hyp.add(
Segment(segments[0].start, segments[n_nodes[0]].end)
)
hyp.add(
Segment(segments[0].start, segments[n_nodes[0]].end)
)
for i, j in enumerate(n_nodes):
hyp.add(
Segment(
segments[n_nodes[i - 1]].end,
segments[n_nodes[i]].end
)
)
Coupure = nx.minimum_edge_cut(G, T[j + 1], T[j])
if len(Coupure) == 0:
hypothesis.add(hyp[i])
return hypothesis
示例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: 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__))
示例10: 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))
示例11: test_icosahedral_cutset
def test_icosahedral_cutset():
G = nx.icosahedral_graph()
for flow_func in flow_funcs:
kwargs = dict(flow_func=flow_func)
# edge cuts
edge_cut = nx.minimum_edge_cut(G, **kwargs)
assert_equal(5, 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(5, 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__))
示例12: FiveA
def FiveA():
'''
Description:
Runs Karger's algorithm on random graphs.
Creates the plot of the number of nodes in a
graph vs. the probability of finding a min cut.
'''
prob_min_cut = {}
p = 0.5
# Create random graphs of 5,...,20 nodes
for n in range(5, 21):
min_cuts_found = 0.0
for i in range(1, (10*(n**2))+1):
G = CreateGraph(n, p)
#plt.figure(1)
#DrawGraph(G, 'ps3_q5.png')
# Get the min cut using a built in method from networkx
min_edge_cut = len(nx.minimum_edge_cut(G))
G = RunKarger(G)
# See if karger's returns the correct min cut
if G.number_of_edges() == min_edge_cut:
min_cuts_found += 1
# For every n node sized graph find the probability
# of getting the min cut each time karger's is run
# for a total of 10*n**2 runs
prob_min_cut[n] = float('{0:.3f}'.format(min_cuts_found/i))
# Output results to a csv file
Write('ps3_q5_output.txt', n, prob_min_cut[n])
# Read in results file and create a plot of results
ReadCSV('ps3_q5_output.txt')
示例13: cut
def cut(graph):
''' parame:
graph:a nx.DiGraph obj
return:
cs : edge cut set of the graph
g1 , g2 : subgraphs induced by cs
'''
debug = True
assert isinstance(graph, nx.DiGraph), "Input_para.__class__ %s " % graph.__class__
assert graph.number_of_nodes() > 1, "Number of nodes: %d" % graph.number_of_nodes()
if debug: print "\nDigraph Edges Are:\n %s" % str(graph.edges())
unigraph = nx.Graph(graph) #将输入的图转为无向图
cs = nx.minimum_edge_cut(unigraph) #找出该无向图的minimum edge cut -> CS
#balance函数调用cut前,graph一定是一个un-balance 结构,所以一定有CUT?
if not cs:
raise Exception,"Cut Set of this graph is Empty"
#CS中的边,可能不存在于原来的有向图中,所以需要将这种边的方向颠倒
#将所有real edge,存到RCS中
rcs = []
original_edges = graph.edges()
for eachEdge in cs:
if not eachEdge in original_edges:
eachEdge = (eachEdge[1], eachEdge[0]) #调换方向
rcs.append(eachEdge)
graph.remove_edges_from(rcs) #在原图中移除CS
if debug: print "Edge Cut Set RCS :\n %s" % str(rcs)
if debug: print "After remove RCS :\n %s" % str(graph.edges())
# 移除RCS中的边之后得到的两个Weakly Connected Subgraph
glist = []
for eachCntComp in nx.weakly_connected_component_subgraphs(graph):
#找到移除CS后的两个弱连接分量
glist.append(eachCntComp)
if debug:
print "Weakly CC %d:" % len(glist)
print " nodes:%s" % eachCntComp.nodes()
print " edges:%s" % eachCntComp.edges()
assert len(glist) == 2
return rcs, glist[0], glist[1]
示例14: build_graph
def build_graph(self):
""" insert notes and edges based on user dictionary"""
# for key in self.user_dic.keys():
# self.graph.add_node(key)
print "start building the graph"
distinct_user = Set([])
distinct_user.union(set( self.user_dic.keys() ))
for value in self.user_dic.values():
distinct_user.union(set(value))
for eachuser in distinct_user:
self.graph.add_node(key)
for key in self.user_dic.keys():
for value in self.user_dic[key]:
# g.add_edges( [(1,2)] )
self.graph.add_edge(key, value)
for node in self.graph.nodes():
self.color_dic[node] = "white"
self.node_color = [self.color_dic[node] for node in self.graph.nodes()]
allgraph = list(nx.connected_component_subgraphs(self.graph))
mincut = nx.minimum_edge_cut(self.graph)
print "mincut is ", mincut
print "length of all connected component is ", len(allgraph)
for graph in allgraph:
# min_weighted_dominating_set(graphUD, weight=None)
print graph.number_of_nodes()
print "finish building the graph"
示例15: cut_edges_detection
def cut_edges_detection(self, feature, first_segments):
"""Recherche des arcs de coupure
first_segments: segmenation initiale"""
G, n_nodes, T = self.build_nodes_edges(feature, first_segments)
hyp = Timeline()
hypothesis = Timeline()
hyp.add(
Segment(first_segments[0].start, first_segments[n_nodes[0]].end)
)
for i, j in enumerate(n_nodes):
hyp.add(
Segment(
first_segments[n_nodes[i - 1]].end,
first_segments[n_nodes[i]].end
)
)
Coupure = nx.minimum_edge_cut(G, T[j + 1], T[j])
if len(Coupure) == 0:
hypothesis.add(hyp[i])
return hypothesis