Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Introduction To Algorithm

introduction to algorithms

Uploaded by

preciousteyim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Introduction To Algorithm

introduction to algorithms

Uploaded by

preciousteyim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

INTRODUCTION TO ALGORITHM

CHAPTER I: ALGORITHM, FLOWCHART AND


PSEUDOCODE

Learning objectives:
After studying this lesson, student should be able to:
• Discuss what an algorithm is and how to use it to represent the solution of a problem.
• Use Flowchart and Pseudocode represent algorithms

I. ALGORITHM

I.1 Definitions:
• Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite
time.
• It is a step-by-step problem-solving procedure, especially an established, recursive computational
procedure for solving a problem in a finite number of steps.
The specification of an algorithm must be “precise”. To write a precise algorithm, we need to pay attention
to three things:
o Capability. Make sure that the computer knows what and how to do the operations.
o Language. Ensure that the description is unambiguous—that is, it can only be read and
understood one way.
o Context. Make few assumptions about the input and execution setting. I.2 Properties of
algorithms:
To write algorithms that are specified well enough for a computer to follow, make sure every algorithm
has five essential properties:
❖ Inputs specified
❖ Outputs specified
❖ Definiteness or unambiguous
❖ Effectiveness
❖ Finiteness

1. Input specified:
An algorithm accepts zero or more inputs. The inputs are the data that will be transformed during the
computation to produce the output. We must specify the type of the data, the amount of data, and the
form that the data will take.
• Suppose the algorithm is a recipe. We must list the ingredients (type of inputs), their quantities
(amount of input), and their preparation, if any (form of inputs), as in “1/4 cup onion, minced.”
2. Outputs specified:
The outputs are the data resulting from the computation, the intended result. It produces at least one output.
3. Definiteness:
Algorithms must specify every step. Definiteness means specifying the sequence of operations for
transforming the inputs into the outputs. Every detail of each step must be spelled out, including how to
handle errors. Definiteness ensures that if the algorithm is performed at different times or by different

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 1


INTRODUCTION TO ALGORITHM
agents (people or computers) using the same data, the output will be the same. There must be no ambiguity
in any instruction. This means that the action specified by the step cannot be interpreted in multiple ways
& can be performed without any confusion.
4. Effectiveness:
Whereas definiteness specifies which operations to do, effectiveness means that they are doable.
Examples of ineffectiveness: “Enter the amount of income you would have received this year had you
worked harder”, “Print the length of the longest run of 9s in the decimal expansion of π”

5. Finiteness:

An algorithm must have finiteness; that is the algorithm have to stop after a finite number of instructions
are executed. It must eventually stop, either with the right output or with a statement that no solution is
possible.
If no answer comes back, we can’t tell whether the algorithm is still working on an answer or is just plain
“stuck”. Finiteness becomes an issue for computer algorithms because if the algorithm doesn’t specify
when to stop the repetition, the computer will continue to repeat the instructions forever. For example,
divide 3 into 10. Any process having these five properties will be called an algorithm.

I.3 Example of algorithm

We use algorithms in our daily life. For example, to wash hand, the following algorithms may be used.

1. Start 1. Start
2. Turn on water 2. Turn on water
3. dispense soap 3. dispense soap
4. wash hand till clean 4. Repeat Rub hands together
5. Rince soap off 5. until hand clean
6. Turn off water 6. Rince soap off
7. Dry hand 7. Turn off water
8. Stop 8. Dry hand
9. Stop

The above-mentioned algorithm terminates after six steps. This explains the feature of finiteness. Every
action of the algorithm is precisely defined; hence, there is no scope for ambiguity.

II. ALGORITHM REPRESENTATION


Algorithms can be represented in different ways. The different tools used to represent algorithms are:
❖ Flowchart
❖ Pseudo-code
I.1. Flowchart
Flowchart is the pictorial (diagrammatic) representation of an algorithm with the help of symbols carrying
certain meaning. Using flowchart, we can easily understand a program.

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 2


INTRODUCTION TO ALGORITHM
a) Flowchart Symbols
Flowcharts use simple geometric symbols and arrows to define relationships. Some standard symbols that
are frequently required for flowcharts are shown

