0% found this document useful (0 votes)
24 views44 pages

CPT212 - Graphs Pt.4 (ELearn)

This document discusses algorithms for finding shortest paths in graphs. It begins by introducing the shortest path problem and properties. It then covers Dijkstra's algorithm and Bellman-Ford algorithm to solve the problem. Dijkstra's algorithm uses a greedy approach and cannot handle graphs with negative edge weights, while Bellman-Ford can find shortest paths in graphs with negative edges. The document provides pseudocode and examples to illustrate how the algorithms work.

Uploaded by

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

CPT212 - Graphs Pt.4 (ELearn)

This document discusses algorithms for finding shortest paths in graphs. It begins by introducing the shortest path problem and properties. It then covers Dijkstra's algorithm and Bellman-Ford algorithm to solve the problem. Dijkstra's algorithm uses a greedy approach and cannot handle graphs with negative edge weights, while Bellman-Ford can find shortest paths in graphs with negative edges. The document provides pseudocode and examples to illustrate how the algorithms work.

Uploaded by

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

Graph Algorithms – Part 4

CPT212 – Design & Analysis of


Algorithms

Teh Je Sen (2019)


Abllah Zawawi Talib (2023) 1
Learning Objectives
 In this topic, we will learn about
algorithms that can be used to find the
following:
 Shortest path
 Spanning trees
 Note:
 : Number of vertices
 : Number of edges

2
SHORTEST PATH

3
Scenario
 So far, we have been able to identify if
two vertices can reach other.
 What if we want to find the shortest
path between two vertices?
 Example:
 Find the fastest way to travel from one city
to another.

4
Weighted Graph
 The shortest path problem is applicable
to weighted graphs.
Each edge is associated
 Example: to a weight.

849 PVD
1843 ORD 2
SFO 14

802

1205
43 LGA
17
337

7
HNL 2555 138 10
99
1233
LAX DFW 1120
MIA
5
Shortest Path Problem
 Given a weighted graph and two
vertices, and , find a path with
minimal weight between and .
 Weights can refer to distance, cost,
time, etc.
 Applications:
 Internet packet routing
 Flight reservations
 Driving directions 6
Shortest Path Properties
 Property 1: A subpath of a shortest
path is itself a shortest path

849 PVD
1843 ORD 2
SFO 14

802

1205
43 LGA
17
337

7
HNL 2555 138 10
99
1233
LAX DFW 1120
MIA
7
Verify of the highlighted paths
are the shortest paths
 Shortest path from PVD to HNL
 Shortest path from PVD to LAX
 Shortest path from PVD to ORD
849 PVD
1843 ORD 2
SFO 14

802

1205
43 LGA
17
337

7
HNL 2555 138 10
99
1233
LAX DFW 1120
MIA
8
Shortest Path Properties
 Property 2: There is a tree of shortest
paths from a start vertex to all the
other vertices
 Tree of shortest paths with PVD as
starting vertex:
849 PVD
1843 ORD 2
SFO 802 14

1205
7 43 LGA
337

1 87 10
2555 13
HNL 1233 99
LAX DFW 1120
MIA 9
Algorithms to Find the
Shortest Path
 Dijkstra’s Algorithm
 A greedy method
 Cannot handle negative edges
 Bellman-Ford Algorithm
 Slower than Djikstra
 Can handle negative edges

10
Dijkstra’s Algorithm
 Can be seen as a “weighted” BFS
starting from a source vertex,
 Iteratively grow a “cloud” of vertices
out of
 In each iteration, the closest vertex to the
cloud is chosen to be included
 Algorithm terminates when there are no
more vertices outside the cloud, or the
vertices are not connected to the cloud
11
Dijkstra’s Algorithm
 Notations:
 For every vertex, we assign a label
 will store the length of the best path from
at the time (will be updated with each
iteration)
 At each iteration, a vertex with the
smallest will be pulled into the cloud
 Then update the label of every vertex
adjacent to and is outside the cloud (edge
relaxation) 12
Dijkstra’s Algorithm
Psuedocode

13
Dijkstra’s Algorithm Simple
Example (Undirected Graph)

𝐷[𝑎]=0
a
9 2

