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

Graphs PC

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Graphs PC

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Intro to Shortest

Intro to Shortest
Path Algorithms
Path Algorithms
Session Contents
Dijkstras Bellman Ford Floyd Warshall
Algorithm Algorithm Algorithm

The structure of the The structure of the The structure of the


Algorithm Algorithm Algorithm
The working of the The working of the The working of the
Algorithm Algorithm Algorithm
A sample problem on it Detecting Negative Cycles Intro to a Sample
Drawbacks of the Algorithm Intro to a Sample Problem Problem
Dijkstra Algo
Choose the vertex with the smallest path length from
root and mark it as marked.
For all its children if we can update the path lengths
then do update them.
Once all children are updated we mark the unmarked
vertex with smallest path length from root as marked.
Once a vertex is marked no further changes will occur
in its path length from the root
VISUALIZING
THE

DIJKSTRA
ALGO
Try it yourself using the link
DIJAKSTRA
It works on both Directed and Undirected
graphs.
Select Minimum distance vertex and
Discard marked vertices.
Data Structure to use : Priority Queue or
Sets
Priority Queue : Insertion and Deletion
O(logn)
Declaring a priority queue
of pairs on minimum priority

If weight of child can be


updated,we update and push
it in for processing

Time Complexity : O(n+m)


DRAWBACK
Doesn't work on
negative weighted graphs
CSES : SHORTEST PATH I
Bellman Ford Algo
Choose all the edges one by one from the edge set and
check if the weight of vertex can be updated or not.
If we can lower the weight of the child vertex then
we do it.
The above process is to be repeated n-1 times, where n
is the number of vertices.

Note : Relaxation of an edge -> Update the distance of


child vertex if possible.
VISUALIZING
THE

BELLMAN FORD
ALGO
Try it yourself using the link
BELLMAN FORD
It works on both Directed and
Undirected graphs.
Select the edges one by one and
update the distances if possible.
Updating the distance due to an
edge if possible : Relaxation of Edge.
Repeat the process n-1 time :)
Declaring a struct to store
the properties of the edge

Declaring a vector of edge


to store the edges

Taking in the input for


Undirected Graphs

Taking in the input for


Directed Graphs
Iterating n-1 times

Relaxing the Edge

Checking for
disconnected
components
Time Complexity : O(n*m)
ADVANTAGE
Works on Negative Edge Weighted Graphs
too
We can detect a negative cycle in the graph
using this Algorithm
Note that in case there is a negative cycle
then the minimum distance of the vertex in
the cycle becomes (-infinity)
DETECTING NEGATIVE
WEIGHT CYCLE
Iteration 0 : Iteration 1 :
dist(h) = 0 dist(h) = -3
dist(i) = inf dist(i) = 2
dist(j) = inf dist(j) = 5

Iteration 2 : Iteration 3 :
dist(h) = -8 dist(h) = -13
dist(i) = -3 dist(i) = -8
dist(j) = 0 dist(j) = -5
CSES : HIGH SCORE
FLOYD WARSHALL
Make a distance matrix, where dist[i][j]
stores shortest distance between i and j.
Initialise it with dist[i][i] = 0 and if
there is an edge b/w u and v of weight c,
then dist[u][v]=c & dist[v][u] = c.
Go over the vertex list, let's say we are at
vertex k.
For every pair of vertices, update dist[i][j]
with dist[i][k]+dist[k][j] if it is smaller.
VISUALIZING
THE
FLOYD WARSHALL
ALGO
Try it yourself using the link
ADVANTAGE
Very easy to code and implement.
Finds distance of each vertex from every
other vertex.
Works on Negative Edge Weighted Graphs
too.
We can use it to detect negative cycles in
the graph too.
We can even find the sequence of vertices
in the shortest path between every pair of
vertices.
Drawback: Takes O(n^3) time to run.
CSES : SHORTEST PATH II
THANK
YOU

You might also like