0% found this document useful (0 votes)
7 views

Graphs - Shortest Paths 02 _ Class Notes

The lecture covers algorithms related to shortest paths in graphs, specifically focusing on Dynamic Programming (DP) techniques such as Dijkstra's, Bellman-Ford, and Floyd-Warshall. It contrasts DP with Greedy and Divide & Conquer methods, emphasizing the efficiency of DP in solving overlapping subproblems. The document also discusses the applicability of these algorithms in various graph-related problems and provides insights into their complexities and optimality conditions.

Uploaded by

bheeshmachava19
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)
7 views

Graphs - Shortest Paths 02 _ Class Notes

The lecture covers algorithms related to shortest paths in graphs, specifically focusing on Dynamic Programming (DP) techniques such as Dijkstra's, Bellman-Ford, and Floyd-Warshall. It contrasts DP with Greedy and Divide & Conquer methods, emphasizing the efficiency of DP in solving overlapping subproblems. The document also discusses the applicability of these algorithms in various graph-related problems and provides insights into their complexities and optimality conditions.

Uploaded by

bheeshmachava19
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/ 36

Data Science

& AI
Algorithms

Graphs - Shortest Paths

Lecture No.- 02
1z By- Aditya sir
Recap of Previous Lecture

Topic PYG
Dijkstra
Topic Intro to DP

Topic
Top Down
Topics to be Covered

Topic Bottom Up
Topic
Bellman Food

Topic
Floyd Warshall
Topic : Dynamic Programming: (DP)
Normal Recursive Implementation of Fib (n):

