13
Decrease-and-Conquer Algorithm Desig TOPI Spec At the Cogn 1 2 3 Affec 1 2 3 MAT o o TOPI o o o o TOPI The desig The f 1 2 3 4 5 Design and Ana gn Technique IC TITLE: Decrease-and-Conquer Algorithm Des cific Objectives: e end of the topic session, the students are expecte nitive: 1. Define Decrease-and-Conquer algorithm design 2. Explain how Decrease-and-Conquer algorithm is applied in solving different problem types. 3. Explain how Decrease-and-Conquer algorithm is applied in Insertion sort, Depth-first search, search. ctive: 1. Listen to others with respect. 2. Participate in class discussions actively. 3. Share ideas to the class. TERIALS/EQUIPMENT: o topic slides o OHP IC PREPARATION: o Have the students research on the following: § Decrease-and-Conquer algorithm desig its advantages and drawbacks § Sample applications of Decrea algorithm design technique o Provide sample problems that can be solved by and-Conquer algorithm design technique. o It is imperative for the instructor to incorporate teaching strategies while discussing the sugge instructor may use the suggested learning ac facilitate a thorough and creative discussion of t o Prepare the slides to be presented in the class. IC PRESENTATION: topic will cover the application of Divide-and-Co gn technique following is the suggested flow of discussion for the 1. Identify the topics to be covered. 2. Ask the students to share their research abou Conquer algorithm design technique. 3. Discuss Decrease-and-Conquer algorithm desig explain the three variations involved. 4. Ask the students to cite some algorithms wher Conquer Algorithm Design Technique can be ap 5. Explain how Divide-and-Conquer algorithm des applied to Insertion sort, Depth-fist search, a search traversals. alysis of Algorithm * Property of STI Page 1 of 13 sign Technique ed to: n technique. design technique design technique and Breadth-first gn technique and ase-and-Conquer y using Decrease- e various kinds of ested topics. The ctivities below to the topic. onquer algorithm e course topic: ut Decrease-and- gn technique and re Decrease-and- pplied. sign technique is and Breadth-First

MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Embed Size (px)

DESCRIPTION

MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Citation preview

Page 1: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

TOPIC TITLE: Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:

1.2.

3.

Affective:

1.2.3.

MATERIALS/EQUIPMENT:

oo

TOPIC PREPARATION:

o

o

o

o

TOPIC PRESENTATION: The topic will design technique The following is the

1.2.

3.

4.

5.

Design and Analysis of Algorithm

Algorithm Design Technique

TOPIC TITLE: Decrease-and-Conquer Algorithm Design

Specific Objectives:

At the end of the topic session, the students are expected to:

Cognitive:

1. Define Decrease-and-Conquer algorithm design technique2. Explain how Decrease-and-Conquer algorithm design technique

is applied in solving different problem types. 3. Explain how Decrease-and-Conquer algorithm design technique

is applied in Insertion sort, Depth-first search, and search.

Affective:

1. Listen to others with respect. 2. Participate in class discussions actively. 3. Share ideas to the class.

MATERIALS/EQUIPMENT:

o topic slides o OHP

TOPIC PREPARATION:

o Have the students research on the following: § Decrease-and-Conquer algorithm design technique

its advantages and drawbacks § Sample applications of Decrease

algorithm design technique o Provide sample problems that can be solved by using

and-Conquer algorithm design technique. o It is imperative for the instructor to incorporate various kinds of

teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of th

o Prepare the slides to be presented in the class.

TOPIC PRESENTATION:

The topic will cover the application of Divide-and-Conquerdesign technique

The following is the suggested flow of discussion for the course topic:

1. Identify the topics to be covered. 2. Ask the students to share their research about

Conquer algorithm design technique. 3. Discuss Decrease-and-Conquer algorithm design technique and

explain the three variations involved. 4. Ask the students to cite some algorithms where Decrease

Conquer Algorithm Design Technique can be applied.5. Explain how Divide-and-Conquer algorithm design technique

applied to Insertion sort, Depth-fist search, and Breadthsearch traversals.

Design and Analysis of Algorithm

* Property of STI Page 1 of 13

Design Technique

At the end of the topic session, the students are expected to:

algorithm design technique. Conquer algorithm design technique

algorithm design technique , and Breadth-first

algorithm design technique and

Decrease-and-Conquer

sample problems that can be solved by using Decrease-

It is imperative for the instructor to incorporate various kinds of teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to

e a thorough and creative discussion of the topic.

Conquer algorithm

suggested flow of discussion for the course topic:

Ask the students to share their research about Decrease-and-

algorithm design technique and

algorithms where Decrease-and-can be applied.

Conquer algorithm design technique is fist search, and Breadth-First

Page 2: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer Algorithm Design Technique Page 1 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 1 of 22

Decrease-and-ConquerAlgorithm Design Technique

c Definition of Decrease-and-Conquer algorithm design technique

c Steps in Decrease-and-Conquer algorithm design technique

c Variation of Decrease-and-Conquer algorithm design technique such as:q Decrease by a constant q Decrease by a constant factorq Variable size decrease

c Application of Decrease-and-Conquer algorithm design technique in the following algorithms:q Insertion sortq Depth-first search (DFS)q Breadth-first search (BFS)

Divide These are the topics to be discussed under algorithm design t

ooo

o

