You are on page 1of 39

path algorithm

shortest
-
-

v Graphs 2 1
Bipartite Graphs, Dijkstra, Bellman Ford
-- -

70 -Priyansh Agarwal
5
Dijkstra

Beiman
"
-
-
-
⑧ and
-
&
-

I ↳ O

·
-

⑧ da
Bi-partite Graphs
~
- Definition
- Algorithm
A colorable
-
- Odd Length Cycle -> bi-partitely
not
v
- Tree Property
v
- Problem: Link
-- H.W
-
-
-
Bi artite Graphs: Every 2
adj.
have different
- -

-
nodes

⑧ ↳

②m



I ⑧
-2
R G

*↓to
- Fionnected
graph

9 it
. Given an

3 Y
an
only have

2 possisy colorings
(ti-partites
-
-

-

-
⑧. ⑪
catal
A aloed in
graph can be a

if
Di-partite
manner

it
does not

ungte cycl
o d
connected have either
A graph can

0 Sipartitecolorings CO

-
2
-

⑧ ⑧

-> 8 doo.·
If a
graph contains a

connected components
how many
have
si-partity coloring can you


·orwaterre
R G
-
Choose a random node and color

itwith first Color



-

·

vector [int (n, 0);
>
co
W 1 =

boo dis [int


zaur, edges, colm) R2 =

tool true
I
ans=
for lint
neighbour:edges/cur)
if (colors (night)
0)
=
=

↳saurian
I

re
else if (Coles (neighbours colon
==
(cur))
return fabe;
retur uns;

y true
0(n)
-
-
book ans=

for inti 0; icn; i++)


(
=

((o(v(i)
#
=

=
c)
Colors (i) = I

am=au dos(i, edges, color) I


Dijkstra (most important)
W
- Single Source Shortest Path Algorithm - Idea + Visualization>
~
- Non-negative edge weights -
-

- Proof/Intuition:
~
- On every iteration the marked vertex is the one that can never have

-
- Code
=
a better distance later on.

- Retrieving the shortest path?


- Problems
- Google Interview Problem

all
edge weights

o
Y
-
n
>0

kon-negative

sa
a
d-
· -b
do-
to
or
50


suc

-


-
-I
woodImine
⑩ coming
e
I 582
② the

offic

0
y
La
IED
-
Dijkstra Visualization
dist.

=.
·

.
+ distin)
weight

.
.
.
.
.
. edge
.
-
-
-
- >
->
-

0 5 1

⑲. N
=


= 2 +

T
100
Dijkstra Visualization dist
I O -

.
-

- -

X atio
·


5 10
+

->N=
D
weight -> disting
-
4
/
M3),(20,4) Set


-

4
o
-
0
-

vis/3)= true 2 3
-

-
①Given a
weighted undirected connected Graph
-
~find the
out from
shortestpath source

X
Y entralt
to

↳ I Consides all
-

paths
shortest
from

all the

and
find out
W -

nodes spath one such

-
-
20 10

-- Y
0
6
-

%
R
- dist(X,Y) = 20

o

10
10

X y

E
10
12
0(n)
-

⑧ =>

20
A
- -
⑧ ->
to - t
-
X

-
E Y
dist(A, B) 15
=

↓ -
-

->>.


e

& is/y,B3 15
dist (A, us
=

t
-
-

-
dest(A,B)
- =
dijkstra from A
① I


-
-

% -
-

② C-dis istra from B

!
-
-
--
-

dist -

distA [27+distB (n) -dist(A, B3


-
=>
-

->
> >
>
> =
=>
>
Mot -- d
I10

l

OC

is
&
5/10 B
A
0-00

Lo
7
22
23

5
=
from A
⑪ Run dijkstra

from

a
Run dijtestry

distA(n)
if distal
remove is
from the goupy
from A
③ Rud &inal dijkstra
- --
- -
-
Bellman Ford
- Single Source Shortest Path Algorithm - Idea + Intuition
- Negative edge weights (in directed) without negative cycles
- Proof/Intuition:
- Principle of Mathematical Induction
- Code
- Breaking Early
- Retrieving the shortest path?
- Finding a negative cycle
Bellman Ford Visualization

You might also like