Design and Analysis of Algorithms Notes 2 - TutorialsDuniya
Design and Analysis of Algorithms Notes 2 - TutorialsDuniya
COM
Chapter
1
Basic Concepts
Algorithm
m
An Algorithm is a finite sequence of instructions, each of which has a clear meaning
and can be performed with a finite amount of effort in a finite length of time. No
matter what the input values may be, an algorithm terminates after executing a finite
number of instructions. In addition every algorithm must satisfy the following
co
criteria:
Input: there are zero or more quantities, which are externally supplied;
a.
Output: at least one quantity is produced;
iy
Finiteness: if we trace out the instructions of an algorithm, then for all cases
the algorithm will terminate after a finite number of steps;
un
Effectiveness: every instruction must be sufficiently basic that it can in
principle be carried out by a person using only pencil and paper. It is not
enough that each operation be definite, but it must also be feasible.
sD
Performance of a program:
to
The performance of a program is the amount of computer memory and time needed
to run a program. We use two approaches to determine the performance of a
program. One is analytical, and the other experimental. In performance analysis we
Tu
Time Complexity:
The limiting behavior of the complexity as size increases is called the asymptotic time
complexity. It is the asymptotic complexity of an algorithm, which ultimately
determines the size of problems that can be solved by the algorithm.
1
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Space Complexity:
Instruction space: Instruction space is the space needed to store the compiled
version of the program instructions.
Data space: Data space is the space needed to store all constant and variable
values. Data space has two components:
m
Space needed by dynamically allocated objects such as arrays and class
instances.
co
Environment stack space: The environment stack is used to save information
needed to resume execution of partially completed functions.
a.
factors such as:
The three basic design goals that one should strive for in a program are:
sD
Classification of Algorithms
If „n‟ is the number of data items to be processed or degree of polynomial or the size
of the file to be sorted or searched or the number of nodes in a graph etc.
Tu
Log n When the running time of a program is logarithmic, the program gets
slightly slower as n grows. This running time commonly occurs in
programs that solve a big problem by transforming it into a smaller
problem, cutting the size by some constant fraction., When n is a million,
log n is a doubled. Whenever n doubles, log n increases by a constant,
but log n does not double until n increases to n2.
2
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
n When the running time of a program is linear, it is generally the case that
a small amount of processing is done on each input element. This is the
optimal situation for an algorithm that must process n inputs.
n. log n This running time arises for algorithms that solve a problem by breaking
it up into smaller sub-problems, solving then independently, and then
combining the solutions. When n doubles, the running time more than
doubles.
m
nested loop) whenever n doubles, the running time increases four fold.
co
triple–nested loop) has a cubic running time and is practical for use only
on small problems. Whenever n doubles, the running time increases eight
fold.
a.
2n Few algorithms with exponential running time are likely to be appropriate
for practical use, such algorithms arise naturally as “brute–force”
solutions to problems. Whenever n doubles, the running time squares.
Complexity of Algorithms
iy
un
The complexity of an algorithm M is the function f(n) which gives the running time
and/or storage space requirement of the algorithm in terms of the size „n‟ of the
input data. Mostly, the storage space required by an algorithm is simply a multiple of
the data size „n‟. Complexity shall refer to the running time of the algorithm.
sD
The function f(n), gives the running time of an algorithm, depends not only on the
size „n‟ of the input data but also on the particular data. The complexity function f(n)
for certain cases are:
al
1. Best Case : The minimum possible value of f(n) is called the best case.
3. Worst Case : The maximum value of f(n) for any key possible input.
to
3
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Rate of Growth:
The following notations are commonly use notations in performance analysis and
used to characterize the complexity of an algorithm:
1. Big–OH (O) 1,
2. Big–OMEGA (),
3. Big–THETA () and
4. Little–OH (o)
m
f(n) = O(g(n)), (pronounced order of or big oh), says that the growth rate of f(n) is
less than or equal (<) that of g(n).
co
a.
iy
un
sD
f(n) = (g(n)) (pronounced omega), says that the growth rate of f(n) is greater
than or equal to (>) that of g(n).
al
ri
to
Tu
1
In 1892, P. Bachmann invented a notation for characterizing the asymptotic behavior of
functions. His invention has come to be known as big oh notation.
4
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
m
co
Little–OH (o)
a.
T(n) = o(p(n)) (pronounced little oh), says that the growth rate of T(n) is less than
the growth rate of p(n) [if T(n) = O(p(n)) and T(n) (p(n))].
Analyzing Algorithms iy
un
Suppose „M‟ is an algorithm, and suppose „n‟ is the size of the input data. Clearly the
complexity f(n) of M increases as n increases. It is usually the rate of increase of f(n)
we want to examine. This is usually done by comparing f(n) with some standard
sD
O(1), O(log2 n), O(n), O(n. log2 n), O(n2), O(n3), O(2n), n! and nn
The execution time for six of the typical functions is given below:
ri
n log2 n n*log2n n2 n3 2n
1 0 0 1 1 2
to
2 1 2 4 8 4
4 2 8 16 64 16
Tu
8 3 24 64 512 256
16 4 64 256 4096 65,536
32 5 160 1024 32,768 4,294,967,296
64 6 384 4096 2,62,144 Note 1
128 7 896 16,384 2,097,152 Note 2
256 8 2048 65,536 1,677,216 ????????
5
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Note 2: The value here is about 500 billion times the age of the universe in
nanoseconds, assuming a universe age of 20 billion years.
Graph of log n, n, n log n, n2, n3, 2n, n! and nn
m
co
a.
O(log n) does not depend on the base of the logarithm. To simplify the analysis, the
convention will not have any particular units of time. Thus we throw away leading
iy
constants. We will also throw away low–order terms while computing a Big–Oh
running time. Since Big-Oh is an upper bound, the answer provided is a guarantee
that the program will terminate within a certain time period. The program may stop
un
earlier than this, but never later.
One way to compare the function f(n) with these standard function is to use the
functional „O‟ notation, suppose f(n) and g(n) are functions defined on the positive
integers with the property that f(n) is bounded by some multiple g(n) for almost all
sD
„n‟. Then,
f(n) = O(g(n))
Which is read as “f(n) is of order g(n)”. For example, the order of complexity for:
al
Suppose that T1(n) and T2(n) are the running times of two programs fragments P1
and P2, and that T1(n) is O(f(n)) and T2(n) is O(g(n)). Then T1(n) + T2(n), the
running time of P1 followed by P2 is O(max f(n), g(n)), this is called as rule of sums.
For example, suppose that we have three steps whose running times are respectively
O(n2), O(n3) and O(n. log n). Then the running time of the first two steps executed
sequentially is O (max(n2, n3)) which is O(n3). The running time of all three
together is O(max (n3, n. log n)) which is O(n3).
6
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
If T1(n) and T2(n) are O(f(n) and O(g(n)) respectively. Then T1(n)*T2(n) is O(f(n)
g(n)). It follows term the product rule that O(c f(n)) means the same thing as O(f(n))
if „c‟ is any positive constant. For example, O(n2/2) is same as O(n2).
Suppose that we have five algorithms A1–A5 with the following time complexities:
A1 : n
A2 : n log n
A3 : n2
A4: n3
A5: 2n
m
The time complexity is the number of time units required to process an input of size
„n‟. Assuming that one unit of time equals one millisecond. The size of the problems
co
that can be solved by each of these five algorithms is:
a.
A1 n 1000 6 x 104 3.6 x 106
A2 n log n 140 4893 2.0 x 105
n2
A3 31 244 1897
A4
A5
n3
2n
10
9
iy 39
15
153
21
un
The speed of computations has increased so much over last thirty years and it might
seem that efficiency in algorithm is no longer important. But, paradoxically, efficiency
matters more today then ever before. The reason why this is so is that our ambition
has grown with our computing power. Virtually all applications of computing
simulation of physical data are demanding more speed.
sD
The faster the computer run, the more need are efficient algorithms to take
advantage of their power. As the computer becomes faster and we can handle larger
problems, it is the complexity of an algorithm that determines the increase in
problem size that can be achieved with an increase in computer speed.
al
Suppose the next generation of computers is ten times faster than the current
generation, from the table we can see the increase in size of the problem.
ri
A1 n S1 10 S1
A2 n log n S2 10 S2 for large S2
n2
A3 S3 3.16 S3
Tu
n3
A4 S4 2.15 S4
A5 2n S5 S5 + 3.3
7
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
When solving a problem we are faced with a choice among algorithms. The basis for
this can be any one of the following:
ii. We would like an algorithm that makes efficient use of the computer‟s
m
resources, especially, one that runs as fast as possible.
co
Measuring the running time of a program
a.
1. The input to the program.
2. The quality of code generated by the compiler used to create the object
3.
program.
iy
The nature and speed of the instructions on the machine used to execute the
un
program, and
The running time depends not on the exact input but only the size of the input. For
sD
many programs, the running time is really a function of the particular input, and not
just of the input size. In that case we define T(n) to be the worst case running time,
i.e. the maximum overall input of size „n‟, of the running time on that input. We also
consider Tavg(n) the average, over all input of size „n‟ of the running time on that
input. In practice, the average running time is often much harder to determine than
al
the worst case running time. Thus, we will use worst–case running time as the
principal measure of time complexity.
ri
Seeing the remarks (2) and (3) we cannot express the running time T(n) in standard
time units such as seconds. Rather we can only make remarks like the running time
of such and such algorithm is proportional to n 2. The constant of proportionality will
to
remain un-specified, since it depends so heavily on the compiler, the machine and
other factors.
Tu
Our approach is based on the asymptotic complexity measure. This means that we
don‟t try to count the exact number of steps of a program, but how that number
grows with the size of the input to the program. That gives us a measure that will
work for different operating systems, compilers and CPUs. The asymptotic complexity
is written using big-O notation.
The most important property is that big-O gives an upper bound only. If an algorithm
is O(n2), it doesn‟t have to take n2 steps (or a constant multiple of n2). But it can‟t
8
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
take more than n2. So any algorithm that is O(n), is also an O(n 2) algorithm. If this
seems confusing, think of big-O as being like "<". Any number that is < n is also <
n2.
1. Ignoring constant factors: O(c f(n)) = O(f(n)), where c is a constant; e.g.
O(20 n3) = O(n3)
2. Ignoring smaller terms: If a<b then O(a+b) = O(b), for example O(n2+n)
= O(n2)
m
4. n and log n are "bigger" than any constant, from an asymptotic view (that
means for large enough n). So if k is a constant, an O(n + k) algorithm is
co
also O(n), by ignoring smaller terms. Similarly, an O(log n + k) algorithm
is also O(log n).
a.
which is O(n(log n + 1)), can be simplified to O(n log n).
iy
Calculating the running time of a program:
Let us now look into how big-O bounds can be computed for some common
algorithms.
un
Example 1:
sD
x = 3*y + 2;
z = z + 1;
al
If y, z are scalars, this piece of code takes a constant amount of time, which we write
as O(1). In terms of actual computer instructions or clock ticks, it‟s difficult to say
exactly how long it takes. But whatever it is, it should be the same whenever this
ri
Example 2:
O
2n2 + 5n – 6 = O (n3) 2n2 + 5n – 6 (n3)
2n2 + 5n – 6 = O (n2) 2n2 + 5n – 6 = (n2)
2n2 + 5n – 6 O (n) 2n2 + 5n – 6 (n)
9
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Example 3:
If the first program takes 100n2 milliseconds and while the second takes 5n3
milliseconds, then might not 5n3 program better than 100n2 program?
As the programs can be evaluated by comparing their running time functions, with
constants by proportionality neglected. So, 5n3 program be better than the 100n2
program.
5 n3/100 n2 = n/20
for inputs n < 20, the program with running time 5n3 will be faster than those the
m
one with running time 100 n2. Therefore, if the program is to be run mainly on inputs
of small size, we would indeed prefer the program whose running time was O(n3)
co
However, as „n‟ gets large, the ratio of the running times, which is n/20, gets
arbitrarily larger. Thus, as the size of the input increases, the O(n 3) program will take
significantly more time than the O(n2) program. So it is always better to prefer a
program whose running time with the lower growth rate. The low growth rate
a.
function‟s such as O(n) or O(n log n) are always better.
Example 4:
This loop will run exactly n times, and because the inside of the loop takes constant
time, the total running time is proportional to n. We write it as O(n). The actual
number of instructions might be 50n, while the running time might be 17n
microseconds. It might even be 17n+3 microseconds because the loop needs some
al
time to start up. The big-O notation allows a multiplication factor (like 17) as well as
an additive factor (like 3). As long as it‟s a linear function which is proportional to n,
the correct notation is O(n) and the code is said to have linear running time.
ri
Example 5:
to
The outer for loop executes N times, while the inner loop executes n times for every
execution of the outer loop. That is, the inner loop executes n n = n2 times. The
assignment statement in the inner loop takes constant time, so the running time of
the code is O(n2) steps. This piece of code is said to have quadratic running time.
10
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Example 6:
Lets start with an easy case. Multiplying two n n matrices. The code to compute the
matrix product C = A * B is given below.
m
There are 3 nested for loops, each of which runs n times. The innermost loop
therefore executes n*n*n = n3 times. The innermost statement, which contains a
co
scalar sum and product takes constant O(1) time. So the algorithm overall takes
O(n3) time.
a.
Example 7:
iy
The main body of the code for bubble sort looks something like this:
This looks like the double. The innermost statement, the if, takes O(1) time. It
doesn‟t necessarily take the same time when the condition is true as it does when it
is false, but both times are bounded by a constant. But there is an important
difference here. The outer loop executes n times, but the inner loop executes a
number of times that depends on i. The first time the inner for executes, it runs i =
al
n-1 times. The second time it runs n-2 times, etc. The total number of times the
inner if statement executes is therefore:
ri
N 1 2
(n i) n(n i)
n
n
i 1 2 2 2
Tu
The value of the sum is n(n-1)/2. So the running time of bubble sort is O(n(n-1)/2),
which is O((n2-n)/2). Using the rules for big-O given earlier, this bound simplifies to
O((n2)/2) by ignoring a smaller term, and to O(n 2), by ignoring a constant factor.
Thus, bubble sort is an O(n2) algorithm.
11
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Example 8:
Binary search is a little harder to analyze because it doesn‟t have a for loop. But it‟s
still pretty easy because the search interval halves each time we iterate the search.
The sequence of search intervals looks something like this:
It‟s not obvious how long this sequence is, but if we take logs, it is:
m
log2 n, log2 n - 1, log2 n - 2, ..., 3, 2, 1, 0
Since the second sequence decrements by 1 each time down to 0, its length must be
co
log2 n + 1. It takes only constant time to do each test of binary search, so the total
running time is just the number of times that we iterate, which is log 2n + 1. So
binary search is an O(log2 n) algorithm. Since the base of the log doesn‟t matter in an
asymptotic bound, we can write that binary search is O(log n).
a.
General rules for the analysis of programs
iy
In general the running time of a statement or group of statements may be
parameterized by the input size and/or by one or more variables. The only
permissible parameter for the running time of the whole program is „n‟ the input size.
un
1. The running time of each assignment read and write statement can usually be
taken to be O(1). (There are few exemptions, such as in PL/1, where
assignments can involve arbitrarily larger arrays and in any language that
allows function calls in arraignment statements).
sD
the condition is normally O(1) the time for an if–then–else construct is the
time to evaluate the condition plus the larger of the time needed for the
statements executed when the condition is true and the time for the
to
4. The time to execute a loop is the sum, over all times around the loop, the
time to execute the body and the time to evaluate the condition for
Tu
termination (usually the latter is O(1)). Often this time is, neglected constant
factors, the product of the number of times around the loop and the largest
possible time for one execution of the body, but we must consider each loop
separately to make sure.
12
Download FREE Computer Science Notes at TutorialsDuniya.com
Download FREE Computer Science Notes at TutorialsDuniya.com
Chapter
2
Advanced Data Structures and
Recurrence Relations
m
Priority Queue, Heap and Heap Sort:
Heap is a data structure, which permits one to insert elements into a set and also to
find the largest element efficiently. A data structure, which provides these two
co
operations, is called a priority queue.
a.
A max heap is an almost complete binary tree such that the value of each node is
greater than or equal to those in its children.
95
iy 15
un
85 45 45 25
75 25 35 15 55 65 35 75
sD
55 65 85 95 Min heap
Max heap
A min heap is an almost complete binary tree such that the value of each node is less
than or equal to those in its children. Figure 2.1 shows the maximum and minimum
ri
heap tree.
to
Since heap is a complete binary tree, a heap tree can be efficiently represented using
Tu
one dimensional array. This provides a very convenient way of figuring out where
children belong to.
13
For example let us consider the following elements arranged in the form of array as
follows:
65 45 60 40 25 50 55 30
The elements of the array can be thought of as lying in a tree structure. A heap tree
m
represented using a single array looks as follows:
x[1]
co
65 x[3]
x[2]
45 60
x[6] x[7]
x[4]
a.
40 x[5] 25 50 55
x[8] 30
iy
Figure 2. 2. Heap Tree
un
Operations on heap tree:
sD
1. Insertion,
2. Deletion and
al
3. Merging.
This operation is used to insert a node into an existing heap tree satisfying the
properties of heap tree. Using repeated insertions of data, starting from an empty
to
Let us consider the heap (max) tree. The principle of insertion is that, first we have
Tu
to adjoin the data in the complete binary tree. Next, we have to compare it with the
data in its parent; if the value is greater than that at parent then interchange the
values. This will continue between two nodes on path from the newly inserted node
to the root node till we get a parent whose value is greater than its child or we
reached the root.
For illustration, 35 is added as the right child of 80. Its value is compared with its
parent‟s value, and to be a max heap, parent‟s value greater than child‟s value is
satisfied, hence interchange as well as further comparisons are no more required.
As another illustration, let us consider the case of insertion 90 into the resultant heap
tree. First, 90 will be added as left child of 40, when 90 is compared with 40 it
14
requires interchange. Next, 90 is compared with 80, another interchange takes place.
Now, our process stops here, as 90 is now in root node. The path on which these
comparisons and interchanges have taken places are shown by dashed line.
The algorithm Max_heap_insert to insert a data into a max heap tree is as follows:
Max_heap_insert (a, n)
{
//inserts the value in a[n] into the heap which is stored at a[1] to a[n-1]
integer i, n;
i = n;
item = a[n] ;
m
while ( (i > 1) and (a[ i/2 ] < item ) do
{
a[i] = a[ i/2 ] ; // move the parent down
co
i = i/2 ;
}
a[i] = item ;
return true ;
a.
}
Example:
iy
Form a heap by using the above algorithm for the given data 40, 80, 35, 90, 45, 50,
70.
un
1. Insert 40:
40
sD
2. Insert 80:
80
40 80
al
40
80 40
ri
3. Insert 35:
to
80
40 35
Tu
15
4. Insert 90:
90
80 90
80
90
40 35 80 35
40
90 40
m
5. Insert 45:
90
co
80 35
40 45
a.
6. Insert 50:
90
50 iy 90
un
80 35 80 50
35
40 45 50 40 45 35
sD
7. Insert 70:
90 90
70
al
80 50 80 70
50
40 45 35 70 40 45 35 50
ri
Any node can be deleted from a heap tree. But from the application point of view,
deleting the root node has some special importance. The principle of deletion is as
Tu
follows:
16
delmax (a, n, x)
// delete the maximum from the heap a[n] and store it in x
{
if (n = 0) then
{
write (“heap is empty”);
return false;
}
x = a[1]; a[1] = a[n];
adjust (a, 1, n-1);
return true;
m
}
adjust (a, i, n)
// The complete binary trees with roots a(2*i) and a(2*i + 1) are combined with a(i) to form a
co
single heap, 1 < i < n. No node has an address greater than n or less than 1. //
{
j = 2 *i ;
item = a[i] ;
a.
while (j < n) do
{
if ((j < n) and (a (j) < a (j + 1)) then j j + 1;
// compare left and right child and let j be the larger child
if (item > a (j)) then break;
iy
// a position for item is found
else a[ j / 2 ] = a[j] // move the larger child up a level
un
j = 2 * j;
}
a [ j / 2 ] = item;
}
sD
Here the root node is 99. The last node is 26, it is in the level 3. So, 99 is replaced by
26 and this node with data 26 is removed from the tree. Next 26 at root node is
compared with its two child 45 and 63. As 63 is greater, they are interchanged. Now,
26 is compared with its children, namely, 57 and 42, as 57 is greater, so they are
interchanged. Now, 26 appear as the leave node, hence re-heap is completed. This is
al
26 63
ri
99
26 63
57
45 63
to
45 57
26
35 29 57 42
35 29 26 42
Tu
27 12 24 26
27 12 24
Deleting the node with data 99 After Deletion of node with data 99
Figure 2. 3.
17
Consider, two heap trees H1 and H2. Merging the tree H2 with H1 means to include
all the node from H2 to H1. H2 may be min heap or max heap and the resultant tree
will be min heap if H1 is min heap else it will be max heap.
Merging operation consists of two steps: Continue steps 1 and 2 while H2 is not
empty:
m
92 13
co
59 67 19 80
38 45 92 93 96
a.
H1: max heap H2: min heap
+
96 iy
un
93 67
80 92 13 19
sD
18
HEAP SORT:
A heap sort algorithm works by first organizing the data to be sorted into a special
type of binary tree called a heap. Any kind of data can be sorted either in ascending
order or in descending order using heap tree. It does this with the following steps:
m
c. Place the deleted node in the output.
3. Continue step 2 until the heap tree is empty.
co
Algorithm:
This algorithm sorts the elements a[n]. Heap sort rearranges them in-place in non-
decreasing order. First transform the elements into a heap.
a.
heapsort(a, n)
{
heapify(a, n);
for i = n to 2 by – 1 do
{
temp = a[I];
iy
un
a[i] = a[1];
a[1] = t;
adjust (a, 1, i – 1);
}
}
sD
heapify (a, n)
//Readjust the elements in a[n] to form a heap.
{
for i n/2 to 1 by – 1 do adjust (a, i, n);
al
adjust (a, i, n)
// The complete binary trees with roots a(2*i) and a(2*i+1) are combined
ri
// with a(i) to form a single heap, 1<i<n. No node has an address greater
// than n or less than 1.
{
to
j = 2 *i ;
item = a[i] ;
while (j < n) do
{
Tu
19
Time Complexity:
Each „n‟ insertion operations takes O(log k), where „k‟ is the number of elements in
the heap at the time. Likewise, each of the „n‟ remove operations also runs in time
O(log k), where „k‟ is the number of elements in the heap at the time. Since we
always have k ≤ n, each such operation runs in O(log n) time in the worst case.
Thus, for n elements it takes O(n log n) time, so the priority queue sorting algorithm
runs in O(n log n) time when we use a heap to implement the priority queue.
Example 1:
m
Form a heap from the set of elements (40, 80, 35, 90, 45, 50, 70) and sort the data
using heap sort.
co
Solution:
First form a heap tree from the given set of data and then sort by repeated deletion
operation:
a.
1. E xc h a n g e ro ot 9 0 w it h t h e la st e le me n t 3 5 of t h e array a n d re - h e a p ify
80
45 35
80
35
70
iy 45
80
70
un
40 45 50 90 40 35 50 90
35
sD
2. E xc h a n g e ro ot 8 0 w it h t h e la st e le me n t 5 0 of t h e array a n d re - h e a p ify
70
50 70
50
45 70 45 50
al
40 35 80 90 40 35 80 90
ri
35 50
35
45 50 45 35
Tu
40 70 80 90 40 70 80 90
20
4. Exchange root 50 with the last element 40 of the array and re-heapify
45
40 45
40
45 35 40 35
50 70 80 90 50 70 80 90
5. Exchange root 45 with the last element 35 of the array and re-heapify
40
m
35 40
35
40 45 35 45
co
50 70 80 90 50 70 80 90
a.
6. Exchange root 40 with the last element 35 of the array and re-heapify
35
40 35
40
35 45
iy 40 45
un
50 70 80 90 50 70 80 90
Priority queue can be implemented using circular array, linked list etc. Another
simplified implementation is possible using heap tree; the heap, however, can be
al
As heap trees allow the duplicity of data in it. Elements associated with their priority
values are to be stored in from of heap tree, which can be formed based on their
to
priority values. The top priority element that has to be processed first is at the root;
so it can be deleted and heap can be rebuilt to get the next element to be processed,
and so on.
Tu
Process P1 P2 P3 P4 P5 P6 P7 P8 P9 P10
Priority 5 4 3 4 5 5 3 2 1 5
These processes enter the system in the order as listed above at time 0, say. Assume
that a process having higher priority value will be serviced first. The heap tree can be
formed considering the process priority values. The order of servicing the process is
successive deletion of roots from the heap.
21
A binary search tree has binary nodes and the following additional property. Given a
node t, each node to the left is “smaller” than t, and each node to the right is
“larger”. This definition applies recursively down the left and right sub-trees. Figure
shows a binary search tree where characters are stored in the nodes.
b h
m
a e g
co
d
c Inorder: a b c d e f g h
a.
Figure 2.5. Binary Search tree
iy
Figure 2.5 also shows what happens if you do an inorder traversal of a binary search
tree: you will get a list of the node contents in sorted order. In fact, that‟s probably
how the name inorder originated.
un
Binary Tree Searching:
The search operation starts from root node R, if item is less than the value in the
sD
root node R, we proceed to the left child; if item is greater than the value in the node
R, we proceed to its right child. The process will be continued till the item is found or
we reach to a dead end. The Figure 2.6 shows the path taken when searching for a
node “c”.
al
f
ri
b h
a e g
to
d
Tu
c Path to find c
Binary search trees provide an efficient way to search through an ordered collection
of items. Consider the alternative of searching an ordered list. The search must
proceed sequentially from one end of the list to the other. On average, n/2 nodes
must be compared for an ordered list that contains n nodes. In the worst case, all n
22
nodes might need to be compared. For a large collection of items, this can get very
expensive.
To obtain the smallest height, a tree must be balanced, where both the left and right
sub trees have approximately the same number of nodes. Also, each node should
m
have as many children as possible, with all levels being full except possibly the last.
Figure 2.7 shows an example of a well-constructed tree.
co
e
c g
a.
b d f h
iy
Figure 2.7. Well constructed binary search tree.
un
Unfortunately, trees can become so unbalanced that they‟re no better for searching
than linked lists. Such trees are called degenerate trees. Figure 2.8 shows an
example. For a degenerate tree, an average of n/2 comparisons are needed, with a
sD
a
al
c
ri
d
to
When nodes are being added and deleted in a binary search tree, it‟s difficult to
maintain the balance of the tree. We will investigate methods of balancing trees in
the next section.
When adding nodes to a binary search tree, we must be careful to maintain the
binary search tree property. This can be done by first searching the tree to see
whether the key we are about to add is already in the tree. If the key cannot be
found, a new node is allocated and added at the same location where it would go if
23
the search had been successful. Figure 2.9 shows what happens when we add some
nodes to a tree.
d d
d
b f b f
insert a insert e b f
a e
a
m
Figure 2.9. Inserting nodes into a binary search tree.
co
Deletions from a binary search tree are more involved than insertions. Given a node
to delete, we need to consider these tree cases:
a.
1. The node is a leaf
2. The node has only one child.
3. The node has two children.
iy
Case 1: It is easy to handle because the node can simply be deleted and the
un
corresponding child pointer of its parent set to null. Figure 2.10 shows
an example.
b b
sD
a c a c
d
al
Case 2: It is almost as easy to manage. Here, the single child can be promoted
up the tree to take the place of the deleted node, as shown in figure
to
2.11.
b b
Tu
a c a e
e d f
d f
24
Case 3: The node to be deleted has two children, is more difficult. We must
find some node to take the place of the one deleted and still maintain
the binary search tree property. There are two obvious cases:
The predecessor of a node can be found by going down to the left once, and then all
the way to the right as far as possible. To find the successor, an opposite traversal is
m
used: first to the right, and then down to the left as far as possible. Figure 2.12
shows the path taken to find both the predecessor and successor of a node.
co
e e
b i b i
a.
a d g j a d g j
c f h
(a) Predecessor of e.
iy c f h
(b) Successor of e.
un
Figure 2.12. Finding predecessor and successor of nodes.
Both the predecessor and successor nodes are guaranteed to have no more than one
sD
child, and they may have none. Detaching the predecessor or successor reduces to
either case 1 or 2, both of which are easy to handle. In figure 2.13 (a), we delete a
node from the tree and use its predecessor as the replacement and in figure 2.13
(b), we delete a node from the tree and use its successor as the replacement.
al
d f
ri
b i b i
to
a c g j a d g j
f h c h
Tu
25
Balanced Trees:
For maximum efficiency, a binary search tree should be balanced. Every un-balanced
trees are referred to as degenerate trees, so called because their searching
performance degenerates to that of linked lists. Figure 2.14 shows an example.
a a
b i
c
b
m
d
h
e
c
co
f
g
g
(a) h d (b)
a.
i f
d
al
b h
a c f i
ri
e g
to
Balancing Acts:
26
Tree Rotations:
p
c b
t
b 4 a c
a 3
1 2 3 4
m
1 2 (a) Right Rotation.
co
p
b
a
t a c
a.
1 b
1 2 3 4
2 c
3 4
iy
(a) Left Rotation.
un
Figure 2.16. Single Rotations
Another type of rotation is known as a double rotation. Figure 2.17 shows the two
symmetrical cases.
sD
g
c c b
p
a 4 b 4 a c
al
1 b
a 3 1 2 3 4
ri
2 3
1 2
(a) Left - Right Rotation.
to
p
b
a a
Tu
t a c
1 c 1 b
1 2 3 4
b 4 2 c
2 3 3 4
27
Both single and double rotations have one important feature: in order traversals of
the trees before and after the rotations are preserved. Thus, rotations help balance
trees, but still maintain the binary search tree property.
Rotations don‟t guarantee a balanced tree, however for example, figure 2.18 shows
a right rotations that makes a tree more un-balanced. The trick lies in determining
when to do rotation and what kind of notations to do.
b a
m
a c
co
Figure 2.18. Un-balanced rotation.
The Red-black trees have certain rules to be used to determine when a how to
rotate.
a.
Dictionary:
iy
A Dictionary is a collection of pairs of form (k, e), where k is a key and e is the
element associated with the key k (equivalently, e is the element whose key is k). No
two pairs in a dictionary should have the same key.
un
Examples:
which is the key and the meaning of the word, pronunciation and etymologies
etc. are associated with the word.
3. The list of students enrolled for the data structures course, compiler and
Operating System course. The list contains (CourseName, RollID) pairs.
ri
The above last two examples are dictionaries, which permit two or more (key,
element) pairs having the same key.
to
Operations on Dictionaries:
Get the element associated with a specified key from the dictionary.
Tu
For example, get(k) returns the element with the key k.
Insert or put an element with a specified key into the dictionary.
For example, put(k, e) puts the element e whose key is k into the dictionary.
28
Set:
A set is a collection of distinct elements. The Set can be represented, for
examples, as S1={1,2,5,10}.
Disjoint Sets:
The disjoints sets are those do not have any common element.
m
For example S1={1,7,8,9} and S2={2,5,10}, then we can say that S1 and S2 are
two disjoint sets.
co
The disjoint set operations are
1. Union
2. Find
a.
Disjoint set Union:
If Si and Sj are tow disjoint sets, then their union Si U Sj consists of all the
elements x such that x is in Si or Sj.
Example:
S1={1,7,8,9} S2={2,5,10} iy
un
S1 U S2={1,2,5,7,8,9,10}
Find:
Given the element I, find the set containing i.
Example:
sD
Set Representation:
The set will be represented as the tree structure where all children will store
the address of parent / root node. The root node will store null at the place of parent
address. In the given set of elements any element can be selected as the root node,
ri
Example:
to
Disjoint Union:
To perform disjoint set union between two sets Si and Sj can take any one
root and make it sub-tree of the other. Consider the above example sets S1 and S2
then the union of S1 and S2 can be represented as any one of the following.
29
Find:
To perform find operation, along with the tree structure we need to maintain
the name of each set. So, we require one more data structure to store the set
names. The data structure contains two fields. One is the set name and the other one
m
is the pointer to root.
co
a.
iy
un
Union and Find Algorithms:
In presenting Union and Find algorithms, we ignore the set names and
identify sets just by the roots of trees representing them. To represent the sets, we
use an array of 1 to n elements where n is the maximum value among the elements
of all sets. The index values represent the nodes (elements of set) and the entries
sD
represent the parent node. For the root value the entry will be „-1‟.
Example:
For the following sets the array representation is as shown below.
al
ri
to
i [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Tu
p -1 -1 -1 3 2 3 1 1 1 2
Algorithm SimpleUnion(i,j)
{
P[i]:=j;
}
30
m
1 2 3 4 ...... n
co
Then if we want to perform following sequence of operations Union(1,2) ,
Union(2,3)……. Union(n-1,n) and sequence of Find(1), Find(2)……… Find(n).
a.
The sequence of Union operations results the degenerate tree as below.
n
iy
un
n-1
sD
n-2
al
1
ri
Since, the time taken for a Union is constant, the n-1 sequence of union can be
processed in time O(n). And for the sequence of Find operations it will take time
to
complexity of O ( i ) = O(n ).
i1
2
We can improve the performance of union and find by avoiding the creation of
Tu
31
m
To implement weighting rule we need to know how many nodes are there in every
co
tree. To do this we maintain “count” field in the root of every tree. If „i' is the root
then count[i] equals to number of nodes in tree with root i.
Since all nodes other than roots have positive numbers in parent (P) field, we can
a.
maintain count in P field of the root as negative number.
Algorithm WeightedUnion(i,j)
//Union sets with roots i and j , i≠j using the weighted rule
// P[i]=-count[i] and p[j]=-count[j]
{
temp:= P[i]+P[j]; iy
un
if (P[i]>P[j]) then
{
// i has fewer nodes
P[i]:=j;
P[j]:=temp;
sD
}
else
{
// j has fewer nodes
al
P[j]:=i;
P[i]:=temp;
}
ri
}
to
32
m
co
a.
iy
un
sD
al
ri
to
Find(8), Find(8)………………………Find(8)
If SimpleFind() is used each Find(8) requires going up three parent link fields for a
total of 24 moves .
When Collapsing find is used the first Find(8) requires going up three links and
resetting three links. Each of remaining seven finds require going up only one link
field. Then the total cost is now only 13 moves.( 3 going up + 3 resets + 7 remaining
finds).
33
Algorithm CollapsingFind(i)
// Find the root of the tree containing element i
// use the collapsing rule to collapse all nodes from i to root.
{
r:=i;
while(P[r]>0) do r:=P[r]; //Find root
while(i≠r)
{
//reset the parent node from element i to the root
s:=P[i];
P[i]:=r;
m
i:=s;
}
}
co
a.
iy
un
sD
al
ri
to
Tu
34
Recurrence Relations
Recurrence Relation for a sequence of numbers S is a formula that relates all but a
finite number of terms of S to previous terms of the sequence, namely, {a 0, a1, a2, .
. . . . , an-1}, for all integers n with n ≥ n0, where n0 is a nonnegative integer.
m
Recurrence relations are also called as difference equations.
Sequences are often most easily defined with a recurrence relation; however the
co
calculation of terms by directly applying a recurrence relation can be time
consuming. The process of determining a closed form expression for the terms of a
sequence from its recurrence relation is called solving the relation. Some guess and
check with respect to solving recurrence relation are as follows:
a.
Make simplifying assumptions about inputs
Tabulate the first few values of the recurrence
Look for patterns, guess a solution
iy
Generalize the result to remove the assumptions
un
Examples: Factorial, Fibonnaci, Quick sort, Binary search etc.
form of the recurrence for each occurrence of the function T on the right-hand side.
For example, performing such a substitution with the merge sort recurrence equation
yields the equation.
Tu
The hope in applying the iterative substitution method is that, at some point, we will
see a pattern that can be converted into a general closed-form equation (with T only
35
appearing on the left-hand side). In the case of merge-sort recurrence equation, the
general form is:
T(n) = 2 i T (n/2i) + i b n
Note that the general form of this equation shifts to the base case, T(n) = b, where
n = 2i, that is, when i = log n, which implies:
T(n) = b n + b n log n.
In other words, T(n) is O(n log n). In a general application of the iterative
substitution technique, we hope that we can determine a general pattern for T(n) and
m
that we can also figure out when the general form of T(n) shifts to the base case.
co
The Recursion Tree:
a.
substitution to solve a recurrence equation, but it differs from the iterative
substitution method in that, rather than being an algebraic approach, it is a visual
approach.
iy
In using the recursion tree method, we draw a tree R where each node represents a
different substitution of the recurrence equation. Thus, each node in R has a value of
the argument n of the function T (n) associated with it. In addition, we associate an
un
overhead with each node v in R, defined as the value of the non-recursive part of the
recurrence equation for v.
needed to merge the subproblem solutions coming from the children of v. The
recurrence equation is then solved by summing the overheads associated with all the
nodes of R. This is commonly done by first summing values across the levels of R and
then summing up these partial sums for all the levels of R.
al
b if n 3
T(n)
ri
3 T n / 3 b n if n 3
This is the recurrence equation that we get, for example, by modifying the merge
to
sort algorithm so that we divide an unsorted sequence into three equal – sized
sequences, recursively sort each one, and then do a three-way merge of three sorted
sequences to produce a sorted version of the original sequence. In the recursion tree
Tu
R for this recurrence, each internal node v has three children and has a size and an
overhead associated with it, which corresponds to the time needed to merge the sub-
problem solutions produced by v‟s children. We illustrate the tree R as follows:
36
O v erhe a d
bn
bn
bn
The overheads of the nodes of each level, sum to bn. Thus, observing that the depth
m
of R is log3 n, we have that T(n) is O(n log n).
co
The Guess-and-Test Method:
a.
solution of the recurrence equation might look like and then justifying the guesses,
usually by induction. For example, we can use the guess-and-test method as a kind
of “binary search” for finding good upper bounds on a given recurrence equation. If
iy
the justification of our current guess fails, then it is possible that we need to use a
faster-growing function, and if our current guess is justified “too easily”, then it is
possible that we need to use a slower-growing function. However, using this
un
technique requires case careful, in each mathematical step we take, in trying to
justify that a certain hypothesis holds with respect to our current “guess”.
T (n) = 2 T(n/2) + b n log n. (assuming the base case T(n) = b for n < 2)
This looks very similar to the recurrence equation for the merge sort routine, so we
might make the following as our first guess:
al
for some constant c>0. We can certainly choose c large enough to make this true for
the base case, so consider the case when n > 2. If we assume our first guesses an
inductive hypothesis that is true for input sizes smaller than n, then we have:
to
for some constant c > 0. We can again choose c large enough to make this true for
the base case, so consider the case when n ≥ 2. If we assume this guess as an
37
inductive hypothesis that is true for input sizes smaller than n, then we have
inductive hypothesis that is true for input sizes smaller than n, then we have:
m
Provided c ≥ b. Thus, we have shown that T (n) is indeed O(n log 2 n) in this case.
We must take care in using this method. Just because one inductive hypothesis for T
(n) does not work, that does not necessarily imply that another one proportional to
this one will not work.
co
Example 2.10.2: Consider the following recurrence equation (assuming the base
case T(n) = b for n < 2): T (n) = 2T (n/2) + log n
a.
This recurrence is the running time for the bottom-up heap construction. Which is
O(n). Nevertheless, if we try to prove this fact with the most straightforward
inductive hypothesis, we will run into some difficulties. In particular, consider the
following:
But there is no way that we can make this last line less than or equal to cn for n > 2.
Thus, this first guess was not sufficient, even though T (n) is indeed O (n). Still, we
can show this fact is true by using:
ri
For some constant c > 0. We can again choose c large enough to make this true for
the base case; in fact, we can show that it is true any time n < 8. So consider the
case when n ≥ 8. If we assume this guess as an inductive hypothesis that is true for
input sizes smaller than n, then we have:
Tu
Provided c ≥ 3 and n ≥ 8. Thus, we have shown that T (n) is indeed O (n) in this
case.
Each of the methods described above for solving recurrence equations is ad hoc and
requires mathematical sophistication in order to be used effectively. There is,
nevertheless, one method for solving divide-and-conquer recurrence equations that is
quite general and does not require explicit use of induction to apply correctly. It is
the master method. The master method is a “cook-book” method for determining the
asymptotic characterization of a wide variety of recurrence equations. It is used for
recurrence equations of the form:
c if n d
T(n)
a T(n / b) f (n) if n d
m
Where d > 1 is an integer constant, a > 0, c > 0, and b > 1 are real constants, and f
(n) is a function that is positive for n ≥ d.
The master method for solving such recurrence equations involves simply writing
co
down the answer based on whether one of the three cases applies. Each case is
distinguished by comparing f (n) to the special function nlogb a (we will show later why
this special function is so important.
a.
The master theorem: Let f (n) and T (n) be defined as above.
(n) is n logb a
. iy
un
2. k
If there is a constant K ≥ 0, such that f (n) is nlogb a log n , then T (n)
is n
logb a
logk 1 n .
nlogb a
sD
3. If there are small constant > 0 and < 1, such that f (n) is
Case 1 characterizes the situation where f (n) is polynomial smaller than the special
al
function, nlogba.
Case 2 characterizes the situation when f (n) is asymptotically close to the special
ri
function, and
Case 3 characterizes the situation when f (n) is polynomially larger than the special
to
function.
We illustrate the usage of the master method with a few examples (with each taking
the assumption that T (n) = c for n < d, for constants c > 1 and d > 1).
Tu
In this case, nlogba = nlog2 4 = n2. Thus, we are in case 1, for f (n) is O (n2-) for = 1.
This means that T (n) is (n2) by the master.
In this case, nlogba = nlog 22 = n. Thus, we are in case 2, with k=1, for f (n) is
(n log n). This means that T (n) is (n log2n) by the master method.
39
In this case nlogba = nlog31 =n 0 = 1. Thus, we are in Case 3, for f (n) is (n0+), for =
1, and af(n/b) = n/3 = (1/3) f(n). This means that T (n) is (n) by the master
method.
In this case, nlogba = nlog 39 = n2. Thus, we are in Case 3, since f (n) is (n 2+) (for
=1/2) and af(n/b) = 9 (n/3)2.5 = (1/3)1/2 f(n). This means that T (n) is (n2.5) by
the master method.
m
Example 2.11.5: Finally, consider the recurrence T (n) = 2T (n1/2) + log n
Unfortunately, this equation is not in a form that allows us to use the master method.
co
We can put it into such a form, however, by introducing the variable k = log n, which
lets us write:
T (n) = T (2k) = 2 T (2k/2) + k
a.
Substituting into this the equation S (k) = T (2k), we get that
S (k) = 2 S (k/2) + k
iy
Now, this recurrence equation allows us to use master method, which specifies that S
un
(k) is O (k log k). Substituting back for T (n) implies T (n) is O (log n log log n).
n
al
i 1 2 . . . . . . . n n(n2 1)
i 1
ri
i
to
2
1 4 9 . . . . . . . n n(n 1) (2n 1)
i1
6
Tu
Sum of cubes
n 2 2
i 3 3 3 3
1 2 3 . . . . . . . n 3
n (n 1)
4
i1
Geometric progression
2 i
20 21 22 . . . . . . . 2n 2n 1 1
i 0
40
n n 1
r 1
r i
r 1
i 0
n n
r 1
r i
r 1
i1
m
SOLVING RECURRENCE RELATIONS
co
Example 2.13.1. Solve the following recurrence relation:
2 ,n1
T(n) n
2 .T
a.
7 , n 1
2
Solution: We first start by labeling the main part of the relation as Step 1:
n n
T 2.T 7
2
2 2
al
Now substitute this back into the last T (n) definition (last line from step 1):
n
T n 2. 2. T 7 7
ri
2
2
n
22 .T 3.7
to
22
n
Step 3: let's expand the recursive call, this time, it's T :
Tu
22
n n
T 2.T 7
2 3
2 2
Now substitute this back into the last T(n) definition (last line from step 2):
n
T n 22 . 2. T 7 7
3
2
41
n
23 .T 7.7
3
2
From this, first, we notice that the power of 2 will always match i, current. Second,
we notice that the multiples of 7 match the relation 2i – 1 : 1, 3, 7, 15. So, we can
write a general solution describing the state of T (n).
m
However, how many times could we take these "steps"? Indefinitely? No… it would
stop when n has been cut in half so many times that it is effectively 1. Then, the
co
original definition of the recurrence relation would give us the terminating condition T
(1) = 1 and us restrict size n = 2i
n
When, 1
a.
2i
i
2 n
log2 2 i log2 n
iy
un
i .log2 2 log2 n
i log2 n
sD
Now we can substitute this "last value for i" back into a our general Step 4 equation:
T (n) =
2 . T 2 1 . 7
i n i
i
2
al
n
= 2log2 n . T 2log2 n 1 . 7
2log2 n
ri
= n . T(1) + (n - 1) . 7
= n . 2 + (n - 1) . 7
to
= 9.n-7
Tu
Example 2.13.2. Imagine that you have a recursive program whose run time is
described by the following recurrence relation:
1 , n 1
T(n) n
2.T 4. n , n 1
2
42
Solve the relation with iterated substitution and use your solution to determine a
tight big-oh bound.
Solution:
2
n
Step 2: figure out what T is:
m
2
n
T 2 . T
n n
4 .
co
2
2 2 2
Now substitute this back into the last T (n) definition (last line from step 1):
a.
n n
T n 2 . 2 . T 4 . 4 . n
2
2 2
= 22 . T
n
2 .
4. n
iy
un
2
2
n
Step 3: figure out what T is:
22
sD
n n n
T 2 . T 4.
22 23 22
al
Now substitute this back into the last T (n) definition (last time from step 2):
n
n
T n 2 . 2 . T
2 4 . 2 . 4 . n
ri
3 2
2 2
3 n
= 2 . T 3 . 4 . n
to
3
2
n
Tn 2i . T i .4 .n
2 i
The parameter to the recursive call in the last line will equal 1 when i = log2 n (use
the same analysis as the previous example). In which case T (1) = 1 by the original
definition of the recurrence relation.
Now we can substitute this back into a our general Step 4 equation:
43
Tn 2i . T . 4 .n
n
i
2
i
n
= 2log2 n . T log n . 4 . n
2
2log2 n
= n . T(1) 4 . n . log2 n
= n + 4 . n . log2 n
m
This implies that T (n) is O(n log n).
co
Example 2.13.3. Write a recurrence relation and solve the recurrence relation for
the following fragment of program:
a.
Write T(n) in terms of T for fractions of n, for example, T(n/k) or T(n -
k).
return (max2);
}
ri
Solution:
to
T (n) = 2, if n = 1
2 T(n/2) + 8, if n > 1
Tu
44
i 1
= 2i T (n/2i) + 8 ( 2k )
m
k 0
co
Step 3: Define i in terms of n:
a.
T(n) = 2i T (n/2i) + 8 (2i - 1)
= n T (1) + 8 (n-1)
= 2n + 8n - 8 = 10n - 8
Example 2.13.4. If K is a non negative constant, then prove that the recurrence
sD
k ,n1
n
T(n)
3 .T k . n , n 1
2
al
log2 3
T(n) 3 k . n - 2k .n
ri
Solution:
n
to
n
Substituting n for n in equation (1), we get
2
n n n
T 3T k (2)
2 4 2
n
Substituting equation (2) for T in equation (1)
2
45
n n
T (n) = 3 3T k k . n
4 2
n 3
T(n) 32 T k . n k . n (3)
4 2
n
Substituting n for n equation (1)
4
n n n
T 3T k (4)
m
4 8 4
co
Substituting equation (4) for T in equation (3)
4
n n 3
T(n) 32 3 T k k.n k. n
a.
8 4 2
n 9 3
33 T k . n k .n k . n (5)
8 4 2
iy
Continuing in this manner and substituting n = 2i, we obtain
un
i n 3i 1 9k n 3
T(n) 3 T k .n . . . . . . . k . n k .n
i
2 2i 1 4 2
sD
i
3 1
n 1 r n 1
n 2
T (n) 3i T
2 3 1
i
k . n as r i
r 1
i 0
al
2
n 3
= 3i T 2 . i k . n 2 k n (6)
ri
i
2 2
to
3i
T(n) 3i k 2. . k n 2k n
Tu
= 3i 3k 2 k n
log 2n
=3k.3 2 kn
log2 3
= 3k n 2k n
The Towers of Hanoi is a game played with a set of donut shaped disks stacked on
one of three posts. The disks are graduated in size with the largest on the bottom.
The objective of the game is to transfer all the disks from post B to post A moving
one disk at a time without placing a larger disk on top of a smaller one. What is the
minimum number of moves required when there are n disks?
In order to visualize the most efficient procedure for winning the game consider the
following:
1. Move the first n-1 disks, as per the rules in the most efficient manner
possible, from post B to post C.
m
2. Move the remaining, largest disk from post B to post A.
co
3. Move the n - 1 disks, as per the rules in the most efficient manner possible,
from post C to post A.
Let mn be the number of moves required to transfer n disks as per the rules in the
a.
most efficient manner possible from one post to another. Step 1 requires moving n
- 1 disks or mn-1 moves. Step 2 requires 1 move. Step 3 requires m n-1 moves again.
We have then,
iy
Mn = mn-1 + 1 + mn-1, for n > 2 = 2mn-1 + 1
un
Because only one move is required to win the game with only one disk, the initial
condition for this sequence is m1 = 1. Now we can determine the number of moves
required to win the game, in the most efficient manner possible, for any number of
disks.
sD
m1 = 1
m2 = 2(1) + 1 = 3
m3 = 2(3) + 1 = 7
al
m4 = 2(7) + 1 = 15
m5 = 2(15) + 1 = 31
ri
m6 = 2(31) + 1 = 63
m7 = 2(63) + 1 = 127
to
Lets look for a pattern that may be useful in determining an explicit formula for mn.
In looking for patterns it is often useful to forego simplification of terms. m n = 2mn-
1 + 1, m1 = 1
m1 = 1
m2 = 2(1) + 1 = 2+1
m3 = 2(2+1) + 1 = 22+2+1
m4 = 2(22+2+1) + 1 = 23+22+2+1
m5 = 2(23+22+2+1) + 1 = 24+23+22+2+1
47
m6 = 2(24+23+22+2+1) + 1 = 25+24+23+22+2+1
m7 = 2(25+24+23+22+2+1) + 1 = 26+25+24+23+22+2+1
Mk = 2k-1 + 2k-2 + . . . . . . + 22 + 2 + 1
By sum of a Geometric Sequence, for any real number r except 1, and any integer n
> 0.
n
n 1
r 1
m
r
i 0
i
r 1
co
our formula is then
n 1 2n 1
mk 2k 2n 1
a.
k 0 2 1
iy
Thus providing an explicit formula for the number of steps required to win the Towers
of Hanoi Game.
un
Example 2.13.6 Solve the recurrence relation T (n) = 5 T (n/5) + n/log n
When n = 5k
ri
Use the substitution method, guess that the solution is T (n) = O (n log n)
48
We need to prove T (n) < = c n log n for some c and all n > n o.
If we take n0 as 15, then for all n > n0, n/3+5 <= 2n/3, so
m
<= c n log n - (c n (log 3-1) + n/2 – 15 c log n - 15 c (log 3 - 1)
co
Which implies T (n) = O (n log n)
Similarly, by guessing T (n) >= c1 n log n for some c1 and all n > n0 and substituting
a.
in the recurrence, we get.
iy
>= c1 n log n + n/2 + c1 n (1 - log 3) + 15 c1 + 15 c1 (log n – log 3)
When n = 2k
= c n + n log (k)
= c n + n log (log n)
49
Use the substitution method, guess that the solution is T (n) = O (n log n).
We need to prove T (n) <= c n log n for some c and all n > no.
m
co
Substituting the guess in the given recurrence, we get
T (n) <= c (n/2) log (n/2) + c (n/4) log (n/4) + c (n/8) log (n/8) + n
a.
<= c (n/2) (log n - 1) + c (n/4) (log n - 2) + c (n/8) (log n - 3) + n
<= c n log n
sD
From the recurrence equation, it can be inferred that T (n) > = n. So, T (n) = (n)
3
Let us consider : nlog a f (n) cn
b
log a b log 2
ri
n n 3
ax
According to the law of indices: , we can write ax-y
to
ay
c n3 28
n 3
T(n) nlog28
3 T(1) h(n)
28
nlog 3 T (1) 0(1) n log 28
3
Example 2.13.12: Solve the recurrence relation T (n) = T ( n) + c. n>4
50
T (n) = T (n1/2)+ C
T (n)1/2 = T (n1/2)1/2 + C
T (n1/2) = T (n1/4)+ C + C
T (n) = T (n1/4)+ 2c
T (n) = T (n1/2)+ 3c
= T (n1/2i)+ i c
= T (n1 / n ) C log2 n
m
T n C log
n
2 n = (log n)
co
Example 2.13.13: Solve the recurrence relation
1 n 4
T(n)
a.
2 T ( n) logn
n 4
T (n)
2 2 T (n1 / 2 )1 / 2 log n
T (n) 4 2T n1 / 2
1/4
logn1 / 4 2 logn 1 / 4 logn
sD
T(n)
8 2T (n1 / 16 ) logn1 / 8 4 logn 1/ 4
2 logn1 / 2 logn
al
n
1 / 2i
= 2i T n 2
i k
i k
logn1 / 2
to
k 1
n T n1 / n n
lognk / n
2k
Tu
= (log n)
51
Chapter
3
Divide and Conquer
General Method
Divide and conquer is a design strategy which is well known to breaking down
m
efficiency barriers. When the method applies, it often leads to a large improvement in
time complexity. For example, from O (n2) to O (n log n) to sort the elements.
co
Divide and conquer strategy is as follows: divide the problem instance into two or
more smaller instances of the same problem, solve the smaller instances recursively,
and assemble the solutions to form a solution of the original instance. The recursion
stops when an instance is reached which is too small to divide. When dividing the
a.
instance, one can either use whatever division comes most easily to hand or invest
time in making the division carefully so that the assembly is simplified.
Divide : iy
Divide and conquer algorithm consists of two parts:
Divide the problem into a number of sub problems. The sub problems
un
are solved recursively.
Conquer : The solution to the original problem is then formed from the solutions
to the sub problems (patching together the answers).
Traditionally, routines in which the text contains at least two recursive calls are called
sD
divide and conquer algorithms, while routines whose text contains only one recursive
call are not. Divide–and–conquer is a very powerful use of recursion.
A control abstraction is a procedure whose flow of control is clear but whose primary
operations are specified by other procedures whose precise meanings are left
ri
undefined. The control abstraction for divide and conquer technique is DANDC(P),
where P is the problem to be solved.
to
DANDC (P)
{
if SMALL (P) then return S (p);
else
Tu
{
divide p into smaller instances p 1, p2, …. Pk, k 1;
apply DANDC to each of these sub problems;
return (COMBINE (DANDC (p1) , DANDC (p2),…., DANDC (pk));
}
}
SMALL (P) is a Boolean valued function which determines whether the input size is
small enough so that the answer can be computed without splitting. If this is so
function „S‟ is invoked otherwise, the problem „p‟ into smaller sub problems. These
sub problems p1, p2, . . . , pk are solved by recursive application of DANDC.
53
If the sizes of the two sub problems are approximately equal then the computing
time of DANDC is:
g (n) n small
T (n) =
2 T (n/2) f (n) otherwise
m
Binary Search
If we have „n‟ records which have been ordered by keys so that x1 < x2 < … < xn .
When we are given a element „x‟, binary search is used to find the corresponding
co
element from the list. In case „x‟ is present, we have to determine a value „j‟ such
that a[j] = x (successful search). If „x‟ is not in the list then j is to set to zero (un
successful search).
a.
In Binary search we jump into the middle of the file, where we find key a[mid], and
compare „x‟ with a[mid]. If x = a[mid] then the desired record has been found.
If x < a[mid] then „x‟ must be in that portion of the file that precedes a[mid], if there
iy
at all. Similarly, if a[mid] > x, then further search is only necessary in that past of
the file which follows a[mid]. If we use recursive procedure of finding the middle key
a[mid] of the un-searched portion of a file, then every un-successful comparison of
un
„x‟ with a[mid] will eliminate roughly half the un-searched portion from consideration.
Since the array size is roughly halved often each comparison between „x‟ and
a[mid], and since an array of length „n‟ can be halved only about log 2n times before
reaching a trivial length, the worst case complexity of Binary search is about log2n
sD
Algorithm Algorithm
al
BINSRCH (a, n, x)
// array a(1 : n) of elements in increasing order, n 0,
// determine whether „x‟ is present, and if so, set j such that x = a(j)
// else return j
ri
{
low :=1 ; high :=n ;
to
low and high are integer variables such that each time through the loop either „x‟ is
found or low is increased by at least one or high is decreased by at least one. Thus
we have two sequences of integers approaching each other and eventually low will
become greater than high causing termination in a finite number of steps if „x‟ is not
present.
54
Index 1 2 3 4 5 6 7 8 9
Elements -15 -6 0 7 9 23 54 82 101
m
6 9 7
8 9 8
9 9 9
co
found
Number of comparisons = 4
a.
2. Searching for x = 82 low high mid
1 9 5
6 9 7
Number of comparisons = 3
8 9
iy 8
found
un
3. Searching for x = 42 low high mid
1 9 5
6 9 7
sD
6 6 6
7 6 not found
Number of comparisons = 4
al
1 4 2
1 1 1
2 1 not found
to
Number of comparisons = 3
Continuing in this manner the number of element comparisons needed to find each of
nine elements is:
Tu
Index 1 2 3 4 5 6 7 8 9
Elements -15 -6 0 7 9 23 54 82 101
Comparisons 3 2 3 4 1 3 2 3 4
There are ten possible ways that an un-successful search may terminate depending
upon the value of x.
55
If x < a[1], a[1] < x < a[2], a[2] < x < a[3], a[5] < x < a[6], a[6] < x < a[7] or
a[7] < x < a[8] the algorithm requires 3 element comparisons to determine that „x‟
is not present. For all of the remaining possibilities BINSRCH requires 4 element
comparisons. Thus the average number of element comparisons for an unsuccessful
search is:
(3 + 3 + 3 + 4 + 4 + 3 + 3 + 3 + 4 + 4) / 10 = 34/10 = 3.4
The time complexity for a successful search is O(log n) and for an unsuccessful
search is Θ(log n).
m
Θ(1), Θ(log n), Θ(log n) Θ(log n)
Best average worst best, average and worst
co
Analysis for worst case
a.
The algorithm sets mid to [n+1 / 2]
Therefore,
T(0)
T(n)
= 0
= 1 iyif x = a [mid]
un
= 1 + T([(n + 1) / 2] – 1) if x < a [mid]
= 1 + T(n – [(n + 1)/2]) if x > a [mid]
sD
2K – 1 - 1 2K – 1 - 1
al
2K 1
ri
n 1 2 K 1 1 K – 1
Algebraically this is = 2 for K > 1
2 2
to
Giving,
T(0) = 0
Tu
k
T(2 – 1) = 1 if x = a [mid]
= 1 + T(2K - 1 – 1) if x < a [mid]
k-1
= 1 + T(2 – 1) if x > a [mid]
56
w(2k – 1) = 1 + w(2k - 1 – 1)
= 1 + [1 + w(2k - 2 –1)]
= 1 + [1 + [1 + w(2k - 3 –1)]]
= ........
= ........
= i + w(2k - i
– 1)
m
For i < k, letting i = k gives w(2k –1) = K + w(0) = k
co
w(n) = log2(n + 1) = O(log n)
a.
Although it might seem that the restriction of values of „n‟ of the form 2 K–1 weakens
the result. In practice this does not matter very much, w(n) is a monotonic
increasing function of „n‟, and hence the formula given is a good approximation even
when „n‟ is not of the form 2K–1.
iy
un
External and Internal path length:
The lines connecting nodes to their non-empty sub trees are called edges. A non-
empty binary tree with n nodes has n–1 edges. The size of the tree is the number of
nodes it contains.
sD
When drawing binary trees, it is often convenient to represent the empty sub trees
explicitly, so that they can be seen. For example:
al
b c
ri
d
to
The tree given above in which the empty sub trees appear as square nodes is as
follows:
Tu
The square nodes are called as external nodes E(T). The square node version is
sometimes called an extended binary tree. The round nodes are called internal nodes
I(T). A binary tree with n internal nodes has n+1 external nodes.
The height h(x) of node „x‟ is the number of edges on the longest path leading down
from „x‟ in the extended tree. For example, the following tree has heights written
inside its nodes:
57
1 2
0 0 1 0
0 0
The depth d(x) of node „x‟ is the number of edges on path from the root to „x‟. It is
the number of internal nodes on this path, excluding „x‟ itself. For example, the
following tree has depths written inside its nodes:
m
0
1 1
2 2 2 2
co
3 3
The internal path length I(T) is the sum of the depths of the internal nodes of „T‟:
a.
I(T) = d(x)
x I(T )
The external path length E(T) is the sum of the depths of the external nodes:
E(T) = d(x)
iy
un
x E(T )
For example, the tree above has I(T) = 4 and E(T) = 12.
sD
A binary tree T with „n‟ internal nodes, will have I(T) + 2n = E(T) external nodes.
8
ri
4 12
to
2 6 10 14
1 3 5 7 9 13
Tu
11 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16
58
Then we have,
1
CN 1 C'N 1
N
m
External path length is always 2N more than the internal path length.
co
Merge Sort
Merge sort algorithm is a classic example of divide and conquer. To sort an array,
recursively, sort its left and right halves separately and then merge them. The time
a.
complexity of merge mort in the best case, worst case and average case is O(n log n)
and the number of comparisons used is nearly optimal.
iy
This strategy is so simple, and so efficient but the problem here is that there seems
to be no easy way to merge two adjacent sorted arrays together in place (The result
must be build up in a separate array).
un
The fundamental operation in this algorithm is merging two sorted lists. Because the
lists are sorted, this can be done in one pass through the input, if the output is put in
a third list.
sD
The basic merging algorithm takes two input arrays „a‟ and ‟b‟, an output array „c‟,
and three counters, a ptr, b ptr and c ptr, which are initially set to the beginning of
their respective arrays. The smaller of a[a ptr] and b[b ptr] is copied to the next
entry in „c‟, and the appropriate counters are advanced. When either input list is
al
To illustrate how merge process works. For example, let us consider the array „a‟
containing 1, 13, 24, 26 and „b‟ containing 2, 15, 27, 38. First a comparison is done
ri
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1
h j i
Tu
and then 2 and 13 are compared. 2 is added to „c‟. Increment b ptr and c ptr.
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1 2
h j i
ptr ptr ptr
59
then 13 and 15 are compared. 13 is added to „c‟. Increment a ptr and c ptr.
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1 2 13
h j i
ptr ptr ptr
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1 2 13 15
m
h j i
ptr ptr ptr
co
24 and 27 are compared. 24 is added to „c‟. Increment a ptr and c ptr.
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
a.
1 13 24 26 2 15 27 28 1 2 13 15 24
h j i
ptr ptr ptr
iy
26 and 27 are compared. 26 is added to „c‟. Increment a ptr and c ptr.
un
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1 2 13 15 24 26
h j i
ptr ptr ptr
sD
As one of the lists is exhausted. The remainder of the b array is then copied to „c‟.
al
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 13 24 26 2 15 27 28 1 2 13 15 24 26 27 28
h j i
ptr ptr ptr
ri
to
Algorithm
{
if (low < high)
{
mid := (low + high)/2 //finds where to split the set
MERGESORT(low, mid) //sort one subset
MERGESORT(mid+1, high) //sort the other subset
MERGE(low, mid, high) // combine the results
}
}
60
m
}
else
{
co
b[i] :=a[j]; j := j + 1;
}
i := i + 1;
}
a.
if (h > mid) then
for k := j to high do
{
else
}
for k := h to mid do
iy
b[i] := a[k]; i := i + 1;
un
{
b[i] := a[K]; i := i + l;
}
for k := low to high do
sD
a[k] := b[k];
}
Example
al
7, 2, 9, 4 | 3, 8, 6, 1 1, 2, 3, 4, 6, 7, 8, 9
to
Tu
7, 2 | 9, 4 2, 4, 7, 9 3, 8 | 6, 1 1, 3, 6, 8
7 | 2 2, 7 9 | 4 4, 9 3 | 8 3, 8 6 | 1 1, 6
7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1
61
The following figure represents the sequence of recursive calls that are produced by
MERGESORT when it is applied to 8 elements. The values in each node are the values
of the parameters low and high.
1, 8
1, 4 5, 8
m
1, 2 3, 4 5, 6 7, 8
co
1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8
a.
Tree Calls of MERGE()
iy
The tree representation of the calls to procedure MERGE by MERGESORT is as
follows:
un
1, 1, 2 3, 3, 4 5, 5, 6 7, 7, 8
sD
1, 2, 4 5, 6, 8
al
1, 4, 8
ri
We will assume that „n‟ is a power of 2, so that we always split into even halves, so
we solve for the case n = 2k.
Otherwise, the time to merge sort „n‟ numbers is equal to the time to do two
recursive merge sorts of size n/2, plus the time to merge, which is linear. The
equation says this exactly:
T(1) = 1
T(n) = 2 T(n/2) + n
This is a standard recurrence relation, which can be solved several ways. We will
solve by substituting recurrence relation continually on the right–hand side.
62
T(n/2) = 2 T(n/4) + n
T(n) = 4 T(n/4) + 2n
m
= 8 T(n/8) + n
So we have,
co
T(n/4) = 2 T(n/8) + n
T(n) = 8 T(n/8) + 3n
a.
T(n) = 2k T(n/2k) + K. n
T (n) 2
log 2n
T 2 k log2 n . n
2
iy
As n = 2k, K = log2n, substituting this in the above equation
k
un
= n T(1) + n log n
= n log n + n
sD
We have assumed that n = 2k. The analysis can be refined to handle cases when „n‟
al
Although merge sort‟s running time is O(n log n), it is hardly ever used for main
ri
memory sorts. The main problem is that merging two sorted lists requires linear
extra memory and the additional work spent copying to the temporary array and
back, throughout the algorithm, has the effect of slowing down the sort considerably.
to
The Best and worst case time complexity of Merge sort is O(n log n).
The matrix multiplication of algorithm due to Strassens is the most dramatic example
of divide and conquer technique (1969).
The usual way to multiply two n x n matrices A and B, yielding result matrix „C‟ as
follows :
for i := 1 to n do
for j :=1 to n do
c[i, j] := 0;
for K: = 1 to n do
c[i, j] := c[i, j] + a[i, k] * b[k, j];
63
We apply divide and conquer to this problem. For example let us considers three
multiplication like this:
A 11 A
12 B 11 B
12
C 11 C 12
A 21 A 22 B 21 B 22 C 21 C 22
Then cij can be found by the usual matrix multiplication algorithm,
m
C11 = A11 . B11 + A12 . B21
C12 = A11 . B12 + A12 . B22
C21 = A21 . B11 + A22 . B21
co
C22 = A21 . B12 + A22 . B22
a.
multiplication by partitioning the matrices into quarters and performing eight
(n/2)x(n/2) matrix multiplications and four (n/2)x(n/2) matrix additions.
T(1)
T(n)
=
=
1
8 T(n/2)
iy
un
Which leads to T (n) = O (n3), where n is the power of 2.
Strassens insight was to find an alternative method for calculating the C ij, requiring
seven (n/2) x (n/2) matrix multiplications and eighteen (n/2) x (n/2) matrix
additions and subtractions:
sD
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P + R - Q + U.
This method is used recursively to perform the seven (n/2) x (n/2) matrix
multiplications, then the recurrence equation for the number of scalar multiplications
performed is:
64
T(1) = 1
T(n) = 7 T(n/2)
T(2k) = 7 T(2k–1)
= 72 T(2k-2)
= ------
= ------
m
= 7i T(2k–i)
Put i = k
co
= 7k T(1)
= 7k
a.
That is, T(n) = 7 log2 n
log 7
= n 2
= O(n log 7
2 )
2.81
= O(n )
iy
un
So, concluding that Strassen‟s algorithm is asymptotically more efficient than the
standard algorithm. In practice, the overhead of managing the many small matrices
does not pay off until „n‟ revolves the hundreds.
sD
Quick Sort
The main reason for the slowness of Algorithms like SIS is that all comparisons and
exchanges between keys in a sequence w1, w2, . . . . , wn take place between
adjacent pairs. In this way it takes a relatively long time for a key that is badly out of
al
place to work its way into its proper position in the sorted sequence.
Hoare his devised a very efficient way of implementing this idea in the early 1960‟s
ri
that improves the O(n2) behavior of SIS algorithm with an expected performance that
is O(n log n).
to
In essence, the quick sort algorithm partitions the original array by rearranging it
into two groups. The first group contains those elements less than some arbitrary
chosen value taken from the set, and the second group contains those elements
Tu
The chosen value is known as the pivot element. Once the array has been rearranged
in this way with respect to the pivot, the very same partitioning is recursively applied
to each of the two subsets. When all the subsets have been partitioned and
rearranged, the original array is sorted.
The function partition() makes use of two pointers „i‟ and „j‟ which are moved toward
each other in the following fashion:
65
Repeat the steps 1, 2 and 3 till the „i‟ pointer crosses the „j‟ pointer. If „i‟
pointer crosses „j‟ pointer, the position for pivot is found and place pivot
element in „j‟ pointer position.
The program uses a recursive function quicksort(). The algorithm of quick sort
function sorts all elements in an array „a‟ between positions „low‟ and „high‟.
It terminates when the condition low >= high is satisfied. This condition
will be satisfied only when the array is completely sorted.
m
Here we choose the first element as the „pivot‟. So, pivot = x[low]. Now it
calls the partition function to find the proper position j of the element
x[low] i.e. pivot. Then we will have two sub-arrays x[low], x[low+1], . . . .
co
. . . x[j-1] and x[j+1], x[j+2], . . .x[high].
a.
partition function).
iy
. . . x[high] between positions j+1 and high.
Algorithm Algorithm
un
QUICKSORT(low, high)
/* sorts the elements a(low), . . . . . , a(high) which reside in the global array A(1 :
n) into ascending order a (n + 1) is considered to be defined and must be greater
sD
}
}
to
Algorithm PARTITION(a, m, p)
{
V a(m); i m; j p; // A (m) is the partition element
do
Tu
{
loop i := i + 1 until a(i) > v // i moves left to right
loop j := j – 1 until a(j) < v // p moves right to left
if (i < j) then INTERCHANGE(a, i, j)
} while (i > j);
a[m] :=a[j]; a[j] :=V; // the partition element belongs at position P
return j;
}
66
Algorithm INTERCHANGE(a, i, j)
{
P:=a[i];
a[i] := a[j];
a[j] := p;
}
Example
Select first element as the pivot element. Move „i‟ pointer from left to right in search
of an element larger than pivot. Move the „j‟ pointer from right to left in search of an
m
element smaller than pivot. If such elements are found, the elements are swapped.
This process continues till the „i‟ pointer crosses the „j‟ pointer. If „i‟ pointer crosses „j‟
pointer, the position for pivot is found and interchange pivot and element at „j‟
co
position.
Let us consider the following example with 13 elements to analyze quick sort:
a.
1 2 3 4 5 6 7 8 9 10 11 12 13 Remarks
38
pivot
08 16 06 79
i
04
57 24
iy
56 02 58 04
j
79
70 45
swap i & j
un
i j swap i & j
02 57
j i
sD
swap pivot
(24 08 16 06 04 02) 38 (56 57 58 79 70 45)
&j
swap pivot
pivot j, i
&j
(02 08 16 06 04) 24
al
j i
swap pivot
(06 04) 08 (16)
&j
Tu
pivot,
i
j
swap pivot
(04) 06
&j
04
pivot,
j, i
16
pivot,
j, i
(02 04 06 08 16 24) 38
(56 57 58 79 70 45)
67
m
j i
swap pivot
(57) 58 (70 79)
&j
co
57
pivot,
j, i
(70 79)
a.
pivot, swap pivot
i
j &j
70
iy 79
pivot,
j, i
un
(45 56 57 58 70 79)
02 04 06 08 16 24 38 45 56 57 58 70 79
sD
Like merge sort, quick sort is recursive, and hence its analysis requires solving a
recurrence formula. We will do the analysis for a quick sort, assuming a random pivot
al
The running time of quick sort is equal to the running time of the two recursive calls
plus the linear time spent in the partition (The pivot selection takes only constant
time). This gives the basic quick sort relation:
to
The pivot is the smallest element, all the time. Then i=0 and if we ignore T(0)=1,
which is insignificant, the recurrence is:
68
T (n – 1) = T (n – 2) + C (n – 1)
T (n – 2) = T (n – 3) + C (n – 2)
------- -
n
T (n) T (1)
i
m
i 2
= O (n2) - (3)
co
Best Case Analysis
In the best case, the pivot is in the middle. To simply the math, we assume that the
a.
two sub-files are each exactly half the size of the original and although this gives a
slight over estimate, this is acceptable because we are only interested in a Big – oh
answer.
T (n) = 2 T (n/2) + C n
T(n / 2) T(n / 4)
C - (6)
n /2 n/4
al
T(n / 4) T(n / 8)
C - (7)
n/4 n/8
--------
to
--------
Continuing in this manner, we obtain:
Tu
T(2) T(1)
C - (8)
2 1
We add all the equations from 4 to 8 and note that there are log n of them:
T(n) T(1)
C log n - (9)
n 1
This is exactly the same analysis as merge sort, hence we get the same answer.
69
The number of comparisons for first call on partition: Assume left_to_right moves
over k smaller element and thus k comparisons. So when right_to_left crosses
left_to_right it has made n-k+1 comparisons. So, first call on partition makes n+1
comparisons. The average case complexity of quicksort is
m
nT(n) = n(n+1) + 2 [T(0) +T(1) + T(2) + ----- + T(n-2) + T(n-1)]
co
Subtracting both sides:
a.
nT(n) = 2n + (n-1)T(n-1) + 2T(n-1) = 2n + (n+1)T(n-1)
T(n) = 2 + (n+1)T(n-1)/n
The recurrence relation obtained is:
T(n)/(n+1) = 2/(n+1) + T(n-1)/n
iy
un
Using the method of subsititution:
1/ k
Tu
Straight insertion sort is used to create a sorted list (initially list is empty) and at
each iteration the top number on the sorted list is removed and put into its proper
70
place in the sorted list. This is done by moving along the sorted list, from the
smallest to the largest number, until the correct place for the new number is located
i.e. until all sorted numbers with smaller values comes before it and all those with
larger values comes after it. For example, let us consider the following 8 elements for
sorting:
Index 1 2 3 4 5 6 7 8
Elements 27 412 71 81 59 14 273 87
Solution:
m
Sorted 27
co
Sorted 27 412
a.
Iteration 3: unsorted 81 39 14 273 87
Sorted 27 71 81 412
Iteration 4: unsorted
Sorted
59
274
14
59 iy
273
71
87
81 412
un
Iteration 5: unsorted 14 273 87
Sorted 14 27 59 71 81 412
Iteration 7: unsorted 87
Sorted 14 27 59 71 81 87 273 412
al
ri
to
Tu
71
Chapter
4
Greedy Method
GENERAL METHOD
Greedy is the most straight forward design technique. Most of the problems have n
inputs and require us to obtain a subset that satisfies some constraints. Any subset
m
that satisfies these constraints is called a feasible solution. We need to find a feasible
solution that either maximizes or minimizes the objective function. A feasible solution
that does this is called an optimal solution.
co
The greedy method is a simple strategy of progressively building up a solution, one
element at a time, by choosing the best possible element at each stage. At each stage,
a decision is made regarding whether or not a particular input is in an optimal solution.
This is done by considering the inputs in an order determined by some selection
a.
procedure. If the inclusion of the next input, into the partially constructed optimal
solution will result in an infeasible solution then this input is not added to the partial
solution. The selection procedure itself is based on some optimization measure. Several
iy
optimization measures are plausible for a given problem. Most of them, however, will
result in algorithms that generate sub-optimal solutions. This version of greedy
technique is called subset paradigm. Some problems like Knapsack, Job sequencing
un
with deadlines and minimum cost spanning trees are based on subset paradigm.
For the problems that make decisions by considering the inputs in some order, each
decision is made using an optimization criterion that can be computed using decisions
already made. This version of greedy method is ordering paradigm. Some problems like
sD
optimal storage on tapes, optimal merge patterns and single source shortest path are
based on ordering paradigm.
CONTROL ABSTRACTION
al
{
solution := ; // initialize the solution to empty
for i:=1 to n do
to
{
x := select (a);
if feasible (solution, x) then
Tu
Procedure Greedy describes the essential way that a greedy based algorithm will look,
once a particular problem is chosen and the functions select, feasible and union are
properly implemented.
The function select selects an input from „a‟, removes it and assigns its value to „x‟.
Feasible is a Boolean valued function, which determines if „x‟ can be included into the
solution vector. The function Union combines „x‟ with solution and updates the objective
function.
72
KNAPSACK PROBLEM
Let us apply the greedy method to solve the knapsack problem. We are given „n‟
objects and a knapsack. The object „i‟ has a weight wi and the knapsack has a capacity
„m‟. If a fraction xi, 0 < xi < 1 of object i is placed into the knapsack then a profit of pi
xi is earned. The objective is to fill the knapsack that maximizes the total profit earned.
Since the knapsack capacity is „m‟, we require the total weight of all chosen objects to
be at most „m‟. The problem is stated as:
n
maximize p xi
m
i
i 1
n
subject to a i xi M where, 0 < xi < 1 and 1 < i < n
co
i 1
a.
Algorithm
iy
If the objects are already been sorted into non-increasing order of p[i] / w[i] then the
algorithm given below obtains solutions corresponding to this strategy.
{
for i := 1 to n do x[i] := 0.0 // initialize x
U := m;
for i := 1 to n do
al
{
if (w(i) > U) then break;
x [i] := 1.0; U := U – w[i];
ri
}
if (i < n) then x[i] := U / w[i];
}
to
Running time:
Tu
Example:
Consider the following instance of the knapsack problem: n = 3, m = 20, (p1, p2, p3) =
(25, 24, 15) and (w1, w2, w3) = (18, 15, 10).
73
1. First, we try to fill the knapsack by selecting the objects in some order:
x1 x2 x3 wi xi pi xi
1/2 1/3 1/4 18 x 1/2 + 15 x 1/3 + 10 x 1/4 25 x 1/2 + 24 x 1/3 + 15 x 1/4 =
= 16.5 24.25
2. Select the object with the maximum profit first (p = 25). So, x 1 = 1 and profit
earned is 25. Now, only 2 units of space is left, select the object with next largest
profit (p = 24). So, x2 = 2/15
m
x1 x2 x3 wi xi pi xi
1 2/15 0 18 x 1 + 15 x 2/15 = 20 25 x 1 + 24 x 2/15 = 28.2
co
3. Considering the objects in the order of non-decreasing weights wi.
a.
x1 x2 x3 wi xi pi xi
0 2/3 1 15 x 2/3 + 10 x 1 = 20 24 x 2/3 + 15 x 1 = 31
Sort the objects in order of the non-increasing order of the ratio pi / xi. Select the
object with the maximum pi / xi ratio, so, x2 = 1 and profit earned is 24. Now, only 5
al
units of space is left, select the object with next largest pi / xi ratio, so x3 = ½ and the
profit earned is 7.5.
x1 x2 x3 wi xi pi xi
ri
There are „n‟ programs that are to be stored on a computer tape of length „L‟. Each
program „i‟ is of length li, 1 ≤ i ≤ n. All the programs can be stored on the tape if and
only if the sum of the lengths of the programs is at most „L‟.
We shall assume that whenever a program is to be retrieved from this tape, the tape is
initially positioned at the front. If the programs are stored in the order i = i 1, i2, . . . . .
, in, the time tJ needed to retrieve program iJ is proportional to
1 k j
l ik
74
If all the programs are retrieved equally often then the expected or mean retrieval time
(MRT) is:
1
.
t
n 1 J n j
For the optimal storage on tape problem, we are required to find the permutation for
the „n‟ programs so that when they are stored on the tape in this order the MRT is
minimized.
n J
d (I) li k
J 1 K 1
m
Example
co
Let n = 3, (l1, l2, l3) = (5, 10, 3). Then find the optimal ordering?
Solution:
a.
There are n! = 6 possible orderings. They are:
Ordering I d(I)
1, 2, 3
1, 3, 2 iy
5 + (5 +10) +(5 + 10 + 3)
5 + (5 + 3) + (5 + 3 + 10)
=
=
38
31
un
2, 1, 3 10 + (10 + 5) + (10 + 5 + 3) = 43
2, 3, 1 10 + (10 + 3) + (10 + 3 + 5) = 41
3, 1, 2 3 + (3 + 5) + (3 + 5 + 10) = 29
sD
3, 2, 1 3 + (3 + 10) + (3 + 10 + 5) = 34
From the above, it simply requires to store the programs in non-decreasing order
(increasing order) of their lengths. This can be carried out by using a efficient sorting
al
algorithm (Heap sort). This ordering can be carried out in O (n log n) time using heap
sort algorithm.
ri
The tape storage problem can be extended to several tapes. If there are m 1 tapes,
To, . . . . . . ,Tm – 1, then the programs are to be distributed over these tapes.
m 1
to
The programs are to be sorted in non decreasing order of their lengths l i‟s, l1 < l2 < .. .
.. . . ln.
The first „m‟ programs will be assigned to tapes To, . . . . ,Tm-1 respectively. The next „m‟
programs will be assigned to T0, . . . . ,Tm-1 respectively. The general rule is that
program i is stored on tape Ti mod m.
75
Algorithm:
m
}
}
co
On any given tape, the programs are stored in non-decreasing order of their lengths.
a.
When we are given a set of „n‟ jobs. Associated with each Job i, deadline di > 0 and
profit Pi > 0. For any job „i‟ the profit pi is earned iff the job is completed by its
iy
deadline. Only one machine is available for processing jobs. An optimal solution is the
feasible solution with maximum profit.
un
Sort the jobs in „j‟ ordered by their deadlines. The array d [1 : n] is used to store the
deadlines of the order of their p-values. The set of jobs j [1 : k] such that j [r], 1 ≤ r ≤
k are the jobs in „j‟ and d (j [1]) ≤ d (j[2]) ≤ . . . ≤ d (j[k]). To test whether J U {i} is
feasible, we have just to insert i into J preserving the deadline ordering and then verify
that d [J[r]] ≤ r, 1 ≤ r ≤ k+1.
sD
Example:
Let n = 4, (P1, P2, P3, P4,) = (100, 10, 15, 27) and (d1 d2 d3 d4) = (2, 1, 2, 1). The
al
sequence
1 1,2 2,1 110
to
5 3,4 4,3 42
6 1 1 100
7 2 2 10
8 3 3 15
9 4 4 27
76
Algorithm:
The algorithm constructs an optimal set J of jobs that can be processed by their
deadlines.
m
if (all jobs in J U {i} can be completed by their dead lines)
then J := J U {i};
}
co
}
a.
Given „n‟ sorted files, there are many ways to pair wise merge them into a single sorted
file. As, different pairings require different amounts of computing time, we want to
iy
determine an optimal (i.e., one requiring the fewest comparisons) way to pair wise
merge „n‟ sorted files together. This type of merging is called as 2-way merge patterns.
To merge an n-record file and an m-record file requires possibly n + m record moves,
un
the obvious choice choice is, at each step merge the two smallest files together. The
two-way merge patterns can be represented by binary merge trees.
struct treenode
{
treenode * lchild;
treenode * rchild;
al
};
{
pt new treenode
(pt lchild) least (list); // merge two trees with smallest
Tu
lengths
(pt rchild) least (list);
(pt weight) ((pt lchild) weight) + ((pt rchild) weight);
insert (list, pt);
}
return least (list); // The tree left in list is the merge
tree
}
77
Example 1:
Suppose we are having three sorted files X1, X2 and X3 of length 30, 20, and 10 records
each. Merging of the files can be carried out as follows:
S.No First Merging Record moves in Second Record moves in Total no. of
first merging merging second merging records moves
1. X1 & X2 = T1 50 T1 & X3 60 50 + 60 = 110
2. X2 & X3 = T1 30 T1 & X1 60 30 + 60 = 90
m
Example 2:
co
Given five files (X1, X2, X3, X4, X5) with sizes (20, 30, 10, 5, 30). Apply greedy rule to
find optimal way of pair wise merging to give an optimal solution using binary merge
tree representation.
a.
Solution:
20 30 10 5 30
iy
un
X1 X2 X3 X4 X5
X1 X2 Z1 X5
20 30 15 30
al
5 10
ri
X2 Z2 X5
30 35 30
Tu
Z1 15 20 X1
X4 5 10 X3
78
Z2 Z3
35 60
Z1 15 20 30 30
X1 X5 X2
5 10
m
X4 X3
Merge Z2 and Z3 to get 90 record moves. This is the answer. Call this Z4.
co
Z4
95
a.
Z2 35 60 Z3
Z1 15 20
X1
30
X5
30
X2 iy
un
5 10
X4 X3
Huffman Codes
al
Suppose that we have a file only with characters a, e, i, s, t, spaces and new lines, the
frequency of appearance of a's is 10, e's fifteen, twelve i's, three s's, four t's, thirteen
banks and one newline.
to
Using a standard coding scheme, for 58 characters using 3 bits for each character, the
file requires 174 bits to represent. This is shown in table below.
Tu
79
Representing by a binary tree, the binary code for the alphabets are as follows:
m
a e i s l sp nl
The representation of each character can be found by starting at the root and recording
co
the path. Use a 0 to indicate the left branch and a 1 to indicate the right branch.
If the character ci is at depth di and occurs fi times, the cost of the code is equal to
d
a.
i fi
With this representation the total number of bits is 3x10 + 3x15 + 3x12 + 3x3 + 3x4 +
3x13 + 3x1 = 174
nl
al
a e i s l sp
The basic problem is to find the full binary tree of minimal total cost. This can be done
ri
Huffman's Algorithm:
weights of a tree is equal to the sum of the frequencies of its leaves. If the number of
characters is 'c'. c - 1 times, select the two trees T1 and T2, of smallest weight, and
form a new tree with sub-trees T1 and T2. Repeating the process we will get an optimal
Huffman coding tree.
Example:
10 15 12 3 4 13 1
a e i s t sp nl
80
The two trees with the lowest weight are merged together, creating the forest, the
Huffman algorithm after the first merge with new root T1 is as follows: The total weight
of the new tree is the sum of the weights of the old trees.
10 15 12 4 13 4
a e i t sp T1
s nl
m
We again select the two trees of smallest weight. This happens to be T1 and t, which
are merged into a new tree with root T2 and weight 8.
co
10 15 12 13 8
a e i sp T2
a.
T1 t
s nl
In next step we merge T2 and a creating T3, with weight 10+8=18. The result of this
operation in iy
un
15 12 13 18
e i sp T3
sD
T2 a
T1 t
al
s nl
After third merge, the two trees of lowest weight are the single node trees representing
ri
i and the blank space. These trees merged into the new tree with root T4.
to
15 25 18
e T4 T3
Tu
i sp T2 a
T1 t
s nl
81
The fifth step is to merge the trees with roots e and T3. The results of this step is
25 33
T4 T5
i sp T3 e
T2 a
m
T1 t
s nl
co
Finally, the optimal tree is obtained by merging the two remaining trees. The optimal
trees with root T6 is:
a.
T6
0 1
0
T5
1 0
T4
1
iy
un
T3 e i sp
0 1
T2 a
0 1
sD
T1 t
0 1
s nl
al
The full binary tree of minimal total cost, where all characters are obtained in the
leaves, uses only 146 bits.
ri
E 01 15 30
I 10 12 24
S 00000 3 15
T 0001 4 16
Space 11 13 26
New line 00001 1 5
Total : 146
82
GRAPH ALGORITHMS
Basic Definitions:
Graph G is a pair (V, E), where V is a finite set (set of vertices) and E is a finite
set of pairs from V (set of edges). We will often denote n := |V|, m := |E|.
m
The graph which has such function assigned is called weighted.
co
deg(v)). The number of incoming edges to a vertex v is called in–degree of
the vertex (denote indeg(v)). The number of outgoing edges from a vertex is
called out-degree (denote outdeg(v)).
a.
Representation of Graphs:
iy
Adjacency matrix represents the graph as an n x n matrix A = (ai,j), where
un
1, if (vi , v j ) E,
a i, j
0, otherwise
We may consider various modifications. For example for weighted graphs, we may
have
al
w (vi, v j ), if (vi , v j ) E,
a i, j
default, otherwise,
ri
Where default is some sensible value based on the meaning of the weight function
(for example, if weight function represents length, then default can be , meaning
value larger than any other value).
to
Adjacency List: An array Adj [1 . . . . . . . n] of pointers where for 1 < v < n, Adj [v]
points to a linked list containing the vertices which are adjacent to v (i.e. the vertices
Tu
that can be reached from v by a single edge). If the edges have weights then these
weights may also be stored in the linked list elements.
83
A path is a sequence of vertices (v1, v2, . . . . . . , vk), where for all i, (vi, vi+1) E. A
path is simple if all vertices in the path are distinct.
A (simple) cycle is a sequence of vertices (v 1, v2, . . . . . . , vk, vk+1 = v1), where for
all i, (vi, vi+1) E and all vertices in the cycle are distinct except pair v 1, vk+1.
m
E.
The undirected graph G is connected, if for every pair of vertices u, v there exists a
co
path from u to v. If a graph is not connected, the vertices of the graph can be divided
into connected components. Two vertices are in the same connected component iff
they are connected by a path.
a.
Tree is a connected acyclic graph. A spanning tree of a graph G = (V, E) is a tree
that contains all vertices of V and is a subgraph of G. A single graph can have multiple
spanning trees.
iy
Lemma 1: Let T be a spanning tree of a graph G. Then
1. Any two vertices in T are connected by a unique simple path.
un
2. If any edge is removed from T, then T becomes disconnected.
3. If we add any edge into T, then the new graph will contain a cycle.
4. Number of edges in T is n-1.
sD
A spanning tree for a connected graph is a tree whose vertex set is the same as the
al
vertex set of the given graph, and whose edge set is a subset of the edge set of the
given graph. i.e., any connected graph will have a spanning tree.
ri
Weight of a spanning tree w (T) is the sum of weights of all edges in T. The Minimum
spanning tree (MST) is a spanning tree with the smallest possible weight.
to
Tu
84
G:
A gra p h G:
T hre e ( of ma n y p o s s ib le) s p a n n in g t re e s fro m gra p h G:
2 2
m
4
G: 3 5 3
6
co
1 1
a.
Here are some examples:
iy
To explain further upon the Minimum Spanning Tree, and what it applies to, let's
consider a couple of real-world examples:
un
1. One practical application of a MST would be in the design of a network. For
instance, a group of individuals, who are separated by varying distances, wish
to be connected together in a telephone network. Although MST cannot do
anything about the distance from one connection to another, it can be used to
determine the least cost paths with no cycles in this network, thereby
sD
2. Another useful application of MST would be finding airline routes. The vertices of
the graph would represent cities, and the edges would represent routes between
al
the cities. Obviously, the further one has to travel, the more it will cost, so MST
can be applied to optimize airline routes by finding the least costly paths with no
cycles.
ri
To explain how to find a Minimum Spanning Tree, we will look at two algorithms: the
Kruskal algorithm and the Prim algorithm. Both algorithms differ in their methodology,
to
but both eventually end up with the MST. Kruskal's algorithm uses edges, and Prim‟s
algorithm uses vertex connections in determining the MST.
Tu
Kruskal’s Algorithm
This is a greedy algorithm. A greedy algorithm chooses some local optimum (i.e.
picking an edge with the least weight in a MST).
Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep on adding the
shortest (least cost) edge, while avoiding the creation of cycles, until (n - 1) edges
have been added. Sometimes two or more edges may have the same cost. The order in
which the edges are chosen, in this case, does not matter. Different MSTs may result,
but they will all have the same total cost, which will always be the minimum cost.
85
Algorithm:
The algorithm for finding the MST, using the Kruskal‟s method is as follows:
m
// Each vertex is in a different set.
i := 0; mincost := 0.0;
while ((i < n -1) and (heap not empty)) do
co
{
Delete a minimum cost edge (u, v) from the heap and
re-heapify using Adjust;
j := Find (u); k := Find (v);
a.
if (j k) then
{
i := i + 1;
}
iy
t [i, 1] := u; t [i, 2] := v;
mincost :=mincost + cost [u, v];
Union (j, k);
un
}
if (i n-1) then write ("no spanning tree");
else return mincost;
}
sD
Running time:
The number of finds is at most 2e, and the number of unions at most n-1.
al
Including the initialization time for the trees, this part of the algorithm has a
complexity that is just slightly more than O (n + e).
ri
We can add at most n-1 edges to tree T. So, the total time for operations on T is
O(n).
to
Example 1:
10 50
1 2
4 5 40 3
30 35
4 25 5
55
20 15
6
86
Cost 10 15 20 25 30 35 40 45 50 55
Edge (1, 2) (3, 6) (4, 6) (2, 6) (1, 4) (3, 5) (2, 5) (1, 5) (2, 3) (5, 6)
The edge set T together with the vertices of G define a graph that has up to n
connected components. Let us represent each component by a set of vertices in it.
These vertex sets are disjoint. To determine whether the edge (u, v) creates a cycle,
we need to check whether u and v are in the same vertex set. If so, then a cycle is
created. If not then no cycle is created. Hence two Finds on the vertex sets suffice.
When an edge is included in T, two components are combined into one and a union is
to be performed on the two sets.
m
Edge Cost Spanning Forest Edge Sets Remarks
co
1 2 3 4 5 6 {1}, {2}, {3},
{4}, {5}, {6}
a.
(1, 2) 10 1 2 3 4 5 6 {1, 2}, {3}, {4}, The vertices 1 and
{5}, {6} 2 are in different
4 6
is combined
ri
4
is combined
6
Tu
87
A given graph can have many spanning trees. From these many spanning trees, we
have to select a cheapest one. This tree is called as minimal cost spanning tree.
Minimal cost spanning tree is a connected undirected graph G in which each edge is
labeled with a number (edge labels may signify lengths, weights other than costs).
Minimal cost spanning tree is a spanning tree for which the sum of the edge labels is as
small as possible
The slight modification of the spanning tree algorithm yields a very simple algorithm for
finding an MST. In the spanning tree algorithm, any vertex not in the tree but
m
connected to it by an edge can be added. To find a Minimal cost spanning tree, we
must be selective - we must always add a new vertex for which the cost of the new
edge is as small as possible.
co
This simple modified algorithm of spanning tree is called prim's algorithm for finding an
Minimal cost spanning tree.
a.
Prim's algorithm is an example of a greedy algorithm.
(E, cost, n, t) iy
// E is the set of edges in G. cost [1:n, 1:n] is the cost
un
// adjacency matrix of an n vertex graph such that cost [i, j] is
// either a positive real number or if no edge (i, j) exists.
// A minimum spanning tree is computed and stored as a set of
// edges in the array t [1:n-1, 1:2]. (t [i, 1], t [i, 2]) is an edge in
sD
near [j] := 0
for k:= 1 to n do // Update near[].
if ((near [k] 0) and (cost [k, near [k]] > cost [k, j]))
then near [k] := j;
}
return mincost;
}
88
Running time:
For each vertex u in the graph we dequeue it and check all its neighbors in (1 + deg
(u)) time. Therefore the running time is:
1 degv n degv (n m)
m
v V v V
co
EXAMPLE 1:
Use Prim‟s Algorithm to find a minimal spanning tree for the graph shown below
starting with the vertex A.
a.
4
B D
A
3 2
C
1
4
2
E
2
4
G
iy
un
6
2 F 1
SOLUTION:
sD
0 3 6
3 0 2 4
6 2 0 1 4 2
The cost adjacency matrix is 4
al
4 1 0 2
4 2 0 2 1
2 2 0 1
ri
4 1 1
0
to
Step 1:
B 3 D Vertex A B C D E F G
Status 0 1 1 1 1 1 1
E Dist. 0 3 6
A 0 G Next * A A A A A A
6
F
C
89
Step 2:
4 D Vertex A B C D E F G
B 3
Status 0 0 1 1 1 1 1
Dist. 0 3 2 4
E
Next * A B B A A A
A 0 2 G
C
F
m
Step 3:
Vertex A B C D E F G
B 3 1 D
Status 0 0 0 1 1 1 1
co
Dist. 0 3 2 1 4 2
4 E Next * A B C C C A
A 0 2 G
a.
C 2 F
Step 4:
B 3 1 D
iy
Vertex
Status
A
0
B
0
C
0
D
0
E
1
F
1
G
1
un
2 E Dist. 0 3 2 1 2 2 4
A 0 2 4 G Next * A B C D C D
C 2 F
sD
Step 5:
Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 1 0 1
al
Dist. 0 3 2 1 2 2 1
2 E Next * A B C D C E
A 0 2 1 G
ri
C 2 F
Step 6:
to
Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 0 1 0
Tu
Dist. 0 3 2 1 2 1 1
2 E Next * A B C D G E
A 0 2 1 G
C 1 F
Step 7:
1 D Vertex A B C D E F G
B 3
Status 0 0 0 0 0 0 0
Dist. 0 3 2 1 2 1 1
2 E
A 0 2 1 G Next * A B C D G E
C 1 F
90
EXAMPLE 2:
Considering the following graph, find the minimal spanning tree using prim‟s algorithm.
8
1 4 4
9
4 3 5
1
2 3 3
4
m
4 9 8
co
4 4 1
The cost adjacent matrix is 9 4 3 3
8 1 3 4
a.
3 4
The minimal spanning tree obtained as:
Vertex 1 Vertex 2 1 iy 4
un
2 4
4 1 3 5
3 4 3
5 3 2 3
sD
1 2
The algorithm starts by selecting the minimum cost from the graph. The minimum cost
to
K = 2, l = 4
Min cost = cost (2, 4) = 1
Tu
T [1, 1] = 2
T [1, 2] = 4
91
i=2
is cost (2, 4) < cost (2, 2) 2 4
m
1 < , Yes
So near [2] = 4 1 2 3 4 5
co
i=3
is cost (3, 4) < cost (3, 2) 2 4 4
1 < 4, Yes
a.
So near [3] = 4 1 2 3 4 5
i=4
is cost (4, 4) < cost (4, 2)
< 1, no
So near [4] = 2
2
1
4
2 3
4
iy
2
4 5
un
i=5
is cost (5, 4) < cost (5, 2) 2 4 4 2 4
sD
4 < , yes
So near [5] = 4 1 2 3 4 5
end
al
2 0 4 0 4
near [k] = near [l] = 0
near [2] = near[4] = 0 1 2 3 4 5
ri
i=2
for j = 1 to 5
Tu
j=1
near(1)0 and cost(1, near(1))
2 0 and cost (1, 2) = 4
j=2
near (2) = 0
j=3
is near (3) 0
4 0 and cost (3, 4) = 3
92
j=4
near (4) = 0
J=5
Is near (5) 0
4 0 and cost (4, 5) = 4
m
=1+3=4 T (2, 1) = 3
T (2, 2) = 4
T (2, 1) = 3
co
T (2, 2) = 4
2 0 0 0 4
Near [j] = 0 1 2 3 4 5
a.
i.e. near (3) =0
for (k = 1 to n)
K=1
is near (1) 0, yes
iy
un
20
and cost (1,2) > cost(1, 3)
4 > 9, No
sD
K=2
Is near (2) 0, No
K=3
Is near (3) 0, No
al
K=4
Is near (4) 0, No
ri
K=5 2 0 0 0 3
to
Is near (5) 0
4 0, yes 1 2 3 4 5
and is cost (5, 4) > cost (5, 3)
4 > 3, yes
Tu
i=3
for (j = 1 to 5)
J=1
is near (1) 0
20
cost (1, 2) = 4
J=2
Is near (2) 0, No
93
J=3
Is near (3) 0, no
Near (3) = 0
J=4
Is near (4) 0, no
Near (4) = 0
J=5
Is near (5) 0
Near (5) = 3 3 0, yes
And cost (5, 3) = 3
m
Choosing the min cost from
the above obtaining costs
co
which is 3 and corresponding J
=5 T (3, 1) = 5
T (3, 2) = 3
Min cost = 4 + cost (5, 3)
a.
=4+3=7
T (3, 1) = 5
T (3, 2) = 3
k=1
is near (1) 0, yes
sD
K=2
Is near (2) 0 no
al
K=3
Is near (3) 0 no
ri
K=4
Is near (4) 0 no
to
K=5
Is near (5) 0 no
Tu
i=4
for J = 1 to 5
J=1
Is near (1) 0
2 0, yes
cost (1, 2) = 4
j=2
is near (2) 0, No
94
J=3
Is near (3) 0, No
Near (3) = 0
J=4
Is near (4) 0, No
Near (4) = 0
J=5
Is near (5) 0, No
Near (5) = 0
m
Choosing min cost from the
above it is only '4' and
corresponding J = 1
co
Min cost = 7 + cost (1,2)
= 7+4 = 11 0 0 0 0 0
a.
T (4, 1) = 1 T (4, 1) = 1
T (4, 2) = 2 1 2 3 4 5 T (4, 2) = 2
for (k = 1 to 5)
iy
un
K=1
Is near (1) 0, No
K=2
sD
Is near (2) 0, No
K=3
Is near (3) 0, No
al
K=4
Is near (4) 0, No
ri
K=5
Is near (5) 0, No
to
End.
Tu
In the previously studied graphs, the edge labels are called as costs, but here we think
them as lengths. In a labeled graph, the length of the path is defined to be the sum of
the lengths of its edges.
In the single source, all destinations, shortest path problem, we must find a shortest
path from a given source vertex to each of the vertices (called destinations) in the
graph to which there is a path.
Dijkstra‟s algorithm is similar to prim's algorithm for finding minimal spanning trees.
Dijkstra‟s algorithm takes a labeled graph and a pair of vertices P and Q, and finds the
95
shortest path between then (or one of the shortest paths) if there is more than one.
The principle of optimality is the basis for Dijkstra‟s algorithms.
The figure lists the shortest paths from vertex 1 for a five vertex weighted digraph.
8 0 1
4 2 1 3
1 2 5
m
2 4 5 3 1 3 4
3 4 3
1 4 1 2
co
Graph
6 1 3 4 5
Shortest Paths
a.
Algorithm:
iy
// dist [j], 1 < j < n, is set to the length of the shortest path
// from vertex v to vertex j in the digraph G with n vertices.
// dist [v] is set to zero. G is represented by its
un
// cost adjacency matrix cost [1:n, 1:n].
{
for i :=1 to n do
{
S [i] := false; // Initialize S.
sD
if (dist [w] > (dist [u] + cost [u, w]) then // Update distances
dist [w] := dist [u] + cost [u, w];
}
to
}
Tu
Running time:
96
Example 1:
Use Dijkstras algorithm to find the shortest path from A to each of the other six
vertices in the graph:
4
B D
4
3 2 1 2
4 E 1
A C 2 G
6
2 F 1
m
Solution:
co
0 3 6
3 0 2 4
6 2 0 1 4 2
a.
The cost adjacency matrix is 4 1 0 2 4
4 2 0 2 1
2 2 0 1
4
iy
1 1
0
un
The problem is solved by considering the following information:
Status[v] will be either „0‟, meaning that the shortest path from v to v0 has
definitely been found; or „1‟, meaning that it hasn‟t.
sD
Dist[v] will be a number, representing the length of the shortest path from v to
v0 found so far.
Next[v] will be the first vertex on the way to v0 along the shortest path found so
far from v to v0
al
Step 1:
to
B 3 D Vertex A B C D E F G
Status 0 1 1 1 1 1 1
E Dist. 0 3 6
Tu
G Next * A A A A A A
A 0 6
F
C
Step 2:
4 7 D Vertex A B C D E F G
B 3
Status 0 0 1 1 1 1 1
2
Dist. 0 3 5 7
E
Next * A B B A A A
A 0 5 G
C
F
97
Step 3:
Vertex A B C D E F G
B 3 6 D
Status 0 0 0 1 1 1 1
Dist. 0 3 5 6 9 7
9 E G Next * A B C C C A
A 0 5
F 7
C
m
Step 4:
co
B 3 7 D Vertex A B C D E F G
Status 0 0 0 0 1 1 1
8 E Dist. 0 3 5 6 8 7 10
0 Next * A B C D C D
a.
A 5 10 G
C 7 F
Step 5:
iy
un
Vertex A B C D E F G
B 3 6 D
Status 0 0 0 0 1 0 1
Dist. 0 3 5 6 8 7 8
8 E
sD
Next * A B C D C F
A 0 5 8 G
C 7 F
al
Step 6:
Vertex A B C D E F G
ri
B 3 8 D
Status 0 0 0 0 0 0 1
Dist. 0 3 5 6 8 7 8
8 E Next * A B C D C F
to
A 0 5 8 G
C 7 F
Tu
Step 7:
B 3 9 D Vertex A B C D E F G
Status 0 0 0 0 0 0 0
Dist. 0 3 5 6 8 7 8
8 E
A 0 5 8 G Next * A B C D C F
C 7 F
98
Chapter
5
Dynamic Programming
Dynamic programming is a name, coined by Richard Bellman in 1955. Dynamic
programming, as greedy method, is a powerful algorithm design technique that can
be used when the solution to the problem may be viewed as the result of a sequence
of decisions. In the greedy method we make irrevocable decisions one at a time,
m
using a greedy criterion. However, in dynamic programming we examine the decision
sequence to see whether an optimal decision sequence contains optimal decision
subsequence.
co
When optimal decision sequences contain optimal decision subsequences, we can
establish recurrence equations, called dynamic-programming recurrence equations,
that enable us to solve the problem in an efficient way.
a.
Dynamic programming is based on the principle of optimality (also coined by
Bellman). The principle of optimality states that no matter whatever the initial state
iy
and initial decision are, the remaining decision sequence must constitute an optimal
decision sequence with regard to the state resulting from the first decision. The
principle implies that an optimal decision sequence is comprised of optimal decision
un
subsequences. Since the principle of optimality may not hold for some formulations
of some problems, it is necessary to verify that it does hold for the problem being
solved. Dynamic programming cannot be applied when this principle does not hold.
Dynamic programming differs from the greedy method since the greedy method
to
produces only one feasible solution, which may or may not be optimal, while dynamic
programming produces all possible sub-problems at most once, one of which
guaranteed to be optimal. Optimal solutions to sub-problems are retained in a table,
Tu
thereby avoiding the work of recomputing the answer every time a sub-problem is
encountered
The divide and conquer principle solve a large problem, by breaking it up into smaller
problems which can be solved independently. In dynamic programming this principle
is carried to an extreme: when we don't know exactly which smaller problems to
solve, we simply solve them all, then store the answers away in a table to be used
later in solving larger problems. Care is to be taken to avoid recomputing previously
computed values, otherwise the recursive program will have prohibitive complexity.
In some cases, the solution can be improved and in other cases, the dynamic
programming technique is the best approach.
99
m
5.1 MULTI STAGE GRAPHS
co
A multistage graph G = (V, E) is a directed graph in which the vertices are
partitioned into k > 2 disjoint sets Vi, 1 < i < k. In addition, if <u, v> is an edge in E,
then u Vi and v Vi+1 for some i, 1 < i < k.
a.
Let the vertex „s‟ is the source, and „t‟ the sink. Let c (i, j) be the cost of edge <i, j>.
The cost of a path from „s‟ to „t‟ is the sum of the costs of the edges on the path. The
iy
multistage graph problem is to find a minimum cost path from „s‟ to „t‟. Each set Vi
defines a stage in the graph. Because of the constraints on E, every path from „s‟ to
„t‟ starts in stage 1, goes to stage 2, then to stage 3, then to stage 4, and so on, and
eventually terminates in stage k.
un
A dynamic programming formulation for a k-stage graph problem is obtained by first
noticing that every s to t path is the result of a sequence of k – 2 decisions. The ith
decision involves determining which vertex in vi+1, 1 < i < k - 2, is to be on the
sD
path. Let c (i, j) be the cost of the path from source to destination. Then using the
forward approach, we obtain:
<j, l> E
ALGORITHM:
ri
100
The multistage graph problem can also be solved using the backward approach.
Let bp(i, j) be a minimum cost path from vertex s to j vertex in Vi. Let Bcost(i, j) be
the cost of bp(i, j). From the backward approach we obtain:
m
Bcost [1] := 0.0;
for j := 2 to n do
{ // Compute Bcost [j].
co
Let r be such that (r, j) is an edge of
G and Bcost [r] + c [r, j] is minimum;
Bcost [j] := Bcost [r] + c [r, j];
D [j] := r;
a.
} //find a minimum cost path
p [1] := 1; p [k] := n;
for j:= k - 1 to 2 do p [j] := d [p [j + 1]];
}
Complexity Analysis:
iy
un
The complexity analysis of the algorithm is fairly straightforward. Here, if G has E
edges, then the time for the first for loop is (V +E).
sD
EXAMPLE 1:
Find the minimum cost path from s to t in the multistage graph of five stages shown
al
below. Do this first using forward approach and then using backward approach.
ri
2 4 6
2 6 9
9 2 5 4
1
to
3 4
7 7 2
7 10 12 t
s 1 3 3
Tu
4 11
2 5
5
8 11
11 6
5 8
FORWARD APPROACH:
We use the following equation to find the minimum cost path from s to t:
101
<j, l> E
cost (1, 1) = min {c (1, 2) + cost (2, 2), c (1, 3) + cost (2, 3), c (1, 4) + cost (2, 4),
c (1, 5) + cost (2, 5)}
= min {9 + cost (2, 2), 7 + cost (2, 3), 3 + cost (2, 4), 2 + cost (2, 5)}
cost (2, 2) = min{c (2, 6) + cost (3, 6), c (2, 7) + cost (3, 7), c (2, 8) + cost (3, 8)}
= min {4 + cost (3, 6), 2 + cost (3, 7), 1 + cost (3, 8)}
m
cost (3, 6) = min {c (6, 9) + cost (4, 9), c (6, 10) + cost (4, 10)}
= min {6 + cost (4, 9), 5 + cost (4, 10)}
co
cost (4, 10) = min {c (10, 12) + cost (5, 12)} = 2
a.
cost (3, 7) = min {c (7, 9) + cost (4, 9) , c (7, 10) + cost (4, 10)}
iy
= min {4 + cost (4, 9), 3 + cost (4, 10)}
cost (3, 8) = min {c (8, 10) + cost (4, 10), c (8, 11) + cost (4, 11)}
= min {5 + cost (4, 10), 6 + cost (4 + 11)}
al
Therefore, cost (2, 3) = min {c (3, 6) + cost (3, 6), c (3, 7) + cost (3, 7)}
= min {2 + cost (3, 6), 7 + cost (3, 7)}
Tu
102
The path is 1 2 7 10 12
or
1 3 6 10 12
BACKWARD APPROACH:
m
We use the following equation to find the minimum cost path from t to s:
co
l vi – 1
<l, j> E
Bcost (5, 12) = min {Bcost (4, 9) + c (9, 12), Bcost (4, 10) + c (10, 12),
Bcost (4, 11) + c (11, 12)}
a.
= min {Bcost (4, 9) + 4, Bcost (4, 10) + 2, Bcost (4, 11) + 5}
Bcost (4, 9) = min {Bcost (3, 6) + c (6, 9), Bcost (3, 7) + c (7, 9)}
iy
= min {Bcost (3, 6) + 6, Bcost (3, 7) + 4}
Bcost (3, 6) = min {Bcost (2, 2) + c (2, 6), Bcost (2, 3) + c (3, 6)}
un
= min {Bcost (2, 2) + 4, Bcost (2, 3) + 2}
Bcost (3, 7) = min {Bcost (2, 2) + c (2, 7), Bcost (2, 3) + c (3, 7),
al
Bcost (4, 10) = min {Bcost (3, 6) + c (6, 10), Bcost (3, 7) + c (7, 10),
Bcost (3, 8) + c (8, 10)}
Tu
Bcost (3, 8) = min {Bcost (2, 2) + c (2, 8), Bcost (2, 4) + c (4, 8),
Bcost (2, 5) + c (5, 8)}
Bcost (2, 4) = min {Bcost (1, 1) + c (1, 4)} = 3
Bcost (4, 11) = min {Bcost (3, 8) + c (8, 11)} = min {Bcost (3, 8) + 6}
= min {10 + 6} = 16
103
Bcost (5, 12) = min {15 + 4, 14 + 2, 16 + 5} = min {19, 16, 21} = 16.
EXAMPLE 2:
Find the minimum cost path from s to t in the multistage graph of five stages shown
below. Do this first using forward approach and then using backward approach.
4 1
3
2 4 7
6 7
5
3 6
m
s 1 5 2 9 t
2 5
3
co
3 8
6
8 2
6
a.
SOLUTION:
FORWARD APPROACH:
cost (1, 1) = min {c (1, 2) + cost (2, 2), c (1, 3) + cost (2, 3)}
= min {5 + cost (2, 2), 2 + cost (2, 3)}
sD
cost (2, 2) = min {c (2, 4) + cost (3, 4), c (2, 6) + cost (3, 6)}
= min {3+ cost (3, 4), 3 + cost (3, 6)}
cost (3, 4) = min {c (4, 7) + cost (4, 7), c (4, 8) + cost (4, 8)}
= min {(1 + cost (4, 7), 4 + cost (4, 8)}
al
cost (3, 6) = min {c (6, 7) + cost (4, 7), c (6, 8) + cost (4, 8)}
= min {6 + cost (4, 7), 2 + cost (4, 8)} = min {6 + 7, 2 + 3} = 5
Tu
cost (2, 3) = min {c (3, 4) + cost (3, 4), c (3, 5) + cost (3, 5), c (3, 6) + cost
(3,6)}
cost (3, 5) = min {c (5, 7) + cost (4, 7), c (5, 8) + cost (4, 8)}= min {6 + 7, 2 + 3}
=5
BACKWARD APPROACH:
Bcost (5, 9) = min {Bcost (4, 7) + c (7, 9), Bcost (4, 8) + c (8, 9)}
= min {Bcost (4, 7) + 7, Bcost (4, 8) + 3}
m
Bcost (4, 7) = min {Bcost (3, 4) + c (4, 7), Bcost (3, 5) + c (5, 7),
Bcost (3, 6) + c (6, 7)}
= min {Bcost (3, 4) + 1, Bcost (3, 5) + 6, Bcost (3, 6) + 6}
co
Bcost (3, 4) = min {Bcost (2, 2) + c (2, 4), Bcost (2, 3) + c (3, 4)}
= min {Bcost (2, 2) + 3, Bcost (2, 3) + 6}
a.
Bcost (2, 3) = min (Bcost (1, 1) + c (1, 3)} = min {0 + 2} = 2
iy
Therefore, Bcost (3, 4) = min {5 + 3, 2 + 6} = min {8, 8} = 8
Bcost (4, 8) = min {Bcost (3, 4) + c (4, 8), Bcost (3, 5) + c (5, 8),
Bcost (3, 6) + c (6, 8)}
= min {8 + 4, 7 + 2, 10 + 2} = 9
al
In the all pairs shortest path problem, we are to find a shortest path between every
to
pair of vertices in a directed graph G. That is, for every pair of vertices (i, j), we are
to find a shortest path from i to j as well as one from j to i. These two paths are the
same when G is undirected.
Tu
When no edge has a negative length, the all-pairs shortest path problem may be
solved by using Dijkstra‟s greedy single source algorithm n times, once with each of
the n vertices as the source vertex.
The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the
length of a shortest path from i to j. The matrix A can be obtained by solving n
single-source problems using the algorithm shortest Paths. Since each application of
this procedure requires O (n2) time, the matrix A can be obtained in O (n3) time.
105
The dynamic programming solution, called Floyd‟s algorithm, runs in O (n3) time.
Floyd‟s algorithm works even when the graph has negative length edges (provided
there are no negative length cycles).
m
Ak (i, j) = {min {min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n
co
Algorithm All Paths (Cost, A, n)
// cost [1:n, 1:n] is the cost adjacency matrix of a graph which
// n vertices; A [I, j] is the cost of a shortest path from vertex
a.
// i to vertex j. cost [i, i] = 0.0, for 1 < i < n.
{
for i := 1 to n do
for j:= 1 to n do
A [i, j] := cost [i, j];
for k := 1 to n do iy // copy cost into A.
un
for i := 1 to n do
for j := 1 to n do
A [i, j] := min (A [i, j], A [i, k] + A [k, j]);
}
sD
Example 1:
Given a weighted digraph G = (V, E) with weight. Determine the length of the
ri
shortest path between all pairs of vertices in G. Here we assume that there are no
cycles with zero or negative cost.
to
6
1 2 0 4 11
4
2
Cost adjacency matrix (A0) = 6 0
Tu
3 1 1 2
3 0
3
General formula: min {Ak-1 (i, k) + Ak-1 (k, j)}, c (i, j)}
1<k<n
106
m
A1 (3, 2) = min {(Ao (3, 1) + Ao (1, 2)), c (3, 2)} = min {(3 + 4), } = 7
A1 (3, 3) = min {(Ao (3, 1) + Ao (1, 3)), c (3, 3)} = min {(3 + 11), 0} = 0
co
0 4 11
(1)
A = 2
6 0
3 7 0
a.
Step 2: Solving the equation for, K = 2;
iy
A2 (1, 1) = min {(A1 (1, 2) + A1 (2, 1), c (1, 1)} = min {(4 + 6), 0} = 0
A2 (1, 2) = min {(A1 (1, 2) + A1 (2, 2), c (1, 2)} = min {(4 + 0), 4} = 4
un
A2 (1, 3) = min {(A1 (1, 2) + A1 (2, 3), c (1, 3)} = min {(4 + 2), 11} = 6
A2 (2, 1) = min {(A (2, 2) + A (2, 1), c (2, 1)} = min {(0 + 6), 6} = 6
A2 (2, 2) = min {(A (2, 2) + A (2, 2), c (2, 2)} = min {(0 + 0), 0} = 0
sD
A2 (2, 3) = min {(A (2, 2) + A (2, 3), c (2, 3)} = min {(0 + 2), 2} = 2
A2 (3, 1) = min {(A (3, 2) + A (2, 1), c (3, 1)} = min {(7 + 6), 3} = 3
A2 (3, 2) = min {(A (3, 2) + A (2, 2), c (3, 2)} = min {(7 + 0), 7} = 7
A2 (3, 3) = min {(A (3, 2) + A (2, 3), c (3, 3)} = min {(7 + 2), 0} = 0
al
0 4 6
ri
A(2) = 2
6 0
3 7 0
to
Step 3: Solving the equation for, k = 3;
A3 (1, 1) = min {A2 (1, 3) + A2 (3, 1), c (1, 1)} = min {(6 + 3), 0} = 0
Tu
A3 (1, 2) = min {A2 (1, 3) + A2 (3, 2), c (1, 2)} = min {(6 + 7), 4} = 4
A3 (1, 3) = min {A2 (1, 3) + A2 (3, 3), c (1, 3)} = min {(6 + 0), 6} = 6
A3 (2, 1) = min {A2 (2, 3) + A2 (3, 1), c (2, 1)} = min {(2 + 3), 6} = 5
A3 (2, 2) = min {A2 (2, 3) + A2 (3, 2), c (2, 2)} = min {(2 + 7), 0} = 0
A3 (2, 3) = min {A2 (2, 3) + A2 (3, 3), c (2, 3)} = min {(2 + 0), 2} = 2
A3 (3, 1) = min {A2 (3, 3) + A2 (3, 1), c (3, 1)} = min {(0 + 3), 3} = 3
A3 (3, 2) = min {A2 (3, 3) + A2 (3, 2), c (3, 2)} = min {(0 + 7), 7} = 7
107
A3 (3, 3) = min {A2 (3, 3) + A2 (3, 3), c (3, 3)} = min {(0 + 0), 0} = 0
0 4 6
(3)
A = 5 0 2
3 7 0
TRAVELLING SALESPERSON PROBLEM
Let G = (V, E) be a directed graph with edge costs Cij. The variable cij is defined such
that cij > 0 for all I and j and cij = if < i, j> E. Let |V| = n and assume n > 1. A
m
tour of G is a directed simple cycle that includes every vertex in V. The cost of a tour
is the sum of the cost of the edges on the tour. The traveling sales person problem is
to find a tour of minimum cost. The tour is to be a simple path that starts and ends
at vertex 1.
co
Let g (i, S) be the length of shortest path starting at vertex i, going through all
vertices in S, and terminating at vertex 1. The function g (1, V – {1}) is the length of
an optimal salesperson tour. From the principal of optimality it follows that:
a.
g 1, V - 1 min c1k g k, V 1, k -- 1
2 k n
The Equation can be solved for g (1, V – 1}) if we know g (k, V – {1, k}) for all
choices of k.
sD
Complexity Analysis:
For each value of |S| there are n – 1 choices for i. The number of distinct sets S of
n 2
al
is:
n 1 n 2
n 1 k
to
k 0
To calculate this sum, we use the binominal theorem:
Tu
n 1 n 2
k 0
n 1 k (n 1) 2n 2
This is Φ (n 2n-2), so there are exponential number of calculate. Calculating one g (i,
S) require finding the minimum of at most n quantities. Therefore, the entire
algorithm is Φ (n2 2n-2). This is better than enumerating all n! different tours to find
the best one. So, we have traded on exponential growth for a much smaller
exponential growth. The most serious drawback of this dynamic programming
solution is the space needed, which is O (n 2n). This is too large even for modest
values of n.
m
Example 1:
co
For the following graph find minimum cost tour for the traveling salesperson
problem:
a.
1 2
0 10 15 20
5
iy
The cost adjacency matrix =
6
8
0
13
8
9
0
9
10
12
0
un
3 4
Let us start the tour from vertex 1:
sD
g (2, ) = C21 = 5
g (3, ) = C31 = 6
to
g (4, ) = C41 = 8
Tu
g (1, {2, 3, 4}) = min {c12 + g (2, {3, 4}, c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
g (2, {3, 4}) = min {c23 + g (3, {4}), c24 + g (4, {3})}
= min {9 + g (3, {4}), 10 + g (4, {3})}
109
Therefore, g (2, {3, 4}) = min {9 + 20, 10 + 15} = min {29, 25} = 25
g (3, {2, 4}) = min {(c32 + g (2, {4}), (c34 + g (4, {2})}
Therefore, g (3, {2, 4}) = min {13 + 18, 12 + 13} = min {41, 25} = 25
g (4, {2, 3}) = min {c42 + g (2, {3}), c43 + g (3, {2})}
m
g (2, {3}) = min {c23 + g (3, } = 9 + 6 = 15
co
Therefore, g (4, {2, 3}) = min {8 + 15, 9 + 18} = min {23, 27} = 23
g (1, {2, 3, 4}) = min {c12 + g (2, {3, 4}), c13 + g (3, {2, 4}), c14 + g (4, {2, 3})}
a.
= min {10 + 25, 15 + 25, 20 + 23} = min {35, 40, 43} = 35
iy
un
OPTIMAL BINARY SEARCH TREE
Let us assume that the given set of identifiers is {a 1, . . . , an} with a1 < a2 < . . . . <
an. Let p (i) be the probability with which we search for ai. Let q (i) be the probability
sD
that the identifier x being searched for is such that a i < x < ai+1, 0 < i < n (assume
a0 = - and an+1 = +). We have to arrange the identifiers in a binary search tree in
a way that minimizes the expected total access time.
In a binary search tree, the number of comparisons needed to access an element at
depth 'd' is d + 1, so if 'ai' is placed at depth 'di', then we want to minimize:
al
Pi (1 di ) .
i 1
ri
Let P (i) be the probability with which we shall be searching for 'ai'. Let Q (i) be the
probability of an un-successful search. Every internal node represents a point where
a successful search may terminate. Every external node represents a point where an
to
The expected cost contribution for the internal node for 'ai' is:
Tu
Unsuccessful search terminate with I = 0 (i.e at an external node). Hence the cost
contribution for this node is:
110
n n
Given a fixed set of identifiers, we wish to create a binary search tree organization.
We may expect different binary search trees for the same identifier set to have
different performance characteristics.
The computation of each of these c(i, j)‟s requires us to find the minimum of m
quantities. Hence, each such c(i, j) can be computed in time O(m). The total time for
all c(i, j)‟s with j – i = m is therefore O(nm – m2).
m
The total time to evaluate all the c(i, j)‟s and r(i, j)‟s is therefore:
nm m O n
co
2 3
1 m n
a.
Example 1: The possible binary search trees for the identifier set (a1, a2, a3) = (do,
if, stop) are as follows. Given the equal probabilities p (i) = Q (i) = 1/7 for all i, we
have:
st o p
iy if
un
if do st o p
do
sD
Tree 2
Tree 1
do do
al
if st o p
ri
st o p if
to
Tree 3 T ree 4
Tu
1 1 1 1 1 1 1
Cost (tree # 1) = x 1 x2 x 3 x 1 x2 x 3 x 3
7 7 7 7 7 7 7
1 2 3 1 2 3 3 6 9 15
=
7 7 7 7
1 1 1 1 1 1 1
Cost (tree # 2) = x 1 x 2 x 2 x 2 x 2 x 2 x 2
7 7 7 7 7 7 7
1 2 2 2 2 2 2 5 8 13
=
7 7 7 7
111
1 1 1 1 1 1 1
Cost (tree # 3) = x 1 x2 x 3 x 1 x2 x 3 x 3
7 7 7 7 7 7 7
1 2 3 1 2 3 3 6 9 15
=
7 7 7 7
1 1 1 1 1 1 1
Cost (tree # 4) = x 1 x2 x 3 x 1 x2 x 3 x 3
7 7 7 7 7 7 7
1 2 3 1 2 3 3 6 9 15
=
7 7 7 7
m
Huffman coding tree solved by a greedy algorithm has a limitation of having the data
only at the leaves and it must not preserve the property that all nodes to the left of
co
the root have keys, which are less etc. Construction of an optimal binary search tree
is harder, because the data is not constrained to appear only at the leaves, and also
because the tree must satisfy the binary search tree property and it must preserve
the property that all nodes to the left of the root have keys, which are less.
a.
A dynamic programming solution to the problem of obtaining an optimal binary
search tree can be viewed by constructing a tree as a result of sequence of decisions
by holding the principle of optimality. A possible approach to this is to make a
iy
decision as which of the ai's be arraigned to the root node at 'T'. If we choose 'ak'
then is clear that the internal nodes for a1, a2, . . . . . ak-1 as well as the external
nodes for the classes Eo, E1, . . . . . . . Ek-1 will lie in the left sub tree, L, of the root.
un
The remaining nodes will be in the right subtree, R. The structure of an optimal
binary search tree is:
ak
sD
L R
K K
n n
ri
C (i, J) = min {C (i, k-1) + C (k, J) + P (K) + w (i, K-1) + w (K, J)}
i<k<J
Tu
Equation (1) may be solved for C (0, n) by first computing all C (i, J) such that J - i =
1
Next, we can compute all C (i, J) such that J - i = 2, Then all C (i, J) with J - i = 3
and so on.
112
C (i, J) is the cost of the optimal binary search tree 'T ij' during computation we record
the root R (i, J) of each tree 'Tij'. Then an optimal binary search tree may be
constructed from these R (i, J). R (i, J) is the value of 'K' that minimizes equation (1).
We solve the problem by knowing W (i, i+1), C (i, i+1) and R (i, i+1), 0 ≤ i < 4;
Knowing W (i, i+2), C (i, i+2) and R (i, i+2), 0 ≤ i < 3 and repeating until W (0, n),
C (0, n) and R (0, n) are obtained.
m
Example 1:
co
Let n = 4, and (a1, a2, a3, a4) = (do, if, need, while) Let P (1: 4) = (3, 3, 1, 1) and Q
(0: 4) = (2, 3, 1, 1, 1)
a.
Solution:
Column
Row
0 1 iy
2 3 4
un
0 2, 0, 0 3, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 8, 8, 1 7, 7, 2 3, 3, 3 3, 3, 4
sD
4 16, 32, 2
al
This computation is carried out row-wise from row 0 to row 4. Initially, W (i, i) = Q
(i) and C (i, i) = 0 and R (i, i) = 0, 0 < i < 4.
ri
113
m
1, 2; i < k ≤ J. Start with i = 0; so j = 2; as i < k ≤ J, so the possible values for k =
1 and 2.
co
W (0, 2) = P (2) + Q (2) + W (0, 1) = 3 + 1 + 8 = 12
C (0, 2) = W (0, 2) + min {(C (0, 0) + C (1, 2)), (C (0, 1) + C (2, 2))}
= 12 + min {(0 + 7, 8 + 0)} = 19
R (0, 2) = 1
a.
Next, with i = 1; so j = 3; as i < k ≤ j, so the possible value for k = 2 and 3.
iy
C (1, 3) = W (1, 3) + min {[C (1, 1) + C (2, 3)], [C (1, 2) + C (3, 3)]}
= W (1, 3) + min {(0 + 3), (7 + 0)} = 9 + 3 = 12
R (1, 3) = 2
un
Next, with i = 2; so j = 4; as i < k ≤ j, so the possible value for k = 3 and 4.
and 3.
C (0, 3) = W (0, 3) + min {[C (0, 0) + C (1, 3)], [C (0, 1) + C (2, 3)],
[C (0, 2) + C (3, 3)]}
= 14 + min {(0 + 12), (8 + 3), (19 + 0)} = 14 + 11 = 25
to
R (0, 3) = 2
114
From the table we see that C (0, 4) = 32 is the minimum cost of a binary search tree
for (a1, a2, a3, a4). The root of the tree 'T04' is 'a2'.
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
m
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
co
The left and right sub trees for T24 are T22 and T34 respectively.
a.
The root of T22 is null
a1
T 01
T 04
a2
T 24
a3 iy
do
if
read
un
a4
T 00 T 11 T 22 T 34 while
sD
Example 2:
1/16 and p1 = 1/4, p2 = 1/8, p3 = p4 =1/16. Construct an optimal binary search tree.
Solving for C (0, n):
115
R (2, 3) = 3
m
W (0, 2) = P (2) + Q (2) + W (0, 1) = 2 + 1 + 9 = 12
C (0, 2) = W (0, 2) + min {(C (0, 0) + C (1, 2)), (C (0, 1) + C (2, 2))}
co
= 12 + min {(0 + 6, 9 + 0)} = 12 + 6 = 18
R (0, 2) = 1
Next, with i = 1; so j = 3; as i < k ≤ j, so the possible value for k = 2 and 3.
a.
W (1, 3) = P (3) + Q (3) + W (1, 2) = 1 + 1+ 6 = 8
C (1, 3) = W (1, 3) + min {[C (1, 1) + C (2, 3)], [C (1, 2) + C (3, 3)]}
= W (1, 3) + min {(0 + 3), (6 + 0)} = 8 + 3 = 11
R (1, 3) = 2
iy
Next, with i = 2; so j = 4; as i < k ≤ j, so the possible value for k = 3 and 4.
un
W (2, 4) = P (4) + Q (4) + W (2, 3) = 1 + 1 + 3 = 5
C (2, 4) = W (2, 4) + min {[C (2, 2) + C (3, 4)], [C (2, 3) + C (4, 4)]
= 5 + min {(0 + 3), (3 + 0)} = 5 + 3 = 8
R (2, 4) = 3
sD
116
= 16 + min [0 + 18, 9 + 8, 18 + 3, 25 + 0] = 16 + 17 = 33
R (0, 4) = 2
Column
0 1 2 3 4
Row
0 2, 0, 0 1, 0, 0 1, 0, 0 1, 0, 0, 1, 0, 0
1 9, 9, 1 6, 6, 2 3, 3, 3 3, 3, 4
m
3 14, 25, 2 11, 18, 2
co
4 16, 33, 2
From the table we see that C (0, 4) = 33 is the minimum cost of a binary search tree
for (a1, a2, a3, a4)
a.
The root of the tree 'T04' is 'a2'.
iy
Hence the left sub tree is 'T01' and right sub tree is T24. The root of 'T01' is 'a1' and the
root of 'T24' is a3.
un
The left and right sub trees for 'T01' are 'T00' and 'T11' respectively. The root of T01 is
'a1'
The left and right sub trees for T24 are T22 and T34 respectively.
sD
a2
T 04 a2
a1 a3
ri
T 01 T 24 a1 a3
a4
T 00 T 11 T 22 T 34 a4
to
Tu
Example 3:
WORD PROBABILITY
A 4
B 2
C 1
D 3
E 5
F 2
G 1
117
Solving c(0,n):
m
next with i = 1 ; so j = 2; as i < k ≤ j, so the possible value for k = 2
co
W(1, 2) = P(2) + Q(2)+W(1, 1) = 2+0+0 = 2
C(1, 2) = W(1, 2)+ min {C (1, 1) + C(2, 2) }=2 + [ (0 + 0 ) ] = 2
R(1, 2) = 2
next with i = 2 ; so j = 3; as i < k ≤ j, so the possible value for k = 3
a.
W(2, 3) = P(3) + Q(3)+W(2, 2) = 1+0+0 = 1
C(2, 3) = W(2, 3)+ min {C (2, 2) + C(3, 3) }=1 + [ (0 + 0 ) ] = 1
R(2, 3) = 3
iy
next with i = 3 ; so j = 4; as i < k ≤ j, so the possible value for k = 4
un
W(3, 4) = P(4) + Q(4)+W(3, 3) = 3+0+0 = 3
C(3, 4) = W(3, 4)+ min {C (3, 4) + C(4, 4) }=3 + [ (0 + 0 ) ] = 3
R(3, 4) = 4
sD
118
m
R(2, 4) = 4
next with i = 3 ; so j = 5; as i < k ≤ j, so the possible values for k = 4 and 5.
co
W(3, 5) = P(5) + Q(5)+W(3, 4) = 5+ 0 + 3 =8
C(3, 5) = W(3, 5)+ min {C (3, 3) + C(4,5) ,C(3,4) + C(5, 5)}
= 8 +min{ 0 + 5, 3 + 0} = 11
R(3, 5) = 5
a.
next with i = 4 ; so j = 6; as i < k ≤ j, so the possible values for k = 5 and 6.
= 7 +min{ 0 + 2, 5 + 0} = 9
R(4, 6) = 5
iy
C(4, 6) = W(4, 6)+ min {C (4, 4) + C(5,6) ,C(4, 5) + C(6, 6)}
un
next with i = 5 ; so j = 7; as i < k ≤ j, so the possible values for k = 6 and 7.
= 3 +min{ 0 + 1, 2 + 0} = 4
R(5, 7) = 6
R(0, 3) = 1
119
m
C(4, 7) = W(4, 7)+ min {C (4, 4) + C(5, 7) ,C(4 ,5) + C(6, 7),C(4, 6) + C(7, 7)}
= 8 +min{ 0 + 4 , 5 + 1 ,9 + 0} = 12
R(4, 7) = 5
co
Fourth, computing all c(i, j) such that j – i = 4 ;j = i + 4 and as 0 ≤ i < 4 ; i = 0, 1,
2, 3 for i < k ≤ j. Start with i = 0 ; so j = 4; as i < k ≤ j, so the possible values for
k = 1,2 ,3 and 4.
a.
W(0, 4) = P(4) + Q(4)+W(0, 3) = 3+ 0 + 7 = 10
C(0, 4) = W(0, 4)+ min {C (0, 0) + C(1,4) ,C(0, 1) + C(2, 4),C(0, 2) + C(3, 4),
C(0, 3) + C(4, 4)}
iy
= 10 +min{ 0 + 10, 4 + 5,8 + 3,11 + 0} = 19
R(0, 4) = 2
un
next with i = 1 ; so j = 5; as i < k ≤ j, so the possible values for k = 2,3 ,4 and 5.
120
R(0, 5) = 2
m
W(2, 7) = P(7) + Q(7)+W(2, 6) = 1+ 0 + 11 = 12
C(2, 7) = W(2, 7)+ min {C (2, 2) + C(3, 7) ,C(2, 3) + C(4, 7),C(2, 4) + C(5, 7)
C(2, 5) + C (6, 7),C(2, 6) + C(7,7)}
co
= 12 +min{ 0 + 18, 1 + 12 , 5 + 4, 14 + 1, 18 + 0} = 21
R(2, 7) = 5
a.
i < k ≤ j. Start with i = 0; so j = 6; as i < k ≤ j, so the possible values for k = 1,
2, 3, 4 5 & 6.
W(0, 6) = P(6) + Q(6)+W(0, 5) = 2+ 0 + 15 = 17
iy
C(0, 6) = W(0,6 )+ min {C (0, 0) + C(1,6) ,C(0, 1) + C(2, 6),C(0, 2) + C(3, 6),
C(0, 3) + C(4, 6),C(0, 4) + C(5, 6),C(0, 5) + C(6, 6)}
= 17 +min{ 0 + 25, 4 + 18, 8 + 15,19 + 2, 31 + 0} = 37
R(0, 6) = 4
un
next with i = 1 ; so j = 7; as i < k ≤ j, so the possible values for k = 2, 3, 4, 5, 6
and 7.
sD
3, 4, 5, 6 and 7.
C(0, 7) = W(0, 7 )+ min {C (0, 0) + C(1, 7) ,C(0, 1) + C(2, 7),C(0, 2) + C(3, 7),
C(0, 3) + C(4, 7),C(0, 4) + C(5, 6),C(0, 5) + C (6, 7 ),C(0, 6) + C(7, 7)}
= 18 +min{ 0 + 28, 4 + 21, 8 + 18,19 +4, 31 + 1, 37 + 0} = 41
R(0, 7) = 4
Tu
0/1 – KNAPSACK
We are given n objects and a knapsack. Each object i has a positive weight wi and a
positive value Vi. The knapsack can carry a weight not exceeding W. Fill the knapsack
so that the value of objects in the knapsack is optimized.
121
decisions on the xi are made in the order xn, xn-1, . . . .x1. Following a decision on xn,
we may be in one of two possible states: the capacity remaining in m – wn and a
profit of pn has accrued. It is clear that the remaining decisions x n-1, . . . , x1 must be
optimal with respect to the problem state resulting from the decision on xn.
Otherwise, xn,. . . . , x1 will not be optimal. Hence, the principal of optimality holds.
m
Equation-2 can be solved for fn (m) by beginning with the knowledge fo (y) = 0 for all
y and fi (y) = - , y < 0. Then f1, f2, . . . fn can be successively computed using
equation–2.
co
When the wi‟s are integer, we need to compute fi (y) for integer y, 0 < y < m. Since fi
(y) = - for y < 0, these function values need not be computed explicitly. Since
each fi can be computed from fi - 1 in Θ (m) time, it takes Θ (m n) time to compute
fn. When the wi‟s are real numbers, fi (y) is needed for real numbers y such that 0 <
a.
y < m. So, fi cannot be explicitly computed for all y in this range. Even when the wi‟s
are integer, the explicit Θ (m n) computation of fn may not be the most efficient
computation. So, we explore an alternative method for both cases.
iy
The fi (y) is an ascending step function; i.e., there are a finite number of y‟s, 0 = y1
< y2 < . . . . < yk, such that fi (y1) < fi (y2) < . . . . . < fi (yk); fi (y) = - , y < y1; fi
un
(y) = f (yk), y > yk; and fi (y) = fi (yj), yj < y < yj+1. So, we need to compute only fi
(yj), 1 < j < k. We use the ordered set Si = {(f (yj), yj) | 1 < j < k} to represent fi
(y). Each number of Si is a pair (P, W), where P = fi (yj) and W = yj. Notice that S0 =
{(0, 0)}. We can compute Si+1 from Si by first computing:
sD
Now, Si+1 can be computed by merging the pairs in S i and Si1 together. Note that if
Si+1 contains two pairs (Pj, Wj) and (Pk, Wk) with the property that Pj < Pk and Wj >
Wk, then the pair (Pj, Wj) can be discarded because of equation-2. Discarding or
al
purging rules such as this one are also known as dominance rules. Dominated tuples
get purged. In the above, (Pk, Wk) dominates (Pj, Wj).
ri
Example 1:
to
Consider the knapsack instance n = 3, (w1, w2, w3) = (2, 3, 4), (P1, P2, P3) = (1, 2,
5) and M = 6.
Tu
Solution:
122
m
Finally, f3 (6) = max {3, 1 + 5} = 6
co
Other Solution:
a.
S1 = (S0 U S01) = {(0, 0), (1, 2)}
X - 2 = 0 => x = 2.
X - 2 = 1 => x = 3.
iy
y – 3 = 0 => y = 3
y – 3 = 2 => y = 5
un
S11 = {(2, 3), (3, 5)}
S3 = (S2 U S21) = {(0, 0), (1, 2), (2, 3), (3, 5), (5, 4), (6, 6), (7, 7), (8, 9)}
ri
S3 = (S2 U S21) = {(0, 0), (1, 2), (2, 3), (5, 4), (6, 6)}
to
From (6, 6) we can infer that the maximum Profit pi xi = 6 and weight xi wi = 6
Tu
Reliability Design
123
If stage i contains mi copies of device Di. Then the probability that all mi have a
mi mi
malfunction is (1 - ri) . Hence the reliability of stage i becomes 1 – (1 - ri) .
Our problem is to use device duplication. This maximization is to be carried out under
a cost constraint. Let ci be the cost of each unit of device i and let c be the maximum
allowable cost of the system being designed.
We wish to solve:
i mi
m
Maximize
1 i n
Subject to Cm C
co
i i
1 i n
a.
Assume each Ci > 0, each mi must be in the range 1 < mi < ui, where
iy
un
n
ui C C i
C J
Ci
1
The upper bound ui follows from the observation that mj > 1
sD
1 j i
Subject to the constrains:
ri
C
1 j i
J mJ x and 1 < mj < uJ, 1 < j < i
to
The last decision made requires one to choose mn from {1, 2, 3, . . . . . un}
Once a value of mn has been chosen, the remaining decisions must be such as to use
Tu
124
clearly, f0 (x) = 1 for all x, 0 < x < C and f (x) = - for all x < 0.
There is atmost one tuple for each different „x‟, that result from a sequence of
decisions on m1, m2, . . . . mn. The dominance rule (f1, x1) dominate (f2, x2) if f1 ≥ f2
and x1 ≤ x2. Hence, dominated tuples can be discarded from Si.
m
Example 1:
Design a three stage system with device types D1, D2 and D3. The costs are $30, $15
and $20 respectively. The Cost of the system is to be no more than $105. The
co
reliability of each device is 0.9, 0.8 and 0.5 respectively.
Solution:
a.
We assume that if if stage I has mi devices of type i in parallel, then i (mi) =1 – (1-
ri)mi
iy
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui. Where:
un
n
ui C C i
C
J Ci
1
sD
Using the above equation compute u1, u2 and u3.
S1 = depends on u1 value, as u1 = 2, so
S1 S , S
1
1
1
2
S2 = depends on u2 value, as u2 = 3, so
125
S 2 S 2
1
, S 2 , S2
2 3
S3 = depends on u3 value, as u3 = 3, so
S 3 S 3
1
, S 3 , S3
2 3
Now find,S11 f (x), x
1
m
Compute 1 (1) and 1 (2) using the formula: i mi) 1 (1 ri )mi
co
1 2 1 1 0.92 0.99
a.
1
2 1 1 1 rImi
= 1 – (1 – 0.8)
1
= 1 – 0.2 = 0.8
2 2 1 1 0.8 2
0.96
al
2 3 1 1 0.8 3
0.992
ri
S2 S1
2
, S2 , S2
2 3
By applying Dominance rule to S2:
Dominance Rule:
126
If Si contains two pairs (f1, x1) and (f2, x2) with the property that f1 ≥ f2 and x1 ≤ x2,
then (f1, x1) dominates (f2, x2), hence by dominance rule (f2, x2) can be discarded.
Discarding or pruning rules such as the one above is known as dominance rule.
Dominating tuples will be present in Si and Dominated tuples has to be discarded
from Si.
m
S2 = {(0.72, 45), (0.864, 60), (0.8928, 75)}
3 1 1 1 rI mi
co
= 1 – (1 – 0.5)1 = 1 – 0.5 = 0.5
a.
3 3 1 1 0.5 3 0.875
S13 0.5 (0.72), 45
If cost exceeds 105, remove that tuples
to
The best design has a reliability of 0.648 and a cost of 100. Tracing back for the
solution through Si „s we can determine that m3 = 2, m2 = 2 and m1 = 1.
Tu
Other Solution:
Since, we can assume each ci > 0, each mi must be in the range 1 ≤ mi ≤ ui.
Where:
127
n
ui C C i C J / C i
i
Using the above equation compute u1, u2 and u3.
m
105 20 3015 20 60
u3 3
20 20
co
f3 (105) = max {3 (m3). f2 (105 – 20m3)}
1 m3 u3
a.
= max {3(1) f2(105 - 20), 3(2) f2(105 - 20x2), 3(3) f2(105 -20x3)}
iy
= max {0.5 x 0.8928, 0.75 x 0.864, 0.875 x 0.72} = 0.648.
1 m1 u1
1 m1 u1
128
= max {2(1) f1(65 - 15), 2(2) f1(65 - 15x2), 2(3) f1(65 - 15x3)}
m
= max {1(1) f0(50 - 30), 1(2) f0(50 - 30x2)}
co
f1 (35) = max 1(m1). f0(35 - 30m1)}
1 m1 u1
a.
= max {1(1).f0(35-30), 1(2).f0(35-30x2)}
1 m2 u2
= max {2(1) f1(45 - 15), 2(2) f1(45 - 15x2), 2(3) f1(45 - 15x3)}
al
Cost = 30 x 1 + 15 x 2 + 20 x 2 = 100.
m3 = 2, m2 = 2 and m1 = 1.
129
Chapter
6
BASIC TRAVERSAL
AND SEARCH TECHNIQUES
m
Search means finding a path or traversal between a start node and one of a set of
goal nodes. Search is a study of states and their transitions.
Search involves visiting nodes in a graph in a systematic manner, and may or may
co
not result into a visit to all nodes. When the search necessarily involved the
examination of every vertex in the tree, it is called the traversal.
a.
Techniques for Traversal of a Binary Tree:
A binary tree is a finite (possibly empty) collection of elements. When the binary tree
iy
is not empty, it has a root element and remaining elements (if any) are partitioned
into two binary trees, which are called the left and right subtrees.
un
There are three common ways to traverse a binary tree:
1. Preorder
2. Inorder
3. postorder
sD
In all the three traversal methods, the left subtree of a node is traversed before the
right subtree. The difference among the three orders comes from the difference in the
time at which a node is visited.
al
Inorder Traversal:
ri
In the case of inorder traversal, the root of each subtree is visited after its left subtree
has been traversed but before the traversal of its right subtree begins. The steps for
traversing a binary tree in inorder traversal are:
to
treenode = record
{
Type data; //Type is the data type of data.
Treenode *lchild; treenode *rchild;
}
130
Preorder Traversal:
m
In a preorder traversal, each node is visited before its left and right subtrees are
traversed. Preorder search is also called backtracking. The steps for traversing a
binary tree in preorder traversal are:
co
1. Visit the root.
2. Visit the left subtree, using preorder.
3. Visit the right subtree, using preorder.
a.
The algorithm for preorder traversal is as follows:
iy
// t is a binary tree. Each node of t has three fields; lchild, data, and rchild.
un
{
if t 0 then
{
visit (t);
Preorder (t lchild);
Preorder (t rchild);
sD
}
}
al
Postorder Traversal:
In a postorder traversal, each root is visited after its left and right subtrees have been
traversed. The steps for traversing a binary tree in postorder traversal are:
ri
131
Example 1:
A
Preord eri ng of t he vertices:
A, B, D, C, E, G, F, H, I.
B C
m
Post ord eri ng of t he vertices:
D E F D, B, G, E, H, I, F, C, A.
co
Inord eri ng of t he vertices:
G H I D, B, A, E, G, C, H, F, I
a.
Bin a ry T re e Pre, P o st a n d In- ord er T rav ers in g
Example 2:
iy
Traverse the following binary tree in pre, post, inorder and level order.
un
A • P reo rde r t ra v e rs a l y ie lds:
A, B, D, C , E, G , F , H, I
B C
• Posto rde r t ra v e rs a l y ie lds:
sD
D, B, G , E, H, I, F , C , A
D E F
• Ino rde r t ra v e rs a l y ie lds:
G H I D, B, A, E, G , C , H, F , I
• Le v e l o rde r t ra v e rs a l y ie lds:
al
A, B, C , D, E, F , G , H, I
Example 3:
to
Traverse the following binary tree in pre, post, inorder and level order.
P , F , B, H, G , S, R, Y, T, W , Z
F S
• Posto rde r t ra v e rs a l y ie lds:
B H R Y
B, G , H, F , R, W , T, Z, Y, S, P
W • Le v e l o rde r t ra v e rs a l y ie lds:
P , F , S, B, H, R, Y, G , T, Z,
W
Bin a ry T re e Pre, P o st , In ord er a n d lev e l ord er T rav ers in g
132
Example 4:
Traverse the following binary tree in pre, post, inorder and level order.
m
• Le v e l o rde r t ra v e rs a l y ie lds:
2 , 7 , 5 , 2 , 6 , 9 , 5 , 11 , 4
co
Bin a ry T re e Pre, P o st , In ord er a n d lev e l ord er T rav ers in g
Example 5:
a.
Traverse the following binary tree in pre, post, inorder and level order.
B
A
C
iy
•
•
Preo rde r t rav e rs al y ie lds:
A, B, D, G , K, H, L, M , C , E
At first glance, it appears we would always want to use the flat traversal functions
ri
since the use less stack space. But the flat versions are not necessarily better. For
instance, some overhead is associated with the use of an explicit stack, which may
negate the savings we gain from storing only node pointers. Use of the implicit
to
function call stack may actually be faster due to special machine instructions that can
be used.
Tu
Inorder Traversal:
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path rooted at vertex, pushing each vertex onto
the stack and stop when there is no left son of vertex.
2. Pop and process the nodes on stack if zero is popped then exit. If a vertex with
right son exists, then set right son of vertex as current vertex and return to
step one.
133
Algorithm inorder()
{
stack[1] = 0
vertex = root
top: while(vertex ≠ 0)
{
push the vertex into the stack
vertex = leftson(vertex)
}
m
while(vertex ≠ 0)
{
print the vertex node
co
if(rightson(vertex) ≠ 0)
{
vertex = rightson(vertex)
goto top
a.
}
pop the element from the stack and made it as vertex
}
}
Preorder Traversal: iy
un
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path by pushing the right son of vertex onto stack,
sD
if any and process each vertex. The traversing ends after a vertex with no left
child exists.
2. Pop the vertex from stack, if vertex ≠ 0 then return to step one otherwise exit.
al
Algorithm preorder( )
ri
{
stack[1]: = 0
vertex := root.
to
while(vertex ≠ 0)
{
print vertex node
if(rightson(vertex) ≠ 0)
Tu
134
Postorder Traversal:
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path rooted at vertex. At each vertex of path push
vertex on to stack and if vertex has a right son push –(right son of vertex)
onto stack.
2. Pop and process the positive nodes (left nodes). If zero is popped then exit. If
a negative node is popped, then ignore the sign and return to step one.
m
The algorithm for postorder Non Recursive traversal is as follows:
Algorithm postorder( )
co
{
stack[1] := 0
vertex := root
top: while(vertex ≠ 0)
a.
{
push vertex onto stack
if(rightson(vertex) ≠ 0)
}
push -(vertex) onto stack
vertex := leftson(vertex)
if(vertex < 0)
{
vertex := -(vertex)
goto top
}
al
Example 1:
ri
Traverse the following binary tree in pre, post and inorder using non-recursive
traversing algorithm.
to
A
Tu
135
Inorder Traversal:
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path rooted at vertex, pushing each vertex onto
the stack and stop when there is no left son of vertex.
2. Pop and process the nodes on stack if zero is popped then exit. If a vertex with
right son exists, then set right son of vertex as current vertex and return to
step one.
m
Current
Stack Processed nodes Remarks
vertex
A 0 PUSH 0
co
0ABDGK PUSH the left most path of A
K 0ABDG K POP K
a.
G 0ABD KG POP G since K has no right son
H
0AB
0ABHL
KGD
KGD
iy vertex
PUSH the leftmost path of H
un
L 0ABH KGDL POP L
A 0 KGDLHMBA
vertex
C 0CE KGDLHMBA PUSH the left most path of C
ri
E 0C KGDLHMBAE POP E
Postorder Traversal:
Tu
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path rooted at vertex. At each vertex of path push
vertex on to stack and if vertex has a right son push -(right son of vertex)
onto stack.
2. Pop and process the positive nodes (left nodes). If zero is popped then exit. If
a negative node is popped, then ignore the sign and return to step one.
136
Current
Stack Processed nodes Remarks
vertex
A 0 PUSH 0
PUSH the left most path of A with
0 A -C B D -H G K
a -ve for right sons
0 A -C B D -H KG POP all +ve nodes K and G
H 0 A -C B D KG Pop H
PUSH the left most path of H with
0 A -C B D H -M L KG
a -ve for right sons
0 A -C B D H -M KGL POP all +ve nodes L
m
M 0 A -C B D H KGL Pop M
PUSH the left most path of M with
0 A -C B D H M KGL
a -ve for right sons
co
0 A -C KGLMHDB POP all +ve nodes M, H, D and B
C 0A KGLMHDB Pop C
PUSH the left most path of C with
0ACE KGLMHDB
a.
a -ve for right sons
0 KGLMHDBECA POP all +ve nodes E, C and A
Preorder Traversal:
iy
un
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
sD
1. Proceed down the left most path by pushing the right son of vertex onto stack,
if any and process each vertex. The traversing ends after a vertex with no left
child exists.
2. Pop the vertex from stack, if vertex ≠ 0 then return to step one otherwise exit.
al
Current
Stack Processed nodes Remarks
vertex
ri
A 0 PUSH 0
PUSH the right son of each vertex onto
0CH ABDGK stack and process each vertex in the left
to
most path
H 0C ABDGK POP H
PUSH the right son of each vertex onto
Tu
137
Example 2:
Traverse the following binary tree in pre, post and inorder using non-recursive
traversing algorithm.
m
5 11 4 • Ino rde r t rav ars al y ie lds:
2, 7 , 5 , 6 , 11 , 2 , 5 , 4 , 9
co
Bin a ry T re e Pre, P o st a n d In ord er T rav ers in g
Inorder Traversal:
a.
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
2. Pop and process the nodes on stack if zero is popped then exit. If a vertex with
un
right son exists, then set right son of vertex as current vertex and return to
step one.
Current
Stack Processed nodes Remarks
sD
vertex
2 0
0272
2 027 2
al
7 02 27
6 0265 27
ri
5 026 275
11 02 2 7 5 6 11
5 05 2 7 5 6 11 2
to
9 094 2 7 5 6 11 2 5
4 09 2 7 5 6 11 2 5 4
Tu
Postorder Traversal:
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
1. Proceed down the left most path rooted at vertex. At each vertex of path push
vertex on to stack and if vertex has a right son push –(right son of vertex)
onto stack.
138
2. Pop and process the positive nodes (left nodes). If zero is popped then exit. If
a negative node is popped, then ignore the sign and return to step one.
Current
Stack Processed nodes Remarks
vertex
2 0
0 2 -5 7 -6 2
2 0 2 -5 7 -6 2
6 0 2 -5 7 2
0 2 -5 7 6 -11 5 2
m
5 0 2 -5 7 6 -11 25
11 0 2 -5 7 6 11 25
0 2 -5 2 5 11 6 7
co
5 0 2 5 -9 2 5 11 6 7
9 02594 2 5 11 6 7
0 2 5 11 6 7 4 9 5 2 Stop since stack is empty
a.
Preorder Traversal:
iy
Initially push zero onto stack and then set root as vertex. Then repeat the following
steps until the stack is empty:
un
1. Proceed down the left most path by pushing the right son of vertex onto stack,
if any and process each vertex. The traversing ends after a vertex with no left
child exists.
sD
2. Pop the vertex from stack, if vertex ≠ 0 then return to step one otherwise exit.
Current
Stack Processed nodes Remarks
vertex
al
2 0
056 272
6 0 5 11 27265
ri
11 05 27265
05 2 7 2 6 5 11
to
5 09 2 7 2 6 5 11
9 0 2 7 2 6 5 11 5
0 2 7 2 6 5 11 5 9 4 Stop since stack is empty
Tu
Given a graph G = (V, E) and a vertex V in V (G) traversing can be done in two ways.
139
With depth first search, the start state is chosen to begin, then some successor of the
start state, then some successor of that state, then some successor of that and so on,
trying to reach a goal state.
If depth first search reaches a state S without successors, or if all the successors of a
state S have been chosen (visited) and a goal state has not get been found, then it
“backs up” that means it goes to the immediately previous state or predecessor
formally, the state whose successor was „S‟ originally.
For example consider the figure. The circled letters are state and arrows are
m
branches.
co
A
E
a.
ST A RT
S B
H G G OA L
C F
I
K
iy
un
Suppose S is the start and G is the only goal state. Depth first search will first visit S,
then A then D. But D has no successors, so we must back up to A and try its second
successor, E. But this doesn‟t have any successors either, so we back up to A again.
But now we have tried all the successors of A and haven‟t found the goal state G so
sD
we must back to „S‟. Now „S‟ has a second successor, B. But B has no successors, so
we back up to S again and choose its third successor, C. C has one successor, F. The
first successor of F is H, and the first of H is J. J doesn‟t have any successors, so we
back up to H and try its second successor. And that‟s G, the only goal state. So the
solution path to the goal is S, C, F, H and G and the states considered were in order
al
S, A, D, E, B, C, F, H, J, G.
Disadvantages:
ri
1. It works very fine when search graphs are trees or lattices, but can get
struck in an infinite loop on graphs. This is because depth first search can
to
To eliminate this keep a list of states previously visited, and never permit
search to return to any of them.
Tu
2. One more problem is that, the state space tree may be of infinite depth, to
prevent consideration of paths that are too long, a maximum is often
placed on the depth of nodes to be expanded, and any node at that depth
is treated as if it had no successors.
140
Time Complexity:
Let n = |V| and e = |E|. Observe that the initialization portion requires (n) time.
Since we never visit a vertex twice, the number of times we go through the loop is at
most n (exactly n assuming each vertex is reachable from the source). As, each
vertex is visited at most once. At each vertex visited, we scan its adjacency list once.
Thus, each edge is examined at most twice (once at each endpoint). So the total
running time is O (n + e).
Alternatively,
If the average branching factor is assumed as „b‟ and the depth of the solution as „d‟,
m
and maximum depth m ≥ d.
The worst case time complexity is O(bm ) as we explore bm nodes. If many solutions
co
exists DFS will be likely to find faster than the BFS.
Space Complexity:
a.
We have to store the nodes from root to current leaf and all the unexpanded siblings
of each node on path. So, We need to store bm nodes.
algorithm for computing shortest paths (where the length of a path = number of
edges on the path). Breadth-first search is named because it visits vertices across the
entire breadth.
D
ri
A
E
J
to
ST A RT
S B
H G G OA L
Tu
C F
K
I
Breadth first search finds states level by level. Here we first check all the immediate
successors of the start state. Then all the immediate successors of these, then all the
immediate successors of these, and so on until we find a goal node. Suppose S is the
start state and G is the goal state. In the figure, start state S is at level 0; A, B and C
are at level 1; D, e and F at level 2; H and I at level 3; and J, G and K at level 4. So
breadth first search, will consider in order S, A, B, C, D, E, F, H, I, J and G and then
stop because it has reached the goal node.
141
Breadth first search does not have the danger of infinite loops as we consider states
in order of increasing number of branches (level) from the start state.
One simple way to implement breadth first search is to use a queue data structure
consisting of just a start state. Any time we need a new state, we pick it from the
front of the queue and any time we find successors, we put them at the end of the
queue. That way we are guaranteed to not try (find successors of) any states at level
„N‟ until all states at level „N – 1‟ have been tried.
Time Complexity:
m
The running time analysis of BFS is similar to the running time analysis of many graph
traversal algorithms. Let n = |V| and e = |E|. Observe that the initialization portion
requires (n) time. Since we never visit a vertex twice, the number of times we go
co
through the loop is at most n (exactly n, assuming each vertex is reachable from the
source). So, Running time is O (n + e) as in DFS. For a directed graph the analysis is
essentially the same.
a.
Alternatively,
If the average branching factor is assumed as „b‟ and the depth of the solution as „d‟.
iy
In the worst case we will examine 1 + b + b2 + b3 + . . . + bd = (bd + 1 - 1) / (b –1) =
O(bd ).
un
In the average case the last term of the series would be bd / 2. So, the complexity is
still O(bd)
sD
Space Complexity:
Before examining any node at depth d, all of its siblings must be expanded and
stored. So, space requirement is also O(bd).
al
The exploration of a new node cannot begin until the node currently being explored is
fully explored. D-search like state space search is called LIFO (Last In First Out)
search which uses stack data structure. To illustrate the D-search let us consider the
to
following tree:
D
Tu
A
E
ST A RT J
S B
H G G OA L
C F
K
I
142
Time Complexity:
m
objects are mathematically distinct from one another.
Let G = (V, E) be a digraph with n = |V| and let e = |E|. We will assume that the
co
vertices of G are indexed {1, 2, . . . . . , n}.
1
A v, w if v,w E
0 otherwise
a.
Adjacency List: An array Adj [1 . . . . . . . n] of pointers where for 1 < v < n, Adj [v]
points to a linked list containing the vertices which are adjacent to v (i.e. the vertices
iy
that can be reached from v by a single edge). If the edges have weights then these
weights may also be stored in the linked list elements.
un
1 2 3
1 1 1 2 3
1 1 1
2 3
sD
2 0 0 1
3 0 1 0 3 2
storage.
Adjacency matrices allow faster access to edge queries (for example, is (u, v) E)
to
and adjacency lists allow faster access to enumeration tasks (for example, find all the
vertices adjacent to v).
Tu
BFS and DFS impose a tree (the BFS/DFS tree) along with some auxiliary edges
(cross edges) on the structure of graph. So, we can compute a spanning tree in a
graph. The computed spanning tree is not a minimum spanning tree. Trees are much
more structured objects than graphs. For example, trees break up nicely into
subtrees, upon which subproblems can be solved recursively. For directed graphs the
other edges of the graph can be classified as follows:
Back edges: (u, v) where v is a (not necessarily proper) ancestor of u in the tree.
(Thus, a self-loop is considered to be a back edge).
143
Cross edges: (u, v) where u and v are not ancestors or descendents of one another
(in fact, the edge may go between different trees of the forest).
Depth first search of undirected graph proceeds as follows. The start vertex V is
visited. Next an unvisited vertex 'W' adjacent to 'V' is selected and a depth first
search from 'W' is initiated. When a vertex 'u' is reached such that all its adjacent
vertices have been visited, we back up to the last vertex visited, which has an
m
unvisited vertex 'W' adjacent to it and initiate a depth first search from W. The search
terminates when no unvisited vertex can be reached from any of the visited ones.
co
Let us consider the following Graph (G):
a.
1
2 3
4 5
iy
6 7
un
8
Gra p h
sD
V ert e x
al
2 3
1
2 1 4 5
ri
3 1 6 7
4 2 8
to
5 2 8
6 3 8
Tu
7 3 8
8 4 5 6 7
If the depth first is initiated from vertex 1, then the vertices of G are visited in the
order: 1, 2, 4, 8, 5, 6, 3, 7. The depth first spanning tree is as follows:
144
2 3
4 5 6 7
De p t h F irst S p a n n in g T re e
m
The spanning trees obtained using depth first searches are called depth first spanning
trees. The edges rejected in the context of depth first search are called a back edges.
Depth first spanning tree has no cross edges.
co
Breadth first search and traversal:
a.
Starting at vertex 'V' and marking it as visited, BFS differs from DFS in that all
unvisited vertices adjacent to V are visited next. Then unvisited vertices adjacent to
there vertices are visited and so on. A breadth first search beginning at vertex 1 of
the graph would first visit 1 and then 2 and 3.
iy
un
1
2 3
sD
4 5 6 7
8
al
Bre a dt h F irst S p a n n in g T re e
ri
Next vertices 4, 5, 6 and 7 will be visited and finally 8. The spanning trees obtained
using BFS are called Breadth first spanning trees. The edges that were rejected in the
breadth first search are called cross edges.
to
145
Biconnected
Components
Articulation Point
m
Bridge
co
Biconnected graphs and articulation points are of great interest in the design of
network algorithms, because these are the “critical" points, whose failure will result in
a.
the network becoming disconnected.
Let us consider the typical case of vertex v, where v is not a leaf and v is not the root.
iy
Let w1, w2, . . . . . . . wk be the children of v. For each child there is a subtree of the
DFS tree rooted at this child. If for some child, there is no back edge going to a
proper ancestor of v, then if we remove v, this subtree becomes disconnected from
the rest of the graph, and hence v is an articulation point.
un
On the other hand, if every one of the subtree rooted at the children of v have back
edges to proper ancestors of v, then if v is removed, the graph remains connected
(the back edges hold everything together). This leads to the following:
sD
Observation 1: An internal vertex v of the DFS tree (other than the root) is
an articulation point if and only if there is a subtree rooted at a child of v such
that there is no back edge from any vertex in this subtree to a proper ancestor
of v.
al
Thus, after deletion of a leaf from a tree, the rest of the tree remains
connected, thus even ignoring the back edges, the graph is connected after
to
Observation 3: The root of the DFS is an articulation point if and only if it has
two or more children. If the root has only a single child, then (as in the case of
Tu
leaves) its removal does not disconnect the DFS tree, and hence cannot
disconnect the graph in general.
146
Determining the articulation turns out to be a simple extension of depth first search.
Consider a depth first spanning tree for this graph.
A H I
B C G
J K
m
D
E
F L M
co
Observations 1, 2, and 3 provide us with a structural characterization of which
vertices in the DFS tree are articulation points.
a.
Deleting node E does not disconnect the graph because G and D both have dotted
links (back edges) that point above E, giving alternate paths from them to F. On the
other hand, deleting G does disconnect the graph because there are no such alternate
paths from L or H to E (G‟s parent).
iy
A vertex „x‟ is not an articulation point if every child „y‟ has some node lower in the
un
tree connect (via a dotted link) to a node higher in the tree than „x‟, thus providing an
alternate connection from „x‟ to „y‟. This rule will not work for the root node since
there are no nodes higher in the tree. The root is an articulation point if it has two or
more children.
sD
By using the above observations the articulation points of this graph are:
147
This observation leads to a simple rule to identify articulation points. For each is
define L (u) as follows:
L (u) = min {DFN (u), min {L (w) w is a child of u}, min {DFN (w) (u, w)
is a back edge}}.
L (u) is the lowest depth first number that can be reached from „u‟ using a path of
descendents followed by at most one back edge. It follows that, If „u‟ is not the root
then „u‟ is an articulation point iff „u‟ has a child „w‟ such that:
m
6.6.2. Algorithm for finding the Articulation points:
co
Pseudocode to compute DFN and L.
a.
// spanning tree. It is assumed that the global array dfn is initialized to zero and that // the
global variable num is initialized to 1. n is the number of vertices in G.
{
dfn [u] := num; L [u] := num; num := num + 1;
for each vertex w adjacent from u do
{
if (dfn [w] = 0) then iy
un
{
Art (w, u); // w is unvisited.
L [u] := min (L [u], L [w]);
}
else if (w v) then L [u] := min (L [u], dfn [w]);
sD
}
}
// spanning tree. It is assumed that the global array dfn is initially zero and that the
// global variable num is initialized to 1. n is the number of vertices in G.
{
to
148
6.7.1. Example:
For the following graph identify the articulation points and Biconnected components:
m
8 6
1 1
1 1 5 7
co
2 4
2 4 6 2 7 9
3 3
3 3 8 10
a.
4 1 0 5 9 6 2
4 10 9 5
7 5
Grap h
iy 8 6 7 9
un
8 10
L (u) = min {DFN (u), min {L (w) w is a child of u}, min {DFN (w) w is a vertex
to which there is back edge from u}}
al
L (1) = min {DFN (1), min {L (4)}} = min {1, L (4)} = min {1, 1} = 1
ri
L (4) = min {DFN (4), min {L (3)}} = min {2, L (3)} = min {2, 1} = 1
= min {3, min {L (10), L (9), L (2)}} = min {3, min {4, 5, 1}} = 1
149
m
Vertex 3: is an articulation point as child 10 has L (10) = 4 and DFN (3) = 3,
So, the condition L (10) > DFN (3) is true.
co
Vertex 4: is not an articulation point as child 3 has L (3) = 1 and DFN (4) = 2,
So, the condition L (3) > DFN (4) is false.
a.
Vertex 5: is an articulation point as child 6 has L (6) = 8, and DFN (5) = 7,
So, the condition L (6) > DFN (5) is true.
iy
Vertex 7: is not an articulation point as child 8 has L (8) = 6, and DFN (7) = 9,
So, the condition L (8) > DFN (7) is false.
Example:
For the following graph identify the articulation points and Biconnected components:
al
4 1 1
1
2 3 7 8 2 2 3 3
ri
5 6 4 5 5 6 4 6
to
G ra p h
7 7
Tu
8 8
D F S s p a n ni n g T re e
150
V ert e x
1 2
2 1 3
3 2 5 6 4
4 3 7 8
5 3
6 3
m
7 4 8
8 4 7
co
Adj ac enc y List
a.
L (u) = min {DFN (u), min {L (w) w is a child of u}, min {DFN (w) w is a vertex
to which there is back edge from u}}
iy
L (1) = min {DFN (1), min {L (2)}} = min {1, L (2)} = min {1, 2} = 1
L (2) = min {DFN (2), min {L (3)}} = min {2, L (3)} = min {2, 3} = 2
L (3) = min {DFN (3), min {L (4), L (5), L (6)}} = min {3, min {6, 4, 5}} = 3
un
L (4) = min {DFN (4), min {L (7)} = min {6, L (7)} = min {6, 6} = 6
L (5) = min {DFN (5)} = 4
L (6) = min {DFN (6)} = 5
sD
Check for the condition if L (w) > DFN (u) is true, where w is any child of u.
to
151
Example:
m
For the following graph identify the articulation points and Biconnected components:
1 1
co
5
2 2
1
6 3 3
a.
2 4
4 4
8
3 7 8
iy
5 5
7
V ert e x
2 4 5
1
2 1 3
al
3 2 4 7
4 1 3 5 6 7 8
ri
5 1 4 6
6 4 5 8
to
7 3 4
8 4 6
Tu
L (u) = min {DFN (u), min {L (w) w is a child of u}, min {DFN (w) w is a vertex
to which there is back edge from u}}
L (2) = min {DFN (2), min {L (3)}} = min {2, L (3)} = min{2, 1}= 11
152
L (3) = min {DFN (3), min {L (4)}} = min {3, L (4)} = min {3, L (4)}
= min {3, 1} = 1
L (4) = min {DFN (4), min {L (5), L (7)}, min {DFN (1)}}
= min {4, min {L (5), L (7)}, 1} = min {4, min {1, 3}, 1}
= min {4, 1, 1} = 1
L (5) = min {DFN (5), min {L (6)}, min {DFN (1)}} = min {5, L (6), 1}
= min {5, 4, 1} = 1
L (6) = min {DFN (6), min {L (8)}, min {DFN (4)}} = min(6, L (8), 4}
= min(6, 4, 4} = 4
m
L (7) = min {DFN (7), min {DFN (3)}} = min {8, 3} = 3
co
L (8) = min {DFN (8), min {DFN (4)}} = min {7, 4} = 4
a.
Finding the Articulation Points:
GAME PLAYING
In game-playing literature, the term play is used for a move. The two major components
of game-playing program are plausible move generator, and a static evaluation function
generator.
153
In a game of chess, for every move a player makes, the average branching factor is 35,
that is the opponent can make 35 different moves. It may not be possible to examine all
the states because the amount of time given for a move is limited and the amount of
computational power available for examining various states is also limited. Hence it is
essential that only very selected moves or paths are examined. For this purpose only,
one has a plausible move generator, that expands or generates only selected moves.
Static Evaluation function generator (SEF) is the most important component of the
game-playing program. The process of computing a number that reflects board quality is
called static evaluation. The procedure that does the computation is called a static
evaluator. A static evaluation function, is defined as the one that estimates the value of
the board position without looking any of that position's successors.
m
The SEF gives a snapshot of a particular move. More the SEF value, more is the
probability for a victory. Games can be classified as either a single person playing or
co
multi-person playing. For single person games (for example, Rubik's cube, 8-tile puzzle
etc.) search strategies such as Best-first branch and bound algorithm can be used.
On the other hand, in a two-person games like chess, checkers etc., each player tries to
a.
outsmart the opponent. Each has their own way of evaluating the situation and since
each player tries to obtain the maximum benefits, best first search branch and bound
algorithm do not serve the purpose. The basic methods available for game playing are:
1.
2.
Minimax search.
iy
Minimax search with alpha beta cutoffs.
un
MINIMAX SEARCH
The standard algorithm for two-player games is called minimax search with static
sD
evaluation. We have to add more knowledge to improve search and to reduce the
complexity. Heuristic search adds a small amount of knowledge to a problem space,
giving, surprisingly, a fairly dramatic effect of the efficiency of search algorithm. A good
heuristic is to select roads that go in the direction of the goal. In this case we call it as
heuristic static evaluation function that takes a board position and returns a number that
al
indicates how favorable that position is to one player or other. We call our players as
MAX and MIN.
ri
The large positive values would correspond to strong positions for one player whereas
large negative values would represent advantageous situations for the opponent.
to
The player for whom large positive values are advantageous is called the MAX, and
conversely the opponent is referred to as MIN. The value of a node where it is MAX's
turn to move is maximum of the values of its children, while the value of a node where
MIN is to move is the minimum of the values of its children.
Tu
MAX represents the player trying to win, or to MAXimize his/her advantage. MIN, the
opponent attempts to MINimize MAX's score. (i.e. we can assume that MIN uses the
same information and always attempts to move to a state that is worst for MAX).
At alternate levels of tree, the minimum and the maximum values of the children are
backedup. The backingup values is done as follows: The (MAX node) parent of MIN tip
nodes is assigned a backedup value equal to the maximum of the evaluations of the tip
nodes, on the other hand, if MIN were to choose among tip nodes, he will choose that
having the smallest evaluation (the most negative). Therefore, the (MIN node), parent
of MAX tip nodes is assigned a backedup value equal to the minimum of the evaluation
of the tip nodes.
154
After the parents of all tip nodes have been assigned backedup values, we backup
values another level, assuming that MAX would choose that node with largest backedup
value, while MIN would choose that node with smallest backedup value.
We continue the procedure level by level, until finally, the successors of the start node
are assigned backedup values. The search algorithm then uses these derived values to
select among possible next moves.
A -2 MAX
m
B -6 C -2 D -4 MIN
co
E F G H I J K MAX
9 -6 0 0 -2 -4 -3
a.
The best first move can be extracted from minimax procedure after search terminates.
EXAMPLE
iy
To illustrate these ideas, let us consider a simple game called "Grundy's game". The
rules of the game are as follows: Two players have in front of them a single pile of 7
un
matches. At each move the player must divide a pile of matches into two non-empty
piles with different numbers of matches in each pile. Thus, 6 matches may be divided
into piles of 5 and 1 or 4 and 2, but not 3 and 3. The first player who can no longer
make a move loses the game. The following figure shows the space for a game with 7
sD
M IN 7
al
MAX 6- 1 5- 2 4- 3
ri
M IN 5- 1- 1 4- 2 - 1 3- 2- 2 3- 3- 1
to
MAX 1 3- 2- 1- 1 2- 2- 2- 1
Tu
4- 1- 1 -
M IN
3 - 1- 1 - 1 - 1 2- 2- 1 - 1- 1
MAX 2 - 1 - 1 - 1 - 1- 1
A GA M E G RA P H F O R G R U N DY S GA M E
We can also use the game tree to show that, no matter what min does max can always
win. A winning strategy for max is shown by heavy lines. Max, can always force the
game to a win, regardless of min‟s first move. Min could win only if max played foolishly.
155
The minimax pursues all branches in the space, including many that can be ignored or
pruned by a more intelligent algorithm. Researchers in game playing have developed a
class of search technique called Alpha-beta pruning to improve the efficiency of search in
two-person games.
The idea for alpha-beta search is simple: Two values called alpha and beta are created
during the search. The alpha value, associated with MAX nodes, can never decrease, and
the beta value, associated with MIN nodes, can never increase.
Suppose for example, MAX node's alpha value is 6, then MAX need not consider any
m
backedup value less than or equal to 6 that is associated with any MIN node below it.
Similarly, if MIN has a beta value of 6, it need not consider further any MAX node below
it that has a value of 6 or more.
co
Because of these constraints, we can state the following two rules for terminating the
search:
a.
1. Search can be stopped below any MIN node having a beta value less than or
equal to the alpha value of any of its MAX node ancestors. The final backedup
value of this MIN node can then the set to its beta value.
2.
iy
Search can be stopped below any MAX node having an alpha value greater than
or equal to the beta value of any of its MIN node ancestors. The final backedup
value of this MAX node can then be set to its alpha value.
un
Figure given below shows an example of alpha-beta pruning. The search proceeds
depth-first to minimise the memory requirement, and only evaluates a node when
necessary. After statically evaluating nodes D and E to 6 and 5, respectively, we back up
sD
their maximum value, 6 as the value of node C. After statically evaluating node G as 8,
we know that the backed up value of node F must be greater than or equal to 8, since it
is the maximum of 8 and the unknown value node W. The value of node B must be 6
then, because it is the maximum of 6 and a value that must be greater than or equal to
8. Since we have exactly determined the value of node B, we do not need to evaluate or
al
Similarly, after statically evaluating nodes J and K to 2 and 1, the backed up value is
ri
their maximum or 2. This tells that the backed up value of node H must be less than or
equal to 2, since it the minimum of 2 and the unknown value of node X. Since the value
of node A is the maximum of 6 and a value that must be less than or equal to 2, it must
to
be 6, and hence we have evaluated the root of the tree without generating or evaluating
the nodes X, Y or Z. This is called BETA CUTOFF.
The whole process of keeping track of alpha and beta values and making cutoff's when
Tu
156
A 6
B 6 2 H<=2
C 6 F>=8 8 I 2 X
6 5 8 2 1
W Y Z
m
D E G J K
Alpha - Beta Pruning
co
6.8.1. EXAMPLE 2:
Taking the space of figure as shown below and when alpha-beta pruning applied
is on this problem, is as follows:
a.
Max
3
3
0 2
iy 3 A
3
0
C
D 2 E M in
un
3 9 7 2 6 0 2 Max
3 B
0
2 3 7 4 2 1 5 6 2 3 5 0 2 1 M in
sD
5 9
0
Hy p ot h et ic a l st at e s p ac e t o min i ma x A lp h a - Bet a Pru n in g
C is 3.
to
AND/OR GRAPH:
N, a set of nodes,
An ordinary graph is a special case of hypergraph in which all the sets of decendent
nodes have a cardinality of 1.
157
R
N1
m
N2
N
co
P Q Nk
E xpre s s io n f or P a n d Q - > R A K- C o n n ec t or
a.
The K-connector is represented as a fan of arrows with a single tie is shown above.
The and/or graphs consists of nodes labelled by global databases. Nodes labelled by
iy
compound databases have sets of successor nodes. These successor nodes are called
AND nodes, in order to process the compound database to termination, all the
compound databases must be processed to termination.
un
For example consider, consider a boy who collects stamps (M). He has for the purpose
of exchange a winning conker (C), a bat (B) and a small toy animal (A). In his class
there are friends who are also keen collectors of different items and will make the
following exchanges.
sD
1. 1 winning conker (C) for a comic (D) and a bag of sweets (S).
2. 1 winning conker (C) for a bat (B) and a stamp (M).
al
The problem is how to carry out the exchanges so that all his exchangable items are
converted into stamps (M). This task can be expressed more briefly as:
to
2. Transformation rules:
Tu
a. If C then (D, S)
b. If C then (B, M)
c. If B then (M, M)
d. If A then (B, B, M)
158
(C, B, A)
(D S B A) (B M B A) (C M M A) (C B B B M)
(D S M M A) (M M M B A) (B M M M A) (B M B B B M)
(D S M M B B M) (M M M M M A) (M M M M M A) (M M M B B B M)
m
(D S M M M M B M) (M M M M M B B M)
co
(D S M M M M M M M) (M M M M M M M B M)
(M M M M M M M M M M) GOAL
a.
Expansion for the exchange problem using OR connectors only
The figure shows that, a lot of extra work is done by redoing many of the
transformations. This repetition can be avoided by decomposing the problem into
subproblems. There are two major ways to order the components:
1. iy
The components can either be arranged in some fixed order at the time
un
they are generated (or).
2. They can be dynamically reordered during processing.
The more flexible system is to reorder dynamically as the processing unfolds. It can
be represented by and/or graph. The solution to the exchange problem will be:
sD
Swap conker for a bat and a stamp, then exchange this bat for two stamps.
Swap his own bat for two more stamps, and finally swap the small toy animal
for two bats and a stamp. The two bats can be exchanged for two stamps.
al
(C B A)
to
(D S) (B M) (M M) (B B M)
(M M) (M M) (M M)
159
Example 1:
1. A
2. B
3. C
4. A ^ B -> D
5. A ^ C -> E
6. B ^ D -> F
7. F -> G
8. A ^ E -> H
m
C A B
co
E D
a.
H F
G
iy
un
sD
al
ri
to
Tu
160
Chapter
7
BACKTRACKING
General Method:
m
Backtracking is used to solve problem in which a sequence of objects is chosen from a
specified set so that the sequence satisfies some criterion. The desired solution is
expressed as an n-tuple (x1, . . . . , xn) where each xi Є S, S being a finite set.
co
The solution is based on finding one or more vectors that maximize, minimize, or
satisfy a criterion function P (x1, . . . . . , xn). Form a solution and check at every step
if this has any chance of success. If the solution at any point seems not promising,
ignore it. All solutions requires a set of constraints divided into two categories: explicit
a.
and implicit constraints.
Definition 1: Explicit constraints are rules that restrict each xi to take on values only
iy
from a given set. Explicit constraints depend on the particular instance I
of problem being solved. All tuples that satisfy the explicit constraints
define a possible solution space for I.
un
Definition 2: Implicit constraints are rules that determine which of the tuples in the
solution space of I satisfy the criterion function. Thus, implicit
constraints describe the way in which the xi‟s must relate to each other.
sD
Explicit constraints using 8-tuple formation, for this problem are S= {1, 2, 3,
4, 5, 6, 7, 8}.
al
The implicit constraints for this problem are that no two queens can be the
same (i.e., all queens must be on different columns) and no two queens can
be on the same diagonal.
ri
given problem instance. This search is facilitated by using a tree organization for the
solution space.
Tu
Backtracking is the procedure where by, after determining that a node can lead to
nothing but dead end, we go back (backtrack) to the nodes parent and proceed with
the search on the next child.
A backtracking algorithm need not actually create a tree. Rather, it only needs to
keep track of the values in the current branch being investigated. This is the way we
implement backtracking algorithm. We say that the state space tree exists implicitly
in the algorithm because it is not actually constructed.
161
Terminology:
Solution states are the problem states „S‟ for which the path from the root node to
„S‟ defines a tuple in the solution space.
Answer states are those solution states for which the path from root node to s
defines a tuple that is a member of the set of solutions.
State space is the set of paths from root node to other nodes. State space tree is the
tree organization of the solution space. The state space trees are called static trees.
m
This terminology follows from the observation that the tree organizations are
independent of the problem instance being solved. For some problems it is
advantageous to use different tree organizations for different problem instance. In
co
this case the tree organization is determined dynamically as the solution space is
being searched. Tree organizations that are problem instance dependent are called
dynamic trees.
a.
Live node is a node that has been generated but whose children have not yet been
generated.
Dead node is a generated node that is not to be expanded or explored any further.
un
All children of a dead node have already been expanded.
Branch and Bound refers to all state space search methods in which all children of
an E-node are generated before any other live node can become the E-node.
sD
Depth first node generation with bounding functions is called backtracking. State
generation methods in which the E-node remains the E-node until it is dead, lead to
branch and bound methods.
al
Planar Graphs:
edges to intersect at points other than at vertices of the graph. These points of
interactions are called crossovers.
to
Example:
a
a
e b
the following graph can be redrawn without e b
crossovers as follows:
d c
d c
162
Bipartite Graph:
A bipartite graph is a non-directed graph whose set of vertices can be portioned into
two sets V1 and V2 (i.e. V1 U V2 = V and V1 ∩ V2 = ø) so that every edge has one end
in V1 and the other in V2. That is, vertices in V1 are only adjacent to those in V2 and
vice- versa.
Example:
a b c a c e
m
The graph is bipartite. We can redraw it as
d e f b d f
co
The vertex set V = {a, b, c, d, e, f} has been partitioned into V1 = {a, c, e} and
V2 = {b, d, f}. The complete bipartite graph for which V 1 = n and V2 = m is denoted
Kn,m.
a.
N-Queens Problem:
iy
Let us consider, N = 8. Then 8-Queens Problem is to place eight queens on an 8 x 8
chessboard so that no two “attack”, that is, no two of them are on the same row,
column, or diagonal.
un
All solutions to the 8-queens problem can be represented as 8-tuples (x1, . . . . , x8),
where xi is the column of the ith row where the ith queen is placed.
sD
The explicit constraints using this formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 < i <
8. Therefore the solution space consists of 88 8-tuples.
The implicit constraints for this problem are that no two xi‟s can be the same (i.e., all
queens must be on different columns) and no two queens can be on the same
al
diagonal.
This realization reduces the size of the solution space from 88 tuples to 8! Tuples.
ri
The promising function must check whether two queens are in the same column or
diagonal:
to
Suppose two queens are placed at positions (i, j) and (k, l) Then:
Diag 45 conflict: Two queens i and j are on the same 450 diagonal if:
i – j = k – l.
This implies, j – l = i – k
This implies, j – l = k – i
163
Therefore, two queens lie on the same diagonal if and only if:
j - l = i – k
Where, j be the column of object in row i for the ith queen and l be the column of
object in row „k‟ for the kth queen.
To check the diagonal clashes, let us take the following tile configuration:
*
In this example, we have:
*
m
*
i 1 2 3 4 5 6 7 8
*
* xi 2 5 1 8 4 7 3 6
co
*
Let us consider for the
*
case whether the queens on 3rd row and 8th row
* are conflicting or not. In this
a.
case (i, j) = (3, 1) and (k, l) = (8, 6). Therefore:
j - l = i – k 1 - 6 = 3 – 8
iy 5=5
Example:
sD
*
*
al
*
*
ri
to
Tu
Step 1:
Add to the sequence the next number in the sequence 1, 2, . . . , 8 not yet
used.
Step 2:
If this new sequence is feasible and has length 8 then STOP with a solution. If
the new sequence is feasible and has length less then 8, repeat Step 1.
Step 3:
If the sequence is not feasible, then backtrack through the sequence until we
find the most recent place at which we can exchange a value. Go back to Step
1.
164
Remarks
1 2 3 4 5 6 7 8
7 5 3 1
j - l = 1 – 2 = 1
7 5 3 1* 2*
i – k = 4 – 5 = 1
7 5 3 1 4
j - l = 7 – 2 = 5
7* 5 3 1 4 2*
i – k = 1 – 6 = 5
j - l = 3 – 6 = 3
7 5 3* 1 4 6*
i – k = 3 – 6 = 3
m
7 5 3 1 4 8
j - l = 4 – 2 = 2
7 5 3 1 4* 8 2*
i – k = 5 – 7 = 2
co
j - l = 4 – 6 = 2
7 5 3 1 4* 8 6*
i – k = 5 – 7 = 2
7 5 3 1 4 8 Backtrack
a.
7 5 3 1 4 Backtrack
7 5 3 1 6
7*
7
5
5
3
3
1
1
6
6
2*
4 iy j - l = 1 – 2 = 1
i – k = 7 – 6 = 1
un
7 5 3 1 6 4 2
j - l = 3 – 8 = 5
7 5 3* 1 6 4 2 8*
i – k =3 – 8 = 5
7 5 3 1 6 4 2 Backtrack
sD
7 5 3 1 6 4 Backtrack
7 5 3 1 6 8
7 5 3 1 6 8 2
al
7 5 3 1 6 8 2 4 SOLUTION
*
*
Tu
*
*
*
*
*
*
165
4 – Queens Problem:
Let us see how backtracking works on the 4-queens problem. We start with the root
node as the only live node. This becomes the E-node. We generate one child. Let us
assume that the children are generated in ascending order. Let us assume that the
children are generated in ascending order. Thus node number 2 of figure is generated
and the path is now (1). This corresponds to placing queen 1 on column 1. Node 2
becomes the E-node. Node 3 is generated and immediately killed. The next node
generated is node 8 and the path becomes (1, 3). Node 8 becomes the E-node.
However, it gets killed as all its children represent board configurations that cannot
lead to an answer node. We backtrack to node 2 and generate another child, node 13.
The path is now (1, 4). The board configurations as backtracking proceeds is as
m
follows:
co
1 1 1 1
. . 2 2 2
. . . . . 3
a.
(a) (b) (c) (d)
1
2
1
iy .
1
. . 2
1
2
un
3 3
. . . . . . 4
(e) (f) (g) (h)
The above figure shows graphically the steps that the backtracking algorithm goes
sD
through as it tries to find a solution. The dots indicate placements of a queen, which
were tried and rejected because another queen was attacking.
In Figure (b) the second queen is placed on columns 1 and 2 and finally settles on
al
column 3. In figure (c) the algorithm tries all four columns and is unable to place the
next queen on a square. Backtracking now takes place. In figure (d) the second
queen is moved to the next possible column, column 4 and the third queen is placed
ri
on column 2. The boards in Figure (e), (f), (g), and (h) show the remaining steps that
the algorithm goes through until a solution is found.
to
Tu
166
Complexity Analysis:
n 1
1 n n2 n3 . . . . . . . . . . . nn n 1
n1
m
Program for N-Queens Problem:
# include <stdio.h>
co
# include <conio.h>
# include <stdlib.h>
a.
place (int k)
{
int i;
for (i=1; i < k; i++)
{
iy
if ((x [i] == x [k]) || (abs (x [i] – x [k]) == abs (i - k)))
return (0);
un
}
return (1);
}
nqueen (int n)
{
sD
int m, k, i = 0;
x [1] = 0;
k = 1;
while (k > 0)
{
al
x [k] = x [k] + 1;
while ((x [k] <= n) && (!place (k)))
x [k] = x [k] +1;
if(x [k] <= n)
ri
{
if (k == n)
{
to
i++;
printf (“\ncombination; %d\n”,i);
for (m=1;m<=n; m++)
printf(“row = %3d\t column=%3d\n”, m, x[m]);
Tu
getch();
}
else
{
k++;
x [k]=0;
}
}
else
k--;
}
return (0);
167
}
main ()
{
int n;
clrscr ();
printf (“enter value for N: “);
scanf (“%d”, &n);
nqueen (n);
}
Output:
m
Enter the value for N: 4
Combination: 1 Combination: 2
co
Row = 1 column = 2 3
Row = 2 column = 4 1
Row = 3 column = 1 4
a.
Row = 4 column = 3 2
Sum of Subsets: iy
un
Given positive numbers wi, 1 ≤ i ≤ n, and m, this problem requires finding all subsets
of wi whose sums are „m‟.
Explicit constraints:
Implicit constraints:
The above solutions are then represented by (1, 1, 0, 1) and (0, 0, 1, 1).
For both the above formulations, the solution space is 2n distinct tuples.
For example, n = 4, w = (11, 13, 24, 7) and m = 31, the desired subsets are (11,
13, 7) and (24, 7).
168
The following figure shows a possible tree organization for two possible formulations
of the solution space for the case n = 4.
x 1 =1 1 x 1 =4
x 1 =2 x 1 =3
2 3 4 5
x 2 =4
x 2 =2 x 2 =3 x 2 =4
x 2 =3 x 2 =4
6 7 8 9 10 11
x 3 =3 S
x 3 =4 x 3 =4 x 3 =4
12 13 14 15
S
x 4 =4
m
16
A p o s s ib le s o lut io n s p ac e org a n is at io n f or t h e
s u m of t h e s u b s et s pro b le m.
co
The tree corresponds to the variable tuple size formulation. The edges are labeled
such that an edge from a level i node to a level i+1 node represents a value for xi. At
each node, the solution space is partitioned into sub - solution spaces. All paths from
a.
the root node to any node in the tree define the solution space, since any such path
corresponds to a subset satisfying the explicit constraints.
iy
The possible paths are (1), (1, 2), (1, 2, 3), (1, 2, 3, 4), (1, 2, 4), (1, 3, 4), (2), (2,
3), and so on. Thus, the left mot sub-tree defines all subsets containing w1, the next
sub-tree defines all subsets containing w2 but not w1, and so on.
un
Graph Coloring (for planar graphs):
Let G be a graph and m be a given positive integer. We want to discover whether the
sD
nodes of G can be colored in such a way that no two adjacent nodes have the same
color, yet only m colors are used. This is termed the m-colorabiltiy decision problem.
The m-colorability optimization problem asks for the smallest integer m for which the
graph G can be colored.
al
Given any map, if the regions are to be colored in such a way that no two adjacent
regions have the same color, only four colors are needed.
ri
For many years it was known that five colors were sufficient to color any map, but no
map that required more than four colors had ever been found. After several hundred
years, this problem was solved by a group of mathematicians with the help of a
to
computer. They showed that in fact four colors are sufficient for planar graphs.
The function m-coloring will begin by first assigning the graph to its adjacency matrix,
Tu
setting the array x [] to zero. The colors are represented by the integers 1, 2, . . . , m
and the solutions are given by the n-tuple (x1, x2, . . ., xn), where xi is the color of
node i.
A recursive backtracking algorithm for graph coloring is carried out by invoking the
statement mcoloring(1);
169
m
If (k = n) then // at most m colors have been
// used to color the n vertices.
write (x [1: n]);
else mcoloring (k+1);
co
} until (false);
}
a.
// x [1] , . . . . x [k-1] have been assigned integer values in the range [1, m] such that
// adjacent vertices have distinct integers. A value for x [k] is determined in the range
// [0, m].x[k] is assigned the next highest numbered color while maintaining distinctness
// from the adjacent vertices of vertex k. If no such color exists, then x [k] is 0.
{
repeat
{ iy
un
x [k]: = (x [k] +1) mod (m+1) // Next highest color.
If (x [k] = 0) then return; // All colors have been used
for j := 1 to n do
{ // check if this color is distinct from adjacent colors
if ((G [k, j] 0) and (x [k] = x [j]))
sD
// If (k, j) is and edge and if adj. vertices have the same color.
then break;
}
if (j = n+1) then return; // New color found
} until (false); // Otherwise try to find another color.
al
Example:
ri
Color the graph given below with minimum number of colors by backtracking using
state space tree.
to
x1
1 3
2
Tu
1 2 2 3 1 3 1 2 x2
1 3 1 2 2 3 1 2 2 3 1 3 x3
4 3
Gra p h
x4
2 3 2 2 3 3 1 3 1 3 1 3 1 1 2 2 1 2
170
Hamiltonian Cycles:
m
1 2 3 4 1 2 3
co
8 7 6 5 5 4
Graph G1 Graph G2
a.
Two graphs to illustrate Hamiltonian cycle
The backtracking solution vector (x1, . . . . . xn) is defined so that xi represents the ith
visited vertex of the proposed cycle. If k = 1, then x1 can be any of the n vertices. To
iy
avoid printing the same cycle n times, we require that x1 = 1. If 1 < k < n, then xk
can be any vertex v that is distinct from x 1, x2, . . . , xk–1 and v is connected by an
edge to kx-1. The vertex xn can only be one remaining vertex and it must be connected
un
to both xn-1 and x1.
Hamiltonian(2).
The traveling salesperson problem using dynamic programming asked for a tour that
has minimum cost. This tour is a Hamiltonian cycles. For the simple case of a graph
all of whose edge costs are identical, Hamiltonian will find a minimum-cost tour if a
al
tour exists.
// x [1: k-1] is a path of k – 1 distinct vertices . If x[k] = 0, then no vertex has as yet been
// assigned to x [k]. After execution, x[k] is assigned to the next highest numbered vertex
// which does not already appear in x [1 : k – 1] and is connected by an edge to x [k – 1].
to
171
m
0/1 Knapsack:
co
Given n positive weights wi, n positive profits pi, and a positive number m that is the
knapsack capacity, the problem calls for choosing a subset of the weights such that:
w p
a.
i xi m and i xi is maximized.
1 i n 1 i n
iy
The solution space for this problem consists of the 2n distinct ways to assign zero or
one values to the xi‟s.
un
Bounding functions are needed to kill some live nodes without expanding them. A
good bounding function for this problem is obtained by using an upper bound on the
value of the best feasible solution obtainable by expanding the given live node and
sD
any of its descendants. If this upper bound is not higher than the value of the best
solution determined so far, than that live node can be killed.
We continue the discussion using the fixed tuple size formulation. If at node Z the
values of xi, 1 < i < k, have already been determined, then an upper bound for Z can
al
We have solved TSP problem using dynamic programming. In this section we shall
solve the same problem using backtracking.
Tu
2 7 12
3 5 8 10 13 15
4 6 9 11 14 16
m
We will assume that the starting node is 1 and the ending node is obviously 1. Then
1, {2, … ,4}, 1 forms a tour with some cost which should be minimum. The vertices
co
shown as {2, 3, …. ,4} forms a permutation of vertices which constitutes a tour. We
can also start from any vertex, but the tour should end with the same vertex.
Since, the starting vertex is 1, the tree has a root node R and the remaining nodes
a.
are numbered as depth-first order. As per the tree, from node 1, which is the live
node, we generate 3 braches node 2, 7 and 12. We simply come down to the left
most leaf node 4, which is a valid tour {1, 2, 3, 4, 1} with cost 30 + 5 + 20 + 4 = 59.
iy
Currently this is the best tour found so far and we backtrack to node 3 and to 2,
because we do not have any children from node 3. When node 2 becomes the E-
node, we generate node 5 and then node 6. This forms the tour {1, 2, 4, 3, 1} with
cost 30 + 10 + 20 + 6 = 66 and is discarded, as the best tour so far is 59.
un
Similarly, all the paths from node 1 to every leaf node in the tree is searched in a
depth first manner and the best tour is saved. In our example, the tour costs are
shown adjacent to each leaf nodes. The optimal tour cost is therefore 25.
sD
al
ri
to
Tu
173
Chapter
8
Branch and Bound
General method:
Branch and Bound is another method to systematically search a solution space. Just
like backtracking, we will use bounding functions to avoid generating subtrees that
m
do not contain an answer node. However branch and Bound differs from backtracking
in two important manners:
co
1. It has a branching function, which can be a depth first search, breadth first
search or based on bounding function.
2. It has a bounding function, which goes far beyond the feasibility test as a
a.
mean to prune efficiently the search tree.
Branch and Bound refers to all state space search methods in which all children of
iy
the E-node are generated before any other live node becomes the E-node
Branch and Bound is the generalisation of both graph search strategies, BFS and D-
un
search.
A BFS like state space search is called as FIFO (First in first out) search
as the list of live nodes in a first in first out list (or queue).
sD
A D search like state space search is called as LIFO (Last in first out)
search as the list of live nodes in a last in first out (or stack).
Definition 1: Live node is a node that has been generated but whose children have
not yet been generated.
al
Definition 2: E-node is a live node whose children are currently being explored. In
other words, an E-node is a node currently being expanded.
ri
Definition 4: Branch-an-bound refers to all state space search methods in which all
children of an E-node are generated before any other live node can
Tu
173
In both LIFO and FIFO Branch and Bound the selection rule for the next E-node in
rigid and blind. The selection rule for the next E-node does not give any preference
to a node that has a very good chance of getting the search to an answer node
quickly.
The search for an answer node can be speeded by using an “intelligent” ranking
function c ( ) for live nodes. The next E-node is selected on the basis of this ranking
function. The node x is assigned a rank using:
c ( x ) = f(h(x)) + g( x )
m
where, c ( x ) is the cost of x.
co
h(x) is the cost of reaching x from the root and f(.) is any non-decreasing
function.
g ( x ) is an estimate of the additional effort needed to reach an answer node
a.
from x.
A search strategy that uses a cost function c ( x ) = f(h(x) + g( x ) to select the next
iy
E-node would always choose for its next E-node a live node with least c (.) is called a
LC–search (Least Cost search)
un
BFS and D-search are special cases of LC-search. If g( x ) = 0 and f(h(x)) = level of
node x, then an LC search generates nodes by levels. This is eventually the same as
a BFS. If f(h(x)) = 0 and g( x ) > g( y ) whenever y is a child of x, then the search is
essentially a D-search.
sD
We associate a cost c(x) with each node x in the state space tree. It is not possible to
easily compute the function c(x). So we compute a estimate c ( x ) of c(x).
ri
Let t be a state space tree and c() a cost function for the nodes in t. If x is a node in
t, then c(x) is the minimum cost of any answer node in the subtree with root x. Thus,
c(t) is the cost of a minimum-cost answer node in t.
Tu
A heuristic c (.) is used to estimate c(). This heuristic should be easy to compute and
generally has the property that if x is either an answer node or a leaf node, then
c(x) = c ( x ) .
LC-search uses c to find an answer node. The algorithm uses two functions Least() and
Add() to delete and add a live node from or to the list of live nodes, respectively.
Least() finds a live node with least c(). This node is deleted from the list of live nodes
and returned.
174
Add(x) adds the new live node x to the list of live nodes. The list of live nodes be
implemented as a min-heap.
Algorithm LCSearch outputs the path from the answer node it finds to the root node
t. This is easy to do if with each node x that becomes live, we associate a field parent
which gives the parent of node x. When the answer node g is found, the path from g
to t can be determined by following a sequence of parent values starting from the
current E-node (which is the parent of g) and ending at node t.
Listnode = record
{
Listnode * next, *parent; float cost;
m
}
Algorithm LCSearch(t)
{ //Search t for an answer node
co
if *t is an answer node then output *t and return;
E := t; //E-node.
initialize the list of live nodes to be empty;
repeat
{
a.
for each child x of E do
{
if x is an answer node then output the path from x to t and return;
}
Add (x);
(x parent) := E;
iy
if there are no more live nodes then
//x is a new live node.
// pointer for path to root
un
{
write (“No answer node”);
return;
}
E := Least();
sD
} until (false);
}
The root node is the first, E-node. During the execution of LC search, this list
contains all live nodes except the E-node. Initially this list should be empty.
al
Examine all the children of the E-node, if one of the children is an answer node, then
the algorithm outputs the path from x to t and terminates. If the child of E is not an
answer node, then it becomes a live node. It is added to the list of live nodes and its
ri
parent field set to E. When all the children of E have been generated, E becomes a
dead node. This happens only if none of E‟s children is an answer node. Continue the
search further until no live nodes found. Otherwise, Least(), by definition, correctly
to
chooses the next E-node and the search continues from here.
LC search terminates only when either an answer node is found or the entire state
space tree has been generated and searched.
Tu
Bounding:
A branch and bound method searches a state space tree using any search
mechanism in which all the children of the E-node are generated before another node
becomes the E-node. We assume that each answer node x has a cost c(x) associated
with it and that a minimum-cost answer node is to be found. Three common search
strategies are FIFO, LIFO, and LC. The three search methods differ only in the
selection rule used to obtain the next E-node.
175
A good bounding helps to prune efficiently the tree, leading to a faster exploration of
the solution space.
A cost function c (.) such that c ( x ) < c(x) is used to provide lower bounds on
solutions obtainable from any node x. If upper is an upper bound on the cost of a
minimum-cost solution, then all live nodes x with c(x) > c ( x ) > upper. The starting
value for upper can be obtained by some heuristic or can be set to .
As long as the initial value for upper is not less than the cost of a minimum-cost
answer node, the above rules to kill live nodes will not result in the killing of a live
node that can reach a minimum-cost answer node. Each time a new answer node is
found, the value of upper can be updated.
m
Branch-and-bound algorithms are used for optimization problems where, we deal
directly only with minimization problems. A maximization problem is easily converted
co
to a minimization problem by changing the sign of the objective function.
To formulate the search for an optimal solution for a least-cost answer node in a
state space tree, it is necessary to define the cost function c(.), such that c(x) is
a.
minimum for all nodes representing an optimal solution. The easiest way to do this is
to use the objective function itself for c(.).
iy
For nodes representing feasible solutions, c(x) is the value of the objective
function for that feasible solution.
un
For nodes representing infeasible solutions, c(x) = .
For nodes representing partial solutions, c(x) is the cost of the minimum-cost
node in the subtree with root x.
sD
Since, c(x) is generally hard to compute, the branch-and-bound algorithm will use an
estimate c ( x ) such that c ( x ) < c(x) for all x.
The 15 puzzle is to search the state space for the goal state and use the path from
the initial state to the goal state as the answer. There are 16! (16! ≈ 20.9 x 1012)
ri
As the state space for the problem is very large it would be worthwhile to determine
to
whether the goal state is reachable from the initial state. Number the frame positions
1 to 16.
Tu
Position i is the frame position containing title numbered i in the goal arrangement of
Figure 8.1(b). Position 16 is the empty spot. Let position(i) be the position number in
the initial state of the title number i. Then position(16) will denote the position of the
empty spot.
less(i) be the number of tiles j such that j < i and position(j) > position(i).
16
The goal state is reachable from the initial state iff: less(i) x is even.
i 1
176
Here, x = 1 if in the initial state the empty spot is at one of the shaded positions of
figure 8.1(c) and x = 0 if it is at one of the remaining positions.
1 3 4 15 1 2 3 4
2 5 12 5 6 7 8
7 6 11 14 9 10 11 12
8 9 10 13 13 14 15
(a) An arrangement (b) Goal arrangement (c)
m
Example 1:
co
For the state of Figure 8.1(a) we have less(i) values as follows:
a.
less(5) = 0 less(6) = 0 less(7) = 1 less(8) = 0
less(9) = 0
less(13) = 0
less(10) = 0
less(14) = 4
iy
less(11) = 3
less(15) = 11
less(12) = 6
less(16) = 10
un
16
Therefore, less(i) x (0 + 0 + 1 + 1 + 0 + 0 + 1 + 0 + 0 + 0 + 3 + 6 + 0 +
i 1
4 + 11 + 10) + 0 = 37 + 0 = 37.
sD
Example 2:
al
For the root state of Figure 8.2 we have less(i) values are as follows:
ri
16
Therefore, less(i) x (0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 1 + 1 + 0 + 0 + 1 +
i 1
1 + 1 + 9) + 1 = 15 + 1 = 16.
177
A depth first state space tree generation will result in the subtree of Figure 8.3 when
the next moves are attempted in the order: move the empty space up, right, down
and left. The search of the state space tree is blind. It will take the leftmost path
from the root regardless of the starting configuration. As a result, the answer node
may never be found.
A breadth first search will always find a goal node nearest to the root. However, such
a search is also blind in the sense that no matter what the initial configuration, the
algorithm attempts to make the same sequence of moves.
m
We need a more intelligent search method. We associate a cost c(x) with each node
x in the state space tree. The cost c(x) is the length of a path from the root to a
nearest goal node in the subtree with root x. The easy to compute estimate c (x) of
co
c(x) is as follows:
c (x) = f(x) + g (x)
a.
where, f(x) is the length of the path from the root to node x and
g (x) is an estimate of the length of a shortest path from x to a goal node in
iy
the subtree with root x. Here, g (x) is the number of nonblank tiles not in
their goal position.
un
An LC-search of Figure 8.2, begin with the root as the E-node and generate all child
nodes 2, 3, 4 and 5. The next node to become the E-node is a live node with least
c (x).
c (2) =1+4=5
sD
c (3) =1+4=5
c (4) = 1 + 2 = 3 and
c (5) = 1 + 4 = 5.
al
Node 4 becomes the E-node and its children are generated. The live nodes at this
time are 2, 3, 5, 10, 11 and 12. So:
ri
c (10) = 2 + 1 = 3
c (11) = 2 + 3 = 5 and
c (12) = 2 + 3 = 5.
to
The live node with least c is node 10. This becomes the next E-node. Nodes 22 and 23
are generated next. Node 23 is the goal node, so search terminates.
Tu
LC-search was almost as efficient as using the exact function c(), with a suitable
choice for c (), LC-search will be far more selective than any of the other search
methods.
178
1
1 2 3 4
5 6 8
9 10 7 11
up 13 14 15 12 left
2 down 5
3 right 4
1 2 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 3 8 5 6 8 5 6 7 8 5 6 8
9 10 7 11 9 10 7 11 9 10 11 9 10 7 11
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12
left down right left up left
6 right 7 up 8 9 10 11 d own 12 13 d own 15
14
1 2 4 1 2 4 1 2 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 3 4 1 2 3 4 1 2 3 4
5 6 3 8 5 6 3 8 5 6 8 4 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8 5 2 6 8 5 10 6 8 5 6 8
m
9 10 7 11 9 10 7 11 9 10 7 11 9 10 11 9 10 11 9 10 15 11 9 10 11 9 10 7 11 9 7 11 9 10 7 11
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12 13 14 12 13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12
up
down 16 19 left 22 down 23
1 2 4 8 1 2 3 1 2 3 4 1 2 3 4
co
5 6 3 left 5 6 8 4 5 6 7 5 6 7 8
left
9 10 7 11 9 10 7 11 9 10 11 8 9 10 11 12
down down
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15
17 18 20 21 Goal
a.
1 6 2 4 1 2 4 1 2 3 4 1 2 3 4 Edges are labelled according to the direction in which
the empty space moves
5 3 8 5 6 3 8 5 6 8 11 5 6 8 11
9 10 7 11 9 10 7 11 9 10 7 12 9 10 7
iy
13 14 15 12 13 14 15 12 13 14 15 13 14 15 12
Figure 8.2. Part of the state space tree for 15-puzzle problem
un
1 2 3 4 5
1 2 3 4 1 2 4 1 2 4 1 2 4 8 1 2 4 8
5 6 8 up 5 6 3 8 right 5 6 3 8 down 5 6 3 down 5 6 3 11
sD
9 10 7 11 9 10 7 11 9 10 7 11 9 10 7 11 9 10 7
13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12 13 14 15 12
down
10 9 8 7 6
al
1 2 8 1 2 4 8 1 2 4 8 1 2 4 8 1 2 4 8
5 6 4 11 up 5 6 11 up 5 6 3 11 up 5 6 3 11 left 5 6 3 11
9 10 3 12 9 10 3 12 9 10 12 9 10 7 12 9 10 7 12
ri
13 14 7 15 13 14 7 15 13 14 7 15 13 14 15 13 14 15
We are given n jobs and one processor. Each job i is associated with it a three tuple
(pi, di, ti). Job i requires ti units of processing time. If its processing is not completed
by the deadline di, then a penalty pi is incurred. The objective is to select a subset J
of the n jobs such that all jobs in J can be completed by their deadlines. Hence, a
penalty can be incurred only on those jobs not in J. The subset J should be such that
the penalty incurred is minimum among all possible subsets J. Such a J is optimal.
179
Example:
Consider the following instance with n = 4, (p1, d1, t1) = (5, 1, 1), (p2, d2, t2) = (10,
3, 2), (p3, d3, t3) = (6, 2, 1) and (p4, d4, t4) = (3, 1, 1).
The solution space for this instance consists of all possible subsets of the job index
set {1, 2, 3, 4}. The solution space can be organized into a tree. Figure 8.4
corresponds to the variable tuple size formulation. In figure square nodes represent
infeasible subsets and all non-square nodes are answer nodes. Node 9 represents an
optimal solution and is the only minimum-cost answer node. For this node J={2, 3}
and the penalty is 8.
m
co
a.
iy
The cost function c(x) for any circular node x is the minimum penalty corresponding
un
to any node in the subtree with root x.
An upper bound u(x) on the cost of a minimum-cost answer node in the subtree x is
u(x) = iSx pi . The u(x) is the cost of the solution Sx corresponding to node x.
ri
S(2) = {1} M =1 c2 pi 0
to
i m
i sx
S(3) = {2} m=2 c3 pi pi 5
i 2 i 1
Tu
S(4)= {3} m = 3. c4 pi pi p1 p2 5 10 15
i 3 i 1, 2
S(5) = {4} m=4 c5 pi pi p1 p2 p3 5 10 6 21
i 4 i 1, 2, 3
S(6) = {1, 2} m=2 c6 pi pi 0
i 1, 2 i 1
i sx i s6
S(7) = {1, 3} m=3 c
7 pi p2 = 10
i3
i s7
180
S(8) = {1, 4} m=4
c8
i4
pi p2 p3 10 6 16
i s 8
S(9)={2,3} m=3 c (9)= 5
S(10)={2,4} m=3 c (10)=11
S(11)={3,4} m=4 c (11)=15
m
Calculation of the Upper bound u(x) = pi
i sx
co
U(1) = 0
U(2) = pi p2 p3 p4 10 6 3 19 eliminate job 1
i s 2
a.
U(3) = p1 + p3 + p4 = 5 + 6 + 3 = 14 eliminate job 2
U(4) = p1 + p2 + p4 = 5 + 10 + 3 = 18 eliminate job 3
U(5) = p1 + p2 + p4 = 5 + 10 + 6= 21
U(6) = p3 + p4 = 6 + 3 = 9
iy eliminate job 4
eliminate jobs 1, 2
un
U(7) = p2 + p4 = 10 + 3 = 13 eliminate jobs 1, 3
U(8) = p2 + p3 = 10 + 6 = 16 eliminate jobs 1, 4
U(9) = p1 + p4 = 5 + 3 = 8 eliminate jobs 2, 3
sD
A FIFO branch-and-bound algorithm for the job sequencing problem can begin with
ri
Starting with node 1 as the E-node and using the variable tuple size formulation of
to
Figure 8.4, nodes 2, 3, 4, and 5 are generated. Then u(2) = 19, u(3) = 14, u(4) =
18, and u(5) = 21.
The variable upper is updated to 14 when node 3 is generated. Since c (4) and
Tu
c(5) are greater than upper, nodes 4 and 5 get killed. Only nodes 2 and 3 remain
alive.
Node 2 becomes the next E-node. Its children, nodes 6, 7 and 8 are generated.
Then u(6) = 9 and so upper is updated to 9. The cost c(7) = 10 > upper and node 7
gets killed. Node 8 is infeasible and so it is killed.
Next, node 3 becomes the E-node. Nodes 9 and 10 are now generated. Then u(9) =
8 and so upper becomes 8. The cost c(10) = 11 > upper, and this node is killed.
181
The next E-node is node 6. Both its children are infeasible. Node 9‟s only child is also
infeasible. The minimum-cost answer node is node 9. It has a cost of 8.
The FIFO-based branch-and-bound algorithm with an appropriate c(.) and u(.) is
called FIFOBB.
m
LC Branch and Bound:
co
An LC Branch-and-Bound search of the tree of Figure 8.4 will begin with upper =
and node 1 as the first E-node.
a.
When node 1 is expanded, nodes 2, 3, 4 and 5 are generated in that order.
iy
nodes 4 and 5 are killed as c(4) > upper and c(5) > upper.
Node 2 is the next E-node as c(2) = 0 and c(3) = 5. Nodes 6, 7 and 8 are generated
un
and upper is updated to 9 when node 6 is generated. So, node 7 is killed as c(7) = 10
> upper. Node 8 is infeasible and so killed. The only live nodes now are nodes 3 and
6.
sD
Node 6 is the next E-node as c(6) = 0 < c(3) . Both its children are infeasible.
Node 3 becomes the next E-node. When node 9 is generated, upper is updated to 8
as u(9) = 8. So, node 10 with c(10) = 11 is killed on generation.
al
Node 9 becomes the next E-node. Its only child is infeasible. No live nodes remain.
The search terminates with node 9 representing the minimum-cost answer node.
ri
2 3
The path = 1 3 9 = 5 + 3 = 8
to
By using dynamic programming algorithm we can solve the problem with time
complexity of O(n22n) for worst case. This can be solved by branch and bound
technique using efficient bounding function. The time complexity of traveling sale
person problem using LC branch and bound is O(n 22n) which shows that there is no
change or reduction of complexity than previous method.
We start at a particular node and visit all nodes exactly once and come back to initial
node with minimum cost.
Let G = (V, E) is a connected graph. Let C(i, J) be the cost of edge <i, j>. cij = if
<i, j> E and let |V| = n, the number of vertices. Every tour starts at vertex 1 and
ends at the same vertex. So, the solution space is given by S = {1, , 1 | is a
182
permutation of (2, 3, . . . , n)} and |S| = (n – 1)!. The size of S can be reduced by
restricting S so that (1, i1, i2, . . . . in-1, 1) S iff <ij, ij+1> E, 0 < j < n - 1 and
i0 = in =1.
1. Reduce the given cost matrix. A matrix is reduced if every row and column is
reduced. A row (column) is said to be reduced if it contain at least one zero
and all-remaining entries are non-negative. This can be done as follows:
a) Row reduction: Take the minimum element from first row, subtract it
from all elements of first row, next take minimum element from the
m
second row and subtract it from second row. Similarly apply the same
procedure for all rows.
b) Find the sum of elements, which were subtracted from rows.
co
c) Apply column reductions for the matrix obtained after row reduction.
a.
element from the second column and subtract it from second column.
Similarly apply the same procedure for all columns.
d)
e) iy
Find the sum of elements, which were subtracted from columns.
Obtain the cumulative sum of row wise reduction and column wise
un
reduction.
2. Calculate the reduced cost matrix for every node R. Let A is the reduced cost
al
matrix for node R. Let S be a child of R such that the tree edge (R, S)
corresponds to including edge <i, j> in the tour. If S is not a leaf node, then
the reduced cost matrix for S may be obtained as follows:
ri
b) Set A (j, 1) to .
to
c) Reduce all rows and columns in the resulting matrix except for rows
and column containing only . Let r is the total amount subtracted to
Tu
183
Example:
Find the LC branch and bound solution for the traveling sale person problem whose
cost matrix is as follows:
20 30 10 11
15 16 4 2
The cost matrix is 3 5 2 4
19 6 18 3
16
16 4 7
m
Step 1: Find the reduced cost matrix.
co
Apply row reduction method:
a.
Deduct 2 (which is the minimum) from all values in the 2nd row.
Deduct 2 (which is the minimum) from all values in the 3rd row.
Deduct 3 (which is the minimum) from all values in the 4th row.
Deduct
iy
4 (which is the minimum) from all values in the 5th row.
13
10 20 0 1
un
14 2 0
The resulting row wise reduced cost matrix = 1 3 0 0
3 15 0
16
sD
12 0 3 12
Row wise reduction sum = 10 + 2 + 2 + 3 + 4 = 21
al
Deduct 1 (which is the minimum) from all values in the 1st column.
ri
Deduct 3 (which is the minimum) from all values in the 3rd column.
10 17 0 1
to
12
11 2 0
The resulting column wise reduced cost matrix (A) = 0 3 0 2
Tu
3 12 0
15
11 0 0 12
Column wise reduction sum = 1 + 0 + 3 + 0 + 0 = 4
Cumulative reduced sum = row wise reduction + column wise reduction sum.
= 21 + 4 = 25.
This is the cost of a root i.e., node 1, because this is the initially reduced cost matrix.
184
Starting from node 1, we can next visit 2, 3, 4 and 5 vertices. So, consider to explore
the paths (1, 2), (1, 3), (1, 4) and (1, 5).
i=2 i = 4 i=5
i=3
2 3 4 5
m
Variable „i‟ indicates the next node to visit.
Step 2:
co
Consider the path (1, 2):
Change all entries of row 1 and column 2 of A to and also set A(2, 1) to .
a.
11 2 0
0
15 iy
0
12
2
0
un
11 0 12
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
sD
11 2 0
Then the resultant matrix is 0 0 2
al
12 0
15
11 0 12
ri
Row reduction sum = 0 + 0 + 0 + 0 = 0
to
Therefore, as
c S = 25 + 10 + 0 = 35
Change all entries of row 1 and column 3 of A to and also set A(3, 1) to .
185
12 2 0
3 0 2
0
15 3
11 0 12
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
m
1 2 0
Then the resultant matrix is 3 0 2
co
3 0
4
0 0 12
a.
Row reduction sum = 0
Column reduction sum = 11
Cumulative reduction (r) = 0 + 11 = 11
Therefore, as
iy
c S cR A 1, 3 r
c S = 25 + 17 + 11 = 53
un
Consider the path (1, 4):
sD
Change all entries of row 1 and column 4 of A to and also set A(4, 1) to .
12
11 0
al
0 3 2
3 12 0
ri
11 0 0
to
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
Tu
12
11 0
Then the resultant matrix is 0 3 2
3 12 0
11 0 0
Row reduction sum = 0
Column reduction sum = 0
Cumulative reduction (r) = 0 + 0 = 0
186
Therefore, as c S cR A 1, 4 r
c S = 25 + 0 + 0 = 25
Change all entries of row 1 and column 5 of A to and also set A(5, 1) to .
12
11 2
m
0 3 0
12
15 3
co
0 0 12
Apply row and column reduction for the rows and columns whose rows and
a.
columns are not completely .
10
Then the resultant matrix is 0
iy
9 0
3 0
un
12 0 9
0 0 12
Row reduction sum = 5
sD
Therefore, as c S cR A 1, 5 r
al
c S = 25 + 1 + 5 = 31
ri
i=2 i = 4 i=5
i=3
35 2 53 3 25 4 31 5
Tu
i = 2 i=5
i=3
6 7 8
The cost of the paths between (1, 2) = 35, (1, 3) = 53, (1, 4) = 25 and (1, 5) = 31.
The cost of the path between (1, 4) is minimum. Hence the matrix obtained for path
(1, 4) is considered as reduced cost matrix.
187
12
11 0
A = 0 3 2
3 12 0
11 0 0
The new possible paths are (4, 2), (4, 3) and (4, 5).
m
Change all entries of row 4 and column 2 of A to and also set A(2, 1) to .
co
11 0
0 2
a.
11 0
iy
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
un
11 0
Then the resultant matrix is 0 2
sD
11 0
al
Therefore, as c S cR A 4, 2 r
to
c S = 25 + 3 + 0 = 28
Tu
Change all entries of row 4 and column 3 of A to and also set A(3, 1) to .
12 0
3 2
11 0
188
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
1 0
Then the resultant matrix is 1 0
0 0
Row reduction sum = 2
m
Column reduction sum = 11
Cumulative reduction (r) = 2 + 11 = 13
co
Therefore, as c S cR A 4, 3 r
c S = 25 + 12 + 13 = 50
a.
Consider the path (4, 5):
iy
Change all entries of row 4 and column 5 of A to and also set A(5, 1) to .
un
12
11
0 3
sD
0 0
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
al
1 0
ri
Then the resultant matrix is 0 3
to
0 0
Tu
Therefore, as c S cR A 4, 5 r
c S = 25 + 0 + 11 = 36
189
i=2 i = 4 i=5
i=3
35 2 53 3 25 4 31 5
i = 2 i=5
i=3
m
28 6 7 8
36
50
i=3
i=5
co
9 10
The cost of the paths between (4, 2) = 28, (4, 3) = 50 and (4, 5) = 36. The cost of
a.
the path between (4, 2) is minimum. Hence the matrix obtained for path (4, 2) is
considered as reduced cost matrix.
iy
11 0
un
A = 0 2
11 0
sD
The new possible paths are (2, 3) and (2, 5).
Change all entries of row 2 and column 3 of A to and also set A(3, 1) to .
ri
to
2
Tu
11
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
190
Then the resultant matrix is 0
0
Row reduction sum = 2
Column reduction sum = 11
Cumulative reduction (r) = 2 + 11 = 13
m
Therefore, as c S cR A 2, 3 r
c S = 28 + 11 + 13 = 52
co
Consider the path (2, 5):
a.
Change all entries of row 2 and column 5 of A to and also set A(5, 1) to .
0
iy
un
0
Apply row and column reduction for the rows and columns whose rows and
sD
0
ri
Therefore, as c S cR A 2, 5 r
Tu
c S = 28 + 0 + 0 = 28
The tree organization up to this point is as follows:
191
U =
1 L = 25
i=2 i = 4 i=5
i=3
35 2 53 3 25 4 31 5
i = 2 i=5
i=3
28 6 7 8
36
50
i=3
i=5
52 9 10 28
m
i=3
11
co
The cost of the paths between (2, 3) = 52 and (2, 5) = 28. The cost of the path
between (2, 5) is minimum. Hence the matrix obtained for path (2, 5) is considered
a.
as reduced cost matrix.
A = 0
iy
un
0
The new possible paths is (5, 3).
sD
Change all entries of row 5 and column 3 of A to and also set A(3, 1) to .
Apply row and column reduction for the rows and columns whose rows and
columns are not completely .
ri
to
Row reduction sum = 0
Column reduction sum = 0
Cumulative reduction (r) = 0 + 0 = 0
Therefore, as c S cR A 5, 3 r
c S = 28 + 0 + 0 = 28
192
U =
1 L = 25
i=2 i = 4 i=5
i=3
35 2 53 3 25 4 31 5
i = 2 i=5
i=3
28 6 7 8
36
50
i=3
i=5
52 9 10 28
m
i=3
11 28
co
The path of traveling sale person problem is:
1 4 2 5 3 1
a.
The minimum cost of the path is: 10 + 6 +2+ 7 + 3 = 28.
0/1 knapsack problem can be solved by using branch and bound technique. In this
sD
problem we will calculate lower bound and upper bound for each node.
Profit = P1 + P2 + P3 = 10 + 10 + 12
So, Upper bound = 32
to
To calculate lower bound we can place w4 in knapsack since fractions are allowed in
calculation of lower bound.
3
Lower bound = 10 + 10 + 12 + ( X 18) = 32 + 6 = 38
Tu
9
Knapsack problem is maximization problem but branch and bound technique is
applicable for only minimization problems. In order to convert maximization problem
into minimization problem we have to take negative sign for upper bound and lower
bound.
We choose the path, which has minimum difference of upper bound and lower bound.
If the difference is equal then we choose the path by comparing upper bounds and
we discard node with maximum upper bound.
193
U = -32
1 L = -38
x1 = 1 x1 = 0
U = -32 U = -22
2 3
L = -38 L = -32
Now we will calculate upper bound and lower bound for nodes 2, 3.
For node 2, x1= 1, means we should place first item in the knapsack.
m
3
L = 10 + 10 + 12 + x 18 = 32 + 6 = 38, make it as -38
9
co
For node 3, x1 = 0, means we should not place first item in the knapsack.
a.
5
L = 10 + 12 + x 18 = 10 + 12 + 10 = 32, make it as -32
9
iy
Next, we will calculate difference of upper bound and lower bound for nodes 2, 3
un
For node 2, U – L = -32 + 38 = 6
For node 3, U – L = -22 + 32 = 10
U = - 32
1 L = - 38
x1 = 1 x1 = 0
U = - 32 U = -22
2 3
al
L = - 38 L = - 32
x2 = 1 x2 = 0
U = -32 U = - 22
ri
4 5
L = - 38 L = -36
Now we will calculate lower bound and upper bound of node 4 and 5. Calculate
to
194
U = -32
1 L = -38
x1 = 1 x1 = 0
U = -32 U = -22
2 3
L = -38 L = -32
x2 = 1 x2 = 0
U = -32 U = -22
4 5
L = -38 L = -36
x3 = 1 x3 = 0
m
U = -32 U = -38
6 7
L = -38 L = -38
Now we will calculate lower bound and upper bound of node 8 and 9. Calculate
co
difference of lower and upper bound of nodes 8 and 9.
a.
Choose node 7, since it is minimum difference value of 0.
x1 = 1
1
U = -32
L = -38
x1 = 0iy
un
U = -32 U = -22
2 3
L = -38 L = -32
x2 = 1 x2 = 0
U = -32 U = -22
sD
4 5
L = -38 L = -36
x3 = 1 x3 = 0
U = -32 U = -38
6 7
L = -38 L = -38
al
x4 = 1 x4 = 0
ri
U = -38 U = -20
8 9
L = -38 L = -20
to
Now we will calculate lower bound and upper bound of node 4 and 5. Calculate
difference of lower and upper bound of nodes 4 and 5.
Here the difference is same, so compare upper bounds of nodes 8 and 9. Discard the
node, which has maximum upper bound. Choose node 8, discard node 9 since, it has
maximum upper bound.
X1 = 1
X2 = 1
X3 = 0
195
X4 = 1
The solution for 0/1 Knapsack problem is (x1, x2, x3, x4) = (1, 1, 0, 1)
Pi x i = 10 x 1 + 10 x 1 + 12 x 0 + 18 x 1
= 10 + 10 + 18 = 38.
m
co
a.
iy
un
sD
al
ri
to
Tu
196