• Tidak ada hasil yang ditemukan

Optimizing Divide and Conquer Based Algorithms

N/A
N/A
Protected

Academic year: 2023

Membagikan "Optimizing Divide and Conquer Based Algorithms"

Copied!
65
0
0

Teks penuh

This paper presents a discussion of divide-and-conquer based techniques for various sorting algorithms with special emphasis on heap sorting. The thesis shows that ternary systems are more promising than the more traditional binary systems used in the divide and conquer approach.

Literature Review

Early Historic Examples

Scopes and Objectives

Advantages

In all these examples, the divide-and-conquer approach led to an improvement in the asymptotic cost of the solution. For example, if the base cases are of constant size, the work of splitting the problem and combining the partial solutions is proportional to the size n of the problem, and at each stage there is a bounded number p of subproblems of size n/p. , then the cost of the divide-and-conquer algorithm is O(n log n).

Implementation Issues

In contrast, the traditional approach to exploiting the cache is blocking, as with loop nesting optimization, where the problem is explicitly divided into appropriately sized chunks, the cache can also be used optimally, but only if the algorithm is tuned on the specific cache. size(s) of a particular machine. In any recursive algorithm there is considerable freedom in the choice of the base cases, the small subproblems that are solved immediately to end the recursion.

Quicksort

Proving Correctness of Partition

Let (A, p, r) be any input to partition and let q be the output of partition on this input.

Running Time Analysis

Mergesort

Algorithm: Merge Sort

If the running time of merge sort for a list of length n is T(n), then the iteration T(n) = 2T(n/2) + n follows from the definition of the algorithm (apply the algorithm to two lists of half the size of the original list and add the n steps taken to merge the two resulting lists). For large n and a randomly ordered input list, the expected (average) number of merge sort comparisons approaches α less than the worst case. In the worst case, merge sort makes about 39% fewer comparisons than quicksort in the average case.

In terms of moves, merge sort's worst case complexity is O(n log n) - the same complexity as quick sort's best case, and merge sort's best case takes about half as many iterations as the worst case. Merge sort is more efficient than quicksort for some types of lists if the data to be sorted can only be efficiently accessed sequentially, and is therefore popular in languages ​​such as Lisp, where sequential access to data structures is very common. Unlike some (efficient) implementations of quicksort, merge sort is a stable sort as long as the merge operation is properly implemented.

The most common implementation of merge sort is not sorted in place; therefore, the size of the input memory must be allocated for the sorted output to be stored (see below for versions that only need n/2 extra space).

Variants

Cache-aware versions of the merge sort algorithm, whose operations have been specifically chosen to minimize the movement of pages in and out of a machine's memory cache, have been proposed. For example, the tile merge sort algorithm stops partitioning subarrays when subarrays of size S are reached, where S is the number of data elements that fit into a CPU's cache. Each of these subarrays is sorted with an in-place sorting algorithm, such as insertion sort, to prevent memory swapping, and normal merge sort is then completed in standard recursive fashion.

Although heap sort has the same time limits as merge sort, it only requires Θ(1) auxiliary space instead of merge sort'sΘ(n), and is often faster in practical implementations. On typical modern architectures, efficient fast sort implementations generally outperform merge sort for sorting RAM-based arrays. On the other hand, merge sort is a stable sort and more efficient in dealing with slow-access sequential media.

Merging and sorting is useful for online sorting, where the list to be sorted is received piece by piece, rather than all at the beginning.

Convex Hull

  • Convexity
  • Convex Hull Problem
  • Running Times of Convex Hull Divide and Conquer Algorithm
  • Advantages of the Divide and Conquer Approach
  • Disadvantages of the Divide and Conquer Approach
  • Algorithm Performances for Convex Hull Problem

Similarly, state that the convex hull CH(P) of a finite set of points P is the smallest convex polygon that contains P. In a convex hull problem, a set of points or coordinates is given and one is asked to come up with the smallest polygon where all points are either inside or on the edge of the polygon. The algorithm is as follows: Hull(S): If —S—≤3, compute the convex hull by brute force in O(1) time and return.

Given n points, arbitrarily distributed one point per processor on a grid of size n, the convex hull of the set S of points can be determined in Θ(n12)time. Quick Hull recursively splits the set of points S and assembles the convex hull H(S) by "merging" the results of subproblems or partial solutions. It requires a lot of memory for storing intermediate results of subconvex hulls to combine to form the full convex hull.

Using D&C is not ideal if the points to be considered are too close together, so other approximations to a convex hull will be ideal. Is complicated when large base cases need to be implemented for performance reasons.

Figure 2.3 shows a convex hull P.
Figure 2.3 shows a convex hull P.

Closest Pair

Divide and Conquer for Closest Pair of Points

The brute force solution is O(n2), calculate the distance between each pair and return the smallest one.

Algorithmic Steps

It can be geometrically proven that for each point on the bar, we need to check at most 7 points behind it (note that the bar is ordered by the Y coordinate). 7) Finally return the minimum of d and the distance calculated in the above step (step 6). Algorithm 4 Nearest Pair Nearest Pair o f (xP,yP). returns the closest points o f xP using the brute−f orce other algorithm. dL,pairL)←closest pair o f(xL,yL) (dR,pairR)←closest pair o f(xR,yR) (dmin,pairMin)←(dR,pairR) ifdL

In the second step, a sorted array is created by iteratively removing the largest element from the array and inserting it into the array. The direction of the sorted elements can be changed by selecting a min or max stack in the first step[3]. The heap invariant is preserved after each extraction, so the only cost is that of the extraction.

