0% found this document useful (0 votes)
12 views14 pages

Brute Force Algorithms and Heaps

Chapter 3 discusses the Transform and Conquer technique for problem-solving, emphasizing the breakdown of complex problems into smaller subproblems. It also covers the Heap data structure and the Heap Sort algorithm, detailing the construction, deletion, and sorting processes involved. Additionally, the chapter addresses space-time tradeoffs in algorithms and introduces the input enhancement approach for sorting, specifically through comparison counting.

Uploaded by

Asha K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Brute Force Algorithms and Heaps

Chapter 3 discusses the Transform and Conquer technique for problem-solving, emphasizing the breakdown of complex problems into smaller subproblems. It also covers the Heap data structure and the Heap Sort algorithm, detailing the construction, deletion, and sorting processes involved. Additionally, the chapter addresses space-time tradeoffs in algorithms and introduces the input enhancement approach for sorting, specifically through comparison counting.

Uploaded by

Asha K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 1- Analysis & Design of Algorithms (BCS401)

CHAPTER 3: BRUTE FORCE APPROACHES


Chapter 3:

Transform and Conquer Technique


The Transform and conquer technique is a way of solving problems by breaking them down into smaller
subproblems, solving the smaller subproblems, and then combining the solutions to the subproblems to
solve the original problem. This technique can be used to solve problems in many different areas,
including mathematics, computer science, and engineering. This technique is often used when the original
problem is too difficult to solve directly, or when it is easier to solve the smaller sub-problems.

Heap and Heap Sort Algorithm


A heap can be defined as a binary tree with keys assigned to its nodes (one key per node) provided
the following two conditions are met:
1. The tree’s shape requirement—The binary tree is essentially complete (or simply complete), that is,
all its levels are full except possibly the last level, where only some rightmost leaves may be missing.
2. The parental dominance requirement—The key at each node is greater than or equal to the keys at
its children. (This condition is considered automatically satisfied for all leaves.)

Here is a list of important properties of heaps


1. There exists exactly one essentially complete binary tree with n nodes. Its height is equal to [log2 n] .
2. The root of a heap always contains its largest element.
3. A node of a heap considered with all its descendants is also a heap.
4. A heap can be implemented as an array by recording its elements in the topdown, left-to-right fashion. It
is convenient to store the heap’s elements in positions 1 though n of such an array, leaving H[0] either
unused or putting there a sentinel whose value is greater than every element in the heap. In such a
representation,

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

a. the parental node keys will be in the first [n/2] positions of the array, while the leaf keys will
occupy the last [n/2] positions.
b. the children of a key in the array’s parental position i (1≤ i ≤ [n/2]) will be in positions 2i and 2i
+ 1, and, correspondingly, the parent of a key in position i (2 ≤ i ≤ n) will be in position [i/2] .

How can we construct a heap for a given list of keys?


There are two principal alternatives for doing that. The first is the so-called bottom-up heap construction
algorithm. It initializes the essentially complete binary tree with n nodes by placing keys in the order given
and then “heapifies” the tree.

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

How can we delete an item from a heap?


We consider the most important case of deleting the root’s key, So deleting the root’s key from a heap can
be done with the following algorithm (illustrated in Figure 6.13).
Step 1 Exchange the root’s key with the last key K of the heap.
Step 2 Decrease the heap’s size by 1.
Step 3 “Heapify” the smaller tree by sifting K down the tree exactly in the same way we did it in the bottom-
up heap construction [Link] is, verify the parental dominance for K: if it holds, we are done; if not,
swap K with the larger of its children and repeat this operation until the parental dominance condition holds
for K in its new position.

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Heap sort Algorithm


Q. Write the Heap sort Algorithm
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to
the selection sort where we first find the minimum element and place the minimum element at the
beginning. Repeat the same process for the remaining elements.
This is a two-stage algorithm that works as follows.
Stage 1 (heap construction): Construct a heap for a given array.
Stage 2 (maximum deletions): Apply the root-deletion operation n − 1 times to the remaining heap.
First convert the array into heap data structure using heapify, then one by one delete the root node of the
Max-heap and replace it with the last node in the heap and then heapify the root of the heap. Repeat this
process until size of heap is greater than 1.
 Build a heap from the given input array.
 Repeat the following steps until the heap contains only one element:
 Swap the root element of the heap (which is the largest element) with the last element of
the heap.
 Remove the last element of the heap (which is now in the correct position).
 Heapify the remaining elements of the heap.
 The sorted array is obtained by reversing the order of the elements in the input array.

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Problems on heap sort

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

SPACE-TIME TRADEOFFS:
A tradeoff is a situation where one thing increases and another thing decreases. It is a way to solve a
problem in:
 Either in less time and by using more space, or
 In very little space by spending a long amount of time.
The best Algorithm is that which helps to solve a problem that requires less space in memory and also
takes less time to generate the output. But in general, it is not always possible to achieve both of these
conditions at the same time. The most common condition is an algorithm using a lookup table. This means
that the answers to some questions for every possible value can be written down. One way of solving this
problem is to write down the entire lookup table, which will let you find answers very quickly but will
use a lot of space. Another way is to calculate the answers without writing down anything, which uses
very little space, but might take a long time. Therefore, the more time-efficient algorithms you have, that
would be less space-efficient.

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

Input enhancement Approach


