本文整理汇总了Python中networkx.cycle_basis方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.cycle_basis方法的具体用法?Python networkx.cycle_basis怎么用?Python networkx.cycle_basis使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.cycle_basis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decode_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def decode_graph(adj, prefix):
adj = np.asmatrix(adj)
G = nx.from_numpy_matrix(adj)
# G.remove_nodes_from(nx.isolates(G))
print('num of nodes: {}'.format(G.number_of_nodes()))
print('num of edges: {}'.format(G.number_of_edges()))
G_deg = nx.degree_histogram(G)
G_deg_sum = [a * b for a, b in zip(G_deg, range(0, len(G_deg)))]
print('average degree: {}'.format(sum(G_deg_sum) / G.number_of_nodes()))
if nx.is_connected(G):
print('average path length: {}'.format(nx.average_shortest_path_length(G)))
print('average diameter: {}'.format(nx.diameter(G)))
G_cluster = sorted(list(nx.clustering(G).values()))
print('average clustering coefficient: {}'.format(sum(G_cluster) / len(G_cluster)))
cycle_len = []
cycle_all = nx.cycle_basis(G, 0)
for item in cycle_all:
cycle_len.append(len(item))
print('cycles', cycle_len)
print('cycle count', len(cycle_len))
draw_graph(G, prefix=prefix)
示例2: stabilizers
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def stabilizers(fer_op):
""" stabilizers """
edge_list = bravyi_kitaev_fast_edge_list(fer_op)
num_qubits = edge_list.shape[1]
graph = networkx.Graph()
graph.add_edges_from(tuple(edge_list.transpose()))
stabs = np.asarray(networkx.cycle_basis(graph))
stabilizer_ops = []
for stab in stabs:
a_op = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]])
stab = np.asarray(stab)
for i in range(np.size(stab)):
a_op = a_op * edge_operator_aij(edge_list, stab[i], stab[(i + 1) % np.size(stab)]) * 1j
stabilizer_ops.append(a_op)
return stabilizer_ops
示例3: test_cycle_basis
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def test_cycle_basis(self):
G=self.G
cy=networkx.cycle_basis(G,0)
sort_cy= sorted( sorted(c) for c in cy )
assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
cy=networkx.cycle_basis(G,1)
sort_cy= sorted( sorted(c) for c in cy )
assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
cy=networkx.cycle_basis(G,9)
sort_cy= sorted( sorted(c) for c in cy )
assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5]])
# test disconnected graphs
G.add_cycle(list("ABC"))
cy=networkx.cycle_basis(G,9)
sort_cy= sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
assert_equal(sort_cy, [[0,1,2,3],[0,1,6,7,8],[0,3,4,5],['A','B','C']])
示例4: test_cycle_basis
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def test_cycle_basis(self):
G = self.G
cy = networkx.cycle_basis(G, 0)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
cy = networkx.cycle_basis(G, 1)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
cy = networkx.cycle_basis(G, 9)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
# test disconnected graphs
nx.add_cycle(G, "ABC")
cy = networkx.cycle_basis(G, 9)
sort_cy = sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5],
['A', 'B', 'C']])
示例5: test_cycle_basis
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def test_cycle_basis(self):
G = self.G
cy = networkx.cycle_basis(G, 0)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
cy = networkx.cycle_basis(G, 1)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
cy = networkx.cycle_basis(G, 9)
sort_cy = sorted(sorted(c) for c in cy)
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
# test disconnected graphs
nx.add_cycle(G, "ABC")
cy = networkx.cycle_basis(G, 9)
sort_cy = sorted(sorted(c) for c in cy[:-1]) + [sorted(cy[-1])]
assert_equal(sort_cy, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5],
['A', 'B', 'C']])
示例6: penalized_logp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def penalized_logp(s):
if s is None: return -100.0
mol = Chem.MolFromSmiles(s)
if mol is None: return -100.0
logP_mean = 2.4570953396190123
logP_std = 1.434324401111988
SA_mean = -3.0525811293166134
SA_std = 0.8335207024513095
cycle_mean = -0.0485696876403053
cycle_std = 0.2860212110245455
log_p = Descriptors.MolLogP(mol)
SA = -sascorer.calculateScore(mol)
# cycle score
cycle_list = nx.cycle_basis(nx.Graph(Chem.rdmolops.GetAdjacencyMatrix(mol)))
if len(cycle_list) == 0:
cycle_length = 0
else:
cycle_length = max([len(j) for j in cycle_list])
if cycle_length <= 6:
cycle_length = 0
else:
cycle_length = cycle_length - 6
cycle_score = -cycle_length
normalized_log_p = (log_p - logP_mean) / logP_std
normalized_SA = (SA - SA_mean) / SA_std
normalized_cycle = (cycle_score - cycle_mean) / cycle_std
return normalized_log_p + normalized_SA + normalized_cycle
示例7: cycles_count
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def cycles_count(variables, relations):
g = as_networkx_graph(variables, relations)
cycles = nx.cycle_basis(g)
return len(cycles)
示例8: vacuum_operator
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def vacuum_operator(edge_matrix_indices):
"""Use the stabilizers to find the vacuum state in bravyi_kitaev_fast.
Args:
edge_matrix_indices(numpy array): specifying the edges
Return:
An instance of QubitOperator
"""
# Initialize qubit operator.
g = networkx.Graph()
g.add_edges_from(tuple(edge_matrix_indices.transpose()))
stabs = numpy.array(networkx.cycle_basis(g))
vac_operator = QubitOperator(())
for stab in stabs:
A = QubitOperator(())
stab = numpy.array(stab)
for i in range(numpy.size(stab)):
if i == (numpy.size(stab) - 1):
A = (1j)*A * edge_operator_aij(edge_matrix_indices,
stab[i], stab[0])
else:
A = (1j)*A * edge_operator_aij(edge_matrix_indices,
stab[i], stab[i+1])
vac_operator = vac_operator*(QubitOperator(()) + A)/numpy.sqrt(2)
return vac_operator
示例9: enumerate_cycle_basis
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def enumerate_cycle_basis(molecule):
"""Enumerate a closed cycle basis of bonds in molecule.
This uses cycle_basis from NetworkX:
https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.cycles.cycle_basis.html#networkx.algorithms.cycles.cycle_basis
Parameters
----------
molecule : OEMol
The molecule for a closed cycle basis of Bonds is to be identified
Returns
-------
bond_cycle_basis : list of list of OEBond
bond_cycle_basis[cycle_index] is a list of OEBond objects that define a cycle in the basis
You can think of these as the minimal spanning set of ring systems to check.
"""
g = nx.Graph()
for atom in molecule.GetAtoms():
g.add_node(atom.GetIdx())
for bond in molecule.GetBonds():
g.add_edge(bond.GetBgnIdx(), bond.GetEndIdx(), bond=bond)
bond_cycle_basis = list()
for cycle in nx.cycle_basis(g):
bond_cycle = list()
for i in range(len(cycle)):
atom_index_1 = cycle[i]
atom_index_2 = cycle[(i+1)%len(cycle)]
edge = g.edges[atom_index_1,atom_index_2]
bond = edge['bond']
bond_cycle.append(bond)
bond_cycle_basis.append(bond_cycle)
return bond_cycle_basis
示例10: vacuum_operator
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def vacuum_operator(fer_op):
"""Use the stabilizers to find the vacuum state in bravyi_kitaev_fast.
Args:
fer_op (FermionicOperator): the fermionic operator in the second quantized form
Returns:
WeightedPauliOperator: the qubit operator
"""
edge_list = bravyi_kitaev_fast_edge_list(fer_op)
num_qubits = edge_list.shape[1]
vac_operator = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]])
graph = networkx.Graph()
graph.add_edges_from(tuple(edge_list.transpose()))
stabs = np.asarray(networkx.cycle_basis(graph))
for stab in stabs:
a_op = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]])
stab = np.asarray(stab)
for i in range(np.size(stab)):
a_op = a_op * edge_operator_aij(edge_list, stab[i], stab[(i + 1) % np.size(stab)]) * 1j
# a_op.scaling_coeff(1j)
a_op += WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]])
vac_operator = vac_operator * a_op * np.sqrt(2)
# vac_operator.scaling_coeff()
return vac_operator
示例11: cleanup_fixed_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def cleanup_fixed_graph(pruned):
# remove cycles
while True:
cycles = networkx.cycle_basis(pruned)
if len(cycles) == 0:
break
to_delete = None
worst_val = None
for cycle in cycles:
cur_worst_val = None
cur_worst = None
for a,b,data in pruned.edges(cycle, data=True):
cur_val = data["p"]
if cur_worst_val is None or cur_val < cur_worst_val:
cur_worst_val = cur_val
cur_worst = (a,b)
if worst_val is None or cur_worst_val < worst_val:
worst_val = cur_worst_val
to_delete = cur_worst
pruned.remove_edge(*to_delete)
# remove all cis-edges at the ends of subgraphs
degrees = pruned.degree()
to_delete = []
for node, degree in dict(degrees).items():
if degree == 1:
edge = list(pruned.edges([node], data=True))[0]
if edge[2]["kind"]=="facing":
to_delete.append(node)
pruned.remove_nodes_from(to_delete)
# remove unconnected nodes
pruned.remove_nodes_from([node for (node, degree) in dict(pruned.degree()).items() if degree==0])
return pruned
示例12: correctLoops
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def correctLoops(code, loops_graph, charge_type):
while nx.cycle_basis(loops_graph) != []:
cycle = nx.cycle_basis(loops_graph)[0]
loop = path.Path(cycle)
for data in code.Primal.nodes():
if loop.contains_points([data]) == [True]:
charge = code.Primal.node[data]['charge'][charge_type]
code.Primal.node[data]['charge'][charge_type] = (charge + 1)%2
l = len(cycle)
for i in range(l):
n1, n2 = cycle[i], cycle[(i+1)%l]
loops_graph.remove_edge(*(n1,n2))
return code, loops_graph
示例13: loops_within_feeder
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def loops_within_feeder(self, *args):
"""Returns the number of loops within a feeder."""
if args:
return len(nx.cycle_basis(args[0]))
else:
return len(nx.cycle_basis(self.G.graph))
示例14: get_graph_metrics
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def get_graph_metrics(self):
nxGraph = self.nxGraph.to_undirected()
islands = list(nx.connected_component_subgraphs(nxGraph))
print('Number of islands: ', len(islands))
loops = nx.cycle_basis(nxGraph)
print('Number of loops: ', len(loops))
print('')
for i, eachCycle in enumerate(loops):
print('Loop ' + str(i) + ' : starts with node "' + eachCycle[0] + '"')
示例15: minimum_cycle_basis
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import cycle_basis [as 别名]
def minimum_cycle_basis(G, weight=None):
""" Returns a minimum weight cycle basis for G
Minimum weight means a cycle basis for which the total weight
(length for unweighted graphs) of all the cycles is minimum.
Parameters
----------
G : NetworkX Graph
weight: string
name of the edge attribute to use for edge weights
Returns
-------
A list of cycle lists. Each cycle list is a list of nodes
which forms a cycle (loop) in G. Note that the nodes are not
necessarily returned in a order by which they appear in the cycle
Examples
--------
>>> G=nx.Graph()
>>> G.add_cycle([0,1,2,3])
>>> G.add_cycle([0,3,4,5])
>>> print(nx.minimum_cycle_basis(G))
[[0, 1, 2, 3], [0, 3, 4, 5]]
References:
[1] Kavitha, Telikepalli, et al. "An O(m^2n) Algorithm for
Minimum Cycle Basis of Graphs."
http://link.springer.com/article/10.1007/s00453-007-9064-z
[2] de Pina, J. 1995. Applications of shortest path methods.
Ph.D. thesis, University of Amsterdam, Netherlands
See Also
--------
simple_cycles, cycle_basis
"""
# We first split the graph in commected subgraphs
return sum((_min_cycle_basis(c, weight) for c in
nx.connected_component_subgraphs(G)), [])