graphmatrix.py 5.0 KB

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