def test_normalized_laplacian(self): "Generalized Graph Laplacian" GL = numpy.array( [ [1.00, -0.408, -0.408, -0.577, 0.00], [-0.408, 1.00, -0.50, 0.00, 0.00], [-0.408, -0.50, 1.00, 0.00, 0.00], [-0.577, 0.00, 0.00, 1.00, 0.00], [0.00, 0.00, 0.00, 0.00, 0.00], ] ) Lsl = numpy.array( [ [0.75, -0.2887, -0.2887, -0.3536, 0.0], [-0.2887, 0.6667, -0.3333, 0.0, 0.0], [-0.2887, -0.3333, 0.6667, 0.0, 0.0], [-0.3536, 0.0, 0.0, 0.5, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], ] ) assert_almost_equal(nx.normalized_laplacian(self.G), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.MG), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG, weight="other"), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.Gsl), Lsl, decimal=3)
def test_normalized_laplacian(self): "Generalized Graph Laplacian" GL=numpy.array([[ 1.00, -0.408, -0.408, -0.577, 0.00], [-0.408, 1.00, -0.50, 0.00 , 0.00], [-0.408, -0.50, 1.00, 0.00, 0.00], [-0.577, 0.00, 0.00, 1.00, 0.00], [ 0.00, 0.00, 0.00, 0.00, 0.00]]) assert_almost_equal(nx.normalized_laplacian(self.G),GL,decimal=3) assert_almost_equal(nx.normalized_laplacian(self.MG),GL,decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG),GL,decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG,weight='other'),GL,decimal=3)
def test_normalized_laplacian(self): "Generalized Graph Laplacian" GL = numpy.array([[1.00, -0.408, -0.408, -0.577, 0.00], [-0.408, 1.00, -0.50, 0.00, 0.00], [-0.408, -0.50, 1.00, 0.00, 0.00], [-0.577, 0.00, 0.00, 1.00, 0.00], [0.00, 0.00, 0.00, 0.00, 0.00]]) assert_almost_equal(nx.normalized_laplacian(self.G), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.MG), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG), GL, decimal=3) assert_almost_equal(nx.normalized_laplacian(self.WG, weight='other'), GL, decimal=3)
def normalized_laplacian_spectrum(G, weight='weight'): try: import numpy as np except ImportError: raise ImportError( "laplacian_spectrum() requires NumPy: http://scipy.org/ ") return np.linalg.eigvals(nx.normalized_laplacian(G,weight=weight))
def test_normalized_laplacian(self): "Generalized Graph Laplacian" GL = numpy.array([[1.00, -0.41, -0.41, -0.58, 0.00], [-0.41, 1.00, -0.50, 0.00, 0.00], [-0.41, -0.50, 1.00, 0.00, 0.00], [-0.58, 0.00, 0.00, 1.00, 0.00], [0.00, 0.00, 0.00, 0.00, 0.00]]) assert_almost_equal(nx.normalized_laplacian(self.G), GL, decimal=2)
def test_normalized_laplacian(self): "Generalized Graph Laplacian" GL=numpy.array([[ 1.00, -0.41, -0.41, -0.58, 0.00], [-0.41, 1.00, -0.50, 0.00 , 0.00], [-0.41, -0.50, 1.00, 0.00, 0.00], [-0.58, 0.00, 0.00, 1.00, 0.00], [ 0.00, 0.00, 0.00, 0.00, 0.00]]) assert_almost_equal(nx.normalized_laplacian(self.G),GL,decimal=2)
def test2(): deg = [3, 2, 2, 1, 0] G = nx.havel_hakimi_graph(deg) # Add self loops for node in G.nodes(): G.add_edge(node, node) Ln = nx.normalized_laplacian(G) print Ln np.set_printoptions(4) print repr(Ln)
def test1(): N = 3 G = nx.cycle_graph(N) # Add self loop to all vertices for node in G.nodes(): G.add_edge(node, node) Ln_nx = nx.normalized_laplacian(G) L_nx = nx.laplacian(G) A = nx.adj_matrix(G) # Using G.degree to compute D D1 = np.array([G.degree()[n] for n in G]) Disqrt1 = np.array(1 / np.sqrt(D1)) Disqrt1 = np.diag(Disqrt1) L1 = np.diag(D1) - A Ln1 = np.dot(np.dot(Disqrt1, L1), Disqrt1) # Using A.sum(1) to compute D D2 = np.array(np.sum(A, 1)).flatten() Disqrt2 = np.array(1 / np.sqrt(D2)) Disqrt2 = np.diag(Disqrt2) L2 = np.diag(D2) - A Ln2 = np.dot(np.dot(Disqrt2, L2), Disqrt2) # Using G.degree and nx.laplacian to compute D L3 = nx.laplacian(G) Ln3 = np.dot(np.dot(Disqrt1, L3), Disqrt1) Ln3_ = np.dot(Disqrt1, np.dot(L3, Disqrt1)) print "NetworkX version" print Ln_nx print "\nUsing G.degree to compute D" print Ln1 print "\nUsing A.sum(1) to compute D" print Ln2 print "\nUsing G.degree and nx.laplacian to compute D" print Ln3
def algebraic_connectivity(G): L = nx.normalized_laplacian(G) e = eigenvalues(L) # print sorted(e, reverse=True)[0], sorted(e, reverse=True)[1] return sorted(e)[1]
def findNormalizedLaplacianKernel(g): return nx.normalized_laplacian(g)
##print '%d and %d have the same component --> %d, %d, %d, %d' % (u,v, A[u,v], deg_[u], deg_[v], m_) #Q += float(A[u,v])/float(2*m_) - float(deg_[u]*deg_[v])/float(math.pow(2*m_,2)) ##print Q #print "Q: %f" % Q #print nx.laplacian_spectrum(G) G = nx.Graph() buildG(G, '/Users/kazemjahanbakhsh/Downloads/graph.txt', ',') n = G.number_of_nodes() #|V| print G.nodes() #m_ = G.number_of_edges() #|E| print 'no of nodes: %d' % n f_gl = open('/Users/kazemjahanbakhsh/Downloads/community/Glap.dat', 'w') Glap = nx.normalized_laplacian(G) for i in range(0,n): val = '' for j in range(0,n): val += str(Glap[i,j]) if j < n-1: val += ',' val += '\n' f_gl.write(val) f_gl.close() A = nx.adj_matrix(G) #for i in range(0,10): #print A[0,i] m_ = 0.0 #the weighted version for number of edges
import networkx as nx import numpy as np import matplotlib.pyplot as plt G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (2, 3), (1, 4), (4, 5)]) G2 = nx.generators.random_graphs.fast_gnp_random_graph(10, .3) # nx.adjacency_matrix(G) L = nx.laplacian(G) # L=D-A # np.linalg.eigvals(L) np.linalg.eig(L) res = nx.laplacian_spectrum(G) print res print nx.normalized_laplacian(G) c = nx.communicability(G) # drawing nx.draw(G) # default using spring_layout: force-directed # same as: # pos = nx.spring_layout(G) # nx.draw(G, pos) nx.draw_random(G) nx.draw_spectral(G) plt.show() plt.savefig('path.png') plt.cla() # random graph G = nx.generators.random_graphs.random_regular_graph(6, 50) plt.show()
for pair in combinations(points, 2): d = norm(pair[1] - pair[0]) if d < delta: w = weight(pair) G.add_edge(tuple(pair[1]), tuple(pair[0]), weight=w) return G if __name__ == '__main__': points = points_in_circle(30, add_noise=True) G = graph(points) nodes = [tuple(p) for p in points] L = nx.normalized_laplacian(G, nodelist=nodes) #L = nx.normalized_laplacian(G) L = (L + L.T) / 2.0 # iron out numerical wrinkles eigvals, eigvecs = np.linalg.eigh(L) new_point = np.array([0.8, 0.8]) neighbors = [] weights = {} delta = 0.3 for n, p in enumerate(points): d = norm(p - new_point) if d < delta: w = weight((new_point, p)) neighbors.append((n, w)) i = 1 # approximate the second eigenvector
import networkx as nx import numpy as np import matplotlib.pyplot as plt G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (2, 3), (1, 4), (4, 5)]) G2 = nx.generators.random_graphs.fast_gnp_random_graph(10, .3) # nx.adjacency_matrix(G) L = nx.laplacian(G) # L=D-A # np.linalg.eigvals(L) np.linalg.eig(L) res = nx.laplacian_spectrum(G) print res print nx.normalized_laplacian(G) c = nx.communicability(G) # drawing nx.draw(G) # default using spring_layout: force-directed # same as: # pos = nx.spring_layout(G) # nx.draw(G, pos) nx.draw_random(G) nx.draw_spectral(G) plt.show() plt.savefig('path.png') plt.cla() # random graph G = nx.generators.random_graphs.random_regular_graph(6, 50)