You are on page 1of 53

GRAPH THEORY:

TRAVERSING THE
SHORTEST PATH
PROBLEM
MATHEMA
TICS FOR
EFFICIENC
Y
THE PROBLEM
THE PROBLEM

DESTINATION

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

Google Maps will suggest this path.


THE PROBLEM

This is one of the


applications of a
branch of
mathematics
known as graph
theory.
WHAT IS GRAPH
THEORY?

GRAPH THEORY is the study of graphs, which are


mathematical structures used to model pairwise
relations of objects.
ORIGIN OF GRAPH THEORY

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?"
ORIGIN OF GRAPH
THEORY

The famous Swiss mathematician Leonhard


Euler heard of the problem, solved it (its not
possible) and in the process invented Graph
Theory.
ORIGIN OF GRAPH THEORY
ORIGIN OF 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?
UNDIRECTED GRAPH
  An
undirected graph consists of a set of vertices
and a set of edges that join two vertices.
 Vertices and are adjacent if they are joined by an
edge. This edge is denoted by .

 
 
ORDER AND SIZE OF A GRAPH
The order of , denoted by , is the number of
  

vertices in .
 The size of , denoted by , is the number of
edges in .

 
MULTIPLE EDGES AND LOOPS
 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.

 Thereare three
edges that are
incident to both
𝐻  : vertex and .

Moreover, there is
a loop at vertex .
MULTIGRAPH AND SIMPLE GRAPH

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.
DEGREE AND
NEIGHBORHOOD OF A
VERTEX
  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 .

 
WALKING ON A GRAPH
  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).
WALKING ON A GRAPH
  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 called a cycle if are distinct and .

 The walk is path.


WALKING ON A GRAPH
  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 called a cycle if are distinct and .

 The walk is cycle.


CONNECTED
GRAPH VS
DISCONNECTED
GRAPH
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.
WEIGHTED GRAPH
We say that graph is a weighted graph if all
 

the edges of are assigned with numerical


values (weights).
DIRECTED GRAPH
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.

𝐷:
 

 
DIJKSTRA’S
ALGORITHM
  Is
a method for determining a shortest path from a
starting vertex to any destination vertex.
 It applies to simple connected positive-weighted
graphs (directed or undirected).
FORMAL DESCRIPTION
  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 each time a step is carried out.
 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.
9 b
a 6
2
14 For our first
e 11
9 example, copy this
c
graph using pencil
s
10
15
7
f
STEP 1: INITIALIZATION
  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.

  (,t) 9 b   (,t)
CURRENT DISTANCE
a 6 VERTEX FROM
2 STARTING
14   (,t) VERTEX
e 11 s 0
9   (,t)
c
(0,p) s
10
Current vertex
15
7
f
  (,t)
STEP 2: DISTANCE VALUE UPDATE
OF NEIGHBORS OF CURRENT VERTEX

 To each vertex adjacent to the current vertex, update


distance value. Shorter distance replaces longer distance.

  (,t) 9 b   (,t)
(,t)
a 6
2
14   (,t)
 (,t)
e 11
9   (,t)
c
(0,p) s
10
15
7
f
  (,t)
(,t)
STEP 3: CURRENT VERTEX DESIGNATION
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.

  (,t)
9 b CURRENT DISTANCE
  (,t)
a VERTEX FROM
6 STARTING
2 VERTEX
14  (,t)
11   (,t) s 0
e
9 c f 7
(0,p) s
10
15
7
f  (,t)
(,p) Current Vertex
REPEAT STEP 2 AND STEP 3…

9 b   (,t)
  (,t) CURRENT DISTANCE
a 6 VERTEX FROM
2  Current Vertex
(,t)=(17,t) STARTING
14  (9,p)
(,t)   (,t)=(22,t) VERTEX
e 11 s 0
9   (,t)
c (22,t)
f 7
(0,p) s
10 e
e 9
9
15
7
f
 (,p)
REPEAT STEP 2 AND STEP 3

(9+2,t)=(11,t) CURRENT DISTANCE


9 b   (,t)
  (,t)
