Daa Unit-2 (Mid-1)
Daa Unit-2 (Mid-1)
Daa Unit-2 (Mid-1)
5. Return:
o If the target is found, return the index of the target element.
o If the target is not found, return an indicator of failure (e.g., -1).
Pseudo Code:
FUNCTION BinarySearch(array, size, target)
{
low = 0;
high = size-1;
while (low ≤ high) do
{
mid = (low + high) / 2;
if (array[mid] = target)
return mid;
else if (array[mid] < target):
low = mid + 1;
else
high = mid – 1;
}
return -1; // Target not found
}
Time Complexity
Binary Search has a time complexity of O(log n), where n is the number of
elements in the array. This is because the search interval is halved with each
step, leading to a logarithmic reduction in the number of elements to be
considered.
Explanation:
Let T(n) the time used to search n elements. As we need to search only one of
the halves, the Recurrence relation is : T(n) = T(n/2) + c
In the same way:
T(n/2) = T(n/4) + c,
So, T(n) = T(n/4) + 2*c.
Going in this way ...
T(n) = T(n/2^m ) + m*c, and
T(n) = T(n/2^k ) + k*c
T(n)=T(1) + k*c = k*c + 1 = O(log n).
Result: The element -4 is not in the array, so the function would return -1.
Explain the Merge Sort Algorithm? Calculate Time Complexity of it? Draw the
tree of calls of merge sort for the following set 310, 285, 179, 652, 351, 423,
861, 254, 450, 520 using merge sort algorithm and draw the tree of calls of
merge sort.
Do the below task by your own hope u all know drawing the tree
for merge sort (not mentioned in this pdf):
Draw the tree of calls of merge sort for the following set
310, 285, 179, 652, 351, 423, 861, 254, 450, 520
Explain the Quick Sort Algorithm? Calculate Time Complexity of it? Draw the
tree of calls of Quicksort for the following set of elements, (20, 30, 10, 40, 5,
60, 90, 45, 35, 25, 15, 55)
Quick Sort is a divide-and-conquer sorting algorithm that works by selecting a
"pivot" element from the array and partitioning the other elements into two
sub-arrays according to whether they are less than or greater than the pivot.
The sub-arrays are then sorted recursively.
Steps of Quick Sort:
1. Choose a Pivot:
o Select an element from the array to act as the pivot. The choice of
pivot can be random, the first element, the last element, or the
median.
2. Partition:
o Rearrange the elements in the array so that all elements less than
the pivot come before it and all elements greater than the pivot
come after it. The pivot is then in its final position.
3. Recursively Apply:
o Apply the same process recursively to the sub-arrays formed by
dividing the array at the pivot.
Algorithm Pseudo Code:
Algorithm quicksort(array, low, high) {
if low < high {
pivotIndex = partition(array, low, high);
quicksort(array, low, pivotIndex - 1);
quicksort(array, pivotIndex + 1, high);
}
}
Algorithm partition(array, low, high) {
pivot = array[high];
i = low - 1;
for j = low to high - 1 {
if array[j] ≤ pivot {
i = i + 1;
swap(array[i], array[j]);
}
}
swap(array[i + 1], array[high]);
return i + 1;
}
Time Complexity of Quick Sort
• Best Case: O(n log n) when the pivot divides the array into two nearly
equal halves.
• Average Case: O(n log n) on average, when the pivot is reasonably
balanced.
• Worst Case: O(n^2) when the pivot is the smallest or largest element,
leading to unbalanced partitions.
Tree of Calls for Quick Sort
For the Array: {20, 30, 10, 40, 5, 60, 90, 45, 35, 25, 15, 55}
1. Initial Array: {20, 30, 10, 40, 5, 60, 90, 45, 35, 25, 15, 55}
Initial Call:
• Pivot: Choose last element (e.g., 55)
• Partition Result: {20, 30, 10, 40, 5, 60, 90, 45, 35, 25, 15} | 55 (pivot in
correct place)
Left Sub-array: {20, 30, 10, 40, 5, 60, 90, 45, 35, 25, 15}
Recursive Calls for Left Sub-array:
• Pivot: Choose last element (e.g., 15)
• Partition Result: {10, 5, 15, 30, 40, 60, 90, 45, 35, 25, 20} | 15
Left Sub-array of Previous Partition: {10, 5}
• Pivot: 5
• Partition Result: {5, 10} | 5
Right Sub-array of Previous Partition: {30, 40, 60, 90, 45, 35, 25, 20}
• Pivot: 20
• Partition Result: {10, 5, 15, 20, 30, 40, 60, 90, 45, 35, 25} | 20
Left Sub-array of Previous Partition: {30} (Already Sorted)
Right Sub-array of Previous Partition: {40, 60, 90, 45, 35, 25}
• Pivot: 25
• Partition Result: {20, 10, 5, 15, 25, 30, 40, 60, 90, 45, 35} | 25
Left Sub-array of Previous Partition: {40} (Already Sorted)
Right Sub-array of Previous Partition: {60, 90, 45, 35}
• Pivot: 35
• Partition Result: {25, 30, 35, 60, 90, 45} | 35
Left Sub-array of Previous Partition: {30} (Already Sorted)
Right Sub-array of Previous Partition: {60, 90, 45}
• Pivot: 45
• Partition Result: {25, 30, 35, 45, 60, 90} | 45
Left Sub-array of Previous Partition: {60, 90}
• Pivot: 90
• Partition Result: {25, 30, 35, 45, 60, 90} | 90
Final Sorted Array: {5, 10, 15, 20, 25, 30, 35, 40, 45, 60, 90}
Explain about the Process of Tiling the Defective Chess Board with neat
Diagrams.
Conclusion
• Impossibility of Tiling: If a single square is removed from a standard 8x8
chessboard, it is impossible to cover the remaining 63 squares
completely with 31 dominoes due to the color imbalance.
• Generalization: This concept can be extended to larger boards or
different defective patterns, where the balance of covered squares must
be maintained for successful tiling.
Discuss and write the General Control Abstraction for Greedy Technique and
Find the optimal solution of the knapsack instance n = 7, M = 15,
(p1, p2, …p7) = (10, 5, 15, 7, 6, 18, 3) and (w1, w2, ……w7) = (2, 3, 5, 7, 1, 4, 1)
Value-to-Weight Ratio
Item Value (p) Weight (w)
(p/w)
Item 1 10 2 5
Item 2 5 3 1.67
Item 3 15 5 3
Item 4 7 7 1
Item 5 6 1 6
Item 6 18 4 4.5
Item 7 3 1 3
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B.
There are two edges from vertex B that are B to C with weight 10 and edge B to
D with weight 4. Among the edges, the edge BD has the minimum weight. So,
add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the
other edges. In this case, the edges DE and CD are such edges. Add them to
MST and explore the adjacent of C, i.e., E and A. So, select the edge DE and add
it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it
would create a cycle to the graph. So, choose the edge CA and add it to the
MST.
So, the graph produced in step 5 is the minimum spanning tree of the given
graph. The cost of the MST is given below –
Cost of MST = 4 + 2 + 1 + 3 = 10 units.
Discuss about the Minimum Cost Spanning tree? Explain how to find the
minimum cost spanning tree by using Krushkal's algorithm with your own
example.
Example:
As the first step, sort all the edges in the given graph in an ascending order and
store the values in an array.
Edge B→D A→B C→F F→E B→C G→F A→G C→D D→E C→G
Cost 5 6 9 10 11 12 15 17 22 25
The next least cost edge is C → F = 9; add it onto the output graph.
Minimum Cost = 5 + 6 + 9 = 20
Visited array, v = {B, D, A, C, F}
The next edge to be added onto the output graph is F → E = 10.
Minimum Cost = 5 + 6 + 9 + 10 = 30
Visited array, v = {B, D, A, C, F, E}
The next edge from the least cost array is B → C = 11, hence we add it in the
output graph.
Minimum cost = 5 + 6 + 9 + 10 + 11 = 41
Visited array, v = {B, D, A, C, F, E}
The last edge from the least cost array to be added in the output graph is F → G
= 12.
Minimum cost = 5 + 6 + 9 + 10 + 11 + 12 = 53
Visited array, v = {B, D, A, C, F, E, G}
The obtained result is the minimum spanning tree of the given graph
with cost = 6+5+11+9+10+12 = 53.