m3 Aad
m3 Aad
Top-down approach for problem solving. Designs an algorithm that divides the problem into small subproblems.
Solutions of subproblems are easier to find and then combines the partial solutions to get the original solution.
1. Divide the problem into a number of sub-problems that are smaller instances of the same
problem.
2. Conquer the sub-problems by solving them recursively. If they are small enough, solve
the sub-problems as base cases.
3. Combine the solutions to the sub-problems into the solution for the original problem.
o Control Abstraction: It is a procedure whose flow of control is clear but whose primary
operations are specified by other procedure whose precise meanings are left undefined.
o Control Abstraction: Divide and Conquer
Algorithm DAndC(P)
{
if Small(P) then
return S(P)
else
{
Divide P into smaller instances P1, P2, . . . . Pk, k≥1;
apply DAndC to each of these sub-problems;
return Combine(DAndC(P1), DAndC(P2), ......... , DAndC(Pk));
}
}
fraction of
weight of
object to knapsack balance
be placed capacity
sort solution vector as per i (1 to 4)
sort solution vector as per i (1 to7)
Qn) Find the optimal solution for knapsack problem where m=25, n=3, P-
(25,24,17), w=(16,14,9)
Ans) 44.125
Spanning Trees
A spanning tree is a subset of undirected connected Graph G=(V,E), which has all the
vertices covered with minimum possible number of edges.
It does not have cycles and cannot be disconnected.
To construct a spanning tree,
• From a complete graph - removing max(e-n+1) edges,
where e = no. of edges, n= no. of vertices
• Spanning tree will have (n-1) edges.
Complexity
• The edges are maintained as a minheap, then the next edge to consider can be
obtained in O(log |E|) time.
• Construction of heap itself takes O(|E|) time.
• Overall complexity of Kruskal‟s algorithm is O(|E| log|E|).
The shortest path problem is the problem of finding a path between two vertices in a graph
such that the sum of the weights of its constituent edges is minimized.
Different shortest path problems are:
(1) Single Source Shortest Path Problem: To find shortest paths from a source vertex to all other
vertices in the directed graph
(2) Single Destination Shortest Path Problem: To find shortest paths from all
vertices in the directed graph to a single destination vertex v
(3) All Pairs Shortest Path Problem: To find shortest paths between every pair of
vertices in the graph
Qn 1) Let G be a weighted undirected graph with distinct positive edge weights. If every edge
weight is increased by same value, will the shortest path between any pair of vertices change.
Justify your answer
The shortest path may change.
Dijkstra’s Algorithm
Given a graph and a source vertex S in graph, find shortest paths from S to all vertices in the
given graph.
Algorithm Dijkstra(G,W, S)
1. For each vertex v in G
distance[v] = infinity
previous[v] = Null
2. distance[S] = 0
3. Q = set of vertices of graph G
4. While Q is not empty
u = vertex in Q with minimum distance
remove u from Q
for each neighbor v of u which is still in Q
alt = distance[u] + W(u,v)
if alt < distance[v]
distance[v] = alt
previous[v] = u
5. Return distance[], previous[]
Complexity
• The complexity mainly depends on the implementation of Q
• The simplest version of Dijkstra's algorithm stores the vertex set Q as an ordinary
linked list or array, and extract-minimum is simply a linear search through all vertices
in Q. In this case, the running time is O(E + V2) = O(V2)
• Graph represented using adjacency list can be reduced to O(E log V) with the help of
binary heap.
1. Find the shortest path from node A to all nodes using Dijkstra’s shortest path algorithm
Ans)
a) Start from node A. Check the adjacent nodes of A ie; B & G.
Visited A B C D E F G H
Initially 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
Now move to node B (as the distance of B is smaller when compared to the all other
distances). Mark an arrow at B
c) Check the adjacent nodes of B ie; C & E and update its labels
Visited A B C D E F G H
{A} 0 2 ∞ ∞ ∞ ∞ 6 ∞
{A,B} 0 2 9 ∞ 4 ∞ 6 ∞
Now move to node E (as the distance of E is smaller). Mark an arrow at E
d) Check the adjacent nodes of E ie; F & G and update its labels
Visited A B C D E F G H
{A} 0 2 ∞ ∞ ∞ ∞ 6 ∞
{A,B} 0 2 9 ∞ 4 ∞ 6 ∞
{A,B,E} 0 2 9 ∞ 4 6 5 ∞
e) Check the adjacent nodes of G ie; H & E. But E is already visited, so update label
of H.
Visited A B C D E F G H
{A} 0 2 ∞ ∞ ∞ ∞ 6 ∞
{A,B} 0 2 9 ∞ 4 ∞ 6 ∞
{A,B,E} 0 2 9 ∞ 4 6 5 ∞
{A,B,E,G} 0 2 9 ∞ 4 6 5 9
g) Check the adjacent nodes of H ie; G, D & F. But G, F are already visited, so update
label of D.
Visited A B C D E F G H
{A} 0 2 ∞ ∞ ∞ ∞ 6 ∞
{A,B} 0 2 9 ∞ 4 ∞ 6 ∞
{A,B,E} 0 2 9 ∞ 4 6 5 ∞
{A,B,E,G} 0 2 9 ∞ 4 6 5 9
{A,B,E,G,F} 0 2 9 ∞ 4 6 5 8
{A,B,E,G,F,H} 0 2 9 10 4 6 5 8
Thus all nodes are visited from A & the shortest route is
2. Find the shortest path from node A to all nodes using Dijkstra’s shortest path algorithm
Ans) Start from node A.
Visited A B C D E
Initially 0 ∞ ∞ ∞ ∞
Check the adjacent nodes of A ie; B & D. Update the labels of B & D with distance
& path.
Visited A B C D E
{A} 0 4 ∞ 8 ∞
Now move to node B (as the distance of B is smaller when compared to the all other
distances). Mark an arrow at B.
Check the adjacent nodes of B ie; C & A. A is the root node so update the labels of
C only with distance & path.
Visited A B C D E
{A} 0 4 ∞ 8 ∞
{A,B} 0 4 7 8 ∞
Now move to node C (as the distance of C is smaller when compared to the all other
distances). Mark an arrow at C.
Check the adjacent nodes of C ie; B & D. B is visited node, so update the labels of D only with
distance & path. But D’s previous cost 8 which is smaller to present cost 11 from C, so no need
to update cost of D.
Visited A B C D E
{A} 0 4 ∞ 8 ∞
{A,B} 0 4 7 8 ∞
{A,B,C} 0 4 7 8 ∞
Now move to node D (as the distance of C is smaller when compared to the all other distances).
Mark an arrow at D.
Ans)
Qn)
Ans)
◄
apsara
Da te:_. _. _
( ~ .
;r ~ , \.
-
, , I"
~
~ (
r I •
v
c.. D ·G F-
,, I •·
~~· (i(J
I
~ ~
pO r· od ~
'
• I ,tJ 0 o()
-:-
~ 7, 6 (X) 8" 0 IO
I
~ 71., '- ·;i '
, ~ 0 10
.3 ~
s-
~-
...,
1
'~
&
·" fl,-
r 2..
B'
~
0
0
(u
[O
s- -, 10
C b 1 Dvi, 3
I
' ' I '
12--
.. (
0
r
,. .
apsara
Date:_.__ _
- 1 ,. 7
:J
5
J ...
apsQrQ
Date :_·-·-.:_
l ~ )
.2.. Date: _ -__ _
. ----
I • •
' l
,' {_,_-··I I\ i
' ,
...,
apsorQ
Date:_.
--~--- ---- -·----..
t 1
2-
V
----- I l
-----
f (,· l
--- \ :' i
.,
,-'
' ...,, ~
4 \
\
'""' _,
- - ·---- "' '3 ........
'
s-
6-t)
·- \
s -f-
/"
\;:'
,~
5 - A- c- f: -(Ia . >
10
.,
j i ~ 'I I
·, . \.
,·. \
(_
'· '
\~
'' !
"
•!