DAA 3rd Unit Notes
DAA 3rd Unit Notes
Decrease-and-Conquer: Introduction
Decrease or reduce problem instance to smaller instance of the same problem and extend
solution. Conquer the problem by solving smaller instance of the problem.
Decrease and conquer is a technique used to solve problems by reducing the size of the input
data at each step of the solution process. This technique is similar to divide-and-conquer, in
that it breaks down a problem into smaller subproblems, but the difference is that in decrease-
and-conquer, the size of the input data is reduced at each step. The technique is used when it’s
easier to solve a smaller version of the problem, and the solution to the smaller problem can be
used to find the solution to the original problem.
Variations of Decrease and Conquer:
There are three major variations of decrease-and-conquer:
1. Decrease by a constant
2. Decrease by a constant factor
3. Variable size decrease
First Pass:
initially, the first two elements of the array are compared in insertion sort.
12 11 13 5 6
Here, 12 is greater than 11 hence they are not in the ascending order and 12 is not at
its correct position. Thus, swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.
11 12 13 5 6
Second Pass:
Now, move to the next two elements and compare them
11 12 13 5 6
Here, 13 is greater than 12, thus both elements seem to be in ascending order, hence,
no swapping will occur. 12 also stored in a sorted sub-array along with 11
Third Pass:
Now, two elements are present in the sorted sub-array which are 11 and 12
Moving forward to the next two elements which are 13 and 5
11 12 13 5 6
Both 5 and 13 are not present at their correct place so swap them
11 12 5 13 6
After swapping, elements 12 and 5 are not sorted, thus swap again
11 5 12 13 6
Fourth Pass:
Now, the elements which are present in the sorted sub-array are 5, 11 and 12
Moving to the next two elements 13 and 6
5 11 12 13 6
Clearly, they are not sorted, thus perform swap between both
5 11 12 6 13
5 11 6 12
13
5 11
6 12 13
To use the divide and conquer algorithm, recursion is used. Learn about recursion in
different programming languages:
Recursion in Java
Recursion in Python
Recursion in C++
The complexity for the multiplication of two matrices using the naive method is O(n3),
whereas using the divide and conquer approach (i.e. Strassen's matrix multiplication)
is O(n2.8074). This approach also simplifies other problems, such as the Tower of Hanoi.
This approach is suitable for multiprocessing systems.
Binary Search
Merge Sort
Quick Sort
Strassen's Matrix multiplication
Karatsuba Algorithm
Merge Sort Algorithm
Our task is to merge two subarrays A[p..q] and A[q+1..r] to create a sorted array A[p..r] .
4. When we run out of elements in either L or M, pick up the remaining elements and put
in A[p..q]
Merge Sort pseudocode:
These subarrays are further divided into two halves. Now they become array of unit
length that can no longer be divided and array of unit length are always sorted.
Merge Sort: Divide the subarrays into two halves (unit length subarrays here)
These sorted subarrays are merged together, and we get bigger sorted subarrays.
Merge Sort: Merge the unit length subarrys into sorted subarrays
This merging process is continued until the sorted array is built from the smaller
subarrays.
Merge Sort: Merge the sorted subarrys to get the sorted array
The following diagram shows the complete merge sort process for an example array
{38, 27, 43, 10}.
External sorting
E-commerce applications
Quick Sort:
• It works well on large set of data.
• In this method, division is dynamically carried out.
• The three steps of quick sort are as follows:
Step by Step Process
In Quick sort algorithm, partitioning of the list is performed using following steps...
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in the
list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
o Preorder traversal
o Inorder traversal
o Postorder traversal
So, in this article, we will discuss the above-listed techniques of traversing a tree. Now, let's start
discussing the ways of tree traversal.
Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is visited after that
the left subtree is traversed recursively, and finally, right subtree is recursively traversed. As the
root node is traversed before (or pre) the left and right subtree, it is called preorder traversal.
So, in a preorder traversal, each node is visited before both of its subtrees.
Algorithm
Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left subtree of the root
node is traversed, after that recursively traverses the right subtree, and finally, the root node is
traversed. As the root node is traversed after (or post) the left and right subtree, it is called
postorder traversal.
So, in a postorder traversal, each node is visited after both of its subtrees.
Algorithm
Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is visited after
that root node is traversed, and finally, the right subtree is traversed. As the root node is traversed
between the left and right subtree, it is named inorder traversal.
So, in the inorder traversal, each node is visited in between of its subtrees.
The applications of Inorder traversal includes -
Algorithm
We start from A, and following in-order traversal, we move to its left subtree B.B is also
traversed in-order. The process goes on until all the nodes are visited. The output of in-order
traversal of this tree will be −
D→B→E→A→F→C→G
In any binary tree, the minimum number of nodes will be one more than the value
of height. This is the number of nodes required to construct a tree.
Formula:
1. Height = h.
2. The minimum number of nodes = h+1.
3. If h=3, then nodes will be 3+1= 4.
The maximum number of nodes that can be inserted in any binary tree of height h will be
equal to 2h- 1.
Formula:
1. Height = h.
2. Maximum nodes to inserted = 2h- 1.
3. If h= 3, then 23- 1.
4. 8 -1= 7.
Therefore, the maximum number of nodes to be inserted for the height of h=3 will be 7.
The number of leaf nodes in a binary tree is equal to the nodes with degree two, plus one.
Say a binary tree has two children. Then the total number of leaf nodes of that binary tree
will be one greater than the nodes having two children.
Example:
Here, the total number of leaf nodes (no child or a successor) is 3, and the leaf nodes are
E, F, and G.
Whereas the nodes with two children are 2. Node A and B have 2 children. Hence, this
proves the property.
ACBDEGF
4. The maximum number of nodes at any level:
In a binary tree, the maximum number of nodes gained by any level is equal to the power
of 2 for that level.
In a simpler words, the level is donated by n. Then, maximum nodes of that binary tree
will be 2n.
Example:
n = 2 then 22= 4.
ABCDEFG
Binary tree with maximum number of nodes at any level
5. Minimum possible height or levels is equal to Log 2(N+1):
This property says that the minimum number of levels or a height of a binary tree is the
Log2 of (N+1). Here N represents the maximum number of nodes possessed by a binary
tree at the height h.
We have already discussed property 2 along with the example. Let's use that answer to
calculate the height h.
1. N = 7 (Using Property#2)
2. Log2 of (N+1) = Log2 of (7+1)
3. Log2(8) = 3