MCS-208 · Unit 8

Graphs

Section 2 of 8

Block 3 · Unit 8Graphs7 min · Section 2 of 8

8.2Representing Graphs: Adjacency Matrix and Adjacency Lists

Covers IGNOU MCS-208 §8.2

Before running any graph algorithm, the graph must be stored. There are two standard representations, and the choice affects both the memory used and the running time of the algorithms.

Adjacency matrix

For an undirected graph the matrix is symmetric: aij=ajia_{ij} = a_{ji}, i.e. A=ATA = A^T.

Adjacency lists

Comparison

Adjacency matrixAdjacency lists
SpaceO(n2)O(n^2)O(n+e)O(n + e)
Is (u,v)(u, v) an edge?O(1)O(1)O(deg⁡(u))O(\deg(u))
List all neighbours of uuO(n)O(n)O(deg⁡(u))O(\deg(u))
Visit every edgeO(n2)O(n^2)O(n+e)O(n + e)
Best forDense graphs (ee close to n2n^2)Sparse graphs (ee much smaller than n2n^2), most real networks

Check Your Progress 1, Q3

For the directed graph of Figure 8.3 (edges 1→2, 1→3, 3→4, 4→1, 4→2) the matrix has aij=1a_{ij} = 1 only for those ordered pairs; it is not symmetric:

1234
10110
20000
30001
41100

For the undirected graph of Figure 8.4 (edges 1–2, 1–3, 1–4, 2–4, 3–4) the matrix is symmetric:

1234
10111
21001
31001
41110

(The answers in §8.9 are correct, but the second matrix is labelled “Figure 8.3” instead of “Figure 8.4”.)

Key takeaways

  • Matrix: n×nn \times n, O(n2)O(n^2) space, O(1)O(1) edge test; symmetric for undirected graphs.
  • Lists: one list per vertex, O(n+e)O(n + e) space, fast neighbour enumeration.
  • Use a matrix for dense graphs, lists for sparse graphs.
  • For a weighted graph store weights, with ∞ (a large value) for “no edge”.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How much space does an adjacency matrix need for a graph with 1000 vertices and 3000 edges?

Show answer

Answer: C. 1,000,000 cells

A matrix always has n2=106n^2 = 10^6 cells, whatever the number of edges. Adjacency lists would need about n+2e=7000n + 2e = 7000 entries for this undirected graph.

Question 2 · multiple choice

In a digraph, what does row ii of the adjacency matrix contain?

Show answer

Answer: A. The edges leaving vertex ii

aij=1a_{ij} = 1 means an edge from ii to jj. Column ii shows the edges entering ii.

Question 3 · multiple choice

For which graph is an adjacency matrix the better choice?

Show answer

Answer: D. A graph with 50 vertices in which almost every pair is connected

In a dense graph most cells are used, and the O(1)O(1) edge test is valuable. The other three graphs are sparse.

Question 4 · multiple choice

What does this program print?

C · q8-degree.c
#include <stdio.h>

int main(void)
{
    int a[4][4] = {{0, 1, 1, 0},     /* directed graph of Figure 8.3 */
                   {0, 0, 0, 0},
                   {0, 0, 0, 1},
                   {1, 1, 0, 0}};
    int out = 0, in = 0;
    for (int j = 0; j < 4; j++) out += a[3][j];   /* row of vertex 4 */
    for (int i = 0; i < 4; i++) in += a[i][1];    /* column of vertex 2 */
    printf("out(4)=%d in(2)=%d\n", out, in);
    return 0;
}
Show answer

Answer: B. out(4)=2 in(2)=2

Vertex 4 has edges to 1 and 2 (out-degree 2); vertex 2 receives edges from 1 and 4 (in-degree 2). Row sums give out-degrees, column sums give in-degrees.

Question 5 · multiple choice

How long does it take to list all neighbours of a vertex uu with adjacency lists?

Show answer

Answer: C. O(deg⁡(u))O(\deg(u))

The list holds exactly the neighbours. With a matrix the whole row of nn cells must be scanned.

Question 6 · short answer

Draw the adjacency matrix and the adjacency lists for the undirected graph with edges A–B, A–C, B–C, C–D, and state the space used by each.

Show worked answer

Matrix (order A, B, C, D): A: 0 1 1 0; B: 1 0 1 0; C: 1 1 0 1; D: 0 0 1 0 (symmetric). Lists: adj[A] = {B, C}; adj[B] = {A, C}; adj[C] = {A, B, D}; adj[D] = {C}. The matrix uses n2=16n^2 = 16 cells; the lists use n=4n = 4 list heads plus 2e=82e = 8 entries.

↑ ↓ move↵ openTab switch type