Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Visualising social networks

Practical, exam-focused notes on methods and algorithms for visualising social networks (node-link, matrices, force-directed and spectral layouts), with definitions, formulas, worked model answers, common mistakes and quick checks.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Visualising social networks helps an engineer quickly see structure: communities, high-degree (important) users, bridges and isolated groups. Good visualisations support debugging, feature design (recommendation, moderation) and presentations of analytic results from topics such as Community detection and Node centrality measures.

Key ideas

Start with basic terms and then build up methods used in visualisation.

  • Graph: a pair G = (V, E) where V is a set of nodes (also called vertices) and E is a set of edges (unordered pairs for undirected graphs, ordered pairs for directed graphs). Use this to model social networks (users = nodes, ties = edges).

  • Adjacency matrix: for a graph with n = |V| nodes, the adjacency matrix A is an n×n matrix with entries A_{ij} = 1 if there is an edge between node i and node j, else 0. For weighted graphs A_{ij} equals the weight. For directed graphs, A_{ij}=1 indicates an edge from i to j (A is generally not symmetric); for undirected graphs, A is symmetric with A_{ij}=A_{ji}. Adjacency matrices represent exact connections and allow matrix algorithms.

  • Node-link diagram: a drawing that places nodes as points and edges as lines between them. This is the most intuitive visual form for small-to-medium graphs.

  • Matrix visualisation: plot the adjacency matrix as a grid; cell (i,j) is filled or coloured when A_{ij} = 1. This scales better to dense graphs and avoids edge overlap.

  • Ego network: the subgraph formed by a focal node (the ego), its neighbours (alters), and all edges among those alters. Used for local visualisation of a user's immediate ties.

  • Community / Cluster: a subset of nodes with relatively many internal edges and fewer edges to nodes outside. Visualisations should make communities visible by node placement or matrix block structure.

  • Layout: an assignment of coordinates x_i in 2D (or 3D) for every node i. A good layout reveals structure and is reproducible (or explainable).

  • Force-directed layout: a family of layouts that treat edges as springs (attractive forces) and nodes as repelling charges (repulsive forces). Iteratively adjust node positions to reduce a global energy or reach force equilibrium. Common instances: Fruchterman–Reingold and spring-layouts.

    Exact typical rules used in Fruchterman–Reingold (derivation often required in exams): choose a constant k (ideal distance) ~ sqrt(area/n). For two nodes at distance d:

    • attractive force f_a(d) = d^2 / k (pulls connected nodes together)
    • repulsive force f_r(d) = k^2 / d (pushes all nodes apart) These are chosen so that at equilibrium connected nodes tend to be at distance approximately k.
  • Stress and distance-preserving layouts: define target distances d_{ij} (often shortest-path distances in G). Minimise the stress function stress(X) = sum_{i<j} w_{ij} (||x_i - x_j|| - d_{ij})^2, where w_{ij} is a weight (commonly 1/d_{ij}^2). This is a formal optimisation approach; algorithms include stress majorization.

  • Spectral layout: use eigenvectors of the graph Laplacian L = D - A (where D is the diagonal matrix of degrees). Place node i at coordinates given by components of low-order eigenvectors (usually the second and third smallest eigenvalues' eigenvectors for 2D). This tends to separate clusters along spectral axes.

  • Edge bundling: technique to visually group many similar or nearby edges to reduce clutter in dense graphs. It trades some path clarity for reduced visual noise.

  • Multilevel / coarsening layouts: for very large graphs, create smaller graphs by merging nodes (coarsening), compute layout at coarse level, then refine. Improves speed and helps escape local minima.

Diagrams to draw in answers (describe exactly):

  • For node-link: draw 6 labelled circles (1..6), draw straight lines for edges. Label edges with pairs, draw arrowheads if directed.
  • For adjacency matrix: draw a 6×6 square grid. Label rows and columns 1..6. Shade cell (i,j) if edge (i,j) exists. Show blocks for communities.
  • For spectral layout sketch: draw axis X and Y; show two clusters separated along X; annotate that coordinates are eigenvector components.

Assumptions to state when solving: graphs are simple (no multi-edges) unless noted, layouts are in the plane (2D), and positions are continuous (real coordinates).

Formulas

G = (V, E) where V is node set and E is edge set. Symbols: V (set of nodes), E (set of edges). Applies to all graph visualisation.

A_{ij} = 1 if (i,j) in E else 0 — adjacency matrix entry; unitless; applies to simple unweighted graphs.

deg(i) = sum_j A_{ij} — degree of node i; unitless; applies to undirected graphs. outdeg(i) = sum_j A_{ij} and indeg(i) = sum_j A_{ji} — out-degree and in-degree in directed graphs.

D = diag(deg(1), ..., deg(n)) — degree matrix; unitless.

L = D - A — graph Laplacian; unitless; used in spectral layout and many spectral algorithms.

stress(X) = sum_{i<j} w_{ij} (||x_i - x_j|| - d_{ij})^2 — stress function; X = {x_i} are node coordinates; ||x_i - x_j|| is Euclidean distance in layout units; d_{ij} is target distance (graph-theoretic); w_{ij} a weight (often 1/d_{ij}^2). For disconnected pairs where d_{ij} is undefined or infinite, set w_{ij}=0 or restrict the sum to pairs within a connected component.

f_a(d) = d^2 / k — attractive force in Fruchterman–Reingold; d in layout units; k is ideal distance (layout units).

f_r(d) = k^2 / d — repulsive force in Fruchterman–Reingold; same units as above.

k = C * sqrt(area / n) — typical choice for ideal distance; C is a constant (often 1); area in squared layout units; n number of nodes.

Note: all forces and lengths in graph layouts are unitless / arbitrary visual units (not SI). Choose consistent scale when computing.

Worked examples

Example 1 — compare node-link and adjacency matrix (model long-answer) Question: Given graph with nodes {1,2,3,4,5,6} and edges {(1,2),(1,3),(2,3),(3,4),(4,5),(4,6),(5,6)}, draw a node-link diagram and adjacency matrix, then explain which reveals community structure better. Model answer (step-by-step):

  1. Write adjacency matrix A (6×6). Rows/cols ordered 1..6. Row 1: [0 1 1 0 0 0] Row 2: [1 0 1 0 0 0] Row 3: [1 1 0 1 0 0] Row 4: [0 0 1 0 1 1] Row 5: [0 0 0 1 0 1] Row 6: [0 0 0 1 1 0]
  2. Draw node-link: place nodes 1,2,3 on left forming a triangle; nodes 4,5,6 on right forming a triangle; draw edge (3,4) as bridge. Label nodes with numbers.
  3. Draw adjacency matrix: 6×6 grid labelled 1..6 on rows and columns. Shade entries where A_{ij}=1. You will see two near-full 3×3 blocks (rows/cols 1..3 and 4..6) with zeros on the diagonal, and two symmetric off-diagonal filled cells (3,4) and (4,3) representing the bridge.
  4. Conclusion: Both visualisations show two communities. The adjacency matrix with rows/columns ordered by community shows clear block-diagonal structure (better for dense internal connectivity). The node-link diagram is more intuitive to non-technical viewers and shows the bridge edge clearly. For large dense graphs, matrix is superior to avoid edge clutter.

Example 2 — explain Fruchterman–Reingold and show forces numerically for a simple pair Question: Explain the Fruchterman–Reingold layout forces and compute net force on two connected nodes at distance d = 1.0 if k = 1.0. Model answer:

  1. Fruchterman–Reingold defines attractive force f_a(d) = d^2 / k and repulsive f_r(d) = k^2 / d.
  2. For two connected nodes at d = 1.0 and k = 1.0: f_a(1.0) = 1.0^2 / 1.0 = 1.0 (pull towards each other) f_r(1.0) = 1.0^2 / 1.0 = 1.0 (repel each other)
  3. Net radial force magnitude = f_a - f_r = 0.0, so at this d the two forces balance. Interpretation: k is chosen so typical neighbor distances settle near k.
  4. In practice many nodes and uneven topology create nonzero nets and iterations are needed. Cooling schedules reduce step size over iterations.

Example 3 — short model answer on spectral layout (theory) Question: How does spectral layout use the Laplacian? Give a concise answer. Model answer:

  1. Compute Laplacian L = D - A for the graph.
  2. Compute eigenvectors of L. The smallest eigenvalue is 0 with constant eigenvector for connected graphs; use the next smallest eigenvectors (2nd and 3rd) as coordinate axes: for node i, x_i = (v2[i], v3[i]).
  3. This places nodes so that well-connected groups tend to have similar coordinate values, often separating clusters. Mention limitations: scaling, rotated layouts, and sensitivity to graph noise.

Common mistakes

  • Forgetting to state assumptions (directed vs undirected, weighted vs unweighted).
  • Treating layout units as physical SI units — layout distances are arbitrary and relative.
  • Claiming spectral layout always shows communities — it often helps but can fail on certain topologies.
  • Forgetting that Fruchterman–Reingold and similar algorithms are iterative and may produce different outputs on different random initialisations unless fixed seed used.
  • Using node-link diagrams without reordering or bundling for dense graphs, producing hairball plots.

For your exam

Question types you can expect: short definitions, describe/draw diagrams, derive and explain force formulas, compute small adjacency matrices, compare visualization methods, and short notes on algorithms (Fruchterman–Reingold, stress-minimisation, spectral layout).

What full-marks answers include:

  • Clear definitions of all terms used.
  • Exact formulas or algorithm rules (e.g., state f_a and f_r for Fruchterman–Reingold and explain k choice).
  • A labelled diagram when asked: for node-link draw nodes as circles labelled, edges as straight lines, indicate layout coordinates if required; for adjacency matrix draw grid and shade cells.
  • State assumptions (undirected/simple graph, node ordering, layout area).
  • For algorithm questions: state iteration, stopping condition, complexity considerations, and pros/cons.

Quick check

  1. What is an adjacency matrix? 2. Give one advantage of matrix visualisation over node-link. 3. In Fruchterman–Reingold, what is the typical formula for repulsive force? Answers: 1) A square n×n matrix A with A_{ij}=1 if nodes i and j are connected, else 0. 2) Matrix visualisation scales better for dense graphs and avoids edge overlap (reveals block structure). 3) f_r(d) = k^2 / d.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?