(11,t)
(11,p) VERTEX FROM
a STARTING
Current vertex 6
VERTEX
2
14 (9,p) s 0
e 11 (9+11,t)=(20,t)
f 7
9 c (22,t)
(20,t)
e
e 9
9
(0,p) s
10 a
a 11
11
15
7
f
 (,p)
REPEAT STEP 2 AND STEP 3

(11+9,t)=(20,t)
9 b  (20,t)
(,t) CURRENT DISTANCE
(11,p) VERTEX FROM
a 6 STARTING
2 VERTEX
14 (9,p)
Current Vertex s 0
e 11
9 c (20,p) f 7
(20,t)
(0,p) s e
e 9
9
10 a 11
a 11
15 c 20
7 c 20
f
 (,p)
ENDING CURRENT
VERTEX
DISTANCE
FROM
(20+6,t)=(26,t) STARTING
VERTEX
(20,p)
9 b (20,t) s 0
(11,p)
a 6 f 7
2 e
e 9
9
14 (9,p)
a
a 11
11
e 11
9 c (20,p) c 20
c 20
(0,p) s b 20
10 b 20
15
7
f
 (,p)

What is the shortest distance from s to b?


What path must be traversed to achieve this shortest
distance?
ILLUSTRATION 2
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
STEP 1: INITIALIZATION

2
a b
3 8

6 7 1
(0,p) s c

4 4

e f
5
STEP 1: INITIALIZATION

Current Vertex Distance from s


s 0

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

6 7 1
(0,p) s c (∞,t)
Current Vertex
4 4

e f
5
(∞,t) (∞,t)
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX
Current Vertex Distance from s
s 0
Current Vertex a 3
(3,p)
(3,t)
(∞,t) (∞,t)
2
a b
3 8

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

4 4

e f
5
(∞,t) (∞,t)
(4,t)
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX

(3,p) (∞,t)
2
a b
3 8

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

4 4

e f
5
(4,t) (∞,t)
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX
Current Vertex Distance from s
s 0
a 3
e 4
(3,p) (5,t)
2
a b
3 8

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

4 4

e f
5
(4,p)
(4,t) (10,t)
Current Vertex
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX

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

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

4 4

e f
5
(4,p) (10,t)
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX

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

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

4 4

e f
5
(4,p) (10,t)
STEP 2: UPDATE DISTANCE VALUE OF
NEIGHBORS OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT
VERTEX

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

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

4 4

e f
5
(4,p) (9,t)
STEP 2: UPDATE DISTANCE VALUE OF NEIGHBORS
OF CURRENT VERTEX
STEP 3: CHOOSE NEW CURRENT VERTEX
Current Vertex Distance from s
s 0
a 3
e 4
b 5
Current Vertex
(3,p) (5,p)
2
a b
3 8

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

4 4

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

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

4 4

e f
5
(4,p) (9,t)
REPEAT…

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

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

4 4

e f
5
(4,p) (9,t)
REPEAT…
(3,p) (5,p)
2
a b
3 8

6 7 1
(0,p) s c (13,t)

4 4

e f
5
(4,p) (6,t)
REPEAT…

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

6 7 1
(0,p) s c (13,t)

4 4

e f
5
(4,p) (6,t)
Current Vertex Distance from s

REPEAT… s
a
0
3
e 4
b 5
f 6
(3,p) (5,p)
2
a b
3 8

6 7 1
(0,p) s c (13,t)

4 4

e f
5
(4,p) (6,p)
Current Vertex
REPEAT…

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

6 7 1
(0,p) s c (13,t)

4 4

e f
5
(4,p) (6,p)
REPEAT…

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

6 7 1
(0,p) s c (10,t)

4 4

e f
5
(4,p) (6,p)
REPEAT…

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

6 7 1
(0,p) s c (10,p)

4 4

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

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

6 7 1
(0,p) s c (10,p)

4 4

e f
5
(4,p) (6,p)
ENDING

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

6 7 1
(0,p) s c (10,p)

4 4

e f
5
(4,p) (6,p)
KNOWLEDGE CHECK
1.
5A
Find the least expensive route from I to L and state its
cost.

12
J K
0 80
15
0
L
I 40 10 50
0
12 N 30
0 70
40
P
30 M
30
80
O
KNOWLEDGE CHECK
2.
5A
Find the shortest path from vertex A to vertex Z.

You might also like