Unit 7 Decrease and Conquer: Structure
Unit 7 Decrease and Conquer: Structure
Unit 7 Decrease and Conquer: Structure
7.1 Introduction
By now, you should be familiar with the divide and conquer algorithm. This
unit explains the concepts of ‘decrease and conquer’ and the methodology it
uses in various algorithms.
Decrease and conquer is a method by which we find a solution to a given
problem based upon the solution of a number of problems. The principle
idea of decrease and conquer algorithm is to solve a problem by reducing its
instance to a smaller one and then extending the obtained solution to get a
solution to the original instance. Here, the problem instance is reduced by
decreasing its size to a constant. So, the difference between decrease-and-
conquer and divide-and-conquer is that it makes the problem instance
complex than the bubble and selection sorts. It’s often used as the final
stage of more sophisticated sorts, such as quick sort.
There are numerous approaches to sort an unsorted array in insertion sort
like, start sorting the array from:
The left
The right
Any desired place where the marker is placed.
7.3.1 Algorithm
The basic operation of the insertion sort algorithm is the comparison A[j]>v
because the working of the algorithm will be much faster than the computed
results.
Example
Let us consider an example of cricket players lined up in random order to
understand the significance of marker. Each one has a jersey with the
numbering from 1 to 11, and that they are required to stand according to the
numbers. It’s easier to think about the insertion sort if we begin in the middle
of the process, when the team is half sorted.
We also call this marker technique as partial sorting in stages. At this point
there’s an imaginary marker somewhere in the middle of the line. Let us
consider the players to the left of this marker are partially sorted. This
means that they are sorted among themselves; each one has a jersey with
smaller number than the person to his or her left. However, the players
aren’t necessarily in their final positions because they may still need to be
moved when previously unsorted players are inserted between them. The
player where the marker is whom we will call the marked player, and all the
players on his or her right, is as yet unsorted. To have a clear picture let us
consider the figure 7.1 which represents the jersey numbers of the players,
that is, {1, 10, 2, 4, 6, 8, 5, 11, 3, 7, 9}.
Similarly if we can choose to perform insertion sort either from the right or
left end of the array, we will have to position the marker to the last
player/last number.
7.3.2 Best, worst and average cases
The best case input is an array that is already sorted. In this case insertion
sort has a linear running time (i.e., O(n)). Each cycle, the first remaining
element of input is compared with the right-most element of the sorted sub-
section of the array.
The worst case input is an array sorted in reverse order.
In this case every cycle of the inner loop examines the entire sorted sub-
section of the array before inserting the next element. For this case insertion
sort has a quadratic running time (i.e., O(n2)).
The average case is also quadratic, which makes insertion sort unrealistic
for sorting large arrays. However, insertion sort is the fastest algorithm for
sorting arrays containing fewer than ten elements.
7.3.3 Advantages of insertion sort
The following are the advantages of insertion sort:
It is simple to implement.
The method is useful when sorting smaller number of elements.
It is more efficient than other algorithms.
It is a very stable algorithm.
It is easy to understand.
Hope you are clear about insertion sort and its working. Let us move on
to the next section.
The data structure which is used to track the working of Breadth-first search
is a queue. The table 7.2 depicts the working of Breadth-first search. Here,
Breadth-first search is performed level by level.
Table 7.2: Breadth-First Search
Step 1: Here the vertex P would be marked
first, here vertex P is considered to be at the
first level. Our list now has only P.
Activity 1
Draw a Breadth-first search graph traversal for the Depth-first search figure
given.
Directed acyclic graphs are used in many instances. They usually indicate
precedence among events. Topological sorting can be used to arrange
tasks under precedence constraints. Suppose we have a set of tasks to do,
but particular tasks have to be performed before other tasks, we can use
these precedence conditions to form a directed acyclic graph (DAG)., Any
topological sort (also known as a linear extension) describes an order to
perform these tasks such that each task is performed only after all of its
conditions are satisfied.
Example
Let us consider a set of five required assignments that a student has to
complete which are named as S1, S2, S3, S4, and S5. The assignments
should be completed within some time as long as certain conditions are met.
S1 and S2 have no conditions to be met. S3 requires S1 and S2 to be
completed. S4 requires S3 to be completed and S5 needs S3 to be
completed. The student can complete only one assignment in a day. The
order in which the student takes up a course is the main concern here.
Figure 7.2 illustrates the assignments .of the students represented as a
DAG.
If the graph was a DAG, a solution is contained in list L, else, the graph has
at least one cycle and therefore a topological sorting is not possible.
7.6.2 Uniqueness
If a topological sort has the property that every pair of consecutive vertices
in the sorted order is linked by edges, then these edges form a directed
Hamiltonian path A Hamiltonian path, also called a Hamilton path, is a path
between the vertices of a DAG that visits each vertex exactly once. If a
Hamiltonian path is present, the topological sort order is unique. On the
other hand, if a topological sort does not form a Hamiltonian path, the DAG
will have two or more valid topological orderings, in this case it is always
possible to form a second valid ordering by swapping two consecutive
vertices that are not connected by an edge to each other. Therefore, it is
possible to examine in polynomial time whether a unique ordering exists,
and whether a Hamiltonian path exists.
start 1
insert 2 12 21
right to left
efficient algorithm for problems of random size that has been found till date.
Since this problem has applications in operations research, some specific
large-scale examples have been worked out. There are also few methods
available that are suggested by expert mathematicians, but the shortest
possible path which is perfect has yet to be invented
Figure 7.3 shows how travelling salesman problem is solved between four
cities – P, Q, R, and S. The number above the edges indicates the distance
between the cities. Here, distance between two cities is same in each
direction which forms an undirected graph The minimum cost for the cities
travelled would be P->R->S->Q->P= 79.
Hope you are clear about algorithms for generating combinatorial objects.
7.8 Summary
Let us summarize what we have studied in this unit.
Decrease and conquer is a method by which we find a solution to a given
problem based upon the solution of a number of smaller problems. The
principle idea of decrease and conquer algorithm is to solve a problem by
reducing its instance to a smaller one and then extending the obtained
solution to get a solution to the original instance.
Insertion sort is one of the simplest algorithms that is implemented under the
decrease and conquer methodology. In this technique the element is
inserted at the suitable position.
Depth-first search (DFS) is an algorithm for searching a tree, tree structure,
or graph. We begin at the root and then explore along each branch.
Breadth-first search (BFS) is a graph search algorithm that begins at the
root node and explores all the neighbouring nodes. Then for each of those
nearest nodes, it explores their unexplored neighbour nodes, and so on,
until it finds the goal.
A topological sort of a particular graph can be looked upon as a horizontal
line where all directed edges travel from left to right. Thus, topological sort
differs from the usual sorting technique. Topological sorting can be used for
scheduling a sequence of jobs or tasks.
Algorithms for generating combinatorial objects define most important types
of combinatorial objects such as permutations and combinations.
7.9 Glossary
Term Description
Decrease-by-one technique Type of decrease and conquer, which can also be
termed as Decrease by constant.
Combinatorics Mathematical subject which involves solving
problems using figures.
7.11 Answers
Self Assessment Questions
1. Top down or Bottom-up
2. Incremental
3. Solution
4. Two
5. Sorted
6. Left
7. Backtracking
8. Time and space
9. Backtracks
10. Graph
11. Uninformed
12. Goal
13. Directed acyclic graph
14. Vertices
15. Topological
16. Combinatorial
17. Subset
18. Decrease-by-one
Terminal Questions
1. Refer section 7.2 – Concepts of Decrease and Conquer
2. Refer section 7.3.2 – Best, worst and average cases
3. Refer section 7.3.3 – Advantages of insertion sort
4. Refer section 7.4- – Depth-first search
5. Refer section 7.5 – Breadth-first search
6. Refer section 7.7.1 – Generating permutations
References
Puntambekar, A. A. (2008). Design and Analysis of Algorithms, First
edition, Technical publications, Pune.
Anany Levitin (2008). The Design & Analysis of Algorithms, Second
edition, Pearson.
Pandey, Hari Mohan (2008). Design Analysis and Algorithm, First
edition. University science press, New Delhi.
Thomas H. Cormen (2001). Introduction to Algorithms, Second edition.
Mc Graw Hill.
E-References
http://www.cs.sunysb.edu/~algorith/files/topological-sorting.shtml