You are on page 1of 53

GRAPH THEORY:

TRAVERSING THE
SHORTEST PATH PROBLEM

MATHEMATICS
FOR
EFFICIENCY
DESTINATION

STARTING POINT
What is the
shortest
path from
the starting
point to the
destination
point?

Google Maps will suggest this path.


This is one of the
applications of a
branch of
mathematics
known as
Graph Theory.
GRAPH THEORY is the study of graphs, which are
mathematical structures used to model pairwise
relations of objects.
This is Konigsberg, now known as Kaliningrad, in
Russia. In the early 1700’s, people from this town
posed a particular question.
“Is it possible to take a walk through town,
crossing each of the seven bridges just once, and
ending up wherever you started?"
The famous Swiss mathematician Leonhard Euler
heard of the problem, solved it (its not possible)
and in the process invented Graph Theory.
In simple pictorial representation, the diagram looks like this.

Can you draw the diagram without lifting the pen


and without tracing lines more than once?
 An undirected graph consists of a set of vertices
and a set of edges that which join two vertices.
 Vertices and are adjacent if they are joined by an
edge. This edge is denoted by .
 The order of, denoted by , is the
number of vertices in .
 The size of, denoted by , is the number
of edges in .
 If there is more than one edge joining vertices
and , then contains a multiple edge.
 An edge from a vertex to itself is called a loop.

There are three


edges that are
incident to both
vertex and .

Moreover, there is a
loop at vertex .
If a graph contains multiple edges or loops,
then is called a multigraph. Otherwise, is
called a simple graph.

is a simple graph while is a multigraph.


 The degree of the vertex , denoted by , is the
number of edges that are incident to the vertex .

 The neighborhood of , denoted by , is the set of


all vertices that are adjacent to .
 A walk of length from vertex to vertex is a
sequence of vertices such that two
consecutive vertices are joined by an edge.
 Denote this walk by

The walk is
illustrated as (check animation).
 A walk of length from vertex to vertex is a
sequence of vertices such that two
consecutive vertices are joined by an edge.
 Denote this walk by

 The walk is called a path if


are distinct.

The walk is path.


 A walk of length from vertex to vertex is a
sequence of vertices such that two
consecutive vertices are joined by an edge.
 Denote this walk by

 The walk is called a cycle if


are distinct and .

The walk is cycle.


 A graph is said
to be
connected if
there is always
a path from a
vertex to
another.
This is a disconnected graph (WHY?).  Otherwise, is
disconnected.
We say that graph is a weighted graph if all the
edges of are assigned with numerical values
(weights).
 A directed graph consists of a set of vertices and
a set of arcs that are formed using (ordered) pairs
of vertices in .
 An arc from to is written as . In this case, we say
is the initial vertex while the vertex is the terminal
vertex.
 Similar to undirected graphs, the order is the number of
vertices of the digraph and the size is the number of
arcs.
 Is a tool for determining a shortest path from a starting
vertex to any destination vertex.
 It applies to connected simple graphs (directed or
undirected).
 State of a vertex: distance value and status label
 The distance value of a vertex represents an estimate of
its distance from vertex . This may be updated
everytime Dijkstra’s algorithm is used.
 Status labels: Permanent or Temporary.
 Once a vertex reaches permanent status label, that
means we have determined its shortest distance from
the starting point.
 At each step of the algorithm, one vertex is designated
as current.
 Assign the zero distance value to the starting vertex (say ) and
label it as permanent.
 Assign the distance value to all other vertices and label them as
temporary.
 Designate the vertex as the current vertex.

( ,?) 9 b ( ,?)
CURRENT DISTANCE
a 6 VERTEX FROM
2 STARTING
14 ( ,?) VERTEX
e 11 s 0
9 ( ,?)
c
(0,*) s
10
Current vertex
15
7
f
( ,?)
 To each vertex adjacent to the current vertex, update distance
value.
 Among the temporary vertices (this may or may not be adjacent to
the current vertex), choose the one with the lowest distance value.
 The one with the lowest distance value becomes permanent and
becomes the new current vertex.

