DAA-Unit II
DAA-Unit II
Unit II
Divide and Conquer: General Method, Binary Search, Merge sort, Quick sort. Basic traversal
and search techniques: Techniques for Binary trees - Techniques for Graphs.
Divide-and-conquer
Divide and conquer is a design strategy which is well known to breaking down 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.
This algorithm consists of two parts:
Divide : Divide the problem into a number of sub problems. The sub problems are solved
recursively.
Conquer : The solution to the original problem is then formed from the solutions to the sub
problems (patching together the answers).
ALGORITHM
Algorithm DAndC(P)
{
if Small(P) return S(P);
else {
g ( n)
T ( n)
T ( n1 ) T ( n2 ) ... T (nk ) f (n)
– T(n) is the time for Divde_Conquer on any input size n.
– g(n) is the time to compute the answer directly (for small inputs)
– f(n) is the time for dividing P and combining the solutions.
1
Applications of Divide and conquer Algorithm:
Binary search,
Quick sort,
Merge sort,
Strassen’s matrix multiplication.
Binary Search (simplest application of divide-and-conquer)
1. This algorithm finds the position of a specified input value (the search "key") within an array
sorted by key value.
2. In each step, the algorithm compares the search key value with the key value of the middle
element of the array.
3. If the keys match, then a matching element has been found and its index, or position, is
returned.
4. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats
its action on the sub-array to the left of the middle element or, if the search key is greater, then
the algorithm repeats on sub array to the right of the middle element.
5. If the search element is less than the minimum position element or greater than the maximum
position element then this algorithm returns not found.
Algorithm BINSRCH (a, n, x)
// determine whether „x‟ is present, and if so, set j such that x = a(j) else return j
{
low :=1 ; high :=n ;
while (low < high) do
{
mid :=|(low + high)/2|
if (x < a [mid]) then high:=mid – 1;
else if (x > a [mid]) then low:= mid + 1
else return mid;
}
return 0; }
2
Conditions for when to apply Binary Search in a Data Structure:
The data structure must be sorted.
Access to any element of the data structure takes constant time.
Example: Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and target = 23
Analysis
3
Time Complexity
Merge Sort
It is one of the most popular and efficient sorting algorithm. It divides the given list into
two equal halves, calls itself for the two halves and then merges the two sorted halves. We have
to define the merge() function to perform the merging.
The sub-lists are divided again and again into halves until the list cannot be divided
further. Then we combine the pair of one element lists into two-element lists, sorting them in the
process. The sorted two-element pairs is merged into the four-element lists, and so on until we
get the sorted list.
Algorithm MergeSort(low, high)
} }
4
Example
Time Complexity
5
Quick sort
It is a fast sorting algorithm used to sort a list of elements. Quick sort algorithm is
invented by C. A. R. Hoare.
The quick sort algorithm attempts to separate the list of elements into two parts and then sort
each part recursively. That means it use divide and conquer strategy. In quick sort, the partition
of the list is performed based on the element called pivot. Here pivot element is one of the
elements in the list.
The list is divided into two partitions such that "all elements to the left of pivot are smaller than
the pivot and all elements to the right of pivot are greater than or equal to the pivot".
6
7
Quick Sort Pseudocode
procedure quickSort(left, right)
if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure
8
Until all nodes of the tree are not visited
Step 1 - Visit the root node
Step 2 - Traverse the left subtree recursively.
Step 3 - Traverse the right subtree recursively.
Algorithm
Until all nodes of the tree are not visited
Step 1 - Traverse the left subtree recursively.
Step 2 - Visit the root node.
Step 3 - Traverse the right subtree recursively.
9
Output of the inorder traversal of the above tree is - D→B→E→A→F→C→G
Postorder Traversal:
The applications of postorder traversal include -
It is used to delete the tree.
It can also be used to get the postfix expression of an expression tree.
Algorithm
Until all nodes of the tree are not visited
Step 1 - Traverse the left subtree recursively.
Step 2 - Traverse the right subtree recursively.
Step 3 - Visit the root node
10
Graph Traversals :
Graph traversal is a technique used for a searching vertex in a graph.
The graph traversal is also used to decide the order of vertices is visited in the search process.
A graph traversal finds the edges to be used in the search process without creating loops.
There are two common ways of traversing a graph:
Breadth-first search uses a queue to keep track of vertices that still need to be visited.
Depth-first search uses a stack (can also implement it recursively).
11
BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal.
******************************************************************************
12