networkx
ScienceGraph algorithms
What is networkx?
NetworkX is the standard Python library for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It supports undirected graphs, directed graphs (digraphs), and multigraphs, and provides a rich set of algorithms for path finding, centrality, community detection, clustering, and more.
PyRun lets you run NetworkX experiments directly in your browser. Whether you're studying graph theory, modelling a social network, or exploring shortest-path algorithms, NetworkX is available immediately via micropip without any local installation.
Code Example
Build a directed graph and compute shortest paths.
NetworkX Social Graph
Try in Editorimport networkx as nx
G = nx.DiGraph()
G.add_edges_from([
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "Diana"),
("Charlie", "Diana"),
("Diana", "Eve"),
])
print("Nodes:", list(G.nodes()))
print("Edges:", list(G.edges()))
print("\nIn-degree (followers):")
for node, deg in G.in_degree():
print(f" {node}: {deg}")
print("\nShortest path Alice → Eve:")
path = nx.shortest_path(G, "Alice", "Eve")
print(" → ".join(path))
print("\nIs DAG?", nx.is_directed_acyclic_graph(G))Why run networkx in PyRun?
- ✦ Zero setup — no pip install, no virtual environment, no Python download
- ✦ Instant results — powered by WebAssembly, runs locally in your browser
- ✦ Share your code — generate a link and anyone can run it instantly
- ✦ Works offline — after first load, PyRun runs without internet