[DecreaseWhat is Decrease-and-Conquer Algorithm Design Technique? Page 2 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 2 of 22

What is Decrease-and-Conquer Algorithm Design

Technique?

c Decrease-and-Conquer

q uses some techniques to reduce the problem into a single problem that is smaller than the original

q based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem

What is Decrease-and-Conquer Algorithm Design Technique? Page 3 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 3 of 22

What is Decrease-and-Conquer Algorithm Design

Technique?

c General Divide-and-Conquer Algorithm:

What is Decreaseexcept of smaller size, we use some techniquesingle problem thremoved, It is an algorithm designrelationship between a solution to a solution to a smaller instance of the same problem.relationship is established, it can be exploited either top down (recursively) or bottom up (without a recursion). Steps in Decrease

1.

2.3.

Design and Analysis of Algorithm

Algorithm Design Technique

Divide-and-Conquer Design Technique

These are the topics to be discussed under Decreasealgorithm design technique.

o Definition of Decrease-and-Conquer algorithm designo Steps in Decrease-and-Conquer algorithm design techniqueo Variation of Decrease-and-Conquer algorithm design technique

such as: § Decrease by a constant § Decrease by a constant factor § Variable size decrease

o Application of Decrease-and-Conquer algorithm design

technique in the following algorithms: § Insertion sort • Depth-first search (DFS) • Breadth-first search (BFS)

Decrease-and-Conquer Algorithm Design Technique

What is Decrease-and-Conquer Algorithm Design Technique

ecrease-and-conquer technique is similar to divide and conquer, except that instead of partitioning a problem into multiple subof smaller size, we use some techniques to reduce the single problem that is smaller than the original. Note that removed, not halves [LEV07].

It is an algorithm design technique that is based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem.relationship is established, it can be exploited either top down (recursively) or bottom up (without a recursion).

Steps in Decrease-and-Conquer Algorithm Design Technique:

1. Reduce problem instance to a smaller instance of the same problem.

2. Solve smaller instance 3. Extend solution of smaller instance to obtain solution to original

instance • Can be implemented top-down or bottom• Also referred to as inductive or incremental

Design and Analysis of Algorithm

* Property of STI Page 2 of 13

Decrease-and-Conquer

algorithm design technique algorithm design technique

Conquer algorithm design technique

algorithm design

Algorithm Design Technique, Page 1 of 22]

Algorithm Design Technique?

technique is similar to divide and conquer, partitioning a problem into multiple sub-problems

the problem into a . Note that small bits are

is based on exploiting the given instance of a problem and a

solution to a smaller instance of the same problem. Once such a relationship is established, it can be exploited either top down

Conquer Algorithm Design Technique:

problem instance to a smaller instance of the same

Extend solution of smaller instance to obtain solution to original

down or bottom-up incremental approach

Page 3: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

What is Decrease-and-Conquer Algorithm Design Technique? Page 4 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 4 of 22

What is Decrease-and-Conquer Algorithm Design

Technique?

c Illustration of Decrease-and-Conquer algorithm design technique:

What is Decrease-and-Conquer Algorithm Design Technique? Page 5 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 5 of 22

What is Decrease-and-Conquer Algorithm Design

Technique?

c Variations of decrease-and-conquer q Decrease by a constant q Decrease by a constant factorq Variable size decrease

c Application of the three variations of Decrease-and-conquer algorithm technique:

Decrease-and-conquer algorithm Application

Decrease by a constant (usually decrease by 1)

• insertion sort• graph traversal

algorithms (DFS and BFS)

• topological sorting• algorithms for generating

permutations, subsetsDecrease by a constant factor (usually by half)

• binary search and bisection method

• exponentiation by squaring

Variable-size decrease: • Euclid’s algorithm• selection by partition

The figure below illustrates the technique:

The identify a problem of size complex. The problem will then sub-programhelp in finalizing the actual solution to the original problem. The following are the

The table below illustrates the application of the three variations of Decrease

Decrease by a constant (usually decrease by 1)

Decrease by a constant factor(usually by half) Variable

[What is Pages

Design and Analysis of Algorithm

Algorithm Design Technique

The figure below illustrates the Decrease-and-conquer algorithm design technique:

Figure 8.1 Decrease-and-Conquer algorithm design technique

figure shows that the first phase in Decrease-andidentify a problem of size n where a particular problem may be simple or complex. The problem will then be divided into smaller instance called

program and find its solutions. The solutions for subhelp in finalizing the actual solution to the original problem.

e following are the three major variations of decrease1. Decrease by a constant 2. Decrease by a constant factor 3. Variable size decrease

The table below illustrates the application of the three variations of Decrease-and-conquer algorithm technique:

Decrease-and-conquer algorithm Application

Decrease by a constant (usually decrease by 1)

• insertion sort • graph traversal algorithms

(DFS and BFS)• topological sorting• algorithms for generating

permutations, subsetsDecrease by a constant factor (usually by half)

• binary search and bisection method

• exponentiation by squaringVariable-size decrease: • Euclid’s algorithm

• selection by partition

Table 8.1 Decrease-and-Conquer applications

What is Decrease-and-Conquer Algorithm Design TechniquePages 2-5 of 22]

Design and Analysis of Algorithm

* Property of STI Page 3 of 13

conquer algorithm design

Conquer algorithm design technique