Symbol Symbol name Description


Flow lines are used to connect symbols. These lines
Flow Lines indicate the sequence of steps and the direction of
flow.
Terminal The terminator symbol represents the start points,
end points, and potential outcomes of a path.
used to illustrate a process, action or an operation.
Processing Examples: "Add 1 to X"; "save changes" or
similar.
These typically contain a Yes/No question or
Decision True/False test.

It represents data that is available for input or


Input/output output. It may also represent resources used or
generated. Ex: Get X from the user; display X.
Connector Connector symbol is used to join different flow lines.

Off- page This symbol is used to indicate the flowchart


Connecter continues on the next page

Document Document is used to represent a paper document


produced during the flowchart process.

Manual input symbol represents input to be given by


Manual Input
a developer / programmer.
Magnetic Disk This is used to represent data input or output from
and to a magnetic disk.

b) Advantages of flowchart

The flowchart shows how the program works before you begin actually coding it. Some advantages of
flowcharting are the following.

- Communication: Flowcharts are helpful in explaining the program to other people


- Effective analysis: With the help of flowchart, the problem can be analyzed more effectively

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 3


INTRODUCTION TO ALGORITHM
- Proper documentation: Program flowchart serves as a good program documentation, which
is needed for various purposes.
- Efficient coding: Once the flowchart is drawn, it becomes easy to write the program in any
high level language
- Proper debugging: The flowchart helps in the debugging process
- Efficient program maintenance: The maintenance of operating program become easy with
the help of flowchart

c) Limitations of Flowcharts

Flowchart can be used for designing the basic concept of the program in pictorial form, but cannot be
used for programming purposes. Some of the limitations of the flowchart are given below:

• Complex: The major disadvantage in using flowcharts is that when a program is very large, the
flowcharts may continue for many pages, making them hard to follow.
• Costly: If the flowchart is to be drawn for a huge program, the time and cost factor of program
development may get out of proportion, making it a costly affair.

• Difficult to Modify: Due to its symbolic nature, any change or modification to a flowchart
usually requires redrawing the entire logic again, and redrawing a complex flowchart is not a
simple task.
• No Update: Usually, programs are updated regularly. However, the corresponding update of
flowcharts may not take place, especially in the case of large programs

I.2 Pseudocode

It is an English-like representation of the code required for an algorithm. It is part English and part
structured code.
Pseudocode is a detailed yet readable description of what an algorithm must do, expressed in a
formally-styled natural language rather than in a programming language. It describes the entire
logic of the algorithm so that implementation becomes a rote mechanical task of translating line by
line into source code.

a) Pseudocode Structures

Before going ahead with pseudocode, let us discuss some keywords, which are often used to indicate
input, output and processing operations.

• Input: READ, OBTAIN, GET and PROMPT


• Output: PRINT, DISPLAY and SHOW
• Compute: COMPUTE, CALCULATE and DETERMINE
• Initialize: SET and INITIALIZE
• Add One: INCREMENT

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 4


INTRODUCTION TO ALGORITHM

b) Example of pseudocode

The pseudocode given below calculates the area of a rectangle.


Pseudocode: To calculate the area of a rectangle
START
PROMPT the user to enter the length of the rectangle
PROMPT the user to enter the width of the rectangle
COMPUTE the area by multiplying the length with width
DISPLAY the area
STOP

c) Advantages of Using Pseudocode

Some of the most significant benefits of pseudocode are as follows:

• It is easier to develop a program from a pseudocode rather than from a flowchart or decision table.
• Often, it is easy to translate pseudocode into a programming language, a step which can be
accomplished by less-experienced programmers (Ease of understanding)
• Unlike flowcharts, pseudocode is compact and does not tend to run over many pages. Its simple
structure and readability makes it easier to modify as well (Reduced complexity)..

d) Disadvantages of Using Pseudocode

Although pseudocode is a very simple mechanism to simplify problem-solving logic, it has its own
limitations. Some of the most notable limitations are as follows:

• It does not provide visual representation of the program logic.


