Data Structures and Algorithms: B.Tech (CSE) Module-4
Data Structures and Algorithms: B.Tech (CSE) Module-4
B.Tech (CSE)
MODULE-4
ALGORITHM DESIGN PARADIGMS
Dr.B.K.Tripathy
Senior Professor
School of Computer Science and Engineering
VIT University, Vellore-632014
Tamil Nadu, India
DIVIDE AND CONQUER ALGORITHMS
•• The
previous algorithm is of complexity
• A simple divide and conquer algorithm:
• We assume that the size of the matrices is an exact power of
2
• Let
• So, as we have C = A x B,
• We have the following
SQUARE-MATRIX-MULTIPLY-RECURSIVE (A, B)
• STEP 1: n = A. rows
• STEP 2: let C be a new n x n matrix
• STEP 3: If n == 1
• STEP 4:
• STEP 5: else partition A, B and C as in the above
Step-6
equations
• STEP 6- 9: Step-7
Step-8
Step-9
SQUARE-MATRIX-MULTIPLY-RECURSIVE (A, B)
•• STEP
10: Return C
• A query: How do we partition the matrices?
• Solution: If we create 12 (3 x 4 each) new n/2 x n/2 matrices
we would require time copying entries.
We use index calculations
• We identify a sub-matrix by a range of row indices and a
range of column indices of the original matrix
• The advantage is that we can compute STEP 5 in time
• If we represent by T(n) the time to multiply two matrices
using this procedure then
SQUARE-MATRIX-MULTIPLY-RECURSIVE (A, B)
•• When
n = 1, T(1) =
• For n > 1,
•• This
method is to make the recursion tree slightly bushy
• Instead of performing eight recursive multiplications on (n/2) x
(n/2) matrices it performs seven
• The cost of eliminating one matrix multiplication will be several
new additions of (n/2) x (n/2) matrices
• The four steps:
• STEP 1: Divide the input matrices A and B and output matrix C
into (n/2) x (n/2) submatrices. This takes time by index calculation
• STEP 2: Create 10 matrices each of which is (n/2) x (n/2) and is
the sum or difference of two matrices created in step 1. We can
create all 10 matrices in time
STRASSEN’S METHOD
•• STEP
3: Using the submatrices created in step 1 and the 10
submatrices created in step 2, recursively compute seven
matrix products . Each matrix is (n/2) x (n/2)
•• The
10 matrices are defined as follows
•• The
product matrices are given by
STRASSEN’S METHOD
•
• Example:
• We have
• C=
• It matches with the direct computation of the multiplication of the
matrices
•
HEAP SORT
• BUILD-MAX-HEAP is used to build a max heap on the input
array A[1..n]. n = A.length
• Since the maximum element occurs at the root position, we
put it at the position A[n] of the array by interchanging A[1]
with A[n]
• The node ‘n’ is discarded from the heap by decrementing its
size from n to n-1
• Now the max-heap property may be violated after the
interchange
• So, we call MAX-HEAPIFY(A, 1)
HEAP SORT CONTD…
• HEAPSORT (A)
• STEP 1: BUILD-MAX-HEAP (A)
• STEP 2: For I = A.length to 2
• STEP 3: exchange A[1] with A[n]
• STEP 4: A.heap-size = A.heap-size-1
• STEP 5: MAX-HEAPIFY (A, 1)
HEAPSORT- EXAMPLE
• MAX-HEAPIFY(A)
16
10
14
8 7 9 3
2 4 1
EXAMPLE CONTD…
A[1]
14
10
8
4 7 9 3
2 1 A[9]
EXAMPLE CONTD…
8 9
4 7 1 3
2 A[8]
EXAMPLE CONTD…
8 3
4 7 1 2
EXAMPLE CONTD…
3
7
4 2 1
EXAMPLE CONTD…
4 3
1 2
EXAMPLE CONTD…
1
• Exchange A[1] and A[4]. A[4] = 4
• heap-size = heap-size – 1 = 3
3
• MAX-HEAPIFY (A, 1)
2 1
EXAMPLE CONTD…
• SELECTION SORT:
• the entire given list of n elements is scanned to find its
smallest element and exchange it with the first element.
• Thus, the smallest element is moved to its final position in the
sorted list.
• Then, the list is scanned again, starting with the second
element in order to find the smallest element among the n –
1 and exchange it with the second element.
• The second smallest element is put in its final position in the
sorted list. After n-1 passes, the list is sorted.
SELECTION SORT
• ALGORITHM SELECTION-SORT (A[0..n-1])
• STEP 1: for i ← 0 to n-2
• STEP 2: do min ← i
• STEP 3: for j ← i + 1 to n-1
• STEP 4: do if A[j] < A[min]
• STEP 5: min ← j
• STEP 6: swap A[i] and A[min]
5 3 8 4 7 1 3 9 11 14
•• It
is a problem of scheduling several competing activities that
require exclusive use of a common resource with a goal of
selecting a maximum-size set of mutually compatible
activities
• S = be a set of proposed activities
• They wish to use a common resource
• Each activity has a start time and a finish time
• We have
• If selected the activity takes place during the time interval
THE ACTIVITY-SELECTION PROBLEM
•• Two
activities are said to be compatible if the intervals and
do not overlap
• Equivalently, are said to be compatible if
• We assume that the activities are sorted in monotonically
increasing order of finish time; i. e.
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 9 9 10 11 12 14 16
THE ACTIVITY-SELECTION PROBLEM
•• In this example
The subset is a subset of mutually compatible activities
•
• The subset is a largest subset of mutually compatible
activities
• Another such subset is
THE OPTIMAL SUBSTRUCTURE OF THE
ACTIVITY-SELECTION PROBLEM
•• Let
be the set of activities that start after activity finishes
and finish before activity starts
• Suppose we want to find a maximum set of mutually
compatible activities in
• Let is such a subset which includes some such activity
• This generates two sub-problems
• 1. Finding the mutually compatible activities in the set
• 2. Finding the mutually compatible activities in the set
• Let
• So, contains the activities in that finish before starts
• contains the activities in that start after finishes
THE OPTIMAL SUBSTRUCTURE OF THE
ACTIVITY-SELECTION PROBLEM
•• Thus
we have the relation
• So, the maximum size set of mutually compatible activities
in consists of activities
• So, the optimal solution must also include optimal solutions to
the two sub-problems for
• A greedy choice will help us
• That is we will choose an activity that leaves the resource
available for as many other activities as possible
GREEDY CHOICE
•• Of
the activities we end by choosing one of the activities that
finishes first
• So, our intuition tells us to choose the activity in S having the
earliest finish time
• This will leave the resource available for as many activities
that follow it
• If more than one activities have the earliest finish time, we
choose any one of them
• Now, since the activities are sorted in the increasing order of
their finish time, will be our greedy choice
GREEDY CHOICE
•• After
making the greedy choice, we have only one remaining
sub-problem to solve:
• Finding the activities that start after finishes
• Let , which is the set of activities that start after finishes
• So, after selecting (greedy choice), we have only to solve
• So, optimal substructure tells us that if is in the optimal
solution then an optimal solution to the original problem
consists of activity and all the activities in an optimal
solution to the sub-problem
A RECURSIVE GREEDY ALGORITHM
•• Inputs:
(s, f, k, n)
• s and f as arrays containing the start and finish times of all the
activities,
• k is the sub-problem number to be solved,
• n is the size of the original problem
•• The
initial call is RECURSIVE-ACTIVITY-SELECTOR (s, f, 0, n)
• RECURSIVE-ACTIVITY-SELECTOR (s, f, k, n)
• m = k+1 So long as the start time of the next
• While and s[m] < f[k] activities are before the kth activity
• m = m+1 finishes increase m. As all these
activities are not compatible with the
• If kth activity.
• Return
• Else return
EXPLANATIONS
•• The
while loop in steps 2 and 3 looks for the first activity in to
finish
• The loop examines , until it finds the first activity that is
compatible with
• Such an activity has
• After finding such an activity it makes a recursive call with the
new position m to
• RECURSIVE-ACTIVITY-SELECTOR(s, f, m, n)
ILLUSTRATION
k sk fk
0 ---- 0 a0
1 1 4 a0 a1
2 3 5 a2
3 0 6 a3
4 5 7 a4
5 3 9 a5
6 5 9 a6
7 6 10 a7
8 8 11 a8
9 8 12 a9
10 2 14 a10
11 12 16 a11
A ITERATIVE GREEDY ALGORITHM
•• It
is an iterative version of the procedure RECURSIVE-ACTIVITY-
SELECTOR
• It also assumes that the input activities are ordered by
monotonically increasing finish time.
• It collects selected activities into a set A and returns this set
when it is done
• GREEDY-ACTIVITY_SELECTOR (s, f)
• n = s. length, A = k =1
• For m = 2 to n
For the first activity that has the start time is
• If s[m] f[k]
greater than the finish time of the kth activity
• A= add it to A. Set k to m and proceed further.
• k=m
• Return A
WORKING PRINCIPLE
•• The
variable k indexes the most recent addition to A,
corresponding to the activity in the recursive version
• Since we consider the activities in order of monotonically
increasing finish time, is always the maximum finish time of
any activity in A.
• First we select activity . Initialize A and k is this index
• The for loop finds the earliest activity in (the set of activities
which start after is to finish)
• The loop considers each activity in turn and adds to A if it is
compatible with all previously selected activities
• Such an activity is the earliest in to finish
WORKING PRINCIPLE
•• To
see whether activity is compatible with every activity
currently in A, it is sufficient to check that its start time is not
earlier than the finish time of the activity most recently
added to A
• If activity is compatible then this activity is added to A and we
set k = m
• The set A is returned finally
DYNAMIC PROGRAMMING
•• Suppose
an enterprise buys long steel rods and cuts them
into shorter rods
• Then it sells shorter rods
• The cutting process is free
• So, the enterprise wants to know the best way to cut the
rods
• Suppose, the enterprise knows the price of a rod of length i,
i = 1, 2, …n
• It wants to find the maximum revenue obtainable by cutting
up the rod and selling the pieces
ROD CUTTING
9 1 8 5 5
8 1 1 1 5 1 5 1
5 1 1 1 1 1 1
• A sample price table for rods: (Known beforehand)
i 1 2 3 4 5 6 7 8 9 10
pi 1 5 8 9 10 17 17 20 24 30
ROD CUTTING
•• The
revenue function values for in terms of optimal revenues
from shorter rods is given by
•• Since
to find the optimal structure of the original problem,
we depend on finding the optimal structure of the two sub-
problems and so on, we say that rod-cutting problem exhibits
optimal substructure property
• A Simpler Version: (recursive structure for the rod-cutting
problem)
• We view the decomposition as consisting of a first piece of
length i cut off from the left end and a right hand remainder
of length n-i
• Then we cut only the right hand remainder part but not the
left hand one
• The formula now becomes
ROD CUTTING ALGORITHM (RECURSIVE)
•
• The procedure by using the previous equation in a
topdown, straightforward recursive approach is:
3 2 1 0
2 1 0 1 0
0
1 0 0 P(4) calls P(3), P(2), P(1) and P(0)
0 P(3) calls P(2), P(1) and P(0)
P(2) calls P(1) and P(0)
0
P(1) calls P(0)
ROD CUT PROBLEM CONTD…
•• CUT-ROD(p,
n) calls CUT-ROD(p, n-i) for i = 1, 2, …n
• Or
• CUT-ROD(p, n) calls CUT-ROD(p, j) for each j = 0, 1, 2, …n-1
• At the end of the process the amount of work done becomes
exponential
• If T(n) is the total number of calls made to CUT-ROD then
• T(n) = 1 + ‘1’ corresponds to the root
• It can be seen that T(n) = T(1) = 1+ T(0) = 2; T(2) = 1+ T(0)+T(1) = 2 T(1) =
2x2; T(3) = 1+ T(0)+T(1)+T(2) = 2 T(2) = 2x2x2= 2
• This algorithm can be converted pow3. into an efficient one by
using dynamic programming
Suppose it is true for n = k. Then for n = k+1,
T(k+1) = 1 + T(0)+T(1)+…T(k-1) + T(k) = 2 T(k) =
2x 2 pow (k) = 2 pow (k+1) . So, by induction we
get T(n) = 2 pow n
THE KNAPSACK PROBLEM
•• Both
the Dynamic programming and Greedy strategies exploit
substructure
• But, the problem is that one may be tempted to generate a
dynamic programming solution when greedy solution is
sufficient
• May be conversely
• The 0 -1 knapsack problem:
• A thief robbing a store finds n items
• The item is worth rupees and weighs kg.s
• Here, and are integers
THE KNAPSACK PROBLEM
•• Both
the knapsack problems exhibit the optimal-substructure
property
• For the 0 – 1 knapsack problem:
• First take the most valuable item with weight less than W kgs
• Remove this item j from the list
• Then from the remaining n-1 items select the one most valuable
with weight less than W-
• Continue like this
• Fractional knapsack problem:
• Here, if we remove weight w of an item j from the optimal load,
the remaining load must be the most valuable load weighing at
most W-w that the thief can take from the n-1 original items plus
the – w kg.s of item j
SOLUTIONS TO THE KNAPSACK PROBLEMS
•• Although
the problems are similar, we can solve the
fractional knapsack problem by a greedy strategy but we
cannot do so for the 0-1 knapsack problem
• Example: (Fractional Version)
• To solve the fractional problem we compute the value per kg
for each item i.
• Following the greedy strategy the thief can take as much as
he can from the item having the greatest value per kg.
• If the supply of that item is over and he can carry more, then
he can take as much as he can from the next item having
greatest value per kg.
SOLUTIONS TO THE KNAPSACK PROBLEMS
Rs.80
30
30 Rs.120
20 Rs.100
10 Rs.60
10 Rs.60
Total Rs.180
Total Rs.240
• The thief must select a subset of the three items shown whose weight
must not exceed 50 pounds
• The optimal subset includes items 2 and 3
• Any solution with item 1 is suboptimal even though item 1 has the
greatest value per pound
• For the fractional knapsack problem, taking the items in order of
greatest value per pound yields an optimal solution
A COMPARISON
•• Study
of Boolean functions generally is concerned with the
set of truth assignments (assignments of 0 or 1 to each of the
variables) that make the function true
• For example:
• if an instance of SAT contains the clause (x1 ∨ x2), then all
assignments with x1 = x2 = 0 (i.e., false) can be instantly
eliminated.
• This is one of the four possible cases { (0, 0), (0, 1), (1, 0), (1,
1)}
BACK TRACKING
•• Consider
the Boolean formula (w, x, y, z) specified by the
set of clauses
• (w ∨ x ∨ y ∨ z), , ), , ,
• Backtracking shows that the formula is not satisfied
W=0 W=1
( x y z ),( x ),( x y ),( y z ) ( x y ),( y z ),( z ),( z )
x=0 x=1 z=0 z=1
( y z ),( y ),( y z ) ( y z) x y x
( y𝑦( y ))
y= 0 y= 1
( z ),( z ) ()
z=0 z=1
() ()
BACK TRACKING
•• In
the case of Boolean satisfiability, each node of the search
tree can be described either by a partial assignment or by the
clauses that remain when those values are plugged into the
original formula.
• For instance,
• If w = 0 and x = 0 then any clause with or is instantly satisfied
and
• Any literal w or x is not satisfied and can be removed
• The “empty clause” ( ) ruling out satisfiability
• Thus the nodes of the search tree, representing partial
assignments, are themselves SAT sub-problems
EIGHT QUEENS PROBLEM
•• Franz
Nauck published the first solutions in 1850
• Nauck also extended the puzzle to the n queens problem,
with n queens on a chess board of n × n squares
• In 1972, Edsger Dijkstra used this problem to illustrate the
power of what he called structured programming
• He published a highly detailed description of a depth-first
backtracking algorithm
• The Eight Queen problem can be quite computationally
expensive, as there are 4,426,165,368 () possible
arrangements of eight queens on an 8×8 board, but only 92
solutions
EIGHT QUEENS PROBLEM- A SOLUTION
• a
Q
EIGHT QUEENS PROBLEM