Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views61 pages

m3 Aad

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views61 pages

m3 Aad

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

MODULE III

DIVIDE AND CONQUER

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.

Divide and conquer algorithm is having three parts:

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));
}
}

▪ If the given problem is small, return the result


▪ Otherwise, divide the problem into smaller instances P1, P2, ........ Pk
▪ Apply DAndC() to each of these sub-problems.
▪ Finally combine the results of all sub-problems.
2-way Mege Sort
U --> current balance
capacity of knapsack
II I III IV

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.

Here, n=3, e=3


Therefore, number of edges in spanning tree = (n-1) = 2 or
(we remove (e-n+1) edges, ie; 3-3+1 = 1 edges)

Properties of Spanning Tree


• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of edges and
vertices.
• The spanning tree does not have any cycle (loops)
• Removing one edge from the spanning tree will make the graph disconnected,
i.e. the spanning tree is minimally connected
• Adding one edge to the spanning tree will create a circuit or loop, i.e. the
spanning tree is maximally acyclic
• Spanning tree has n-1 edges, where n is the number of nodes.
Application of Spanning Tree
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
• Handwriting Recognition
• Image Segmentation
Maximum number of Spanning Trees of a graph with n nodes
• Complete Graph: nn-2
Other Graphs
1. Create Adjacency Matrix for the given graph.
2. Replace all the diagonal elements with the degree of nodes.
3. Replace all non-diagonal 1‟s with -1.
4. Total number of spanning tree for that graph = Co-factor for any element in that
matrix.

Minimum Spanning Tree (MST)


• In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight than all other spanning trees of the same graph.
• In real-world situations, this weight can be measured as distance, congestion,
traffic load or any arbitrary value denoted to the edges.
Application of Spanning Tree
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
• Handwriting Recognition
• Image Segmentation

Qn) Let (u,v) be a minimum-weight edge in a graph G. Show that (u,v)


belongs to some minimum spanning tree of G.
• Suppose that T is a Minimum Spanning Tree, which does not include the
smallest edge, E.
• Add E to T. Now a circle C is formed.
• This graph will remains connected if an edge is removed from the circle C.
• So remove an edge E‟(except E) from C which also belongs to T
• This operation would result a new spanning tree whose weight is <= weight of T.
• We have a contradiction. Hence, proved.
Qn) Let G be a weighted undirected graph with distinct positive edge weights. If
every edge weight is increased by same value, will the minimum cost spanning tree
change. Justify your answer

• The Minimum Spanning Tree doesn’t change.


• In Kruskal’s algorithm, we will sort the edges first.
• If we increase all weights, then order of edges won’t change.
• So, MST does not change.

Minimum Spanning-Tree Algorithms


• Prim’s Algorithm
• Kruskal's Algorithm
PRIM’S ALGORITHM
• It is a greedy algorithm that finds MST for a connected weighted undirected graph.
• It finds the subset of edges that form a tree that includes every vertex where total weight
of all the edges in a tree is minimized.
STEPS
1. Initialise the MST with a vertex chosen at random.
2. Fina all the edges that connect the tree to new vertices. Find the minimum and add it to
the tree unless it forms a cycle.
3. Keep repeating step 2 until we get a MST.

Complexity of Prim’s algorithm- O(n2)


E is the set of edges and n is the number of vertices in G.
cost[u,v] is the cost of edge (u, v).
t is the set of edges in the minimum cost spanning tree.
The final cost is returned

• Heapify() is used to construct a minheap based on the edge cost of G.


• Adjust() is used to reconstruct a minheap if there is a deletion occurs.
• Initially all vertices are belongs to different sets. Find() returns the set number of
that particular vertex. j and k are the set number of vertex u and v respectively.
• If j=k means vertex u and v are belongs to the same set. Inclusion of (u, v)
should definitly form a cycle. So discard it.
• If j≠k means vertex u and v are belongs to different set. Inclusion of (u, v) will not
form a cycle. So add it to the minimum spanning tree edge list.
• Finally there are n-1 edges, then retrun it. Otherwise there is no spanning tree.

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|).

Difference between Prim’s & Kruskal’s Algorithm


EDGE WEIGHT RESULT
............................................
(0,3) 5 added
(2,4) 5 added
(3,5) 6 added
(0,1) 7 added
(1,4) 7 added
(1,2) 8 discarded
(4,5) 8 discarded
(1,3) 9 discarded
(4,6) 9 added
(5,6) 11 discarded
(3,4) 15 discarded
Shortest Path Algorithms

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.