b 6
c
𝐷[ [𝑐𝑐]=∞
𝐷 ]=2
𝐷 [𝑏
𝐷 ] =∞
𝐶] =∞
=9
4 1

d
𝐷 [ 𝑑 ] =∞

14
Dijkstra’s Algorithm Simple
Example (Undirected Graph)

𝐷[𝑎]=0
a
9 2

b 6
c
𝐷[ [𝑐𝑐]=∞
𝐷 ]=2
𝐷[[𝑏𝐶] =8
𝐷 ] =∞
=∞
=9
4 1
has been removed from
Complete the remaining
the queue and placed into
iterations on your own! d
the cloud.
remain in the queue 𝐷[[𝑑𝑑] =∞
𝐷 =3

15
Task
 Perform Dijkstra’s Algorithm on the
digraph shown below:

b 1 d
10
2 3 9
a 4 6
source
5 7
c 2 e
16
Analysis of Complexity
 Dijkstra’s algorithm uses a priority queue
 The algorithm involves at most:
 insertions into Q
 calls for removing the minimum value
 calls for changing the key value (weight)
 The above operations run in [while loop, ∑ v∈G log n
removal from Q, ∑ v∈G deg(v) log n relaxation]
 So what is the worst case complexity for
Dijkstra’s algorithm?
 because worst case, the graph is fully connected,

17
Negative Edges
 Dijkstra’s algorithm does not work when
negative edges exist because it is based
on the greedy method
 Vertices are added based on increasing
distance
 A negative edge will mess up the distances

18
Negative Edges
 Example – Try Djikstra’s algorithm for
the following graph from source, :

8 A 4
6
7 1
B C D
0 -8
2 5
E F

19
Bellman-Ford Algorithm
 Iteratively update the distance from the
source by checking each edge in the
graph
 Algorithm continues iterating as long as
there is an edge such that

20
Bellman-Ford Algorithm
 Pseudocode:
Algorithm ShortestPath(G,s)
Input: Weighted simple digraph, and a source vertex,
Output: Length shortest paths from to each vertex of
for all vertices

while there is an edge() such that


> + weight(edge())
do
= + weight(edge())

What is the worst case complexity of this algorithm?


For every vertex, you need to check all edges.
21
Bellman-Ford Example
a 2 b (a,2)
c 5 (b,7)

2 2 -3 9

d 4 e f 5
(a,2) (b,-1)
(g,-2)
(a,2) (e,4)
(e,3)
1g
-5 iteration: init 1 2
a 0
(d,3)
b  2
c  7
edges: ab ad ae bc be ce de dg ef ge d 2

e  2 -1 -2
f  4 3
g  3
22
SPANNING TREES

23
Re-cap
 Spanning tree
 A connected sub-graph that includes all
vertices from the original graph and is
also a tree
 A tree is a sub-graph that has no
cycles

24
Scenario
 Bob wants to connect all computers in
his office building using the least
amount of cable
 This problem can be represented as an
undirected weighted graph
 Rather than computing the shortest
path from some vertex, Bob needs to
find a tree that contains all vertices
and has a minimum total weight
25
Minimum Spanning Tree
(MST)
 Spanning tree with minimum weight

6 6 10
10

1
7 1
20

5 5

26
Algorithms for MST Problem
 Both algorithms based on Greedy
method
 Prim-Jarnik algorithm
 Grows MST from a single root vertex
 Kruskal’s algorithm
 Grows MST in clusters

27
MST Properties
 Properties that are related to MSTs:
 Light edge property
 Cycle property
 Partition property

28
Light Edge Property
 The Greedy method for MST relies on the light-edge
property.
 If a connected undirected weighted graph is divided
into two subsets, the minimum-weight edge
that connects the subset is included in the MST of
Any minimum-weight “bridge” edge can
be immediately added into the MST.

29
Other MST Properties
 Cycle Property
 Let be a MST of a weighted graph,
 Let be an edge of that is not in
 Let be a cycle formed by when included
in
f 8 For all edges, in
4
𝑻 C 9 Else, we can get a spanning
2 6
3 e tree with smaller weight by
8 7 replacing with
7
30
Other MST PropertiesU V
f 7
4
 Partition property 2 5
9
8
 If a “bridge” edge 8 e 3
has the same 7
weight as another Replacing f with e yields
“bridge” edge , another MST

replacing with U
f 7
V

will yield another 4


9
MST 2 5
8
8 e 3
7
31
Prim-Jarnik Algorithm
 Similar to Dijkstra’s algorithm
 Grow a MST starting from a “root”
vertex,
 An initial “cloud” will be defined which
includes
 Each iteration, a minimum-weight edge
will be brought into the cloud
 By choosing the minimum-weight edge
each time, a valid edge is added into the
MST (light edge property)
32
Prim-Jarnik Algorithm

What is the difference


between Prim-Jarnik and
Dijkstra?

Update the value for each


vertex with minimum
distance from the tree,
rather than distance from
source vertex.
33
Example
D[D]= D[D]=7
D[B]=2 7 D D[B]=2 7 D
B 4 B 4
D[C]=8 D[F]= D[F]=
9 D[C]=5 9
2 5 F 2 5 F
C C
8 8
8 3 8 3
E E
A A
D[A]=0
7 D[E]=7 7 D[E]=7
D[A]=0
D[D]=7
D[D]=7
D[B]=2 7 D D
D[B]=2 7
B 4 B
D[C]=5 D[F]= 4
9 D[C]=5 9
D[F]=4
2 5 F 5
C 2 F
8 C
3 8
8 8 3
E E
A D[E]=7
A
D[A]=0
7 7 D[E]=7
D[A]=0

34
Example
D[D]=7
D[B]=2 7 D
B 4
D[F]=4
D[C]=5 9
2 5 F
C
8
8 3
E
A D[D]=7
D[A]=0 7 D[E]=3
D[B]=2 7 D
B 4
D[F]=4
D[C]=5 9
2 5 F
C
8
8 3
E
A
D[A]=0 7 D[E]=3

35
Analysis of Complexity
 Complexity of Prim-Jarnik is the same
as Dijkstra’s algorithm
 Involves insertion (n), removal of
minimum value (n), updating values of
priority queue (m), each requiring
 because the graph is when a graph is
connected,

36
Kruskal’s Algorithm
 Basic concept:
 Maintains multiple smaller trees in a forest
 Repeatedly merge until there is a single
tree
 Steps:
 Each vertex starts in its own cluster
 Consider each edge ordered by increasing
weight
 If an edge connects two clusters, add the edge
to the MST
 If the edge is within a cluster, discard it
37
Kruskal’s Algorithm

38
Kruskal’s Algorithm
a b4 c 9
5 6 8 13
7
d e 8
6
7
f

sorted edges: ab ad bd df ce ef de bf bc cd

4
a b c
6
5
7
has 5 edges (n-1 edges) d e
6
𝑀𝑆𝑇 𝑐𝑜𝑚𝑝𝑙𝑒𝑡𝑒 7
f 39
Example
G G
B 8 8
4 B 4
9 E E
5 6 9 6
1 F 1 5 F
C 11 3 C 11 3
2 2
7 D H 7 H
D
A 10 A 10

G G
B 8 B 8
4 4
9 E 9 E
5 6 5 6
1 F 1 F
C 11 3 C 11 3
2 2
7 D H 7 H
D
A 10 A 10
40
Example
G G
B 8 B 8
4 4
9 E 9 E
5 6 6
1 F 1 5 F
C 11 3 C 11 3
2 2
7 D H 7 H
D
A 10 A 10
four steps

s
ep
st
G o G
8 8
tw
B 4 B 4
9 E 9 E
5 6 5 6
1 F 1 F
C 11 3 C 11 3
2 2
7 D H 7 D H
A 10 A 10
41
Analysis of Complexity
 Ordering of edges =
 Remove minimum value for each run is
constant
 For connected graph,
 Thus, the complexity of Kruskal’s
algorithm is equivalent to
 How is this calculated?
 m log n^2 = 2m log n = O(m log n)
42
Task
 Perform Kruskal’s algorithm on the
graph below to find the MST

43
End of Part 4

44

You might also like