and-Conquer is to where a particular problem may be simple or

divided into smaller instance called and find its solutions. The solutions for sub-problems will

help in finalizing the actual solution to the original problem.

decrease-and-conquer

The table below illustrates the application of the three variations of

Application

graph traversal algorithms (DFS and BFS) topological sorting algorithms for generating permutations, subsets binary search and bisection

exponentiation by squaring Euclid’s algorithm selection by partition

Conquer Algorithm Design Technique?,

Page 4: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-by-a-Constant Page 6 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 6 of 22

Decrease-by-a-Constant

c the size of an instance is reduced by the same constant (1) in iteration of the algorithm

c Illustration of Decrease-by-a-constant:

Decrease-by-a-Constant Page 7 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 7 of 22

Decrease-by-a-Constant

c Example:q suppose, we have an exponentiation problem

of computing positive integer exponents.

c Equation: q an = an-1 x a

c f (n) = an can be computed either “top down”

Decrease

Decreasedesign technique whereconstant to 1.

The figure below illustrates the steps in Decrease (by one)algorithm design technique:

The figure shows that a problemmeans thatproblems before coming up with the actual solution of the original problem.

Suppose, we have aninteger exponents. The relationship between a solution to an instance of size n

So the function recursive definition

Another way ofby multiplying

[Decrease

Design and Analysis of Algorithm

Algorithm Design Technique

Decrease-by-a-constant

Decrease-by-a-constant is a type of Decrease-and-conquer algorithm design technique where the size of an instance is reduced by the same constant in iteration of the algorithm and typically, this constant is equal

The figure below illustrates the steps in Decrease (by one)algorithm design technique:

Figure 8. 2 Decrease–by–One technique

The figure shows that a problem is divided with sub-problems ofmeans that different solutions should be collected from different subproblems before coming up with the actual solution of the original problem.

Suppose, we have an exponentiation problem of computing positive integer exponents. The relationship between a solution to an instance of

n and an instance of size n - 1 is achieved by using the

an = an-1 x a

So the function f (n) = an can be computed either “top down” by using its recursive definition by using the function definition below:

nother way of solving the problem is by using the “bottom up”multiplying a by itself n - 1 times.

[Decrease-by-a-Constant, Pages 6-7 of 22]

Design and Analysis of Algorithm

* Property of STI Page 4 of 13

conquer algorithm the size of an instance is reduced by the same

ypically, this constant is equal

The figure below illustrates the steps in Decrease (by one)-and-Conquer

problems of which from different sub-

problems before coming up with the actual solution of the original

exponentiation problem of computing positive integer exponents. The relationship between a solution to an instance of

using the formula:

can be computed either “top down” by using its by using the function definition below:

“bottom up” approach

Page 5: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-by-a-Constant-Factor Page 8 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 8 of 22

Decrease-by-a-Constant-Factor

c suggests reducing a problem’s instance by the same constant factor (2) in iteration of the algorithm

c Illustration of Decrease-by-a-constant-factor : algorithm:

Decrease-by-a-Constant-Factor Page 9 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 9 of 22

Decrease-by-a-Constant-Factor

c If n is odd, we have to compute an-1 by using the rule for even-valued exponents and then multiply the result by:

Decrease by a Constant Factor Decreaseinstance by the same constant factor typically The figure below illustrates the steps in a Decreasetechnique:

The technique which starts from dividing the problem in two up with the solutions for subsolution of the main problem. Usually, in decrease by constant, the word constant is equal to one that may act diffwe have an the instance of half its size obvious relationship between the two But since we consider instances of the exponentiation problem with integer exponents only, the former works only for even have to compute athen multiply the result by

[Decrease

Design and Analysis of Algorithm

Algorithm Design Technique

Decrease by a Constant Factor

ecrease-by-a-constant-factor technique suggests reducing a problem’s instance by the same constant factor in iteration of the algorithmtypically in most applications, this constant factor is equal to two.

The figure below illustrates the steps in a Decrease-by-technique:

Figure 8.3 Decrease-by-a-constant factor technique

figure shows the steps in a Decrease-by-technique which starts from dividing the problem in two up with the solutions for sub-problems and gathered together for the solution of the main problem.

Usually, in decrease by constant, the word constant is equal to one that may act differently for instances of odd and even number. we have an instance of size n that should be computethe instance of half its size is computed using the formula obvious relationship between the two is: an = (an/2)2.

But since we consider instances of the exponentiation problem with integer exponents only, the former works only for even have to compute an-1 by using the rule for even-valued exponents and then multiply the result by:

[Decrease-by-a-Constant-factor, Pages 8-9 of 22]

Design and Analysis of Algorithm

* Property of STI Page 5 of 13

suggests reducing a problem’s iteration of the algorithm and

is equal to two.

-a-constant factor

constant factor technique

-a-constant-factor technique which starts from dividing the problem in two before coming

problems and gathered together for the

Usually, in decrease by constant, the word constant is equal to one that even number. For example,

computed as an, where is computed using the formula an/2. The

But since we consider instances of the exponentiation problem with integer exponents only, the former works only for even n. If n is odd, we

valued exponents and

Page 6: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Variable Size Decrease Page10 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 10 of 22

Variable Size Decrease

c a size reduction pattern varies in iteration of the algorithm to another

c Example: q Computing the greatest common divisor

gcd(m,n) = gcd(n,m mod n)

Variable Size Decrease In vreduction pattern varies One variablethe greatest common divisor

The right part is always smaller than those on the left partare smaller neither by a constant nor by a constant factor. [Variable

Decrease-and-Conquer in Insertion Sort Page 11 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 11 of 22

Decrease-and-Conquer in Insertion sort

c Insertion sortq the list is divided into sorted and unsorted

partsq in each pass, the first element of the unsorted

list is transferred to the sorted list by inserting it at the proper place

c Pseudocode:

1 For j = 2 to length [A] do 2 key = A[j] 3 {Put A[j] into the sorted sequence A[1 . . j-1] 4 i ? j -1 5 while i > 0 and A[i] > key do 6 A[i+1] = A[i] 7 i = i-1 8 A[i+1] = key

Application of in Insertion sort Insertion Sorttwo partselement of the unsorted list is transferred to the sorted list by inserting it at the proper place. The Insertion sort is comparable to the Bubble sort though the former is much more efficient ssorting algorithm, an element is compared to all the previous elements until a lesser element is found. It means to say that, if an element has a value lesser than all the previous elements, it compares thall the previous elements before proceeding to the next comparison. However, this algorithm is still inefficient compared to other algorithms since it moves elements only one position at a time. The approachoriginal array A[0..n an appropriate position for an element A[n elements and inse Pseudocode Example of Let’s examine a simple application of an Insertion sort, for instance we have an original list below:

Design and Analysis of Algorithm

Algorithm Design Technique

Variable Size Decrease

variable-size-decrease variety of decrease-and-conquer, a size reduction pattern varies in iteration of the algorithm to another.

One of the examples to be considered is to illustrate the application of ariable-size-decrease technique is the Euclid’s algorithm for computing

the greatest common divisor. The algorithm for this formula is

gcd(m,n) = gcd(n,m mod n)

The right part is always smaller than those on the left partare smaller neither by a constant nor by a constant factor.

Variable-Size-Decrease, Page 10 of 22]

Application of Decrease-and-conquer Algorithm Design Technique Insertion sort

Insertion Sort is a type of sorting algorithm where the list is divided into two parts: the sorted and the unsorted parts. In each pass, the first element of the unsorted list is transferred to the sorted list by inserting it at the proper place.

The Insertion sort is comparable to the Bubble sort though the former is much more efficient since the elements comparisons are lesser. In this sorting algorithm, an element is compared to all the previous elements until a lesser element is found. It means to say that, if an element has a value lesser than all the previous elements, it compares thall the previous elements before proceeding to the next comparison. However, this algorithm is still inefficient compared to other algorithms since it moves elements only one position at a time.

The Insertion sort is based on the decrease (by one)approach; provided that a smaller array A[0..n - 2] is already sorted,original array A[0..n - 1] can be sorted. Another consideration is to f

appropriate position for an element A[n - 1] among the sorted n elements and insert it there.

Pseudocode of Insertion Sort algorithm

Example of Insertion sort

Let’s examine a simple application of an Insertion sort, for instance we have an original list below:

1. For j = 2 to length [A] do 2. key = A[j] 3. {Put A[j] into the sorted sequence A[1 . . j-4. i ? j -1 5. while i > 0 and A[i] > key do 6. A[i+1] = A[i] 7. i = i-1 8. A[i+1] = key

Design and Analysis of Algorithm

* Property of STI Page 6 of 13

conquer, a size to another.

to illustrate the application of Euclid’s algorithm for computing

. The algorithm for this formula is:

The right part is always smaller than those on the left part. In fact, they are smaller neither by a constant nor by a constant factor.

Algorithm Design Technique

a type of sorting algorithm where the list is divided into the sorted and the unsorted parts. In each pass, the first

element of the unsorted list is transferred to the sorted list by inserting it

The Insertion sort is comparable to the Bubble sort though the former is ince the elements comparisons are lesser. In this

sorting algorithm, an element is compared to all the previous elements until a lesser element is found. It means to say that, if an element has a value lesser than all the previous elements, it compares the element to all the previous elements before proceeding to the next comparison. However, this algorithm is still inefficient compared to other algorithms

one)-and-conquer 2] is already sorted, the

can be sorted. Another consideration is to find among the sorted n - 1

Let’s examine a simple application of an Insertion sort, for instance we

-1]

Page 7: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer in Insertion Sort Page 12 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 12 of 22

Decrease-and-Conquer in Insertion sort

c Example of Insertion sort :

Decrease-and-Conquer in Insertion Sort Page 13 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 13 of 22

Decrease-and-Conquer in Insertion sort

c Analysis of algorithm for Insertion sort

q overall complexity: O(n2)

q shows better performances to almost sorted arrays

q O(n) for a sorted array

For the first pass sorted list by inserting it at the appropriate place. Original list:

After pass 1

After pass 2:

After pass 3:

After pass 4:

After pass 5:

The figure givenof memory cells.unsorted sections of the list by placing a marker after the first number. sorting with the numbers in the sorted section. If the unsorted number is smaller than its sorted neighbor, the computer will swap them.example, tcompares it with the number to the lefnumbers are required to be swapped and the computer will forward the marker at the next position. Notice that only one comparison was needed to sort the 24.array is sort Analysisapproach

Insertion of the method of insertion. sort shows sort to a sorted array.

Design and Analysis of Algorithm

Algorithm Design Technique

24 79 46 9 33

Figure 8.4 Original list in the Insertion sort example

For the first pass the first element of the unsorted list is transferred to the sorted list by inserting it at the appropriate place.

Original list:

24 79 46 9 33

After pass 1

24 79 46 9 33

After pass 2:

24 46 79 9 33

After pass 3:

9 24 46 79 33

After pass 4:

9 24 33 46 79

After pass 5:

9 24 33 46 57

Figure 8. 5 Sample insertion sort using Decrease-and-conquer algorithm

The figure illustrates the insertion sort algorithm after each pass.given a list of unsorted numbers represented by a one dimensional of memory cells. To begin the sort, the computer divides the sorted and unsorted sections of the list by placing a marker after the first number. sorting numbers, it will recursively compare the first unsorted numbwith the numbers in the sorted section. If the unsorted number is smaller than its sorted neighbor, the computer will swap them.example, the first number in the unsorted section is 24, so the computer compares it with the number to the left. Since 24 is less than 79, these numbers are required to be swapped and the computer will forward the marker at the next position. Notice that only one comparison was needed to sort the 24. The process will continue until the content of the array is sorted.

Analysis of algorithm for Insertion sort using the Decreaseapproach

Insertion sort algorithm’s overall complexity is O(n2) on average, the method of insertion. In arrays which is almost sorted

sort shows better performances up to O(n) in case of applying insertion sort to a sorted array.

Design and Analysis of Algorithm

* Property of STI Page 7 of 13

33 57

Original list in the Insertion sort example

is transferred to the

33 57

33 57

33 57

33 57

79 57

57 79

conquer algorithm

the insertion sort algorithm after each pass. We are represented by a one dimensional array

To begin the sort, the computer divides the sorted and unsorted sections of the list by placing a marker after the first number. In

compare the first unsorted number with the numbers in the sorted section. If the unsorted number is smaller than its sorted neighbor, the computer will swap them. In the given

he first number in the unsorted section is 24, so the computer t. Since 24 is less than 79, these

numbers are required to be swapped and the computer will forward the marker at the next position. Notice that only one comparison was

The process will continue until the content of the

Insertion sort using the Decrease-and-Conquer

) on average, in spite almost sorted, the insertion

O(n) in case of applying insertion

Page 8: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Insertion sort elementspracticealgorithms, like

[Application of in Insertion sort

Decrease-and-Conquer in Graph Traversal Page 14 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 14 of 22

Decrease-and-Conquer in Graph Traversal

c Graph traversalq all vertices in a given graph must be visited

c Example:

q Depth-first search : 1,2,5,6,3,4,7q Breadth-first search : 1,2,3,4,5,6,7

Application of Decreasein Graph traversals Many problems require processing all graph vertices and systematic fashionand-conque Graph traversalmust be visited. In the previous chapters, we have already defined tthe dwe move to the adjacent vertex whileadjacent vertices before going to the next level. Let us examine the following example:

The figure illustrates that in applying graph traversals in the sample graph 1,2,3,4,5,6,7 as the value of BF Depth To better understand the DFS algorithm, let us examine its pseudocode as illustrated below:

Design and Analysis of Algorithm

Algorithm Design Technique

Insertion sort is applied to quite small data sets, usually elements. Hence, a practical performance should be considered. practice, insertion sort does better than most of the quadratic sorting algorithms, like selection sort or bubble sort.

Application of Decrease-and-Conquer Algorithm Design Technique Insertion sort, Pages 11-13 of 22]

Application of Decrease-and-Conquer Algorithm Design Technique in Graph traversals

Many problems require processing all graph vertices and systematic fashion. In this topic, we are going to apply the Decrease

conquer algorithm design technique.

Graph traversal is the application where all the vertices in a given graph st be visited. In the previous chapters, we have already defined tdepth-first search (DFS) simply visits all of the descendents before

we move to the adjacent vertex while breadth-first search (BFS)adjacent vertices before going to the next level. Let us examine the following example:

Figure 8. 6 Sample graph traversal using Decrease-and-conquer algorithm

The figure illustrates that in applying graph traversals in the sample graph we will come up with 1,2,5,6,3,4,7 as the value of DFS and 1,2,3,4,5,6,7 as the value of BFS.

Depth-First Search

To better understand the DFS algorithm, let us examine its pseudocode as illustrated below:

Design and Analysis of Algorithm

* Property of STI Page 8 of 13

, usually from 8 to 12 , a practical performance should be considered. In

most of the quadratic sorting

Algorithm Design Technique

Conquer Algorithm Design Technique

Many problems require processing all graph vertices and edges in In this topic, we are going to apply the Decrease-

is the application where all the vertices in a given graph st be visited. In the previous chapters, we have already defined that

all of the descendents before first search (BFS) visits all

adjacent vertices before going to the next level. Let us examine the

conquer algorithm

The figure illustrates that in applying graph traversals in the sample we will come up with 1,2,5,6,3,4,7 as the value of DFS and

To better understand the DFS algorithm, let us examine its pseudocode

Page 9: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer in Graph Traversal Page 15 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 15 of 22

Decrease-and-Conquer in Graph Traversal

c Pseudo code of Depth-First Search:

Decrease-and-Conquer in Graph Traversal Page 16 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 16 of 22

Decrease-and-Conquer in Graph Traversal

c Example of Depth-First Search:

c Graph traversal stack:

The pseudocode shows the simplicity of the DFS application and the ease which can be performed by hand may create a wrong impression about the level of sophistication of this algorithm.vertex ovisited.but if there are different adjacent vertices, a tie can be resolved arbitrarily. Example of Depth Let us exwith the example graph below:

In graph traversal, every node is processed only once. Since there are many paths leading from one node to graph making sure that you do not process some nodes twice.this, we should mark all nodes in the graph as not yet visited node then pick a starting node. Afterwards, mark it as visited node and place it on the reAfter processing, remove it from to visit list nodes and find all its adjacent nodes that are not yet visited and don’t forget to mark the visited nodes. Finally, repeat the process until thempty.

Design and Analysis of Algorithm

Algorithm Design Technique

The pseudocode shows the simplicity of the DFS application and the ease which can be performed by hand may create a wrong impression about the level of sophistication of this algorithm. It starts from traversing vertex of a graph at a random vertex by marking it as having been visited. The iteration of the algorithm proceeds to the adjacent vertex, but if there are different adjacent vertices, a tie can be resolved arbitrarily.

Example of Depth-First Search

Let us examine a sample DFS traversal for undirected graphwith the example graph below:

Figure 8.7 Sample graph for DFS application

In graph traversal, every node is processed only once. Since there are many paths leading from one node to another, it is hard to traverse a graph making sure that you do not process some nodes twice.this, we should mark all nodes in the graph as not yet visited node then pick a starting node. Afterwards, mark it as visited node and place it on the ready list nodes then pick a node on the ready to visit list nodes. After processing, remove it from to visit list nodes and find all its adjacent nodes that are not yet visited and don’t forget to mark the visited nodes. Finally, repeat the process until the ready list node is empty.

Design and Analysis of Algorithm

* Property of STI Page 9 of 13

The pseudocode shows the simplicity of the DFS application and the ease which can be performed by hand may create a wrong impression

It starts from traversing vertex by marking it as having been

the algorithm proceeds to the adjacent vertex, but if there are different adjacent vertices, a tie can be resolved

amine a sample DFS traversal for undirected graph. Let’s start

In graph traversal, every node is processed only once. Since there are another, it is hard to traverse a

graph making sure that you do not process some nodes twice. To avoid this, we should mark all nodes in the graph as not yet visited node then pick a starting node. Afterwards, mark it as visited node and place it on

ady list nodes then pick a node on the ready to visit list nodes. After processing, remove it from to visit list nodes and find all its adjacent nodes that are not yet visited and don’t forget to mark the

e ready list node is

Page 10: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer in Graph Traversal Page 17 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 17 of 22

Decrease-and-Conquer in Graph Traversal

c Depth-First Search Forest:

It is convenient to use a stack to trace the depthWe push a vertex onto the stack when the vertex is reached for the first time and we pop a vertex off thefigure the graph traversal stack based on the given graph in figure 8.7:

The figure shows the traversal stack where the indicates the order in which vertex was visited like pushing an element to the stack On the other hand, the second subscript indicates the order in which it became a dead DFS forest Another application of DFS is the DFS forest which is consists of solid lines which servethe back edges

The figure illustrates the sample DFS forest which based on the sample graph figure 8.7. The visiting of the vertices starts at the vertex serves as the root of the first tree. Every time areached, the vertex is attached as a child to the vertex from which it is being reached. This edge is referred to as the tree edgehand, visited vertex other than its immediate predecessor. referred to as the other than its parent.

Design and Analysis of Algorithm

Algorithm Design Technique

It is convenient to use a stack to trace the depth-first search algorithm. We push a vertex onto the stack when the vertex is reached for the first time and we pop a vertex off the stack when it becomes a dead end. The figure below illustrates the vertices of the graph that will be used to apply the graph traversal stack based on the given graph in figure 8.7:

Figure 8.8 DFS application using Traversal stack

e figure shows the traversal stack where the first subscript number indicates the order in which vertex was visited like pushing an element to the stack On the other hand, the second subscript indicates the order in which it became a dead-end like popping off an element from the stack.

DFS forest

Another application of DFS is the DFS forest which is consists of solid lines which serve as the tree edges and the dashed lines the back edges.

Figure 8.9 DFS application using DFS forest

The figure illustrates the sample DFS forest which based on the sample graph figure 8.7. The visiting of the vertices starts at the vertex serves as the root of the first tree. Every time a new unvisited vertex is reached, the vertex is attached as a child to the vertex from which it is being reached. This edge is referred to as the tree edgehand, the algorithm may also encounter an edge leading to a previously visited vertex other than its immediate predecessor. referred to as the back edge since it connects a vertex to its ancestor, other than its parent.

Design and Analysis of Algorithm

* Property of STI Page 10 of 13

first search algorithm. We push a vertex onto the stack when the vertex is reached for the first

stack when it becomes a dead end. The below illustrates the vertices of the graph that will be used to apply

the graph traversal stack based on the given graph in figure 8.7:

Figure 8.8 DFS application using Traversal stack

first subscript number indicates the order in which vertex was visited like pushing an element to the stack On the other hand, the second subscript indicates the order in

end like popping off an element from the stack.

Another application of DFS is the DFS forest which is consists of solid as the tree edges and the dashed lines which serve as

The figure illustrates the sample DFS forest which based on the sample graph figure 8.7. The visiting of the vertices starts at the vertex that

new unvisited vertex is reached, the vertex is attached as a child to the vertex from which it is being reached. This edge is referred to as the tree edge. On the other

the algorithm may also encounter an edge leading to a previously visited vertex other than its immediate predecessor. This edge is

ertex to its ancestor,

Page 11: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer in Graph Traversal Page 18 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 18 of 22

Decrease-and-Conquer in Graph Traversal

c Efficiency analysis for DFS algorithm:

q The efficiency of DFS can be implemented with graphs represented as:

• adjacency matrices: T (V2)

• adjacency lists: T (|V|+|E|)

Decrease-and-Conquer in Graph Traversal Page 19 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 19 of 22

Decrease-and-Conquer in Graph Traversal

c Pseudo code of Breadth-First Search:

Efficiency analysis for DFS algorithm The efficiency of DFS can be implemented with graphs represented as adjacency matrices: distinct ordering of vertices, the order in which vertices are first encountered (pushed onto stack) and the order in which vertices become dead Breadth BFS simplast visited vertex. This time, instead of a stack, BFS uses a queue data structure. We can simply say that depthcourageously meaning it goes far from “home” as it cafirst search traverse cautiously. It proceeds in a concentric manner by visiting first all the adjacent vertices to a starting vertex, then all unvisited vertices two edges apart from it, and so on until the vertices in the same connected To better understand the application of BFS we will examine its pseudocode below:

Example of BFS traversal of undirected graph

Design and Analysis of Algorithm

Algorithm Design Technique

Efficiency analysis for DFS algorithm

The efficiency of DFS can be implemented with graphs represented as adjacency matrices: T (V2) and adjacency lists: T (|V|+|E|). It yields two distinct ordering of vertices, the order in which vertices are first encountered (pushed onto stack) and the order in which vertices become dead-ends (popped off stack).

Breadth-first search (BFS)

BFS simply visits graph vertices by moving across to all the neighbors of last visited vertex. This time, instead of a stack, BFS uses a queue data structure. We can simply say that depth-first search traverses courageously meaning it goes far from “home” as it cafirst search traverse cautiously. It proceeds in a concentric manner by visiting first all the adjacent vertices to a starting vertex, then all unvisited vertices two edges apart from it, and so on until the vertices in the same connected component as the starting vertex are visited.

To better understand the application of BFS we will examine its pseudocode below:

Example of BFS traversal of undirected graph

Figure 8.10 Sample graph for BFS application

Design and Analysis of Algorithm

* Property of STI Page 11 of 13

The efficiency of DFS can be implemented with graphs represented as and adjacency lists: T (|V|+|E|). It yields two

distinct ordering of vertices, the order in which vertices are first encountered (pushed onto stack) and the order in which vertices

es by moving across to all the neighbors of last visited vertex. This time, instead of a stack, BFS uses a queue data

first search traverses courageously meaning it goes far from “home” as it can while breadth-first search traverse cautiously. It proceeds in a concentric manner by visiting first all the adjacent vertices to a starting vertex, then all unvisited vertices two edges apart from it, and so on until the vertices in the same

To better understand the application of BFS we will examine its

Page 12: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

Decrease-and-Conquer in Graph Traversal Page 20 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 20 of 22

Decrease-and-Conquer in Graph Traversal

c Example of Breadth- First Search:

c Graph traversal queue:

Decrease-and-Conquer in Graph Traversal Page 21 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 21 of 22

Decrease-and-Conquer in Graph Traversal

c Breadth-First Search Forest:

Decrease-and-Conquer in Graph Traversal Page 22 of 22

Decrease-and-Conquer Algorithm Design Technique

Design and Analysis of Algorithm

* Property of STIPage 22 of 22

Decrease-and-Conquer in Graph Traversal

c Efficiency analysis for BFS algorithm:q The efficiency of BFS can be implemented

with graphs represented as:

• adjacency matrices: T (V2)

• adjacency lists: T (|V|+|E|)

The figure illustrates the vertices of the graph traversal queue as shown below:

In the figure above, the number represents the subscript of the vertices with the application of queue data structuincludes numbers indicating the order in which the vertices were visited like Enqueue or Dequeue in queue algorithm to add or remove a vertex. Just like DFS, BFS also has its corresponding BFSstarts from the first vertex that time a new unvisited vertex is achieved for the first time, the vertex is attached as a child to the vertex it is being reached from with an edge which is referred to as a tree edge. leading to a previously visited verpredecessor is encountered, the edge is The figure below illustrates the BFS forest for the sample graph:

Efficiency analysis for BFS BFS has similar efficiency as DFS and can be implemented with represented as adjacency matrices: T (V2)T (|V|+|E|). It yields single ordering of vertices vertices areBFS that has been on the queue the longest [Decrease

Design and Analysis of Algorithm

Algorithm Design Technique

The figure illustrates the vertices of the graph that will be used to apply a the graph traversal queue as shown below:

Figure 8.11 Sample Traversal queue for BFS application

In the figure above, the number represents the subscript of the vertices h the application of queue data structure. The traversal queue

includes numbers indicating the order in which the vertices were visited like Enqueue or Dequeue in queue algorithm to add or remove a vertex.

Just like DFS, BFS also has its corresponding BFS foreststarts from the first vertex that serves as the root of the time a new unvisited vertex is achieved for the first time, the vertex is attached as a child to the vertex it is being reached from with an edge which is referred to as a tree edge. On the other hand, leading to a previously visited vertex other than its immediate predecessor is encountered, the edge is considered as a cross edge. The figure below illustrates the BFS forest for the sample graph:

Figure 8.12 Sample graph for BFS forest

Efficiency analysis for BFS

BFS has similar efficiency as DFS and can be implemented with represented as adjacency matrices: T (V2) and adjacency lists: T (|V|+|E|). It yields single ordering of vertices wherein the ordervertices are added or removed from queue is the same. Unlike DFS, BFS applies a queue list structure of First in, first out, wherethat has been on the queue the longest is removed first.

[Decrease-and-Conquer in Graph Traversal, Pages 14

Design and Analysis of Algorithm

* Property of STI Page 12 of 13

the graph that will be used to apply a

In the figure above, the number represents the subscript of the vertices re. The traversal queue

includes numbers indicating the order in which the vertices were visited like Enqueue or Dequeue in queue algorithm to add or remove a vertex.

forest. The traversal as the root of the first tree. Every

time a new unvisited vertex is achieved for the first time, the vertex is attached as a child to the vertex it is being reached from with an edge

On the other hand, if an edge tex other than its immediate

as a cross edge. The figure below illustrates the BFS forest for the sample graph:

Figure 8.12 Sample graph for BFS forest

BFS has similar efficiency as DFS and can be implemented with graphs and adjacency lists:

the order in which from queue is the same. Unlike DFS,

wherein the node .

, Pages 14-22 of 22]

Page 13: MELJUN CORTES ALGORITHM Decrease-And-Conquer Algorithm Design Technique I

Decrease-and-Conquer Algorithm Design Technique

GENERALIZATION:

o

o

o

o

o

o

o

o

o

o

REFERENCES:

o

o

o

o

o

o

Design and Analysis of Algorithm

Algorithm Design Technique

GENERALIZATION:

o Decrease-and-conquer technique is similar to divide and conquer, except instead of partitioning a problem into multiple sub-problems of smaller size, we use some technique to reduce our problem into a single problem that is smaller than the original, and in fact we remove small bits not halves.

o The three major variations of decrease-andDecrease by a constant, Decrease by a constant factor and Variable size decrease.

o Decrease-by-a-constant is where the size of an instance is reduced by the same constant in iteration of the algorithm and typically, this constant is equal to 1.A graph data collection of nodes (vertices) and line segments

o Decrease-by-a-constant-factor technique suggests reducing a problem’s instance by the same constant factor in iteration of the algorithm and typically in most applications, this constant factor is equal to two.

o In Variable-size-decrease variety of decreasesize reduction pattern varies in iteration of the algorithm to another.

o Insertion Sort is a type of sorting algorithm where the list is divided into two parts the sorted and the unsorted parts. In each pass, the first element of the unsorted list is transferred to thesorted list by inserting it at the proper places.

o Graph traversal is the application where all the vertices in a given graph must be visited.

o Two types of graph traversal are Depth-first search (DFS) and Breadth-first search (BFS)

o Depth-First Search simply visits all of the descendents before we move to the adjacent vertex.

o Breadth-First Search (BFS) visits all adjacent vertices before going to the next level.

REFERENCES:

o www.cs.unc.edu/Courses/comp790090f08/Lecturenotes/Lecture12.ppt

o www.siit.tu.ac.th/bunyarit/courses/its033/slides/ITS033x06xDecreasexConquer.ppt

o www1.pacific.edu/~mdoherty/comp157/lectures/071001_lecture.ppt

o http://www.roseindia.net/java/beginners/arrayexamples/InsertionSort.shtml

o http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch14.html

o Anany Levitin,(2007), The design and analysis of algorithm (2nd ed.), Pearson Education Inc.

Design and Analysis of Algorithm

* Property of STI Page 13 of 13

conquer technique is similar to divide and instead of partitioning a problem into multiple

problems of smaller size, we use some technique to reduce our problem into a single problem that is smaller than the original, and in fact we remove small bits not halves.

and-conquer are Decrease by a constant, Decrease by a constant factor and

is where the size of an instance is reduced by the same constant in iteration of the algorithm and

ata structure is a s (lines).

technique suggests reducing a onstant factor in iteration of the

algorithm and typically in most applications, this constant factor

variety of decrease-and-conquer, a n pattern varies in iteration of the algorithm to

Insertion Sort is a type of sorting algorithm where the list is divided into two parts the sorted and the unsorted parts. In each pass, the first element of the unsorted list is transferred to the

raph traversal is the application where all the vertices in a

first search (DFS) and

all of the descendents before

First Search (BFS) visits all adjacent vertices before

www.cs.unc.edu/Courses/comp790090f08/Lecturenotes/Lecture

unyarit/courses/its033/slides/ITS033x06xDecr

www1.pacific.edu/~mdoherty/comp157/lectures/071001_lecture.

yexamples/Insertion

state.edu/~gurari/course/cis680/cis680Ch14.html Anany Levitin,(2007), The design and analysis of algorithm (2nd