Figure 2.6: Closest Pair Across the Partitions
Figure 2.6: Closest Pair Across the Partitions

Heap Property

Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime. It is clear that a heap of height h has the maximum number of elements when its lowest level is completely filled. In this case the heap is a complete binary tree of height h and therefore has 2h+1−1 nodes[1].

We define the height of a node in a tree to be the number of edges on the longest simple descending path from a node to a leaf. The number of edges on a simple downward path from a root to a leaf is the height of a tree.

Figure 3.1: Heapsort Property
Figure 3.1: Heapsort Property

An Example on Heapsort

Theoretical Complexity of Heapsort

Build Heap Analysis

When Heapify is called, the runtime depends on how far an element can shift down before the process terminates. In the worst case, the element can shift all the way down to leaf level. At the bottom level there are 2h nodes, but we don't call Heapify on any of these, so the work is 0.

There are 2h−1 nodes at the next lowest level and each can screen 1 level down. There are 2h−2 nodes on the 3rd level from the bottom and each can screen 2 levels down. In general, there are 2h−jnodes at level j from the bottom, and each can move down j levels.

In our case, we have a bounded sum, but since the infinite series is bounded, we can use this instead as an easy approximation.

Figure 3.2: Heapsort Tree
Figure 3.2: Heapsort Tree

Comparison with Other Sorting Algorithm

Merge sort parallelizes well and can achieve nearly linear speedup with a trivial implementation; heapsort is not an obvious candidate for a parallel algorithm[2]. Merge sort can be modified to work on linked lists with O(1) extra space.[4] Heapsort can be adapted to work on doubly linked lists with only O(1) additional space overhead[27].

Optimality of Ternary Heapsort

With the invention of the computer, the use of two parametric algebra, the number system, the graph, the tree began rapidly. Here we split the given list of data and again join them in order. Binary system is used here when dividing data and when joining them.

In this paper, we show that the ternary system is more promising than the traditional binary system used in heap sorting. We can enter several values ​​into this equation and try to find the optimal number of children to minimize costs. We found that the cost value increases for a larger value of d, except for 3, which is smaller than d=2 and d=4.

This relation clearly indicates that 3 child or ternary heapsort is theoretically more optimal than other variants of heapsort.

Figure 4.1: A Tree with Height h and Branching Factor d
Figure 4.1: A Tree with Height h and Branching Factor d

Algorithms for Varying Child

For three child versions of the algorithm, we must define an additional child named Simultaneously in the heapify phase, each time an additional comparison is required. In this model, the height of the tree is significantly reduced, which helps to reduce costs.

At the same time, in the heapify phase, the number of required comparisons is increased each time.

Experimental Result

  • Number of Move for Heapsort
  • Number of Comparison for Heapsort
  • Time Required for Heapsort
  • Discussion on Results

The fewer comparisons required for a given data set, the more optimal the algorithm will be. But in comparison, the 3-child version of heapsort is always smaller than the 2-child and 4-child versions. Thus, we can remark that the number of comparisons contributes more to the complexity than the number of moves.

This is because the Windows operating system runs a number of programs in the background, which increases the time required to complete the sorting. Most of these algorithms have an average complexity of O(nlogn) instances which do not consider the number of children. To improve the performance or optimality of the large array, we worked with its number of children.

When replaced by value we found that 3 was the optimal value for the number of children. All three algorithms were tested for optimality, taking the number of moves, the number of comparisons and the time as parameters. Therefore, we can say that an optimal heapsort can be done using the three-child variant of heapsort.

Table 4.1: Number of Move Required for Heapsort (Integer Value) value child Test-01 Test-02 Test-03
Table 4.1: Number of Move Required for Heapsort (Integer Value) value child Test-01 Test-02 Test-03

Recommendations for Further Research

Convex and Non-Convex Shapes

Convex Hull P

Mesh Algorithm

Closest Pair in Separate Partition

Closest Pair Across the Partitions

Heapsort Property

Let us assume that a tree consists of one node and that each node has a child and that the total height of the tree is h. Some of the applications using D&C technology can be named as mergesort, quicksort, convex hull, nearest pair and so on.

Heapsort Tree

A Tree with Height h and Branching Factor d

Graph for Number of Move(Integer Value)

Graph for Number of Move(Float Value)

Graph for Number of Comparison(Integer Value)

Graph for Number of Comparison(Float Value)

Time Taken in Windows Environment(Integer Value)

Time Taken in Linux Environment(Integer Value)

Time Taken in Windows Environment(Float Value)

Time Taken in Linux Environment(Float Value)

Algorithms for convex hull problem

Build heap

Sorting

Number of Move Required for Heapsort (Integer Value)

Number of Move Required for Heapsort (Float Value)

Number of Comparison Required for Heapsort (Integer Value)

Number of Comparison Required for Heapsort (Float Value)

Gambar

Figure 2.1: Recursive Mergesort Algorithm Used to Sort an Array of 7 Integer Values
Figure 2.2: Convex and Non-Convex Shapes
Figure 2.3 shows a convex hull P.
Figure 2.4: Mesh Algorithm
+7

Referensi

Dokumen terkait

องค์ประกอบและตัวบ่งชี้การบริหารเชิงบูรณาการของผู้บริหารสถานศึกษาขั้นพื้นฐาน สังกัดส านักงานเขตพื้นที่การศึกษาประถมศึกษาภาคกลาง The Factors and Indicators of Integrated Management of