0% found this document useful (0 votes)
16 views42 pages

Nptel Week3 Module2 Graphrepresentation

Uploaded by

rajd20284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views42 pages

Nptel Week3 Module2 Graphrepresentation

Uploaded by

rajd20284
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

NPTEL MOOC,JAN-FEB 2015

Week 3, Module 2

DESIGN AND ANALYSIS


OF ALGORITHMS
Representing graphs

MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE


http://www.cmi.ac.in/~madhavan
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 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 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
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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 vs
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
Mark each neighbour as 2 1 0 1 0 0 0 0 0 0 0
reachable 3 1 1 0 0 0 0 0 0 0 0
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

You might also like