Design & Analysis of Algorithms: Lecture#10 Graph Introduction, BFT, DFT, MST, Shortest Path
Design & Analysis of Algorithms: Lecture#10 Graph Introduction, BFT, DFT, MST, Shortest Path
Lecture#10
Graph Introduction,
BFT, DFT,
MST,
Shortest Path
Lecture Contents
Graph
Graph Terminologies
Graph Representations (Adjacency Matrix, Adjacency Link List)
Graph Traversals (Depth First, Breadth First)
Spanning Tree
Minimum Spanning Tree
Kruskalls’ Algorithm
Prim’s Algorithm
Shortest Path
Dijkstra’s Algorithm
Bellman Ford Algorithm
A* Algorithm
Graph
A graph G (V, E) consists of two sets V & E. V is a set of
vertices & E is a set of Edges
Every edge e ϵ E joins two vertices in V
Graph G = (V, E)
where V = Set of vertices
AND E = set of edges
AND E V x V
Example:
G = (V, E)
V = {A, B, C, D, E,F}
E = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
Directed Graph (Digraph)
A directed graph is a graph in which all edges are directed.
Directed graph is also called a digraph.
G = (V, E)
V = {A, B, C, D, F, G}
E = {(A,B), (A,C), (B,D), (D,E), (D,F), (C,E), (E,F), (F,C)}
Undirected Graph
A undirected graph is a graph in which all edges are bidirectional.
In an undirected graph G(V,E),
∀e ϵ E
e1 (u, v) = e2 (v, u)
Connected Graph
A graph is connected if there is path between every two nodes of
that graph.
Complete Graph
•A graph is complete if there is edge between every two nodes of
that graph.
Total number of edges in complete graph are given as:
Degree, In Degree, Out Degree
Degree:
Number of edges connecting a vertex is called the
degree of node.
Degree of vertex A is 6
In Degree:
Number of edges pointing into a vertex is called its
in degree
In Degree of vertex A is 2
Out Degree:
Number of edges going out of a vertex is called its
out degree
Out Degree of vertex A is 4
Adjacency
Two nodes are said to be adjacent if they are connected through an edge.
In the example below nodes B&C, C&D, D&E are adjacent to each other.
Adjacent Nodes
Weighted Graph
A graph in which every edge has some weight
(numeric value) associated with it.
It refers to the effort while going from one vertex to
other using this edge
Weighted Graph
Unweighted Graph
A graph having no weight value associated with
any of its edge is called an unweighted graph
Unweighted Graph
Reachable Node
A node is reachable from another node if there exists a path
between them.
Multigraph
A graph is said to be multigraph if it allows more
than one paths between two nodes
Pseudo Graph
A graph is pseudo graph if it allows if it allows self
loops
Pseudo Graph
Reachable Node
A node is reachable from another node if
there exists a path between them.
1. ABCD is a path
2. ABD is not a path
Cycle
A cycle is a path starting and ending at same vertex
Hamiltonian Cycle:
A, B, C, D, E, F, G, H, A
Eulerian Cycle
An Eulerian Cycle of a connected directed / undirected graph is a cycle
that traverses each edge of a graph exactly once. There may be one
vertex that is visited more than once.
Eulerian Cycle:
A, B, C, D, E, F, C, G back to A
Subgraph
A subgraph H of graph G is a graph that has its vertices and edges
as subsets of vertices and edges of G respectively.
Graph G
V = {A, B, C, D, E, F, G}
Subgraph H Edges={(A,B), (A,G), (B,C), (B, G), (C,D), (C,F), (C,G),
V = {A, B, C, G} (D,E), (D,F), (E,F)}
Edges={(A,B), (A,G), (B,C), (B, G), (C,G)}
Tree
A connected graph without any cycles is call a tree graph or simply a tree.
For a tree graph, there is a unique path between every two nodes of the
graph
Tree
Graph Representation
Graphs are represented through two different ways:
1. Adjacency Matrix
2. Adjacency List
Graph Adjacency Matrix Representation
Say a graph has V={1,2,3,…,n} vertices then graph is represented by
a NXN matrix A such that:
A[i, j] = 1 if there is edge between nodes i & j
= 0 otherwise
Graph G
Graph G 1 2 3 4
1 0 0 1 0
2 0 0 1 1
3 1 1 0 1
4 0 1 1 0
Spanning tree T2
Spanning tree T1
Spanning Tree (ST)
“A spanning tree is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges”
Number of vertices for all spanning trees of a graph are Graph G
same
Number of edges for all spanning trees of a graph are same
All spanning trees form a minimally connected graph i.e.
one takes away one edge from ST and it is no more a
spanning tree
Adding one edge to ST will introduce a cycle i.e. ST is
maximally acyclic Spanning tree T1
Spanning Tree (ST) … Applications
Network Planning & Design
Network Routing Protocols
Cluster Analysis
Approximation Algorithms for NP-hard Problems
Image Registration & Segmentation Algorithms
Minimax Process Control
etc
Minimum Spanning Tree (MST)
“Minimum spanning tree is subset of edges of a
connected, weighted undirected graph that connects all
vertices together and bears the minimum possible total Spanning Tree T1
edge weight”
Minimum Spanning Tree is also called Minimum
Weight Spanning Tree
MST is a tree so it does not contain any cycle
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D)}
{B}, {C}, {D,A,F}, {E}
Edges = {(D,F), (A,D), (C,F), (A,B),
(C,D), (B,C), (D,E), (E,F), (B,D)}
(u,v) = (A, D)
If condition = true
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F)}
{B}, {D,A,F,C}, {E}
Edges = {(D,F), (A,D), (C,F), (A,B),
(C,D), (B,C), (D,E), (E,F), (B,D)}
(u,v) = (C,F)
If condition = true
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B)}
{D,A,F,C,B}, {E}
Edges = {(D,F), (A,D), (C,F), (A,B), (C,D),
(B,C), (D,E), (E,F), (B,D)}
(u,v) = (A,B)
If condition = true
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B)}
{D,A,F,C,B}, {E}
Edges = {(D,F), (A,D), (C,F), (A,B),
(C,D), (B,C), (D,E), (E,F), (B,D)}
(u,v) = (C,D)
If condition = false
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B)}
{D,A,F,C,B}, {E}
Edges = {(D,F), (A,D), (C,F), (A,B), (C,D),
(B,C), (D,E), (E,F), (B,D)}
(u,v) = (B,C)
If condition = false
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B),(D,E)}
{D,A,F,C,B,E}
Edges = {(D,F), (A,D), (C,F), (A,B), (C,D),
(B,C), (D,E), (E,F), (B,D)}
(u,v) = (D,E)
If condition = true
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B),(D,E)}
{D,A,F,C,B,E}
Edges = {(D,F), (A,D), (C,F), (A,B), (C,D),
(B,C), (D,E), (E,F), (B,D)}
(u,v) = (E,F)
If condition = false
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B),(D,E)}
{D,A,F,C,B,E}
Edges = {(D,F), (A,D), (C,F), (A,B), (C,D),
(B,C), (D,E), (E,F), (B,D)}
(u,v) = (B,D)
If condition = false
Graph G
Kruskal’s Algorithm for MST … Demonstration
resultSet={(D,F),(A,D),(C,F),(A,B),(D,E)}
Graph G
Kruskal’s Algorithm for MST … Complexity
proc primMST(G, r) // G = (V, E)
resultSet = {}
Prim’s Algorithm for MST
resultSet = {}
vertices = {A, B, C, D, E, F}
Prim’s Algorithm for MST
resultSet = {}
vertices = {A, B, C, D, E, F}
A(∞, null), B(∞, null), C(∞, null),
D(∞, null), E(∞, null), F(∞, null)
Prim’s Algorithm for MST
resultSet = {}
vertices = {A, B, C, D, E, F}
A(0, null), B(∞, null), C(∞, null),
D(∞, null), E(∞, null), F(∞, null)
Prim’s Algorithm for MST
resultSet = {}
vertices = {B, C, D, E, F}
A(0, null), B(∞, null), C(∞, null),
D(∞, null), E(∞, null), F(∞, null)
u=A
If condition = false
Prim’s Algorithm for MST
resultSet = {}
vertices = {B, C, D, E, F}
A(0, null), B(3, A), C(∞, null),
D(∞, null), E(∞, null), F(∞, null)
u=A
If condition = false
G.adjacentList = {B, D}
v=B
2nd if condition = true
Prim’s Algorithm for MST
resultSet = {}
vertices = {B, C, D, E, F}
A(0, null), B(3, A), C(∞, null),
D(2, A), E(∞, null), F(∞, null)
u=A
If condition = false
G.adjacentList = {B, D}
v=D
2nd if condition = true
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(∞, null),
D(2, A), E(∞, null), F(∞, null)
u=D
If condition = true
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(∞, null),
D(2, A), E(∞, null), F(∞, null)
u=D
1st If condition = true
u.adjacentList={A, B, C, E, F}
v=A
2nd if condition is false
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(∞, null),
D(2, A), E(∞, null), F(∞, null)
u=D
1st If condition = true
u.adjacentList={A, B, C, E, F}
v=B
2nd if condition is false
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(3, D),
D(2, A), E(∞, null), F(∞, null)
u=D
1st If condition = true
u.adjacentList={A, B, C, E, F}
v=C
2nd if condition is true
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(3, D),
D(2, A), E(4, D), F(∞, null)
u=D
1st If condition = true
u.adjacentList={A, B, C, E, F}
v=E
2nd if condition is true
Prim’s Algorithm for MST
resultSet = {(A,D)}
vertices = {B, C, E, F}
A(0, null), B(3, A), C(3, D),
D(2, A), E(4, D), F(1, D)
u=D
1st If condition = true
u.adjacentList={A, B, C, E, F}
v=F
2nd if condition is true
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F)}
vertices = {B, C, E}
A(0, null), B(3, A), C(3, D),
D(2, A), E(4, D), F(1, D)
u=F
1st If condition = true
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F)}
vertices = {B, C, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=F
1st If condition = true
v=C
2nd condition = true
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F)}
vertices = {B, C, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=F
1st If condition = true
v=E
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F)}
vertices = {B, C, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=F
1st If condition = true
v=D
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C)}
vertices = {B, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=C
1st If condition = true
v=B
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C)}
vertices = {B, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=C
1st If condition = true
v=B
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C)}
vertices = {B, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=C
1st If condition = true
v=F
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C)}
vertices = {B, E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=C
1st If condition = true
v=D
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B)}
vertices = {E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=B
1st If condition = true
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B)}
vertices = {E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=B
1st If condition = true
v=A
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B)}
vertices = {E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=B
1st If condition = true
v=D
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B)}
vertices = {E}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=B
1st If condition = true
v=C
2nd condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B), (D,E)}
vertices = {}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=E
1st If condition = true
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B), (D,E)}
vertices = {}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=E
1st If condition = true
v =D
2nd if condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B),
(D,E)}
vertices = {}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
u=E
1st If condition = true
v =F
2nd if condition = false
Prim’s Algorithm for MST
resultSet = {(A,D), (D,F), (F,C), (A,B), (D,E)}
vertices = {}
A(0, null), B(3, A), C(2, F),
D(2, A), E(4, D), F(1, D)
1st If condition = FALSE
Prim’s Algorithm for MST
Execute it yourself
Bellman Ford Algorithm … Demonstration
Execute it yourself
A* Shortest Path Graph Algorithm
Often used for path finding and graph traversal in real life problems and games
While looking for the shortest path between the source and destination nodes, a*
algorithm uses some guidance (called heuristics)
On every step of iterative search process (as said above), heuristics function
guides the process which nodes will be chosen next
Heuristic function typically estimates the smallest cost (distance) from source
node to the destination node
Estimated heuristic function takes very little time to execute and does not add
much cost during execution of a* algorithm
A* Algorithm … Heuristics Function
While calculating the cost of path from source to an destination node, A*
algorithm uses the following formula on every step:
f(n) = g(n) + h(n) where g(n) is the actual cost for reaching to node n starting from
source node and h(n) is the estimated cost (typically the cost of cheapest path from
node n to destination node)
A heuristic function h(n) is only admissible if for every node n,
h(n) ≤ g(n), where g(n) is the true cost to reach the goal state from n.
An admissible heuristic never overestimates the cost to reach the goal, i.e., it is
optimistic
A* Algorithm … Heuristics Function
33
A* … The Algorithm
Properties of A*
A* generates an optimal solution if h(n) is an admissible heuristic and the
search space is a tree:
h(n) is admissible if it never overestimates the cost to reach the destination
node
A* generates an optimal solution if h(n) is a consistent heuristic and the
search space is a graph:
h(n) is consistent if for every node n and for every successor node n’ of n:
h(n) ≤ c(n,n’) + h(n’)
h(n)
n
d
c(n,n’) h(n’)
n’
• If h(n) is consistent then h(n) is admissible
•Frequently when h(n) is admissible, it is also consistent
Admissible Heuristics
A heuristic is admissible if it is too optimistic, estimating the cost to be
smaller than it actually is.
Example:
In the road map domain,
is admissible as normally cities are not connected by roads that make straight
lines