(( ,?)
,s) 9 b ( ,?)
CURRENT DISTANCE
a 6 VERTEX FROM
2 STARTING
14 (( ,s)
,?) VERTEX
e 11 s 0
9 ( ,?)
c
(0,*) s f 7
10
15
7
f
,?) Current Vertex
(( ,s)
 To each temporary vertex adjacent to the current vertex, add the distance value of
the current vertex to the weight of edge joining an adjacent vertex. If the sum the
lower, update distance value of the adjacent vertex. Otherwise, do not update.
 Among the temporary vertices (this may or may not be adjacent to the current
vertex), choose the one with the lowest distance value.
 The one with the lowest distance value becomes permanent and becomes the new
current vertex.

9 b ( ,?)
( ,s)
CURRENT DISTANCE
a 6 VERTEX FROM
2 Current
( ,f)=(17,f)
Vertex STARTING
14 ( ,s) ( ,f)=(22,f) VERTEX
e 11 (22,f) s 0
9 ( ,?)
c
(0,*) s f 7
10 e 9
15
7
f
( ,s)
 To each temporary vertex adjacent to the current vertex, add the distance value of
the current vertex to the weight of edge joining an adjacent vertex. If the sum the
lower, update distance value of the adjacent vertex. Otherwise, do not update.
 Among the temporary vertices (this may or may not be adjacent to the current
vertex), choose the one with the lowest distance value.
 The one with the lowest distance value becomes permanent and becomes the new
current vertex.

(9+2,e)=(11,e)
CURRENT DISTANCE
9 b ( ,t)
( ,s)
(11,e) VERTEX FROM
a 6 STARTING
Current vertex VERTEX
2
14 (9,s) s 0
e 11 (9+11,e)=(20,e)
9 f 7
c (20,e)
(22,f)
(0,*) s e 9
10 a 11
15
7
f
( ,s)
 To each vertex adjacent to the current vertex, add the distance value of the
current vertex to the weight of edge joining an adjacent vertex. If the sum the
lower, update distance value of the adjacent vertex. Otherwise, do not update.
 Among the temporary vertices (this may or may not be adjacent to the current
vertex), choose the one with the lowest distance value.
 The one with the lowest distance value becomes permanent and becomes the
new current vertex.
(11+9,a)=(20,a)
(20,a)
9 b ( ,?) CURRENT DISTANCE
(11,e)
VERTEX FROM
a 6 STARTING
2 VERTEX
14 (9,s)
Current Vertex s 0
e 11
9 c (20,e) f 7
(0,*) s e 9
10
a 11
15
7 c 20
f
( ,s)
CURRENT DISTANCE
VERTEX FROM
(20+6,e)=(26,e) STARTING
VERTEX
9 b (20,a)
(11,e) s 0
a 6 f 7
2 e 9
14 (9,s)
e 11 a 11
9 c (20,e) c 20
(0,*) s
10 b 20
15
7
f
( ,s)
CURRENT DISTANCE
VERTEX FROM
STARTING
VERTEX
9 b (20,a)
(11,e) s 0
a 6 f 7
2 e 9
14 (9,s)
e 11 a 11
9 c (20,e) c 20
(0,*) s
10 b 20
15
7
f
( ,s)

What is the shortest distance from s to b?


What path must be traversed to achieve this shortest distance?
Find the shortest path from vertex to all other vertices of the given
graph.

2
a b
3 8

6 7 1
s c

4 4

e f
5
2
a b
3 8

6 7 1
(0,*) s c

4 4

e f
5
Current Vertex Distance from s
s 0

(∞,?) (∞,?)
2
a b
3 8

6 7 1
(0,*) s c (∞,?)
Current Vertex
4 4

e f
5
(∞,?) (∞,?)
Current Vertex Distance from s
s 0
a 3

Current Vertex
(∞,?)
(3,s) (∞,?)
2
a b
3 8

6 7 1
(0,*) s c (∞,?)

4 4

e f
5
(4,s)
(∞,?) (∞,?)
(3,s) (∞,?)
2
a b
3 8

6 7 1
(0,*) s c (∞,?)

4 4

e f
5
(4,s) (∞,?)
Current Vertex Distance from s
s 0
a 3
e 4

(3,p) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,?)

4 4

e f
5
(4,s)
(4,s) (10,a)
Current Vertex
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,?)

4 4

e f
5
(4,s) (10,a)
Current Vertex
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,t)

4 4

e f
5
(4,s) (10,a)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,t)

4 4

e f
5
(4,s) (9,e)
Current Vertex Distance from s
s 0
a 3
e 4
b 5

Current Vertex
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,t)

4 4

e f
5
(4,s) (9,e)
Current Vertex
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,t)

4 4

e f
5
(4,s) (9,e)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (∞,t)

4 4

e f
5
(4,s) (9,e)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (13,b)

4 4

e f
5
(4,s) (6,b)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (13,b)

4 4

e f
5
(4,s) (6,b)
Current Vertex Distance from s
s 0
a 3
e 4
b 5
f 6

(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (13,b)

4 4

e f
5
(4,s) (6,b)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (13,b)

4 4

e f
5
(4,s) (6,b)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (10,f)

4 4

e f
5
(4,s) (6,b)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (10,f)

4 4

e f
5
(4,s) (6,b)
Current Vertex Distance from s
s 0
a 3
e 4
b 5
f 6
c 10

(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (10,f)

4 4

e f
5
(4,s) (6,b)
(3,s) (5,a)
2
a b
3 8

6 7 1
(0,*) s c (10,f)

4 4

e f
5
(4,s) (6,b)
1. Find the least expensive route from I to L and state its cost.

120
J K
150 80

L
I 40 100 50

120 N 30
70
40
P
30 M
30
80
O
2. Find the shortest path from vertex A to vertex Z.

You might also like