Algo Fib (int n


{
If (n ≤ 1) return (n);
else
{
return (Fib (n-1)+ Fib (n-2));
}
}
Topic : Dynamic Programming: (DP)
Top-down - Memoized implementation of Fib(n)
DPTop down
Algo memiFb(n)
Imal
{ 0 2n O n
if (m [n] is undefined)
{ mm

if (n <=1) result = n;
else
result = memFib (n-1) + memFib(n-2);

}
m[n] = result; //memorizing (caching)
I
return (m[n]);
}
Topic : Dynamic Programming: (DP)
Bottom-up approach of D.P for Fib (n)
Iterate Tabulate
Algo memFib (n)
{ Bottomup
M [0] = 0
M [1] = 1
for i = 2 to n
Iffy
{ 43
M [i] = M [i-1] + M [i-2];
} Fla
TC O n
return (M[n]);
} Flo fli

scale
mfi mfi 2

t.IT mfi
m 1

M 21 me mfo
1 0 1

mfi
MB ma
I I 2

m 2
m 3
my
2 1 3

3
23 mfs m a
2
m
5
3
Bottom up
IDown
1415
F s

419
Flu
f 3

F 3
Fla
t
F 2
F i

Flo
23 Ffi
Topic : Dynamic Programming: (DP)
Dynamic Programming vs Greedy Method vs Divide & Conquer:
• In all methods the problem is divided into subproblem;
• Greedy Method: Building up of the solution to the problem is done in step-wise
manner (incrementally) by applying local options only (local optimality).
• Divide & conquer: Breaking up a problem into separate problems (independent),
then solve each subproblem separately (i.e. independently) & combine the
solution of subproblems to get the solution of original problem.
• Dynamic Programming: Breaking up of a problem into a series of overlapping
subproblems & building up solution of larger & larger subproblems.
Topic : Dynamic Programming: (DP)
• Unlike Divide and Conquer, D.P typically involves solving all subproblems,
rather than a small portion of subproblem.
• D.P tends to solve each subproblem only once since the results of the
subproblems are stored, which are used later again when required. This is going
to reduce the computation drastically. (In most case, the complexity)

Brute force: O(2)


Ex- Fib
D.P Implementation: O(n)
if Dnf vs
II
sub problems
1
Dnc Independent
solved independently

sub problems
2
DI Overlapping

23
DI mayest tf
Independent

It off sublet
A

y a ypgj

A up
D23 IT
subproblem
Fluorelapping
r

23
Applications of DP in
Graphs
SSSP
Path
ngleSouceShotk
BFordAlgo
Paths APSP
2 All Pairs Shortest
warshall
Floyd

3 Multi stage graph


Salesman Problem
4 Travelling
23
Single Source Shortest Paths Problem

Gandy
1
Dijkstra
DP
2 Bellman Ford

both SSSP algos


Distinction between
works when
and which

23
Imppoints
SSSP yardy algo always gives
1
Dijkstra's
solution to SSSP problem provided
optional
the edges in the given graph are

the weights only


of then
wt edge
2
If the qraph anys.am
or may not give
SSSP Algo may
Dijkstra
solution pathiostst.mn
23
optionals
3 more we cut edges
If the graph has 0 or

But NOT a newtcyclesthenthed


Bellman Ford s Talgo DP based always
Tesoptimal solution to SSSP problem

that is
4 If the graph has any ache
then 140 also
reachablefromsources
optional soln Dj

23
gives
IBED
not detect all we cut
5 BF may
Cycles
ne wt
detects
definitely always
But reachable
Source
Cycles that are from

23
mweightcycle matedged match
a
91 A β
Net weight of
sum of all edges
Cycle init
A B
2
0 5 10 5 20

1201151 5

C f we cut cycle reachable from


source
A B
Notalycle 10
detected
5
1
A B
1
C A B
by
B
10 Bellman Ford
min cost path A
15 00
is Not defined so 5 5 10

Bellman
23 Ford Dj botfails to give shortest paths
Fri e ty

d F
Source

nm wt cycle
we at cycle
reachable
or may
not get but not
may
deteadby dmanford from
Saver

23
wt edges But No we cut
92 with we
Graph cycle

SSSP work
Net wt of cycle A B Dijkstra
10 5 3
5
3 1
no guarantee
1 at edge
the f we
not work
may
or may
Ford works
Bellman
u wt edge
Ya
are net cycle
23
IIP ginengraphGCV.ES
SSSP Algo and
5 9 Apply Dijkstra's
it
the min costs reported by
C
give
B
C D E
A B
7 0
6 0 7
in

A B 6 11 7

2 A B E 0 6 9 Z
ede 2
6 7
0
ABED
A 6 7 2
23 AIB E D 0
SSSP Algo
The min Costs Reported by Dijkstra
are
for above graph
A E
B C E
A D
AD
s
C B E
2
A
f 2 f 4
7 1 3

Is A B
7 9
A D C B

7 1 3 2 actual min
7 5 2 actual min lost A B
23
Impobservations
to give
1 In
peer ag Dijkstra
SSSP
failed
vertices because
cost to few
optimal path
the vertex in selected
once
in Dijkstra
considered to be relaxed
it in
HOF further
relaxation is carried
2 Bellman Ford the
In

out
immultipleiteations.­n
w.n.ttheedgrs
23
no.of
vertices
iteration Tn 1
It I Digest
Soul A B A C 10
16 A B

D
no relaxation
later
fix
Bellman FIA

B
A C 20 16
16

23 c IN
Bellman Ford Walkthrough
I_
7ᵗʰ 0 6 72
found 5 a

A B C 6 64
B 9C
461B AT
I Dy Fo D Ef2

2
Items Itery BEV
4
5
62 4 2

1 B C B

XL
A

DE
A
Iteration n 1
p
23 55 1 4 7 Ez 2 2
Dijkstra SSSP Bellman Ford

A 2
A B 6X
A c 4
A C 6
A D 7
A D 7
A E 2

A E 2 X
Y

23
Imp observation

based SSSP Algo failed


Dijkstra's Genedy
shortest path to vertices
to give us the fee
DP based gives
Bellman Ford SSSP Algo
But soln to all the vertices
paths optimal
shortest
me out edges are present
even though
is
Duty
no me out Cycle present
as

23
n 1 iteration

B C
A
mar edges 1
1
D
au
in E
1 iterations result
more than n
If better costs
relaxation
in further

we not cycle is present


23
192 Bellmanford Ite test 3
A B
50114 B
A B

a 01 1
D
00
2
7

n 4 Fters 414 31 1 3

Iterations
0 2 3 A B
1 A B
n
new ending
3
f
4 1

23
2 C7 20 4 31
is
7H n i iteration there
even after
relaxation possible
still further
he wt
is a
This happens because there
Ford fails
so even Bellman
Cycle

23
Bellmanfordlode
i.IE sound
in
91
Algo Bellman Ford Gs w es

cost
matrix
Soure r s adj
1 Initialize Single
Tc 01
2 For 1 1 to n 1 0in

rates
Effole
e Tc O next
for
do 011
Relax u v w

Title __
relaxation is
G it further
3 For dge u v
EE still possible after In 1

U V then
passes iterators
w w
if dfu d
reachable
ne wt cycle
011 returnFall be sock exists
20ᵗʰ he cannot from
3
4
23
Ilcan be solved by Bf
solved by Bellman Ford
0 a

9 fiititi
dlug.TO D
C
1 Hull
II a 0

to u
Tru shortest path
s some via which adj
vertex
Algo slow
140
If 1171 Retealoncess
dfv3 dfu w u v
to v
a is shortest path
23 3 Ts update source via in as acff
from
Complexity Bellman Ford
Omall Time of

n e e c
TC O n

0 2
eD
OIn

23
2 mins Summary

Topic

Topic

Topic

Topic
THANK - YOU

Telegram Link:
https://t.me/AdityaSir_PW

28

You might also like