• There are no accepted standards for writing pseudocodes. Different programmers use their own
style of writing pseudocode.
• It is quite difficult for the beginners to write pseudocode as compared to drawing a flowchart.

III. ALGORITHM STRUCTURES

Computer scientists have defined three constructs for a structured program or algorithm. The three
general programming constructs are:
❖ sequence,
❖ decision (selection)
❖ repetition (loop)
Each of these constructs can be embedded inside any other construct. It has been proven that three basic
constructs for flow of control are sufficient to implement any 'proper' algorithm.

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 5


INTRODUCTION TO ALGORITHM
III.1 Sequence

Sequence construct is a linear progression where one task is performed sequentially after another. The
actions are performed in the same sequence (top to bottom) in which they are written

Fig. Flowchart and Pseudocode for Sequence Construct


Example
Write an algorithm and flowchart for calculating the perimeter and surface of square, if the default length
of the sides of the square is a

Flowchart Pseudocode C
Begin #include<stdio.h>
Input a int main()
P=4×a { int a, P, S; printf(“Enter
S=a×a the length”);
Print P, S scanf(“%d”,&a);
END P=4*a;S=
a*a;
printf(“\nPerimetre = %d”, P);
printf(“\nSurface = %d”, S); return
0; }

Note that there is no branching and no process is repeated again. Each process is contributing something
to the next process.
III.1 Selection (Decision)

Selection is a process of deciding which choice is made between two or more alternative courses
of action. Selection logic is depicted as an IF-THEN-ELSE-ENDIF or CASE- ENDCASE
structure. As the name suggests, in case of the IF-THEN-ELSE-ENDIF construct, if the condition
is true, the true alternative actions are performed and if condition is false, then false alternative
actions are performed on.

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 6


INTRODUCTION TO ALGORITHM

a) IF-THEN-ELSE-ENDIF construct

Flowchart Pseudocode C
• • •
• •
IF condition THEN if(condition)
List of actions {
ELSE List of actions
List of different actions }
ENDIF Else
• • {
• List of different actions
}


Note that the ELSE keyword and 'Action 2' are optional. In case you do not want to choose between
two alternate courses of actions, then simply use IF-THEN-ENDIF

Flowchart Pseudocode C
• • If(condition) •
• { •
IF condition THEN List
List of actions • }
of actions
ENDIF
• •



Hence, if the condition is true, then perform the list of actions listed in the IF-THEN-ENDIF
construct and then move on to the other actions in the process. In case the condition is false, then
move on to the rest of the actions in the process directly. Let us write a pseudocode to find the
largest of three numbers

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 7


INTRODUCTION TO ALGORITHM
Example: Write an algorithm and flowchart which a given number N increased by 100 if N is less than
100, otherwise N is decreased by the 100. Print this result.

a) CASE-ENDCASE construct
If there are a number of conditions to be checked, then using multiple IFs may look very clumsy. Hence,
it is advisable to use the CASE-ENDCASE selection construct for multiple way selection logic. A CASE
construct indicates a multiway branch based on many conditions. CASE construct uses four keywords,
CASE, OF, OTHERS and ENDCASE, along with conditions that are used to
Flowchart Pseudocode C

#include<stdio.h> int
main() { int N;
printf("Enter the number:
START ");
Input N scanf("%d",&N);
If N < 100 Then if(N<100)
N = N × 100 N=N+100;
Else N = N - Else N=N-
100 100;
Print N
printf("\nnumber is %d",N);
STOP return 0;
}

indicate the various alternatives.

Flowchart Pseudocode C
case (expression)
{ case value 1: Sequence
1;
CASE expression OF break ; case
Condition 1: Sequence 1 value 2:
Condition 2: Sequence
Sequence 2 2; break ;
• •
• case value n:

Sequence
Condition n: Sequence n
n; break ;
OTHERS : default
default :
sequence
default sequence ; }
ENCASE

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 8


INTRODUCTION TO ALGORITHM
CASE construct performs the same process as multiple IFs, but it is much easier to read and write.
Conditions are normally numbers or characters indicating the value of 'Expression'

Example: To assign discount according to the code

Flowchart Pseudocode C
START #include<stdio.h>
READ code int main() {
CASE Grade char code ;
OF switch (
A: discount = 0.0 code )
B: discount = 0.1 { case 'A': discount =
C: discount = 0.2 0.0; break;
OTHERS : discount = case 'B': discount = 0.1;
0.3 ENDCASE break;
DISPLAY discount case 'C': discount = 0.2;
STOP break;
default: discount = 0.3;
}
Printf( "discount is: %f ",
discount); }

III.2. Repetition (Looping)

Looping construct is used when some particular task(s) is to be repeated for a number of times
according to the specified condition. By using looping, the programmer avoids repeating the same
set of instructions. As the selection, the loop is also represented in flowchart by a diamond. The
difference is just at the orientation of the arrows.

a) WHILE- ENDWHILE (Loop)

