本文整理汇总了Python中networkx.edge_boundary函数的典型用法代码示例。如果您正苦于以下问题:Python edge_boundary函数的具体用法?Python edge_boundary怎么用?Python edge_boundary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了edge_boundary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cut_size
def cut_size(G, S, T=None, weight=None):
"""Returns the size of the cut between two sets of nodes.
A *cut* is a partition of the nodes of a graph into two sets. The
*cut size* is the sum of the weights of the edges "between" the two
sets of nodes.
Parameters
----------
G : NetworkX graph
S : sequence
A sequence of nodes in ``G``.
T : sequence
A sequence of nodes in ``G``. If not specified, this is taken to
be the set complement of ``S``.
weight : object
Edge attribute key to use as weight. If not specified, edges
have weight one.
Returns
-------
number
Total weight of all edges from nodes in set ``S`` to nodes in
set ``T`` (and, in the case of directed graphs, all edges from
nodes in ``T`` to nodes in ``S``).
Examples
--------
In the graph with two cliques joined by a single edges, the natural
bipartition of the graph into two blocks, one for each clique,
yields a cut of weight one::
>>> G = nx.barbell_graph(3, 0)
>>> S = {0, 1, 2}
>>> T = {3, 4, 5}
>>> nx.cut_size(G, S, T)
1
Each parallel edge in a multigraph is counted when determining the
cut size::
>>> G = nx.MultiGraph(['ab', 'ab'])
>>> S = {'a'}
>>> T = {'b'}
>>> nx.cut_size(G, S, T)
2
Notes
-----
In a multigraph, the cut size is the total weight of edges including
multiplicity.
"""
edges = nx.edge_boundary(G, S, T, data=weight, default=1)
if G.is_directed():
edges = chain(edges, nx.edge_boundary(G, T, S, data=weight, default=1))
return sum(weight for u, v, weight in edges)
示例2: test_null_graph
def test_null_graph(self):
null = nx.null_graph()
assert_equal(list(nx.edge_boundary(null, [])), [])
assert_equal(list(nx.edge_boundary(null, [], [])), [])
assert_equal(list(nx.edge_boundary(null, [1, 2, 3])), [])
assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [4, 5, 6])), [])
assert_equal(list(nx.edge_boundary(null, [1, 2, 3], [3, 4, 5])), [])
示例3: test_null_edge_boundary
def test_null_edge_boundary(self):
"""null nxgraph has empty edge boundaries"""
null=self.null
assert_equal(nx.edge_boundary(null,[]),[])
assert_equal(nx.edge_boundary(null,[],[]),[])
assert_equal(nx.edge_boundary(null,[1,2,3]),[])
assert_equal(nx.edge_boundary(null,[1,2,3],[4,5,6]),[])
assert_equal(nx.edge_boundary(null,[1,2,3],[3,4,5]),[])
示例4: test_directed
def test_directed(self):
"""Tests the edge boundary of a directed graph."""
G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)])
S = {0, 1}
boundary = list(nx.edge_boundary(G, S))
expected = [(1, 2)]
assert_equal(boundary, expected)
示例5: test_multigraph
def test_multigraph(self):
"""Tests the edge boundary of a multigraph."""
G = nx.MultiGraph(list(nx.cycle_graph(5).edges()) * 2)
S = {0, 1}
boundary = list(nx.edge_boundary(G, S))
expected = [(0, 4), (0, 4), (1, 2), (1, 2)]
assert_equal(boundary, expected)
示例6: island_update
def island_update(topology, new_policy):
"""
precondition: Assumes that only one island update is performed,
and no subspace updates have been performed. This assumption is
forced by our use of VLAN tags instead of MPLS labels
provides per-packet
"""
inst.stats.tally_update(new_policy)
log.info("Island update")
old_policy = inst.current_abstract_policy
# Switches which didn't change in new policy
nops = set( s1 for s1, c1 in old_policy \
if switch_covered(c1, new_policy[s1]))
# Everything else
new = set(topology.switches()) - nops
old = set()
fixpoint = island_fixpoint(topology, new_policy)
while new:
additions = fixpoint(new, old)
old |= new
new = additions
mods = old
subpolicy = restrict_policy(mods, new_policy)
boundary = nx.edge_boundary(topology, mods)
fake_edge_ports = \
[topology.node[x]['ports'][y] for (x,y) in boundary \
if topology.node[y]['isSwitch']]
# retrieve current data from inst
current_internal_policy = inst.current_internal_policy
current_edge_policy = inst.current_edge_policy
current_version = inst.current_version
current_priority = inst.current_priority
# calculate new version and priority
new_version = current_version + 1
new_priority = current_priority - 1
# Have to manually construct the correct edge policies by
# distinguishing between "true" edge ports to hosts and "fake"
# edge ports to other switches running the old version.
internal_policy, edge_policy = \
mk_versioned_policies(subpolicy, new_version, new_priority, topology,
old_version=current_version,
fake_edge_ports=fake_edge_ports)
old_internal_policy = restrict_policy(mods, current_internal_policy)
old_edge_policy = restrict_policy(mods, current_edge_policy)
return UpdateObject(internal_policy, edge_policy,
old_internal_policy,
old_edge_policy,
new_priority, new_version)
示例7: test_multidigraph
def test_multidigraph(self):
"""Tests the edge boundary of a multdiigraph."""
edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
G = nx.MultiDiGraph(edges * 2)
S = {0, 1}
boundary = list(nx.edge_boundary(G, S))
expected = [(1, 2), (1, 2)]
assert_equal(boundary, expected)
示例8: find_bridge_edges
def find_bridge_edges(raw, squelched, groupsize):
groups = build_affinity_groups(squelched, groupsize)
for (left, right) in itertools.combinations(groups, 2):
left_name = name_subgraph(left)
right_name = name_subgraph(right)
boundary = nx.edge_boundary(raw, left.nodes(), right.nodes())
if boundary:
yield left_name, right_name, boundary
示例9: check_state
def check_state(self, min_size=None):
# check nodes of G are all in partition assignment
V = set(self.g.nodes())
nodes = set(self.nodes.keys())
assert V == nodes
# check each nodes's partition contains the node
assigned_partitions = set([])
for u in V:
i = self.nodes[u]
assert u in set(self.partition[i])
assigned_partitions.add(i)
assert assigned_partitions == set(self.partition.keys())
# check that the set of nodes in partitioning is exactly V
tmp = reduce(lambda x,y: x+y, self.partition.values())
nodes = set(tmp)
assert len(tmp) == len(nodes)
assert V == nodes
# check min size of partitions
partition_sizes = map(len, self.partition.values())
if min_size != None:
assert min(partition_sizes) >= min_size
assert sum(partition_sizes) == len(V)
assert set(self.partition_graph.nodes()) == set(self.partition.keys())
for i in self.partition_graph:
for j in self.partition_graph:
if i < j:
continue
if i == j:
nodes_i = set(self.partition[i])
neighbors = []
for u in nodes_i:
neighbors += self.g.neighbors(u)
count = 0
for nbr in neighbors:
count += nbr in nodes_i
count /= 2 # each edge is double counted
else:
nodes_i = self.partition[i]
nodes_j = self.partition[j]
count = len(networkx.edge_boundary(self.g, nodes_i, nodes_j))
if self.partition_graph.has_edge(i,j):
stored_count = self.partition_graph.get_edge_data(i,j,key=0)["count"]
assert count == stored_count, \
"Mismatch: edges(%d,%d)=%d stored count=%d" % (i,j,count, stored_count)
else:
assert count == 0, \
"Mismatch: edges(%d,%d)=%d but no count stored" % (i,j, count)
示例10: modularity
def modularity(g, comms):
"""Comput modularity: Community-centric version."""
Q = 0.0
M = float(g.number_of_edges())
for c, nodes in comms.iteritems():
E_in = len(networkx.edge_boundary(g, nodes, nodes))/2
assert E_in/2 == E_in//2
K_in = sum(g.degree(n) for n in nodes)
Q += E_in/(M*1) - (K_in/(2*M))**2
return Q
示例11: test_path_edge_boundary
def test_path_edge_boundary(self):
"""Check edge boundaries in path nxgraph."""
P10=self.P10
assert_equal(nx.edge_boundary(P10,[]),[])
assert_equal(nx.edge_boundary(P10,[],[]),[])
assert_equal(nx.edge_boundary(P10,[1,2,3]),[(3, 4)])
assert_equal(sorted(nx.edge_boundary(P10,[4,5,6])),[(4, 3), (6, 7)])
assert_equal(sorted(nx.edge_boundary(P10,[3,4,5,6,7])),[(3, 2), (7, 8)])
assert_equal(nx.edge_boundary(P10,[8,9,10]),[(8, 7)])
assert_equal(sorted(nx.edge_boundary(P10,[4,5,6],[9,10])),[])
assert_equal(nx.edge_boundary(P10,[1,2,3],[3,4,5]) ,[(2, 3), (3, 4)])
示例12: test_path_graph
def test_path_graph(self):
P10 = cnlti(nx.path_graph(10), first_label=1)
assert_equal(list(nx.edge_boundary(P10, [])), [])
assert_equal(list(nx.edge_boundary(P10, [], [])), [])
assert_equal(list(nx.edge_boundary(P10, [1, 2, 3])), [(3, 4)])
assert_equal(sorted(nx.edge_boundary(P10, [4, 5, 6])),
[(4, 3), (6, 7)])
assert_equal(sorted(nx.edge_boundary(P10, [3, 4, 5, 6, 7])),
[(3, 2), (7, 8)])
assert_equal(list(nx.edge_boundary(P10, [8, 9, 10])), [(8, 7)])
assert_equal(sorted(nx.edge_boundary(P10, [4, 5, 6], [9, 10])), [])
assert_equal(list(nx.edge_boundary(P10, [1, 2, 3], [3, 4, 5])),
[(2, 3), (3, 4)])
示例13: _pre_init
def _pre_init(self, pa_name, group, dgraph, fd, boundary_params):
"""Return a tuple of the form (pa_inputs, pa_outputs, renames)
for the PseudoAssembly that would be created given the nodes in
group and the given graph.
"""
# First, find our group boundary
self._orig_group_nodes = list(group) + list(boundary_params)
allnodes = dgraph.find_prefixed_nodes(self._orig_group_nodes)
out_edges = nx.edge_boundary(dgraph, allnodes)
in_edges = nx.edge_boundary(dgraph,
set(dgraph.nodes()).difference(allnodes))
solver_states = []
if fd is False:
for comp in group:
# Keep any node marked 'solver_state'. Note, only inputs can
# be solver_states.
solver_states.extend([node for node in dgraph.find_prefixed_nodes([comp])
if 'solver_state' in dgraph.node[node]])
pa_inputs = edges_to_dict(in_edges).values()
pa_inputs.extend(solver_states)
pa_outputs = set([a[0] for a in out_edges])
renames = {}
# Add pseudoassy inputs
for varpath in list(flatten_list_of_iters(pa_inputs)) + \
list(pa_outputs):
varname = to_PA_var(varpath, pa_name)
if varpath in dgraph:
renames[varpath] = varname
old = dgraph.base_var(varpath)
if old != varpath:
renames[old] = to_PA_var(old, pa_name)
# make boundary params outputs of the PA
pa_outputs.update(boundary_params)
return pa_inputs, pa_outputs, renames
示例14: test_complete_graph
def test_complete_graph(self):
K10 = cnlti(nx.complete_graph(10), first_label=1)
ilen = lambda iterable: sum(1 for i in iterable)
assert_equal(list(nx.edge_boundary(K10, [])), [])
assert_equal(list(nx.edge_boundary(K10, [], [])), [])
assert_equal(ilen(nx.edge_boundary(K10, [1, 2, 3])), 21)
assert_equal(ilen(nx.edge_boundary(K10, [4, 5, 6, 7])), 24)
assert_equal(ilen(nx.edge_boundary(K10, [3, 4, 5, 6, 7])), 25)
assert_equal(ilen(nx.edge_boundary(K10, [8, 9, 10])), 21)
assert_equal(sorted(nx.edge_boundary(K10, [4, 5, 6], [9, 10])),
[(4, 9), (4, 10), (5, 9), (5, 10), (6, 9), (6, 10)])
assert_equal(sorted(nx.edge_boundary(K10, [1, 2, 3], [3, 4, 5])),
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4),
(2, 5), (3, 4), (3, 5)])
示例15: boundary_nodes
def boundary_nodes(graph, nodes):
# TODO: move to utils
#TODO: use networkx boundary nodes directly: does the same thing
""" returns nodes at boundary of G based on edge_boundary from networkx """
graph = unwrap_graph(graph)
nodes = list(nodes)
nbunch = list(unwrap_nodes(nodes))
# find boundary
b_edges = nx.edge_boundary(graph, nbunch) # boundary edges
internal_nodes = [s for (s, t) in b_edges]
assert(all(n in nbunch for n in internal_nodes)) # check internal
return wrap_nodes(graph, internal_nodes)