Important Graph Algorithms
Important Graph Algorithms
GRAPH
ALGORITHMS
Explained in the
simplest way
a b e a b a b a b e
c d f e c d f
g c d c d e g
a b e a b a b a
c d f e c
g c d c d e g
a b e a b a b a b e a
c d f e c d f
g c d c d e g c d
*Disclaimer*
This document Will walk you through
the most important graph algorithms
from the standpoint Of learning and
cracking DSA Interviews.
1
TRAVERSALS IN
GRAPH
There are two types of graph traversals :
ROOT ROOT
a b e a b e
c d f c d f
g g
DFS = a b e f d c g DFS = a b e f d c g
ROOT
a b e
c d f DFS = a b e f d c g
3
BREADTH FIRST
SEARCH (DFS)
Traverses from a node (also called root node),
and explores the neighbouring nodes first, and
then selects one neighbour node and again
explores its neighbour nodes until it hits the
dead end.
ROOT
a b e
c d f DFS = a b d c e f g
4
MINIMUM SPANNING
TREE (MST)
have a cycle.
formed).
spanning trees.
5
Sum of weights of the edges in a spanning tree is
10
a b a b
15
7
6 12 e 6 7
9
c d c d e
8 8 9
6
TRAVERSALS IN
GRAPH
There are two algorithms to build MST
Prim's algorithm
Kruskal's algorithm
8
KRUSKAL'S
ALGORITHM
Again a greedy algorithm where we start with
sorting all the edges first, and then picking the
edge with lowest weight and repeating the
process.
9
Bosscoderacademy.com
HANg ON!
PREPARINC FOR
TELL ME MORE
10
EULERIAN PATH
& CIRCUIT
a b c HP = a b d e c
11
EULERIAN PATH
& CIRCUIT
a b f
EC = a b f e d c a
Euler Circuit
c d e
Diagram
12
You can say if a graph has :
Eulerian Path -
If the graph has O or 2 nodes have odd degree &
all other nodes have even degree .
Eulerian Circuit
If the graph has all vertices even degree.
13
HAMILTONIAN
PATH & CIRCUIT
HP = a b d e c
a b c
Hamiltonian Path
d e Diagram
14
HAMILTONIAN
PATH & CIRCUIT
HC = a b c e d a
a b c
Hamiltonian Circuit
Diagram
d e
15
TOPOLOGICAL
SORT
b d TS = a b c e d
a
Topological Sort
c e Diagram
16
WIDELY USED SHORTEST
BFS algorithm -
Use BFS if the graph is undirected with unit
weights
Djikstra's Algorithm -
If graph is directed and has no negative weight
cycles then You can use Djikstra Algorithm.
17
Why
Bosscoder?
1000+ Alumni placed at Top
Product-based companies.
Explore More