In case of WHILE-ENDWHILE, the loop will continue as long as the condition is true. The loop
is entered only if the condition is true. The 'statement' is performed for each iteration. At the
conclusion of each iteration, the condition is evaluated and the loop continues as long as the
condition is true.
Flowchart Pseudocode C

Mr. KENFACK DJIOLEU AMPLY G Contact: 671161872 Email: amplydrama@gmail.com 9


INTRODUCTION TO ALGORITHM

WHILE condition is True while (condition)


statements { statements
ENDWHILE }

Example: To display the first ten natural numbers using WHILE-ENDWHILE

Flowchart Pseudocode C

#include<stdio.h>
main()
INITIALIZE Count to zero
WHILE Count >= 10 { int i=0;
ADD 1 to Count while(i<10)
PRINT Count { printf("%d
ENDWHILE ",i); i++;
STOP } return
0;
}

b) DO-WHILE (REPEAT-UNTIL) Loop

The DO-WHILE (REPEAT-UNTIL) Loop is similar to the WHILE-ENDWHILE, except that the test is
performed at the bottom of the loop instead of at the top
• Like a while loop, a do-while loop is a loop that repeats while some condition is satisfied.
• Unlike a while loop, a do-while loop tests its condition at the end of the loop. This means that
its sequence of activities always runs at least once.
Flowchart Pseudocode C

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 10


INTRODUCTION TO ALGORITHM
Repeat
Statements
Until condition is false Do
{
Statements
}
DO While (condition)
Statement
WHILE (Condition)

The 'statement' in this type of loop is always performed at least once, because the test is performed
after the statement is executed. At the end of each iteration, the condition is evaluated, and the loop
repeats until the condition gets true. The loop terminates when the condition becomes true
Example To display the first ten natural numbers using DO-WHILE Loop
Flowchart Pseudocode C

#include<stdio.h>
main()
{ int
INITIALIZE Count to zero
i=0;
REPEAT
do
ADD 1 to Count
{ printf("%d
PRINT Count
",i); i++; }
UNTIL Count is less than
10 while(i<10);
STOP return 0;
}

c) FOR Loop
• The counter has the following three numeric values:
– Initial counter value
– Increment (the amount to add to the counter each time the loop runs) – Final counter
value
• The loop ends when the counter reaches the final counter value, or, if there is an associated test condition,
when the test condition is true.

Flowchart Pseudocode Syntax / C program

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 11


INTRODUCTION TO ALGORITHM

FOR x times FOR (initial counter value; final counter; increment)


Do Something Statement (Do Something)
Increment

FOR (x=1, x<5, x++)


PRINT “Hello World”

Output:

Hello World
Hello World
Hello World
Hello World

APPLICATION EXERCISES
Exercise 1: Write the flowchart corresponding to the following pseudo code

Pseudocode 1 Pseudocode 2 Pseudocode 2


Start Set total to zero initialize passes to zero
num1 = Set grade counter to one initialize failures to zero
5 num2 = 10 num3 = While grade counter is initialize student to one while
15 sum = num1 + less than or equal to ten student counter <= to ten input
num2 + Input the next grade the next exam result if the
num3 average = sum/3.0 Add the grade into the total student passed add one to
print average Set the class average to the passes else add one to failures
Stop total divided by ten add one to student counter
Print the class average. print the number of
passes print the number of
failures if eight or more
students passed print
"raise tuition"

Exercise 2: Create flowcharts to represent these short tasks:


a. “If it’s raining, bring an umbrella.”
b. “Take twenty paces, then turn and shoot.”
c. “Go forward until the Touch Sensor (on port 1) is pressed in, then stop.”
d. “Follow Liberty Avenue for 2 miles, then take a left turn onto 40th Street. Go until you reach the
bridge, but don’t cross the bridge. Instead, make a right turn onto Foster Street, then take the first
left turn. Follow that road until you reach the National Robotics
Engineering Consortium building.”
MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 12
INTRODUCTION TO ALGORITHM
e. “Turn on oven. Cook turkey for 4 hours or until meat thermometer reaches 180 degrees.” Exercise
3: Make the flowchart, and write the pseudocode for the following problem:
Given a Fahrenheit temperature, calculate and display the equivalent centigrade temperature. The
following formula is used for the conversion: C = 5 / 9 * (F – 32) where F and C are the Fahrenheit
and centigrade temperatures.

Exercise 4:
On a separate sheet of paper, make a flowchart organizing the “flow” of getting ready to go to
school in the morning. Be sure to include the following steps in your chart, but don’t be afraid to
add other things if you need them

Exercise 5: Let’s consider the following flowchart


a) Identity the programming construct available in
the flowchart and explain how each of them
works.
b) Do the dry run of the flowchart?
c) What does the flowchart do?
d) Write the corresponding pseudocode of the
flowchart

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 13


