NetworkX

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

NetworkX is a Python library for studying graphs and networks. NetworkX is free software released under the BSD-new license.

NetworkX is a Python library for studying graphs and networks. NetworkX is free software released under the BSD-new license. G = nx.path_graph(4) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) This layout method is used for interactive and exploratory visualizations. The spring layout often reveals the structure of the graph in a intuitive and readable way ====How the spectral layout work==== Source: Construct the Laplacian matrix of the graph. A Laplacian matrix L of a graph is defined as L=D-A , where D is the degree matrix (a diagonal matrix where D_{ii} is the degree of vertex i) and A is the adjacency matrix. For a graph G with n vertices, the adjacency matrix A is an n × n matrix where A_{ij} = 1 if there is an edge between vertex i and vertex i and A_{ij} = 0 otherwise. The Laplacian matrix has eigenvalues and corresponding eigenvectors that encode the graph's structural properties. Compute the eigenvectors corresponding to the smallest non-zero eigenvalues. Use these eigenvectors as coordinate values for positioning vertices. X-coordinates come from the second eigenvector (associated with the second-smallest eigenvalue), and Y-coordinates come from the third eigenvector. Scale and center the resulting layout as needed. ====Why it reveals the network's structure==== Nodes in dense clusters have similar eigenvector entries, causing them to group spatially. The Fiedler vector (second eigenvector) minimizes the ratio cut, separating the graph into clusters with minimal interconnections. In the plot above, we observe that the spectral layout helps capture the global and communal structures embedded in the graph. Comparing both layouts, we see that the spectral layout keeps nodes belonging to the same community closely packed, thereby capturing communities more easily as compared to the spring layout. ====Codes for implementing the plot==== import numpy as np import matplotlib.pyplot as plt import networkx as nx Generate a graph with overlapping nodes in spectral layout G = nx.karate_club_graph() L = nx.laplacian_matrix(G).todense() Compute eigenvectors of L for spectral layout eigenvals, eigenvecs = np.linalg.eigh(L) fiedler_vector = eigenvecs[:, 1] # Second smallest eigenvector eigenvec2 = eigenvecs[:, 2] # Third smallest eigenvector Create spectral layout (2D: use Fiedler vector for x-coord, zeros for y-coord) pos_spectral = {node: (fiedler_vector[i]+ np.random.normal(0, 0.01), eigenvec2[i]+ np.random.normal(0, 0.1)) for i, node in enumerate(G.nodes())} Define layouts to compare layouts = { "Spring Layout": nx.spring_layout(G, seed=42), "Spectral Layout": pos_spectral } Plot all layouts plt.figure(figsize=(15, 8)) for i, (title, pos) in enumerate(layouts.items(), 1): plt.subplot(1, 2, i) nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=800, font_size=10) plt.title(title) plt.tight_layout() plt.savefig('Spec_layout.png') plt.show() simple shell‐layout example G = nx.balanced_tree(2, 2) shells = [[0], [1, 2], [3, 4, 5, 6]] pos = nx.shell_layout(G, nlist=shells) nx.draw(G, pos, with_labels=True) Bipartite layout is specialized for two‐set graphs: it places one node set on a horizontal line at y=0 and the other at y=1. You must supply the two partitions (e.g. via a node attribute or explicit list). Edges then connect vertically between layers, making the bipartite structure immediately clear. It's ideal for affiliation networks, two‐mode data, or any graph with exactly two types of nodes. Positioning along the x-axis is usually automatic, but you can override via the align or manual x-coordinates. ==External links== Official website: NetworkX discussion group Survey of existing graph theory software NetworkX on StackOverflow

Text derived from https://en.wikipedia.org/w/index.php?oldid=1368885731 under CC BY-SA 4.0; modifications have been made.