Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/linalg/graphmatrix.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """ | |
2 Adjacency matrix and incidence matrix of graphs. | |
3 """ | |
4 import networkx as nx | |
5 | |
6 __all__ = ["incidence_matrix", "adj_matrix", "adjacency_matrix"] | |
7 | |
8 | |
9 def incidence_matrix(G, nodelist=None, edgelist=None, oriented=False, weight=None): | |
10 """Returns incidence matrix of G. | |
11 | |
12 The incidence matrix assigns each row to a node and each column to an edge. | |
13 For a standard incidence matrix a 1 appears wherever a row's node is | |
14 incident on the column's edge. For an oriented incidence matrix each | |
15 edge is assigned an orientation (arbitrarily for undirected and aligning to | |
16 direction for directed). A -1 appears for the source (tail) of an edge and | |
17 1 for the destination (head) of the edge. The elements are zero otherwise. | |
18 | |
19 Parameters | |
20 ---------- | |
21 G : graph | |
22 A NetworkX graph | |
23 | |
24 nodelist : list, optional (default= all nodes in G) | |
25 The rows are ordered according to the nodes in nodelist. | |
26 If nodelist is None, then the ordering is produced by G.nodes(). | |
27 | |
28 edgelist : list, optional (default= all edges in G) | |
29 The columns are ordered according to the edges in edgelist. | |
30 If edgelist is None, then the ordering is produced by G.edges(). | |
31 | |
32 oriented: bool, optional (default=False) | |
33 If True, matrix elements are +1 or -1 for the head or tail node | |
34 respectively of each edge. If False, +1 occurs at both nodes. | |
35 | |
36 weight : string or None, optional (default=None) | |
37 The edge data key used to provide each value in the matrix. | |
38 If None, then each edge has weight 1. Edge weights, if used, | |
39 should be positive so that the orientation can provide the sign. | |
40 | |
41 Returns | |
42 ------- | |
43 A : SciPy sparse matrix | |
44 The incidence matrix of G. | |
45 | |
46 Notes | |
47 ----- | |
48 For MultiGraph/MultiDiGraph, the edges in edgelist should be | |
49 (u,v,key) 3-tuples. | |
50 | |
51 "Networks are the best discrete model for so many problems in | |
52 applied mathematics" [1]_. | |
53 | |
54 References | |
55 ---------- | |
56 .. [1] Gil Strang, Network applications: A = incidence matrix, | |
57 http://academicearth.org/lectures/network-applications-incidence-matrix | |
58 """ | |
59 import scipy.sparse | |
60 | |
61 if nodelist is None: | |
62 nodelist = list(G) | |
63 if edgelist is None: | |
64 if G.is_multigraph(): | |
65 edgelist = list(G.edges(keys=True)) | |
66 else: | |
67 edgelist = list(G.edges()) | |
68 A = scipy.sparse.lil_matrix((len(nodelist), len(edgelist))) | |
69 node_index = {node: i for i, node in enumerate(nodelist)} | |
70 for ei, e in enumerate(edgelist): | |
71 (u, v) = e[:2] | |
72 if u == v: | |
73 continue # self loops give zero column | |
74 try: | |
75 ui = node_index[u] | |
76 vi = node_index[v] | |
77 except KeyError as e: | |
78 raise nx.NetworkXError( | |
79 f"node {u} or {v} in edgelist " f"but not in nodelist" | |
80 ) from e | |
81 if weight is None: | |
82 wt = 1 | |
83 else: | |
84 if G.is_multigraph(): | |
85 ekey = e[2] | |
86 wt = G[u][v][ekey].get(weight, 1) | |
87 else: | |
88 wt = G[u][v].get(weight, 1) | |
89 if oriented: | |
90 A[ui, ei] = -wt | |
91 A[vi, ei] = wt | |
92 else: | |
93 A[ui, ei] = wt | |
94 A[vi, ei] = wt | |
95 return A.asformat("csc") | |
96 | |
97 | |
98 def adjacency_matrix(G, nodelist=None, weight="weight"): | |
99 """Returns adjacency matrix of G. | |
100 | |
101 Parameters | |
102 ---------- | |
103 G : graph | |
104 A NetworkX graph | |
105 | |
106 nodelist : list, optional | |
107 The rows and columns are ordered according to the nodes in nodelist. | |
108 If nodelist is None, then the ordering is produced by G.nodes(). | |
109 | |
110 weight : string or None, optional (default='weight') | |
111 The edge data key used to provide each value in the matrix. | |
112 If None, then each edge has weight 1. | |
113 | |
114 Returns | |
115 ------- | |
116 A : SciPy sparse matrix | |
117 Adjacency matrix representation of G. | |
118 | |
119 Notes | |
120 ----- | |
121 For directed graphs, entry i,j corresponds to an edge from i to j. | |
122 | |
123 If you want a pure Python adjacency matrix representation try | |
124 networkx.convert.to_dict_of_dicts which will return a | |
125 dictionary-of-dictionaries format that can be addressed as a | |
126 sparse matrix. | |
127 | |
128 For MultiGraph/MultiDiGraph with parallel edges the weights are summed. | |
129 See `to_numpy_array` for other options. | |
130 | |
131 The convention used for self-loop edges in graphs is to assign the | |
132 diagonal matrix entry value to the edge weight attribute | |
133 (or the number 1 if the edge has no weight attribute). If the | |
134 alternate convention of doubling the edge weight is desired the | |
135 resulting Scipy sparse matrix can be modified as follows: | |
136 | |
137 >>> import scipy as sp | |
138 >>> G = nx.Graph([(1, 1)]) | |
139 >>> A = nx.adjacency_matrix(G) | |
140 >>> print(A.todense()) | |
141 [[1]] | |
142 >>> A.setdiag(A.diagonal() * 2) | |
143 >>> print(A.todense()) | |
144 [[2]] | |
145 | |
146 See Also | |
147 -------- | |
148 to_numpy_array | |
149 to_scipy_sparse_matrix | |
150 to_dict_of_dicts | |
151 adjacency_spectrum | |
152 """ | |
153 return nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight) | |
154 | |
155 | |
156 adj_matrix = adjacency_matrix |