11"""
22Adjacency matrix and incidence matrix of graphs.
33"""
4- # Copyright (C) 2004-2011 by
4+ # Copyright (C) 2004-2011 by
55# Aric Hagberg <hagberg@lanl.gov>
66# Dan Schult <dschult@colgate.edu>
77# Pieter Swart <swart@lanl.gov>
1717 ]
1818
1919
20- def incidence_matrix (G , nodelist = None , edgelist = None ,
21- oriented = False , weight = None ):
20+ def incidence_matrix (G , nodelist = None , edgelist = None ,
21+ oriented = False , weight = None , sparse = False ):
2222 """Return incidence matrix of G.
2323
2424 The incidence matrix assigns each row to a node and each column to an edge.
25- For a standard incidence matrix a 1 appears wherever a row's node is
25+ For a standard incidence matrix a 1 appears wherever a row's node is
2626 incident on the column's edge. For an oriented incidence matrix each
2727 edge is assigned an orientation (arbitrarily for undirected and aligning to
28- direction for directed). A -1 appears for the tail of an edge and 1
28+ direction for directed). A -1 appears for the tail of an edge and 1
2929 for the head of the edge. The elements are zero otherwise.
30-
30+
3131 Parameters
3232 ----------
3333 G : graph
34- A NetworkX graph
34+ A NetworkX graph
3535
3636 nodelist : list, optional (default= all nodes in G)
3737 The rows are ordered according to the nodes in nodelist.
3838 If nodelist is None, then the ordering is produced by G.nodes().
3939
40- edgelist : list, optional (default= all edges in G)
40+ edgelist : list, optional (default= all edges in G)
4141 The columns are ordered according to the edges in edgelist.
4242 If edgelist is None, then the ordering is produced by G.edges().
4343
4444 oriented: bool, optional (default=False)
45- If True, matrix elements are +1 or -1 for the head or tail node
45+ If True, matrix elements are +1 or -1 for the head or tail node
4646 respectively of each edge. If False, +1 occurs at both nodes.
4747
4848 weight : string or None, optional (default=None)
4949 The edge data key used to provide each value in the matrix.
5050 If None, then each edge has weight 1. Edge weights, if used,
5151 should be positive so that the orientation can provide the sign.
5252
53+ sparse : bool, optional (default=False)
54+ If True, matrix returned is in sparse format.
55+
5356 Returns
5457 -------
5558 A : NumPy matrix
5659 The incidence matrix of G.
5760
5861 Notes
5962 -----
60- For MultiGraph/MultiDiGraph, the edges in edgelist should be
63+ For MultiGraph/MultiDiGraph, the edges in edgelist should be
6164 (u,v,key) 3-tuples.
6265
63- "Networks are the best discrete model for so many problems in
66+ "Networks are the best discrete model for so many problems in
6467 applied mathematics" [1]_.
6568
6669 References
@@ -73,14 +76,23 @@ def incidence_matrix(G, nodelist=None, edgelist=None,
7376 except ImportError :
7477 raise ImportError (
7578 "incidence_matrix() requires numpy: http://scipy.org/ " )
79+ if sparse :
80+ try :
81+ import scipy .sparse as sparse
82+ except ImportError :
83+ raise ImportError (
84+ "Sparse output requires numpy: http://scipy.org/ " )
7685 if nodelist is None :
7786 nodelist = G .nodes ()
7887 if edgelist is None :
7988 if G .is_multigraph ():
8089 edgelist = G .edges (keys = True )
8190 else :
8291 edgelist = G .edges ()
83- A = np .zeros ((len (nodelist ),len (edgelist )))
92+ if sparse :
93+ A = sparse .lil_matrix ((len (nodelist ),len (edgelist )))
94+ else :
95+ A = np .zeros ((len (nodelist ),len (edgelist )))
8496 node_index = dict ( (node ,i ) for i ,node in enumerate (nodelist ) )
8597 for ei ,e in enumerate (edgelist ):
8698 (u ,v ) = e [:2 ]
@@ -105,17 +117,20 @@ def incidence_matrix(G, nodelist=None, edgelist=None,
105117 else :
106118 A [ui ,ei ] = wt
107119 A [vi ,ei ] = wt
108- return np .asmatrix (A )
120+ if sparse :
121+ return A
122+ else :
123+ return np .asmatrix (A )
109124
110125def adjacency_matrix (G , nodelist = None , weight = 'weight' ):
111126 """Return adjacency matrix of G.
112127
113128 Parameters
114129 ----------
115130 G : graph
116- A NetworkX graph
131+ A NetworkX graph
117132
118- nodelist : list, optional
133+ nodelist : list, optional
119134 The rows and columns are ordered according to the nodes in nodelist.
120135 If nodelist is None, then the ordering is produced by G.nodes().
121136
0 commit comments