Map
Colouring
Assign each
state or country
a colour
States that
share a border
should be
coloured
differently
How many
colours do we
need?
Map
Colouring
Mark each state
Map
Colouring
Mark each state
Connect states
that share a
border
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
In fact, the
actual map is
irrelevant!
Map
Colouring
In fact, the
actual map is
irrelevant!
All we need is
the underlying
pattern of dots
and connections
Map
Colouring
This kind of
diagram is
called a graph
Dots are nodes
or vertices
One vertex,
many vertices
Connections are
edges
Graph
Colouring
The problem we
have solved is called
graph colouring
We used 4 colours
In fact, 4 colours are
always enough for
such maps
This is a theorem.
Graph
Colouring
Observe that the
original map
used more than
4 colours
Graph
Colouring
The graph
emphasizes the
essential features
of the problem
What is
connected to
what?
Graph
Colouring
The graph
emphasizes the
essential features
of the problem
What is
connected to
what?
We can distort
this figure and the
problem remains
the same
More graph
problems
More graph
problems
Airline routing
More graph
problems
Airline routing
Can I travel from
New Delhi to
Trivandrum
without
changing
airlines?
More graph
problems
Airline routing
Can I travel from
New Delhi to
Trivandrum
without
changing
airlines?
Again, all that is
important is the
underlying
graph
Graphs, formally
A Graph is a non-linear data structure consisting of vertices and edges. The vertices are
sometimes also referred to as nodes and the edges are lines or arcs that connect any two
nodes in the graph. Graphs are mathematical structures used to model pairwise relations
between objects.
• G = (V,E)
• Set of vertices V
• Set of edges E
• E is a subset of pairs (v,v’): E ⊆ V × V
• Undirected graph: (v,v’) and (v’,v) are the same edge
• Directed graph:
• (v,v’) is an edge from v to v’
• Does not guarantee that (v’,v) is also an edge
Graph
Colouring
Undirected
graph
Graph
Colouring
Undirected
graph
Colouring C
assigns each
vertex v a colour
C(v)
Legal colouring:
if (v,v’) is in E,
then C(v) ≠ C(v’)
Finding a
route
Finding a
route
Directed graph
Finding a
route
Directed graph v0
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New
Delhi v1
v3
Each (vi,vi+1) is
an edge in E
v2 v4
vk is
Trivandrum
v5
Finding a
route
Also makes sense
v0
for undirected
graphs
Find a sequence
of vertices v0, v1,
…, vk such that
v1
v0 is New Delhi v3
Each (vi,vi+1) is
an edge in E v4
v2
vk is Trivandrum
v5
Working with graphs
We are given G = (V,E), undirected
Is there a path from source v s to target v t ?
Look at the picture and see if vs and vt are
connected
How do we get an algorithm to “look at the
picture”?
Representing graphs
Let V have n vertices
We can assume vertices are named 1,2,…,n
Each edge is now a pair (i,j), where 1 ≤ i,j ≤ n
Let A(i,j) = 1 if (i,j) is an edge and 0 otherwise
A is an n x n matrix describing the graph
Adjacency matrix
Adjacency matrix
1
1 2 3 4 5 6 7 8 9 10
3 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
2 5 3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
4 5 0 0 0 1 0 1 1 0 0 0
6 7 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
8 0 0 0 1 0 1 0 0 1 0
8 9 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
10
Adjacency matrix
Neighbours of i
1 2 3 4 5 6 7 8 9 10
Any column j in row i 1 0 1 1 1 0 0 0 0 0 0
with entry 1 2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
Scan row i from left 4 1 0 0 0 1 0 0 1 0 0
to right to identify all 5 0 0 0 1 0 1 1 0 0 0
neighbours 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Neighbours of 4 are 8 0 0 0 1 0 1 0 0 1 0
{1,5,8} 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Adjacency matrix
Neighbours of i
1 2 3 4 5 6 7 8 9 10
Any column j in row i 1 0 1 1 1 0 0 0 0 0 0
with entry 1 2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
Scan row i from left 4 1 0 0 0 1 0 0 1 0 0
to right to identify all 5 0 0 0 1 0 1 1 0 0 0
neighbours 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Neighbours of 4 are 8 0 0 0 1 0 1 0 0 1 0
{1,5,8} 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Finding a path
Start with v s
1 2 3 4 5 6 7 8 9 10
New Delhi is 1 1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
Mark each neighbour as
3 1 1 0 0 0 0 0 0 0 0
reachable
4 1 0 0 0 1 0 0 1 0 0
Explore neighbours of 5 0 0 0 1 0 1 1 0 0 0
marked vertices 6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
Check if target is 8 0 0 0 1 0 1 0 0 1 0
marked 9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
vt =10 = Trivandrum
Exploring graphs
Need a systematic algorithm
Mark vertices that have been visited
Keep track of vertices whose neighbours have
already been explored
Avoid going round indefinitely in circles
Two fundamental strategies: breadth first and
depth first
An alternative representation
Adjacency matrix has
many 0’s 1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
Size of the matrix is n2 2 1 0 1 0 0 0 0 0 0 0
regardless of number 3 1 1 0 0 0 0 0 0 0 0
of edges 4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
Maximum size of E is 6 0 0 0 0 1 0 1 1 1 0
n(n-1)/2 if we disallow 7 0 0 0 0 1 1 0 0 0 0
self loops 8 0 0 0 1 0 1 0 0 1 0
9 0 0 0 0 0 1 0 1 0 1
Typically E is much
10 0 0 0 0 0 0 0 0 1 0
smaller
Adjacency list
For each vertex, maintain a
1 list of its neighbours
3 1 2,3,4
2 1,3
2 5 3 1,2
4 1,5,8
4 5 4,6,7
6 7 6 5,7,8,9
7 5,6
8 9 8 4,6,9
9 6,8,10
10 10 9
Comparing representations
Adjacency list typically requires less space
Is j a neighbour of i?
Just check if A[i][j] is 1 in adjacency matrix
Need to scan neighbours of i in adjacency list
Which vertices are neighbours of i?
Scan all n columns in adjacency matrix
Takes time proportional to neighbours in adjacency
list