SlideShare a Scribd company logo
DESIGN AND ANALYSIS
OF ALGORITHMS
Representing graphs
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 2
Graphs, formally
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
Finding a
route
Directed graph
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New
Delhi
Each (vi,vi+1) is
an edge in E
vk is
Trivandrum
v0
v1
v2
v3
v4
v5
Finding a
route
Also makes sense
for undirected
graphs
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New Delhi
Each (vi,vi+1) is
an edge in E
vk is Trivandrum
v0
v1
v2
v3
v4
v5
Working with graphs
We are given G = (V,E), undirected
Is there a path from source vs to target vt?
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
2
3
4
5
6 7
8 9
10
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Adjacency matrix
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Neighbours of i
Any column j in row i
with entry 1
Scan row i from left
to right to identify all
neighbours
Neighbours of 4 are
{1,5,8}
Adjacency matrix
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Neighbours of i
Any column j in row i
with entry 1
Scan row i from left
to right to identify all
neighbours
Neighbours of 4 are
{1,5,8}
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
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
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
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
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Adjacency matrix has
many 0’s
Size of the matrix is n2
regardless of number
of edges
Maximum size of E is
n(n-1)/2 if we disallow
self loops
Typically E is much
smaller
Adjacency list
1
2
3
4
5
6 7
8 9
10
For each vertex, maintain a
list of its neighbours
1 2,3,4
2 1,3
3 1,2
4 1,5,8
5 4,6,7
6 5,7,8,9
7 5,6
8 4,6,9
9 6,8,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

More Related Content

Recently uploaded

Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 

Recently uploaded (20)

Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

graph presentation

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS Representing graphs MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan NPTEL MOOC,JAN-FEB 2015 Week 3, Module 2
  • 2. Graphs, formally 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
  • 3. Finding a route Directed graph Find a sequence of vertices v0, v1, …, vk such that v0 is New Delhi Each (vi,vi+1) is an edge in E vk is Trivandrum v0 v1 v2 v3 v4 v5
  • 4. Finding a route Also makes sense for undirected graphs Find a sequence of vertices v0, v1, …, vk such that v0 is New Delhi Each (vi,vi+1) is an edge in E vk is Trivandrum v0 v1 v2 v3 v4 v5
  • 5. Working with graphs We are given G = (V,E), undirected Is there a path from source vs to target vt? Look at the picture and see if vs and vt are connected How do we get an algorithm to “look at the picture”?
  • 6. 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
  • 7. Adjacency matrix 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0
  • 8. Adjacency matrix 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Neighbours of i Any column j in row i with entry 1 Scan row i from left to right to identify all neighbours Neighbours of 4 are {1,5,8}
  • 9. Adjacency matrix 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Neighbours of i Any column j in row i with entry 1 Scan row i from left to right to identify all neighbours Neighbours of 4 are {1,5,8}
  • 10. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 11. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 12. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 13. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 14. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 15. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 16. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 17. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 18. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 19. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 20. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 21. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 22. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 23. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 24. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 25. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 26. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 27. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 28. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 29. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 30. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 31. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 32. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 33. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 34. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 35. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 36. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 37. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 38. Finding a path 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 39. 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
  • 40. An alternative representation 1 2 3 4 5 6 7 8 9 10 1 0 1 1 1 0 0 0 0 0 0 2 1 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 0 0 0 0 4 1 0 0 0 1 0 0 1 0 0 5 0 0 0 1 0 1 1 0 0 0 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 9 0 0 0 0 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 1 0 Adjacency matrix has many 0’s Size of the matrix is n2 regardless of number of edges Maximum size of E is n(n-1)/2 if we disallow self loops Typically E is much smaller
  • 41. Adjacency list 1 2 3 4 5 6 7 8 9 10 For each vertex, maintain a list of its neighbours 1 2,3,4 2 1,3 3 1,2 4 1,5,8 5 4,6,7 6 5,7,8,9 7 5,6 8 4,6,9 9 6,8,10 10 9
  • 42. 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