Graphs - Shortest Paths 02 _ Class Notes
Graphs - Shortest Paths 02 _ Class Notes
& AI
Algorithms
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):
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)
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
Gandy
1
Dijkstra
DP
2 Bellman Ford
23
Imppoints
SSSP yardy algo always gives
1
Dijkstra's
solution to SSSP problem provided
optional
the edges in the given graph are
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
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
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
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