DS Unit4 Graphs
DS Unit4 Graphs
Syllabus
Graph Terminologies
Topological Sort
Graph Traversals
Breadth first traversal
Depth First Traversal
Minimum Spanning Trees
Prims’ and Kruskal algorithm
Shortest Path Algorithms
Dijikstra’s Algorithm
Bellman Ford Algorithm
Floyds’ Warshall Algorithm
Graph Applications
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Course Outcome
Introduction
Terms to be remembered
Graph, Ways of representation
Sub Graph
Symmetric Digraph / Undirected Graph
Complete Graph
Degree, Incidence, Adjacency, Path
Connected Graph / Strongly Connected Graph
Cycle, acyclic graph
Connected component
Weighted graph
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Graph
“Graph” is non-linear data structure where the
data items are stored in a memory locations by
means of pointer
Introduction
Graph
“Graph” is a collection of nodes / objects called
vertices and edges where vertices are a data
element of the graph & edge is a path between 2
nodes
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Graph
“Graph” is a finite set of points (vertices or
nodes), some of which are connected by lines
or arrows (edges)
Introduction
Graph Representation - 2 ways
1. Undirected Graph
2. Directed Graph
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Edge between 2 nodes are not
Directional oriented. It is bidirectional
Undirected Graph
A A
B C B C
Fig. 1 Fig. 2
Vertices = {A, B, C}
Edges = {(A,B), (A,C), (B,C)} D
Vertices = {A, B, C, D}
Edges = {(A,B), (A,C), (A,D),(B,D),(B,C),(C,D)}
Introduction
Edge between 2 nodes are
Directional oriented. It is Unidirectional
Directed Graph
A A
B C B C
Fig. 1 Fig. 2
Vertices = {A, B, C}
Edges = {(A,B), (A,C), (B,C)} D
Vertices = {A, B, C, D}
Edges = {(A,B), (A,C), (D,A),(B,C),(D,B),(D,C)}
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Note
Any complex problems can be solved using
Graph algorithms
Introduction
Example 1: Find the total number of possible
edges from a given directed graph
A A B
B C C D
Fig. 1 Fig. 2
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Example 2 Find the total number of possible edges
from a given directed graph
A
A
B C
B C Fig. 2
Fig. 1
D
Introduction
Sub graph
A Subgraph of a Graph, G = (V,E), is a graph G’
= (V’,E’) such that V’ V and E’ E
1 1
Example
2 3 2
Fig. 1 Fig. 2
4 3
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Sub graph
A Subgraph of a fig. 1
1 2 3
1
2 3 4
Fig. 1a Fig. 1b
2 3
Fig. 1c
4
Introduction
Sub graph
A Subgraph of a fig. 2
2 1 1
3 2 2
Fig. 2a Fig. 2b
Fig. 2c
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Symmetric Digraph
A “Symmetric Digraph” is a directed graph such
that for every edge ‘vw’ there is also a reverse
edge
Example
Introduction
Symmetric undirected graph
Every Undirected Graph is a Symmetric
undirected graph
Example
1 In Undirected graph, each edge
Facilitates bidirectional path
Edge = {(1,3),(3,1)}
2 3
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Complete Graph
It is a (normally undirected) graph with an edge
between each pair of vertices
Example
A B A B
C D C D
Introduction
Degree, Incidence & Adjacency
Incidence
If there is a edge between the vertices, then
that edge is said to be incident to those
vertices
10
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Degree, Incidence & Adjacency
Degree
It is the number of edges incident to a vertex
Example
A Vertex “A” has:
In-degree : 1
Out-degree: 2
Degree :3
B C
Vertex “C” ?
Introduction
Degree, Incidence & Adjacency
Adjacency
A vertex “A” is adjacent to “B” if there is a
edge from “B” to “A”
A
Here “A” is a Successor
“B” is a Predecessor
B C
11
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Path
A path from “v” to “w” of a Graph, G=(V,E), is a
sum of sequence of edges, v0v1,v1v2,…vk-1vk
vertex.
Path Length = No. of edges traversed
Introduction
Connected Graph
An undirected Graph is said to be connected if
there is a edge between vertices
12
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Strongly Connected Graph
A Directed Graph is said to be Strongly
connected (traveling the one-way street in their
correct direction) if there is a path from ‘v’ to
‘w’
A
B C
Introduction
Cycle
A path from a node to itself is called “Cycle”
13
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Cycle
Example
Here
B C Cycle = (A, B, C, A)
Cycle = ?
D
Introduction
Acyclic Graph
A graph is said to be Acyclic if it has no cycles
B C
14
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Connected Component
Maximal connected Subgraph of Graph ‘G’
1
1
2 3
2 3
4
4
Introduction
Weighted Graph
A “weighted Graph” is a triple (V,E,W) where
(V,E) is a Graph (Directed / Undirected) and ‘W’
is weight of an edge between vertices
10
A B
15 5 20
C D
25
15
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Graph Representation – 2 kinds
1. Adjacency Matrix Representation
2. Adjacency List Representation
Introduction
Adjacency Matrix Representation
A Graph, ‘G’ can be represented by an ‘n x n’
matrix, A = aij called Adjacency Matrix for ‘G’
1 if vi is adjacent to vj E
aij = for 1 i, j n
0 otherwise
16
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Adjacency Matrix Representation
A.M., ‘A’ for weighted graph is defined by:
Introduction
Adjacency List Representation
In this representation, vertex are indexed by
linked list
17
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Example 1
Construct the adjacency matrix & list
representation for the given graph
1 2
3 4
5 6 7
Introduction
Solution
Total number of nodes = 7
18
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Solution
1 2 3 4 5 6 7
1 0 1 1 0 0 0 0
2 1 0 1 1 0 0 0
3 1 1 0 1 0 1 0
4 0 1 1 0 0 1 0
5 0 0 0 0 0 1 0
6 0 0 1 1 1 0 1
7 0 0 0 0 0 1 0
Introduction
Solution
1 Adjacency
2 List: 3 NIL
Total number of nodes = 7
2 1 3 4 NIL
3 1 2 4 6 NIL
4 2 3 6 NIL
5 6 NIL
6 3 4 5 7 NIL
7 6 NIL
19
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Example 2
Construct the adjacency matrix & list
representation for the given graph
25
1 2
10
5 14 6
3 18 4
16 32
42 11
5 6 7
14
Introduction
Solution
Total number of nodes = 7
20
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Solution
1 2 3 4 5 6 7
1 0 25 œ œ œ œ œ
2 œ 0 10 14 œ œ œ
3 5 œ 0 œ œ 16 œ
4 œ 6 18 0 œ œ œ
5 œ œ œ œ 0 œ œ
6 œ œ œ 32 42 0 14
7 œ œ œ œ œ 11 0
Introduction
Solution
1 Adjacency
2 25 List:
NIL
Total number of nodes = 7
2 3 10 4 14 NIL
3 1 5 6 16 NIL
4 2 6 3 18 NIL
5 NIL
6 3 32 5 42 7 14 NIL
7 6 11 NIL
21
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Introduction
Graph Applications
Any type of complex problems can be solved
using Graph Algorithms.
Air Line Route Map
Flow Charts – Directed Graph
Binary Relation
Computer Networks
Electrical Circuits
Graph Traversal
Key points to be remembered
Graph Traversal
It is a problem of visiting all nodes in the
Graph “G” in a particular manner
22
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Graph Traversal
Key points to be remembered
A more general form is to determine for a given
starting vertex v V all vertices such that there
is a path from v to u
Graph Traversal
Key points to be remembered: Types
Let G=(V,E) be a given Graph and a vertex
“V(G)”. If we wish to visit all vertices of a
Graph “G”.
23
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Graph Traversal
Breadth First Search & Traversal
Concept
It starts from any arbitrary vertex of a given
graph
24
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Breadth First Search &
Traversal
Procedure
Starts at initial vertex and make it as having
been visited
The vertex V at this time said to be unexplored
The vertex V is said to be explored, if its
adjacent vertices are visited
All unvisited vertices adjacent from V are
visited next – these vertices are unexplored
vertices
The newly visited vertices have not been
explored and are put on to the end of a list of
unexplored vertices
Exploration continues until no unexplored
vertex is left
25
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Breadth First Search &
Traversal
Steps to be followed
1. Start the traversal process from starting
vertex “v”
1 2
3 4 5 6
26
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Breadth First Search &
Traversal
Solution 1:
Starting vertex
0
1 2
3 4 5 6
A D
B
G
F C E
27
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Breadth First Search &
Traversal
Problem 3: Apply BFS algorithm to visit all the
vertices of a given Graph
1 A
2 4 B C
3 D
C D F H
28
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Key points to be remembered
“DFS” is a recursive algorithm, which is
analogous to preorder traversal of a tree
29
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Key points to be remembered
If path does not exist from one node to other
node, return to a previous node where we
have been before – Backtracking
Constraints
30
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Problem 1: Apply DFS algorithm to visit all the
vertices of a given Graph
A D
B
G
F C E
Backtracks to B – A
And explore A to F
F C
FromFrom
F, it isc possible
–”DeadtoEnd”-
traverse to A
there isor
noCadjacent
– vertices
both Algorithm
are discovered nodes – Fto
Backtracks is referred as Dead End
old solution
select next adjacent vertex
31
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Stack Representation
Stack is used to trace the operation of DFS
A D
B
G
F C E
C D
B B B B
A A A A A
Push A Push B Push C Pop C Push D
C – Dead End
Pop Occurs
32
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Solution 1: using Stack (Syllabus)
D
B B F
A A A A A
Pop D Pop B Push F Pop F Pop A
Forms Cycle
Traversal stops F – Dead End
It goes to B then to A Pop Occurs
1 2
3 4 5 6
33
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Depth First Search &
Traversal
Starting vertex
0
Visiting vertex sequence is
0,1,3,7,4,5,2,6
1 2
3 4 5 6
b c d
e f g
h i
34
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
BFS Vs. DFS
S.No. Factors DFS BFS
1 Data Structures Stack Queue
2 Conceptual Builds the Analogous to preorder
tree level by
level
3 Backtracking No uses
35
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Shortest Path Algorithm
Key points to be remembered
From the given weighted Graph, G={V,E,W}, it
is needed to find a path from one vertex to
other vertex such that the sum of weights on
the path is as small as possible. Such path
can be referred as “Shortest Path”
11 15
6
Among those possible paths, “0-2-1”
is the shortest path whose length is “4”
36
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Shortest Path Algorithm
Key points to be remembered
Note: For non-weighted Graph, Shortest path
can be identified based on the path length
37
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Shortest Path Algorithm
Key points to be remembered
Single source shortest path problem
Given as input a graph “G={V,E}” and a
distinguished vertex “s”, Shortest path can
be calculated from “s” to every other vertex
in “G”
38
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
Key points to be remembered
This algorithm is conceived by Dutch
computer scientist “Edsger Dijkstra” in
1959,
Dijkstra Algorithm
Key points to be remembered
Single Source Shortest path problem
39
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
Key points to be remembered
For a given source vertex (node) in the graph,
the algorithm finds the path with lowest cost
(i.e. the shortest path) between that vertex and
every other vertex
Dijkstra Algorithm
Key points to be remembered
Example
if the vertices of the graph represent cities
and edge path costs represent driving
distances between pairs of cities connected
by a direct road, Dijkstra's algorithm can be
used to find the shortest route between one
city and all other cities
40
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
Dindigul Source Vertex
50
67
kujiliumparai
Madurai
35 25
57
karur 50
110
Vedasandur Dijkstra algorithm
Helps to find shortest path
Palani
Dijkstra Algorithm
Problem 1
Find Shortest path using Dijkstra algorithm?
[Use vertex ‘a’ as source]
4
b c
3
5 6
2
a d e
7 4
41
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Solution 1
Dijkstra Algorithm
Dijkstra Algorithm
Note
In general, the algorithm already has identified
the shortest path to i-1 other vertices nearest
to the source
42
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
Problem 2
Find Shortest path using Dijkstra algorithm?
[Use vertex ‘a’ as source]
1
a b
5 2
c 3 d
Dijkstra Algorithm
Problem 3
Find Shortest path using Dijkstra algorithm?
[Use vertex ‘a’ as source]
1
b c
3
4 4 6
a f d
5 5
2
6 8
e
43
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
Problem 4
Find Shortest path using Dijkstra algorithm?
[Use vertex ‘D’ as source]
4
A B
2
15 17
C D E
23
9 5 11
F G
13
Dijkstra Algorithm
function Dijkstra(Graph, source)
for each vertex v in Graph: // Initializations
dist[v] := infinity // Unknown distance function from
//source to v
previous[v] := undefined // Previous node in optimal path
//from source
dist[source] := 0 // Distance from source to source
Q := the set of all nodes in Graph
// All nodes in the graph are unoptimized - thus are in Q
while Q is not empty: // The main loop
u := vertex in Q with smallest dist[ ]
if dist[u] = infinity:
break // all remaining vertices are inaccessible from
source
remove u from Q
44
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Dijkstra Algorithm
for each neighbor v of u: / where v has not yet been removed from Q.
alt := dist[u] + dist_between(u, v)
if alt < dist[v]: // Relax (u,v,a)
dist[v] := alt
previous[v] := u
return previous[ ]
45
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Minimum Spanning Tree
Key points to be remembered
A “Minimum Spanning Tree” of weighted
connected graph is its subtree of the smallest
weight where weights of the tree is defined as
sum of the weights on all its edges
Prims Algorithm
Key points to be remembered
It is a “greedy technique” for constructing a
Minimum Spanning Tree of a weighted
connected Graph.
46
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Greedy Technique
This technique suggests to construct a
solution to a problem thro’ the sequence of
steps each expands a partially constructed
solution until a complete solution is reached
Prims Algorithm
Greedy Technique
Feasible Solution
Possible Solutions of the given problem that
has to satisfy the problem constraints
Locally Optimal
It is the solution that has to be the best
among all the feasible choices available
Irrevocable
Once made, it can not be changed on
subsequent steps of the algorithm
47
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Greedy Technique
The main objective of Prim’s algorithm is to
construct a Minimum Spanning Tree of the
given weighted connected graph
Prims Algorithm
Greedy Technique
There are 2 obstacles faced when you
constructing M.S.T.
Number of subtrees grows exponentially
with the graph size
48
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Greedy Technique
For the best illustration, consider a Graph with
4 vertices namely: a, b, c and d
1
a b
2
5
c 3 d
Prims Algorithm
Greedy Technique
Construct a sub graph / sub tree
1 1 1
a b a b a b
2 2
5 5
c d c d c d
3 3
Fig. 1 Fig. 2 Fig. 3
Total Weight = 6 Total Weight = 8 Total Weight = 9
49
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Greedy Technique
With reference to the definition of Prims
algorithm, it constructs M.S.T. of a given
weighted connected Graph thro’ the sequence
of steps of expanding sub trees
Prims Algorithm
Greedy Technique
On each successive iteration of an algorithm,
tree is expanded by attaching a vertex to a
previously constructed solution closest to the
vertices already in a tree by “Greedy
approach”.
50
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Example 1: Construct M.S.T using Prims
algorithm? [Use ‘0’ as the starting vertex]
0
10 28
5 1
14 16
25 6 2
24
18
12
4 3
22
Prims Algorithm
Solution Remaining Vertices
Tree Illustration
Create a table with 3 columns
Vertices
0(-,-) 1(0,28), 2(-,œ), 5(0,10), 0
3(-,œ), 4(-,œ),6(-,œ)
5
51
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Tree Remaining Vertices Illustration
Vertices
4(5,25) 1(0,28), 2(-,œ), 3(4,22), 0
6(4,24) 5
4 3
3(4,22) 1(0,28), 2(3,12), 6(3,18) 0
5
2
4 3
Prims Algorithm
Tree Remaining Vertices Illustration
Vertices 0
1
2(3,12) 1(2,16), 6(3,18) 5
2
4 3
1(2,16) 6(1,14)
0
1
5
6 2
4 3
52
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Example 2: Construct M.S.T using Prims
algorithm? [Use ‘A’ as the starting vertex]
A
5 2
F 2 B
6 7
25 G 10
14
12
E C
11 15
D
Prims Algorithm
Example 3: Construct M.S.T using Prims
algorithm? [Use ‘a’ as the starting vertex]
1
b c
3 4 6
4
a f d
5 5
2
6 8
e
53
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Prims Algorithm
Pseudo Code: Prim(G)
Prims Algorithm
Pseudo Code: Prim(G)
54
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Kruskal Algorithm
Key points to be remembered
It is an algorithm to construct M.S.T. of the
given Graph with the help of Greedy Approach
thro the Greedy Inclusion of vertex to the
vertices already in the tree
Kruskal Algorithm
Key points to be remembered
This algorithm begins by sorting the given
graph edges in Non-decreasing order based
on their weights.
constraint
55
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Kruskal Algorithm
Key points to be remembered
Consider a Graph “G” that has n>0 vertices
from which exactly n-1 edges will be selected
for inclusion in “T”
Kruskal Algorithm
Problem 1: Construct M.S.T using Kruskal
algorithm?
1
b c
3
4 4 6
a f d
5 5
2
6 8
e
56
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Kruskal Algorithm
Solution 1:
Step 1: Sort the given edges of a Graph in
Non-decreasing order
1
b c
3
4 4 6
a f d
5 5
2
6 8
e
Edge bc ef ab bf cf af df ae cd de
Wt. 1 2 3 4 4 5 5 6 6 8
Kruskal Algorithm
Key points to be remembered
Step 2: Construct M.S.T.
During this process, scan the sorted list
from lower weight value
2 functions has to be implemented
o Include the edge to a Tree if there is no
cycle
57
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Kruskal Algorithm
Key points to be remembered
Kruskal Algorithm
Selected Tree Function Resultant M.S.T
Edge Involved
ab Inclusion b c
a f
b c
bf Inclusion
a f
58
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Selected Tree Function Resultant M.S.T
Edge Involved
b c
cf Inclusion – forms
a cycle – delete a f
the edge
af Inclusion – forms c
b
a cycle – delete
the edge a f
b c
a f d
ae Inclusion – e
forms a cycle
– delete the
edge
59
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Selected Function Resultant M.S.T
Tree Edge Involved
cd Inclusion –
b c
forms a cycle
– delete the a f d
edge
e
de Inclusion –
forms a cycle
– delete the
edge
Kruskal Algorithm
Problem 2: Construct M.S.T using Kruskal
algorithm?
2
b c
4
3 10
1
7
a f d
2
8 4
5 6
b c
1
60
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Kruskal Algorithm
Problem 3: Construct M.S.T using Kruskal
algorithm?
5
a b
2 3
7 6
e
4 5
c d
4
61
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Shortest Path Algorithm
Key points to be remembered
2 ways – shortest path algorithm may be
considered
1. Single source shortest path problem
a) Dijkstra Algorithm
b) Bellman - Ford Algorithm
62
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Bellman – Ford Algorithm
Key points to be remembered
Coined by “Richard Bellman and Lester Ford”
63
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Bellman – Ford Algorithm
Problem 1: Find Shortest path using Bellman
Ford algorithm? [Use vertex ‘A’ as source]
1
A B
5
2 3
2
7
S T
10
-2
3
C D
5
2 3
Graph has 6 vertices
2
7
S T
10
-2
3
C D
Algorithm has 5 iterations
64
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Bellman – Ford Algorithm
Solution:
S A B C D T
Shortest
0
Distance d(v)
Predecessor
Vertex pi(v)
S A B C D T
Shortest
0 5 -2
Distance d(v)
Predecessor
S S
Vertex pi(v)
65
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
S A B C D T
Shortest
0 5 6 -2
Distance d(v)
Predecessor
S A S
Vertex pi(v)
A = adj{B}
u v > 5+1
A B True;
d(B) = 6 from “A”
S A B C D T
Shortest
0 5 6 -2 13 9
Distance d(v)
Predecessor
S A S B B
Vertex pi(v)
u v -2 > 6+2
B C False; Don’t update d(C)
d(C) = -2 from “A”
u v > 6+7
B D true; update d(D)
d(D) = 13 from “B”
u v > 6+3
B T true; update d(T)
d(T) = 9 from “B”
66
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
S A B C D T
Shortest
0 50 6 -2 9
Distance d(v) 113
Predecessor
SC A S BC B
Vertex pi(v)
u v 5 > -2+2
C A True; update d(A)
d(A) = 0 from “C”
u v 13 > -2+3
C D true; update d(D)
d(D) = 1 from “C”
S A B C D T
Shortest
0 50 6 -2 9
Distance d(v) 113
Predecessor
SC A S BC B
Vertex pi(v)
u v 9 > 1+10
D T False; Don’t update d(T)
d(T) = 9 from “B”
Iteration 1 is over
67
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
S A B C D T
Shortest
0 0 6 -2 1 9
Distance d(v)
Predecessor
C A S C B
Vertex pi(v)
S = adj{A, C}
u v
0 > 0+5
S A
False;
d(A) = 0 from “C”
u v
S C -2 > 0-2
Same;
d(C) = -2 from “S”
S A B C D T
Shortest
0 0 61 -2 1 9
Distance d(v)
Predecessor
C A S C B
Vertex pi(v)
A = adj{B}
u v
6 > 0+1
A B
True;
d(B) = 1 from “A”
68
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
S A B C D T
Shortest
0 0 61 -2 1 94
Distance d(v)
Predecessor
C A S C B
Vertex pi(v)
u v -2 > 1+2
B C False; Don’t update d(C)
d(C) = -2 from “S”
u v 1 > 1+7
B D false; don’t update d(D)
d(D) = 1 from “C”
u v 9 > 1+3
B T true; update d(T)
d(T) = 4 from “B”
S A B C D T
Shortest
0 0 1 -2 1 4
Distance d(v)
Predecessor
C A S C B
Vertex pi(v)
u v 0 > -2+2
C A same; Don’t update d(A)
d(A) = 0 from “C”
u v 1 > -2+3
C D same; don’t update d(D)
d(D) = 1 from “C”
69
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
S A B C D T
Shortest
0 0 1 -2 1 4
Distance d(v)
Predecessor
C A S C B
Vertex pi(v)
u v 4 > 1+10
D T false; Don’t update d(T)
d(T) = 4 from “B”
Upto Iteration 5
S A B C D T
Shortest 0 1 -2
0 1 4
Distance d(v)
Predecessor C A S C B
Vertex pi(v)
3
2
S T
-2
3
C D
70
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Bellman – Ford Algorithm
Problem 2: Find Shortest path using Bellman
Ford algorithm? [Use vertex ‘1’ as source]
Connected Components
Key points to be remembered
A graph is said to be connected if for every pair
of its vertices u and V there is a path from u to V
Sub graph
A sub graph of a given graph G=(V,E) is a
graph G’=(V’,E’) such that V’ V and E’ E
71
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Connected Components
Key points to be remembered
A B C A B
C D E C D
Connected Components
Key points to be remembered
A
G F
B C E
H I
72
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Connected Components
Identifying connected components using BFS
BFS can be used to determine whether G is
connected or not
Connected Components
Identifying connected components using BFS
A* is the reflexive transitive closure matrix of a
undirected graph
73
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Connected Components
Example for connected component using
Spanning tree
Find whether the following graph is connected
0
1 2
3 4 5 6
Connected Components
Solution 1(a): Finding connected component using
BFS
Process starts at initial vertex 0, Spanning tree
is empty; t={0}
As per the ideal of BFS, algorithm visits 0 to 1,
now spanning tree, t={(0,1)}
It visits vertex 2, t={(0,1),(0,2)}
Next visits vertex 3, t={(0,1),(0,2),(1,3)}
Next visits vertex 4, t={(0,1),(0,2),(1,3),(1,4)}
Next visits vertex 5, t={(0,1),(0,2),(1,3),(1,4),(2,5)}
Next visits vertex 6, t={(0,1),(0,2),(1,3),(1,4),(2,5)
,(2,6)}
74
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Connected Components
Solution: Finding connected component using BFS
Next visits vertex 7, t={(0,1),(0,2),(1,3),(1,4),(2,5)
,(2,6),(3,7)}
0
Spanning tree of the graph
Using BFS
1 2
3 4 5 6
Connected Components
Solution 1(a): Finding connected component using
DFS
Process starts at initial vertex 0, Spanning tree
is empty; t={0}
As per the idea of DFS, algorithm visits 0 to 1,
now spanning tree, t={(0,1)}
It visits vertex 3, t={(0,1),(1,3)}
Next visits vertex 7, t={(0,1),(1,3),(3,7)}
Next visits vertex 4, t={(0,1),(1,3),(3,7),(7,4)}
Next visits vertex 5, t={(0,1),(1,3),(3,7),(7,4),(7,5)}
Next visits vertex 2,
t={(0,1),(1,3),(3,7),(7,4),(7,5),(5,2)}
75
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Connected Components
Solution: Finding connected component using BFS
Next visits vertex 6,
t={(0,1),(1,3),(3,7),(7,4),(7,5),(5,2),(2,6)}
0
Spanning tree of the graph
Using DFS
1 2
3 4 5 6
Connected Components
Complexity of connected Graph
Space Complexity
Array = n locations
Queue = (n-1) locations
Edges = (n-1) locations
Remaining variables = 2
Total space needed = 3 n locations
Time Complexity
needs O(n+e) for n vertices and e edges of a graph G
76
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Biconnected Components
Key points to be remembered
A graph is said to be bi-connected iff if it
contains no articulation point in a graph
Articulation Point
A vertex V in a connected graph G is an
articulation point iff if the deletion of vertex V
together with all the edges incident to V
disconnects the graph into 2 or more non-
empty components
Biconnected Components
Example-1 for Articulation Point in a graph
6
10 9
77
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Biconnected Components
Example-1 for Articulation Point in a graph
1 6
After deleting vertex 2
4 5
7
3
10 9 8
Biconnected Components
Example-2 for Articulation Point in a graph
5 4
78
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Biconnected Components
Methods to find Articulation Point
Common vertex in Biconnected component
Depth first spanning tree
Lowest depth first number L(u)
3 ways
Biconnected Components
Methods 1: finding Articulation Point using
common vertex in Biconnected component
2 biconnected component can have at most one
vertex in common and this vertex is referred as
“Articulation Point”
79
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Biconnected Components
Methods 1: finding Articulation Point using
common vertex in Biconnected component
1
6
5
4 2
5
2 7
3
8
3 3
Biconnected Components
Methods 1: finding Articulation Point using
common vertex in Biconnected component
From the biconnected components, it is found
that a graph may have vertex 2 is an articulation
point
80
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Biconnected Components
Methods 1: finding Articulation Point using
common vertex in Biconnected component
6
1 5
Result of connecting
4 2 7 biconnected components
3 8
10 9
81
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Branch & Bound Technique
Key points to be remembered
This idea can be strengthened if we deal with
optimization problem where these problems
are associated with 2 terms:
1. Feasible solution
2. Optimal Solution
For Ex.:
Hamiltonian Circuit in the TSP Problem
Subset of items whose weight does not
exceeds the knapsack capacity
82
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Branch & Bound Technique
Key points to be remembered
Optimal Solution
It is a feasible solution with the best value
that maximizes the objective of the given
function
For Ex.:
Shortest Hamiltonian Circuit
Most valuable subset of items that fits
with the knapsack capacity
E- Node
It is a live node whose children are currently
being explored. In other words, an E-node is
a node currently being explored
83
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Branch & Bound Technique
Basic terminology to be remembered
Dead Node
It is a node that is not to be expanded or
explored further. All children of a dead node
have already been explored
A B
C D
84
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]
Branch & Bound Technique
A B
A B
E D
85
Dr.A.Thomas Paul Roy, Professor, CSE Department, PSNACET, Dindigul. Email: [email protected]