INTRODUCTION TO ALGORITHM
CHAPTER II: ALGORITHM APPROACHES
I. GREEDY ALGORITHM
A greedy algorithm works by choosing the best possible answer in each step and then
moving on to the next step until it reaches the end, without regard for the overall solution. It only hopes
that the path it takes is the globally optimum one, but as proven time and again, this method does not often
come up with a globally optimum solution.
For example: Take the path with the largest sum overall.

A greedy algorithm would take the blue path, as a result of shortsightedness, rather than the orange path,
which yields the largest sum.

ADVANTAGES
➢ Always taking the best available choice is usually easy. It usually requires sorting the choices. ➢
Repeatedly taking the next available best choice is usually linear work. But don’t forget the cost of
sorting the choices.
➢ Much cheaper than exhaustive search. Much cheaper than most other algorithms.

DISADVANTAGES
➢ Sometimes greedy algorithms fail to find the globally optimal solution because they do not
consider all the data. The choice made by a greedy algorithm may depend on choices it has made
so far, but it is not aware of future choices it could make.

II. DYNAMIC PROGRAMMING


Dynamic programming is a technique that breaks the problems into sub-problems, solve the sub-problems,
and saves the result for future purposes so that we do not need to compute the result again.

The sub problems are optimized that is to say we try to find out the minimum or the maximum solution of the
problem

Consider an example of the Fibonacci series. The following series is the Fibonacci series: 0, 1, 1, 2,
3, 5, 8, 13, 21, 34, 55, 89, 144, ,…

The numbers in the above series are not randomly calculated. Mathematically, we could write each of the terms
using the below formula:

F(n) = F(n-1) + F(n-2),

With the base values F(0) = 0, and F(1) = 1. To calculate the other numbers, we follow the above relationship. For
example, F(2) is the sum f(0) and f(1), which is equal to 1.

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 14


INTRODUCTION TO ALGORITHM
How can we calculate F(20)

How does the dynamic programming approach work?

The following are the steps that the dynamic programming follows:

➢ It breaks down the complex problem into simpler subproblems.


➢ It finds the optimal solution to these sub-problems.
➢ It stores the results of sub problems (memorization). The process of storing the results of
subproblems is known as memorization.
➢ It reuses them so that same sub-problem is calculated more than once.
➢ Finally, calculate the result of the complex problem.

Approaches of dynamic programming

There are two approaches to dynamic programming:

➢ Top-down approach
➢ Bottom-up approach

The top-down approach follows the memorization technique, while bottom-up approach follows the
tabulation method.

Here memorization is equal to the sum of recursion and caching. Recursion means calling the function itself,
while caching means storing the intermediate results.