The idea of this approach is to preprocess the problem's input, in whole or in part, and store the additional
information obtained to accelerate solving the problem afterward. Following algorithms are based on this
approach.

 Counting methods for sorting (Section 7.1)

 Boyer-Moore algorithm for string matching and its simplified version suggested by Horspool

Sorting by Counting:
Q. Write the algorithm for Sorting by counting method
As a first example of applying the input enhancement technique, we discuss its application to the sorting
problem. One idea is to count, for each element of a list to be sorted, the total number of elements smaller
than this element and record the results in a table. These numbers will indicate the positions of the
elements in the sorted list: e.g., if the count is 10 for some element, it should be in the 11th position (with
index 10, if we start counting with 0) in the sorted array. Thus, we will be able to sort the list by simply
copying its elements to their appropriate positions in a new, sorted list. This algorithm is called
comparison counting sort.
Example of sorting by comparison counting

Dept. of AIML, GMIT, Davangere


Module 1- Analysis & Design of Algorithms (BCS401)

The time efficiency of this algorithm


It should be quadratic because the algorithm considers all the different pairs of an n-element array. More
formally, the number of times its basic operation, the comparison A[i] < A[j], is executed is equal to the
sum we have encountered several times already:

Dept. of AIML, GMIT, Davangere

Common questions

Powered by AI

A space-time tradeoff is a situation in algorithm design where improving performance in terms of time efficiency might come at the cost of increased space usage, and vice versa. This tradeoff is generally about finding a balance between the two constraints, where using more space can allow for faster computations via precomputed tables or lookup methods, whereas saving space may necessitate more computational time to calculate results as needed. A typical example is using a lookup table to provide quick answers but at the cost of occupying a large amount of space .

Using a lookup table in algorithm optimization allows for drastically increased execution speed because it makes it possible to retrieve pre-computed results instantly, eliminating repetitive calculations during runtime. However, this approach results in higher memory usage, as the results need to be stored in memory. This tradeoff means while an algorithm can perform faster, it requires sufficient storage capacity to benefit from the acceleration. The effectiveness depends on the balance between available memory and the performance requirements .

The bottom-up heap construction algorithm involves initializing a binary tree and adjusting it to satisfy the heap property by iteratively 'heapifying' each node, bottom-up, to ensure parental dominance. On the other hand, deleting the root's key from a heap involves exchanging the root with the last element, reducing the heap's size by one, and then 'heapifying' the new root by sifting it down the tree until the parental dominance condition is satisfied. Both processes ensure that the heap properties are restored after modification but differ in their starting points and directions of heapification—bottom-up for construction and top-down for deletion .

A heap is represented as a binary tree with keys assigned to nodes, following two main properties: the tree must be essentially complete, meaning all levels are full except possibly the last, and every node's key is greater than or equal to its children's keys. In an array representation, elements are stored in a top-down, left-to-right manner from indices 1 to n. The first [n/2] positions hold parental node keys, while the last [n/2] positions hold leaf keys. Children of a node at position i are located at positions 2i and 2i+1, and the parent of a node at position i is positioned at [i/2].

The comparison counting sort algorithm determines positions by counting how many elements in the list are smaller than each element. This inherent need to consider each element with every other element makes its time complexity quadratic, i.e., O(n^2), as all possible pairs of elements must be compared. Hence, while it theoretically ensures correct positioning of all elements, the quadratic nature significantly impacts its efficiency in larger datasets, making it suboptimal compared to more advanced sorting algorithms with better complexity .

The 'transform and conquer' technique involves solving a complex problem by breaking it down into smaller subproblems, solving these subproblems, and then combining their solutions to solve the original problem. This technique is commonly utilized in fields such as mathematics, computer science, and engineering, where direct solutions are challenging, or simplifying the problem can lead to more efficient solutions .

The heap sort algorithm is a comparison-based technique that sorts an array using a binary heap data structure. It involves two main stages: first, the array is converted into a heap using the heapify process, ensuring that the largest element is at the root. Then, the root element (largest) is swapped with the last element of the heap, and the heap size is reduced by one. This is followed by 'heapifying' the root to maintain the heap property. This process of swapping and heapifying is repeated until the heap size is reduced to one, resulting in a sorted array. The array is then output in reverse order to produce the final sorted sequence .

The input enhancement approach improves algorithm efficiency by pre-processing the input data to store additional information that accelerates problem solving later. This method allows for faster execution of critical operations. An example is the comparison counting sort, where pre-counted indices allow for quick placement of elements during sorting. However, the tradeoff is often increased space requirements for storing the pre-processed data, and the approach may not be beneficial if the preprocessing time negates the efficiency gains during the actual algorithm execution .

The transform and conquer approach is preferred when a direct solution to a problem is too complex or inefficient due to the problem's size or constraints. Specifically, this approach is beneficial in scenarios where breaking the problem into manageable subproblems simplifies the overall complexity, such as optimizing algorithms or computing solutions in engineering where localized solutions are more straightforward to obtain and can be effectively combined. It is also chosen in instances where iterative refinement and recombination yield more efficient solutions than tackling the whole problem at once .

The parental dominance requirement dictates that each node's key in a heap must be greater than or equal to its children's keys, influencing every heap operation by maintaining a predictable ordering. This property ensures that the root always holds the maximum value, facilitating efficient removal and replacement of elements, such as in heap sort or root deletion. It influences both the heapification process and the way nodes are repositioned within the structure during Heap operations, maintaining the binary tree’s heap property .

You might also like