• There may be different paths from s to t.


• Let shortest path(Path-1) be of cost 15 and has 5 edges.
• Let there be another path(Path-2) of cost 25 and has 2 edges.
• All edge costs are increased by 10.
• Path-1 cost is increased by 5*10 and becomes 15 + 50=65.
• Path-2 cost is increased by 2*10 and becomes 25 + 20=45
• Now Path-1 cost > Path-2 cost
• So the shortest path may change.

From s to t, there are two paths:


Path 1: (s-a-b-c-d-t) and
Path 2: (s-e-t).
Cost of s-a-b-c-d-t is 15 and
Cost of s-e-t =25
Therefore, Shortest path is Path 1: s-a-b-c-d-t and shortest path cost is 15
Now, all edge costs are increased by 10.

• Path-1 cost is increased by 5*10 and (s-a-b-c-d-t) becomes 15 + 50=65.


• Path-2 cost is increased by 2*10 and (s-e-t) becomes 25 + 20=45
Now path 2 is shortest.
Therefore, if every edge weight is increased by same value, the shortest path between any pair
of vertices may change.
Qn 2) In a weighted graph, assume that the shortest path from a source ‘s’ to a destination ‘t’ is
correctly calculated using a shortest path algorithm. Is the following statement true? If we
increase weight of every edge by 1, the shortest path always remains same. Justify your answer
with proper example.

From s to t, there are two paths:


Path 1: (s-a-b-c-d-t) and
Path 2: (s-e-t).
Cost of s-a-b-c-d-t is 5 and
Cost of s-e-t =6
Therefore, Shortest path is Path 1: (s-a-b-c-d-t) and shortest path cost is 5
Now, all edge costs are increased by 1

• Path-1 (s-a-b-c-d-t) cost is increased by 5*1 and becomes 10


• Path-2 (s-e-t) cost is increased by 2*1 and becomes 8
Now path 2 is shortest
Therefore, if every edge weight is increased by1, the shortest path between any pair of
vertices may change.
Single Source Shortest Path Problem
• Given a connected weighted graph G=(V,E), find the shortest path from a
given source vertex s to every other vertices (V-{s}) in the graph.
• The weight of any path(w(p)) is the sum of the weights of its constituted edges.
• The weight of the shortest path from u to v = min{w(p): p is a path from u to v}

Single Source Shortest Path Algorithms are:


▪ Dijkstra’s Algorithm
▪ Bellman Ford Algorithm

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 ∞ ∞ ∞ ∞ ∞ ∞ ∞

b) Update the labels of B & G with distance & path.


Visited A B C D E F G H
{A} 0 2 ∞ ∞ ∞ ∞ 6 ∞

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 ∞

Now move to node G (as the distance of G is smaller). Mark an arrow at G

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

Now move to node F (as the distance of F is smaller). Mark an arrow at F


f) Check the adjacent nodes of F ie; C & H 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 ∞
{A,B,E,G} 0 2 9 ∞ 4 6 5 9
{A,B,E,G,F} 0 2 9 ∞ 4 6 5 8

Now move to node H (as the distance of H is smaller). Mark an arrow at H

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

Now move to node C (as the distance of C is smaller). Mark an arrow at C


h) Check the adjacent nodes of C ie; D & F. But F is already visited, so update label
of D (no change as 10 is shortest).
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
{A,B,E,G,F,H,C} 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.

Check the adjacent nodes of D ie; E, A, C.


A & C are visited nodes, so update the labels of E only with distance & path.
Visited A B C D E
{A} 0 4 ∞ 8 ∞
{A,B} 0 4 7 8 ∞
{A,B,C} 0 4 7 8 ∞
{A,B,C,D} 0 4 7 8 15

Therefore, shortest path is


Qn) Find the shortest path from s to all other vertices in the following graph using
Dijkstra’s Algorithm

Ans)

Qn)

Ans)

apsara
Da te:_. _. _

( ~ .
;r ~ , \.
-
, , I"
~

~ (
r I •

v
c.. D ·G F-
,, I •·
~~· (i(J
I

~ ~
pO r· od ~

1 ,<J p(J r-::J 0 II()


j r,,(J
'1 pO ~ (70 () oO
3 (?()
~ 1
I ..
e,<!) h ~
r
-~ "'
() oi)
..
s- ,;
0

'
• 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
·, . \.

,·. \
(_
'· '
\~
'' !

"
•!

You might also like