🌐 Random Graph
Phase Transition

Nodes (n) 80
Probability (p) 0.010
threshold p c = 1/(n−1) = 0.0127  
Avg degree <k>:
Components:
Giant size:
Giant fraction:

About this simulation

This simulation builds an Erdos-Renyi G(n,p) random graph: for every pair of the n nodes, an edge is added independently with probability p. A breadth-first search then partitions the graph into connected components, colouring the largest one red, and a force-directed layout (spring attraction on edges, electrostatic repulsion between all nodes) arranges the graph so its structure becomes visible. A live plot tracks the giant-component fraction against p and marks the theoretical critical threshold p c = 1/(n−1) as a dashed line, so you can watch the phase transition happen in real time rather than just read about it.

🔬 What it shows

As you raise p past the critical threshold pc = 1/(n−1), the many small tree-like components suddenly merge into one giant connected component spanning a macroscopic fraction of all n nodes. The HUD reports the live average degree <k> = (n−1)p, the number of separate components, the giant component's size, and its fraction of the whole graph, while the bottom-left plot traces the measured giant fraction against the theoretical curve S = 1 − exp(−<k>S).

🎮 How to use

Drag the n slider (20–200) to set the number of nodes and the p slider (0–1) to set the edge probability. Move p slowly across the highlighted threshold to watch isolated clusters snap together into a single red-highlighted giant component. Pause freezes the force-directed layout so you can inspect the current arrangement, and Reset regenerates a fresh random graph with the same n and p.

💡 Did you know?

The giant-component transition is mathematically the same phenomenon as bond percolation on the complete graph, and it shows up far beyond graph theory: an epidemic becomes self-sustaining once its reproduction number R₀ crosses 1, exactly the same threshold condition as <k> crossing 1 here.

Frequently asked questions

What random graph model does this simulation use?

It implements the classic Erdos-Renyi G(n,p) model. Given n nodes, every one of the n(n−1)/2 possible edges is included independently with probability p, generated by a single loop that rolls a random number for each pair. There is no preferential attachment or rewiring here — every edge is an independent coin flip, which is what makes the giant-component threshold mathematically clean.

How does the simulation find connected components?

After the edges are generated, the code runs a breadth-first search (BFS) from every unvisited node: it explores all nodes reachable through existing edges layer by layer using a queue, marking each one with a component ID. Repeating this from any remaining unvisited node partitions the whole graph into components in O(n + m) time. The largest resulting component is highlighted in red as the giant component.

What determines whether a giant component forms?

The controlling quantity is the average degree <k> = (n−1)·p, shown live in the HUD. Below the critical value <k> = 1 (equivalently p below pc = 1/(n−1)) the graph stays fragmented into small components of size O(log n). Above it, a single giant component emerges whose fraction S of all nodes satisfies the self-consistent equation S = 1 − exp(−<k>S), which the simulation solves numerically and plots alongside the measured data.

What does the force-directed layout actually do?

It is purely cosmetic, not part of the random graph model itself: every pair of nodes repels like charged particles (an inverse-square repulsion), every edge acts as a spring pulling its two endpoints toward a rest length, and a weak force pulls all nodes toward the canvas centre. Iterating these forces each frame lets connected clusters drift apart from unconnected ones, so the emerging giant component becomes visually obvious rather than just numerically reported.

Is the Erdos-Renyi model realistic for real-world networks?

Not usually. Real networks — social graphs, the web, power grids — tend to have heavy-tailed (power-law) degree distributions, high clustering, and community structure, none of which G(n,p) produces, since every edge here has exactly the same independent probability. More realistic alternatives include the Barabasi-Albert preferential-attachment model and the Watts-Strogatz small-world model, but Erdos-Renyi remains the essential mathematical baseline against which those richer models are compared.