0% found this document useful (0 votes)
11 views

Introduction- Algorithms (new)

Uploaded by

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

Introduction- Algorithms (new)

Uploaded by

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

ALGORITHM

ALGORITHM: Introduction

An algorithm is a procedure or method that solves instances


of a problem or problem as whole.

In simple words, we can say “set of instructions or rules in a


uniform language that solves the problem”

Instruction in algorithm,

Add A, B & store in C


Or
C is equal to A plus B
ALGORITHM: Introduction
Qualities of a good algorithm

• Input and output should be defined precisely.


• Each step in an algorithm should be clear and unambiguous.
• Algorithms should be most effective among many different
ways to solve a problem.
• An algorithm shouldn't include computer code. Instead, the
algorithm should be written in such a way that it can be
used in different programming languages.
ALGORITHM: Formal Definition
Definition: An algorithm is a sequence of unambiguous
instructions for solving a problem.

For an algorithm to be an acceptable solution to a problem, it


must also be effective. That is, it must give a solution in a
‘reasonable’ amount of time
ALGORITHM: Formal Definition

One problem can have many algorithms to solve the problem

Efficient= runs in polynomial time.

An algorithm is said to be solvable in polynomial time if the


number of steps required to complete the algorithm for a
given input can be defined in a polynomial equation. That is
in the power of input variable.

Thus, effective efficient


ALGORITHM: Properties
Basic properties are:

Finite: the algorithm must eventually terminate

Complete: Always give a solution when one exists

Correct (sound): Always give a correct solution


ALGORITHM: Qualities
Qualities of a good algorithm

• Input and output should be defined precisely.


• Each step in an algorithm should be clear and unambiguous.
• Algorithms should be most effective among many different
ways to solve a problem.
• An algorithm shouldn't include computer code. Instead, the
algorithm should be written in such a way that it can be
used in different programming languages.
ALGORITHM: Designing
A general approach to designing algorithms is as follows
• Understanding the problem, assess its difficulty
• Choose an approach (e.g., exact/approximate,
deterministic/ probabilistic)
• Choose a strategy
• Prove
• Termination
• Completeness
• Correctness/soundness
• Evaluate complexity
• Implement and test it
• Compare to other known approach and algorithms
ALGORITHM: Example (MAX)
When designing an algorithm, we usually give a formal statement
about the problem to solve.

Problem
Given: a set A={a1,a2,…,an} of integers
Question: find the maximum integer Ai
ALGORITHM: Example (MAX)
A straightforward idea is

1. Start
2. Input 'n', no of elements in list.
3. Input set of integer values in A={a1,a2,…,an}
4. Simply assign a1 to a new variable max (assuming first value as
max)
5. check max with all elements of list, if any value in list is greater
than max, update stored max to that value
6. Output max is the maximum value
7. Stop
ALGORITHM: Example (Factorial)

1. input a number n
2. set variable final as 1
3. final <= final * n
4. decrease n
5. check if n is equal to 0
6. if n is equal to zero, goto step 8 (break out of loop)
7. else goto step 3
8. print the result final
ALGORITHM: Example (Factorial)

1. input a number n
2. set variable final as 1
3. final <= final * n
4. decrease n
5. check if n is equal to 1
6. if n is equal to 1, goto step 8 (break out of loop)
7. else goto step 3
8. print the result final

You might also like