Initially, the first two values, i.e., 0 and 1 can be represented as:

When i=2 then the values 0 and 1 are added shown as below:

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 15


INTRODUCTION TO ALGORITHM

When i=3 then the values 1and 1 are added shown as below:

When i=4 then the values 2 and 1 are added shown as below:

III. DIVIDE AND CONQUER

In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and then each problem
is solved independently. Then solution of all sub-problems is finally merged in order to obtain the solution of the
original problem.

we can understand divide-and-conquer approach in a three-step process.

Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems should represent a part of the
original problem. This step generally takes a recursive approach to divide the problem until no sub-problem is
further divisible. At this stage, sub-problems become atomic in nature but still represent some part of the actual
problem.

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 16


INTRODUCTION TO ALGORITHM
Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the problems are considered
'solved' on their own.

Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them until they formulate a solution of
the original problem. This algorithmic approach works recursively and conquer & merge steps works so close that
they appear as one.

Examples
The following computer algorithms are based on divide-and-conquer programming approach −

• Merge Sort
• Quick Sort
• Binary Search
• Strassen's Matrix Multiplication
• Closest pair (points)
There are various ways available to solve any computer problem, but the mentioned are a good example of divide
and conquer approach. ADVANTAGES:

➢ Solving difficult problems

Divide and conquer is a powerful tool for solving conceptually difficult problems: all it requires is a way
of breaking the problem into sub-problems, of solving the trivial cases and of combining sub-problems to
the original problem. Similarly, decrease and conquer only requires reducing the problem to a single
smaller problem, such as the classic Tower of Hanoi puzzle, which reduces moving a tower of height n to
moving a tower of height n − 1.

➢ Algorithm efficiency

The divide-and-conquer paradigm often helps in the discovery of efficient algorithms. It was the key, for
example, to Karatsuba’s fast multiplication method, the quicksort and mergesort algorithms, the Strassen
algorithm for matrix multiplication, and fast Fourier transforms.

In all these examples, the Divide and Conquer approach led to an improvement in the asymptotic cost of
the solution. For example, if (a) the base cases have constant-bounded size, the work of splitting the
problem and combining the partial solutions is proportional to the problem’s size n, and (b) there is a
bounded number p of sub problems of size ~ n/p at each stage, then the cost of the divide-and-conquer
algorithm will be O(n logpn).

➢ Parallelism

Divide and conquer algorithms are naturally adapted for execution in multi-processor machines,
especially shared-memory systems where the communication of data between processors does not need
to be planned in advance, because distinct sub-problems can be executed on different processors.

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 17


INTRODUCTION TO ALGORITHM
➢ Memory access
Divide-and-conquer algorithms naturally tend to make efficient use of memory caches. The reason is that
once a sub-problem is small enough, it and all its sub-problems can, in principle, be solved within the
cache, without accessing the slower main memory. An algorithm designed to exploit the cache in this
way is called cache-oblivious, because it does not contain the cache size as an explicit parameter.
Moreover, D&C algorithms can be designed for important algorithms (e.g., sorting, FFTs, and matrix
multiplication) to be optimal cache-oblivious algorithms–they use the cache in a probably optimal way,
in an asymptotic sense, regardless of the cache size. In contrast, the traditional approach to exploiting the
cache is blocking, as in loop nest optimization, where the problem is explicitly divided into chunks of the
appropriate size—this can also use the cache optimally, but only when the algorithm is tuned for the
specific cache size(s) of a particular machine.

➢ Round off control

In computations with rounded arithmetic, e.g. with floating point numbers, a divide-and-conquer
algorithm may yield more accurate results than a superficially equivalent iterative method. For example,
one can add N numbers either by a simple loop that adds each datum to a single variable, or by a D&C
algorithm called pairwise summation that breaks the data set into two halves, recursively computes the
sum of each half, and then adds the two sums. While the second method performs the same number of
additions as the first, and pays the overhead of the recursive calls, it is usually more accurate.

DISADVANTAGES:

One of the most common issues with this sort of algorithm is the fact that the recursion is slow, which in
some cases outweighs any advantages of this divide and conquer process.

