本文整理匯總了Python中networkx.complete_graph方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.complete_graph方法的具體用法?Python networkx.complete_graph怎麽用?Python networkx.complete_graph使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.complete_graph方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_networkx2igraph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_networkx2igraph(self):
import networkx as nx
ng = nx.complete_graph(3)
[x, y] = [int(x) for x in nx.__version__.split('.')]
if x == 1:
nx.set_node_attributes(ng, 'vattrib', 0)
nx.set_edge_attributes(ng, 'eattrib', 1)
else:
nx.set_node_attributes(ng, 0, 'vattrib')
nx.set_edge_attributes(ng, 1, 'eattrib')
(e, n) = graphistry.bind(source='src', destination='dst').networkx2pandas(ng)
edges = pd.DataFrame({
'dst': {0: 1, 1: 2, 2: 2},
'src': {0: 0, 1: 0, 2: 1},
'eattrib': {0: 1, 1: 1, 2: 1}
})
nodes = pd.DataFrame({
'__nodeid__': {0: 0, 1: 1, 2: 2},
'vattrib': {0: 0, 1: 0, 2: 0}
})
assertFrameEqual(e, edges)
assertFrameEqual(n, nodes)
示例2: test_nx_pfs_deep_search
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_nx_pfs_deep_search(self):
pfs = EnergyImpactDecomposer._pfs_nodes
# two K4 graphs connected with one edge
graph = nx.complete_graph(4)
graph.add_edges_from(nx.complete_graph(range(4,8)).edges)
graph.add_edge(1, 4)
# use node index for weight/priority
priority = lambda node: node
# make sure once the second cluster is within reach, we deplete it
self.assertEqual(set(pfs(graph, 3, 2, priority)), set([2, 3]))
self.assertEqual(set(pfs(graph, 3, 3, priority)), set([1, 2, 3]))
self.assertEqual(set(pfs(graph, 3, 4, priority)), set([1, 2, 3, 4]))
self.assertEqual(set(pfs(graph, 3, 7, priority)), set([1, 2, 3, 4, 5, 6, 7]))
self.assertEqual(set(pfs(graph, 3, 8, priority)), set([0, 1, 2, 3, 4, 5, 6, 7]))
示例3: is_clique
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def is_clique(graph: nx.Graph) -> bool:
"""Determines if the input graph is a clique. A clique of :math:`n` nodes has exactly :math:`n(
n-1)/2` edges.
**Example usage:**
>>> graph = nx.complete_graph(10)
>>> is_clique(graph)
True
Args:
graph (nx.Graph): the input graph
Returns:
bool: ``True`` if input graph is a clique and ``False`` otherwise
"""
edges = graph.edges
nodes = graph.order()
return len(edges) == nodes * (nodes - 1) / 2
示例4: test_tie
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_tie(self, monkeypatch):
"""Test if function correctly settles ties with random selection. This test starts with
the problem of a 6-dimensional complete graph and a 4-node subgraph, with the objective
of shrinking down to 3 nodes. In this case, any of the 4 nodes in the subgraph is an
equally good candidate for removal since all nodes have the same degree. The test
monkeypatches the ``np.random.choice`` function to simply choose a fixed element,
e.g. the element 2 of the list [0, 1, 2, 3] will be 2. Using this we expect the node to
be removed by ``resize`` in this case to have label equal to the element."""
dim = 6
g = nx.complete_graph(dim)
s = [0, 1, 2, 3]
for i in range(4):
choice_i = functools.partial(self.choice, element=i)
with monkeypatch.context() as m:
m.setattr(np.random, "choice", choice_i)
resized = subgraph.resize(s, g, min_size=3, max_size=3)
resized_subgraph = resized[3]
removed_node = list(set(s) - set(resized_subgraph))[0]
assert removed_node == i
示例5: test_weight_and_degree_ties
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_weight_and_degree_ties(self, dim, monkeypatch, elem):
"""Test if function correctly resizes on a fixed example where the ideal resizing is
known and node-weight based selection is used to settle ties, but with all node weights
equal so that they must be settled uniformly at random. The example is a complete
graph with a starting subgraph of the first ``dim - 2`` nodes. The task is to resize to
one node smaller and larger. This test monkeypatches the ``np.random.choice`` call used in
the function so that instead it returns a fixed element. This element is set to 0 or 1 in
the test, resulting in different nodes being added and removed from the starting
subgraph."""
g = nx.complete_graph(dim)
s = list(range(dim - 2))
min_size = dim - 3
max_size = dim - 1
w = [1] * dim
choice_elem = functools.partial(self.choice, element=elem)
ideal = {dim - 2: s, dim - 1: s + [dim - 2 + elem], dim - 3: list(set(s) - {elem})}
with monkeypatch.context() as m:
m.setattr(np.random, "choice", choice_elem)
resized = subgraph.resize(s, g, min_size, max_size, node_select=w)
assert resized == ideal
示例6: test_dead_end
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_dead_end(self, monkeypatch):
"""Test if function stops when in a dead end (i.e., ``grow == swap``) such that no
swapping is possible. This is achieved by monkeypatching ``grow`` and
``swap`` so that they simply return the same clique as fed in, which should cause
the local search algorithm to conclude that it is already in a dead end and return the
same clique."""
graph = nx.complete_graph(5)
c = [0, 1, 2]
with monkeypatch.context() as m:
m.setattr(clique, "grow", patch_resize)
m.setattr(clique, "swap", patch_resize)
result = clique.search(c, graph, iterations=100)
assert result == c
示例7: test_grow_maximal_degree_tie
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_grow_maximal_degree_tie(self, dim, monkeypatch):
"""Test if function grows using randomness to break ties during degree-based node
selection. The chosen graph is a fully connected graph with the ``dim - 2`` and ``dim -
1`` nodes then disconnected. Starting from the first ``dim - 3`` nodes, one can add
either of the ``dim - 2`` and ``dim - 1`` nodes. As they have the same degree, they should
be selected randomly with equal probability. This function monkeypatches the
``np.random.choice`` call to guarantee that one of the nodes is picked during one run of
``grow`` and the other node is picked during the next run."""
graph = nx.complete_graph(dim)
graph.remove_edge(dim - 2, dim - 1)
s = set(range(dim - 2))
patch_random_choice_1 = functools.partial(patch_random_choice, element=0)
patch_random_choice_2 = functools.partial(patch_random_choice, element=1)
with monkeypatch.context() as m:
m.setattr(np.random, "choice", patch_random_choice_1)
c1 = clique.grow(s, graph, node_select="degree")
with monkeypatch.context() as m:
m.setattr(np.random, "choice", patch_random_choice_2)
c2 = clique.grow(s, graph, node_select="degree")
assert c1 != c2
示例8: test_grow_maximal_weight
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_grow_maximal_weight(self, dim):
"""Test if function grows to expected maximal graph when weight-based node selection is
used. The chosen graph is a fully connected graph where the final three nodes have
subsequently been disconnected from each other, but remain connected to all the other
nodes. We then start from the clique composed of all but the final three nodes and seek
to grow. In this construction, we can add just one of the final three nodes. This test
gives the final node the largest weight, so we expect that one to be added."""
graph = nx.complete_graph(dim)
s = graph.subgraph([dim - 3, dim - 2, dim - 1])
for e in s.edges():
graph.remove_edge(*e)
s = set(range(dim - 3))
weights = list(range(dim))
target = s | {dim - 1}
assert set(clique.grow(s, graph, node_select=weights)) == target
示例9: test_correct_vector_returned
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_correct_vector_returned(self, monkeypatch):
"""Test if function correctly constructs the feature vector. The ``prob_orbit_exact``
and ``prob_orbit_mc`` function called within ``feature_vector_orbits`` are
monkeypatched to return hard-coded outputs that depend only on the orbit."""
with monkeypatch.context() as m:
m.setattr(
similarity,
"prob_orbit_mc",
lambda _graph, orbit, n_mean, samples, loss: 1.0 / sum(orbit),
)
graph = nx.complete_graph(8)
assert similarity.feature_vector_orbits(graph, [[1, 1], [2, 1, 1]], samples=1) == [
0.5,
0.25,
]
with monkeypatch.context() as m:
m.setattr(
similarity,
"prob_orbit_exact",
lambda _graph, orbit, n_mean, loss: 0.5 * (1.0 / sum(orbit)),
)
graph = nx.complete_graph(8)
assert similarity.feature_vector_orbits(graph, [[1, 1], [2, 1, 1]]) == [0.25, 0.125]
示例10: test_networkx_vice_versa_convert
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_networkx_vice_versa_convert():
G = nx.complete_graph(5)
assert G.is_directed() is False
data = from_networkx(G)
assert data.is_directed() is False
G = to_networkx(data)
assert G.is_directed() is True
G = nx.to_undirected(G)
assert G.is_directed() is False
示例11: test_subgraph_convert
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_subgraph_convert():
G = nx.complete_graph(5)
edge_index = from_networkx(G).edge_index
sub_edge_index_1, _ = subgraph([0, 1, 3, 4], edge_index,
relabel_nodes=True)
sub_edge_index_2 = from_networkx(G.subgraph([0, 1, 3, 4])).edge_index
assert sub_edge_index_1.tolist() == sub_edge_index_2.tolist()
示例12: _gen_clique
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def _gen_clique(self, n):
for _ in range(n):
num_v = np.random.randint(self.min_num_v, self.max_num_v)
g = nx.complete_graph(num_v)
self.graphs.append(g)
self.labels.append(6)
示例13: c_0
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def c_0(clique: list, graph: nx.Graph):
"""Generates the set :math:`C_0` of nodes that are connected to all nodes in the input
clique subgraph.
The set :math:`C_0` is defined in :cite:`pullan2006phased` and is used to determine nodes
that can be added to the current clique to grow it into a larger one.
**Example usage:**
>>> graph = nx.complete_graph(10)
>>> clique = [0, 1, 2, 3, 4]
>>> c_0(clique, graph)
[5, 6, 7, 8, 9]
Args:
clique (list[int]): a subgraph specified by a list of nodes; the subgraph must be a clique
graph (nx.Graph): the input graph
Returns:
list[int]: a list containing the :math:`C_0` nodes for the clique
"""
if not is_clique(graph.subgraph(clique)):
raise ValueError("Input subgraph is not a clique")
clique = set(clique)
c_0_nodes = []
non_clique_nodes = set(graph.nodes) - clique
for i in non_clique_nodes:
if clique.issubset(graph.neighbors(i)):
c_0_nodes.append(i)
return c_0_nodes
示例14: test_complete_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_complete_graph(self, dim):
"""Tests that nodes in a complete graph are located in the unit circle when using the
Kamada-Kawai layout"""
graph = nx.complete_graph(dim)
layout = nx.kamada_kawai_layout(graph)
coords = plot._node_coords(graph, layout)
x = coords["x"]
y = coords["y"]
radii = [np.sqrt(x[i] ** 2 + y[i] ** 2) for i in range(dim)]
assert np.allclose(radii, np.ones(dim), atol=1e-5)
示例15: test_custom_layout
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import complete_graph [as 別名]
def test_custom_layout(self):
"""Tests that nodes in a complete graph are correctly located based on a custom layout"""
graph = nx.complete_graph(4)
layout = {0: [1, 1], 1: [1, -1], 2: [-1, 1], 3: [-1, -1]}
coords = plot._node_coords(graph, layout)
x = coords["x"]
y = coords["y"]
assert x == [1, 1, -1, -1]
assert y == [1, -1, 1, -1]