def divrank_scipy(G, alpha=<NUM_LIT>, d=<NUM_LIT>, personalization=None,<EOL>max_iter=<NUM_LIT:100>, tol=<NUM_LIT>, nstart=None, weight='<STR_LIT>',<EOL>dangling=None):
import scipy.sparse<EOL>N = len(G)<EOL>if N == <NUM_LIT:0>:<EOL><INDENT>return {}<EOL><DEDENT>nodelist = G.nodes()<EOL>M = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight,<EOL>dtype=float)<EOL>S = scipy.array(M.sum(axis=<NUM_LIT:1>)).flatten()<EOL>S[S != <NUM_LIT:0>] = <NUM_LIT:1.0> / S[S != <NUM_LIT:0>]<EOL>Q = scipy.sparse.spdiags(S.T, <NUM_LIT:0>, *M.shape, format='<STR_LIT>')<EOL>M = Q * M<EOL>M = scipy.sparse.lil_matrix(M)<EOL>M.setdiag(<NUM_LIT:0.0>)<EOL>M = alpha * M<EOL>M.setdiag(<NUM_LIT:1.0> - alpha)<EOL>x = scipy.repeat(<NUM_LIT:1.0> / N, N)<EOL>if personalization is None:<EOL><INDENT>p = scipy.repeat(<NUM_LIT:1.0> / N, N)<EOL><DEDENT>else:<EOL><INDENT>missing = set(nodelist) - set(personalization)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>p = scipy.array([personalization[n] for n in nodelist],<EOL>dtype=float)<EOL>p = p / p.sum()<EOL><DEDENT>if dangling is None:<EOL><INDENT>dangling_weights = p<EOL><DEDENT>else:<EOL><INDENT>missing = set(nodelist) - set(dangling)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>dangling_weights = scipy.array([dangling[n] for n in nodelist],<EOL>dtype=float)<EOL>dangling_weights /= dangling_weights.sum()<EOL><DEDENT>is_dangling = scipy.where(S == <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>for _ in range(max_iter):<EOL><INDENT>xlast = x<EOL>D_t =  M * x<EOL>x = (<EOL>d * (x / D_t * M * x + sum(x[is_dangling]) * dangling_weights)<EOL>+ (<NUM_LIT:1.0> - d) * p<EOL>)<EOL>err = scipy.absolute(x - xlast).sum()<EOL>if err < N * tol:<EOL><INDENT>return dict(list(zip(nodelist, list(map(float, x)))))<EOL><DEDENT><DEDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>' % max_iter)<EOL>
Returns the DivRank (Diverse Rank) of the nodes in the graph.
This code is based on networkx.pagerank_scipy