Another concern with it is the fact that sometimes it can become more complicated than a basic iterative
approach, especially in cases with a large n. In other words, if someone wanted to add a large amount of
numbers together, if they just create a simple loop to add them together, it would turn out to be a much
simpler approach than it would be to divide the numbers up into two groups, add these groups
recursively, and then add the sums of the two groups together.

Another downfall is that sometimes once the problem is broken down into sub problems, the same sub
problem can occur many times. In cases like these, it can often be easier to identify and save the solution
to the repeated sub problem, which is commonly referred to as memorization.

The last recognizable implementation issue is that these algorithms can be carried out by a non-recursive
program that will store the different sub problems in things called explicit stacks, which gives more
freedom in deciding just which order the sub problems should be solved.

These implementation issues do not make this process a bad decision when it comes to solving difficult
problems, but rather this paradigm is the basis of many frequently used algorithms.

IV. BRANCH AND BOUND

(BB, B&B, or BnB) is an algorithm design paradigm for discrete and combinatorial

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 18


INTRODUCTION TO ALGORITHM
optimization problems, as well as mathematical optimization. A branch-and-bound algorithm consists of a
systematic enumeration of candidate solutions by means of state space search: the set of candidate solutions is
thought of as forming a rooted tree with the full set at the root. The algorithm explores branches of this tree, which
represent subsets of the solution set. Before enumerating the candidate solutions of a branch, the branch is checked
against upper and lower estimated bounds on the optimal solution, and is discarded if it cannot produce a better
solution than the best one found so far by the algorithm.

For example
➢ Tree sort
➢ Graph sort

V. INTRODUCTION TO COMPLEXITY ANALYSIS AND MEASURES

The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the
amount of data the algorithm must process. Usually there are natural units for the domain and range of this
function. There are two main complexity measures of the efficiency of an algorithm:

• Time complexity is the amount of time taken by an algorithm to run, as a function of the length of
the input. It measures the time taken to execute each statement of code in an algorithm.
• Space complexity is a function describing the amount of memory (space) an algorithm takes in
terms of the amount of input to the algorithm.

For example, we might say "this algorithm takes n2 time," where n is the number of items in the input. Or
we might say "this algorithm takes constant extra space," because the amount of extra memory needed
doesn't vary with the number of items processed.

First algorithm is defined to print the statement only once. The time taken to execute is shown as 0
nanoseconds. While the second algorithm is defined to print the same statement but this time it is set to
run the same statement in FOR loop for 10 times. In the second algorithm, the time taken to execute both
the line of code – FOR loop and print statement, is 2 milliseconds. And, the time taken increases, as N
value increases, since the statement is going to get executed N times.

Time Complexity of Insertion Sort:

The time complexity of Insertion Sort in the best case is O(n). In the worst case, the time complexity is O(n^2).

Time Complexity of Merge Sort:

This sorting technique has a stable time complexity for all kinds of cases. The time complexity of Merge Sort in

the best case is O(nlogn). In the worst case, the time complexity is O(nlogn). This is because Merge Sort

implements same number of sorting steps for all kinds of cases.

Time Complexity of Bubble Sort:

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 19


INTRODUCTION TO ALGORITHM
The time complexity of Bubble Sort in the best case is O(n). In the worst case, the time complexity is O(n^2).
Time Complexity of Quick Sort:

The time complexity of Quick Sort in the best case is O(nlogn). In the worst case, the time complexity is O(n^2).

Quicksort is considered to be the fastest of the sorting algorithms due to its performance of O(nlogn) in best and

average cases.

Time Complexity of Searching algorithms

Let us now dive into the time complexities of some Searching Algorithms and understand which of them is faster.

Time Complexity of Linear Search:

Linear Search follows the sequential access. The time complexity of Linear Search in the best case is O(1). In the

worst case, the time complexity is O(n).

Time Complexity of Binary Search:

Binary Search is the faster of the two searching algorithms. However, for smaller arrays, linear search does a better

job. The time complexity of Binary Search in the best case is O(1). In the worst case, the time complexity is O(log

n).

MR Simo Boris Contact: 692343904 Email:Simoboris76@gmail.com 20

You might also like