Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming...

12
Introduction to Algorithms By Mr. Venkatadri. M

Transcript of Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming...

Page 1: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Introduction to Algorithms

ByMr. Venkatadri. M

Page 2: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Two Phases of Programming

• A typical programming task can be divided into two phases:

• Problem solving phase– produce an ordered sequence of steps that describe

solution of problem– this sequence of steps is called an algorithm

• Implementation phase – implement the program in some programming language

Page 3: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

3

Understand the problem

Decide on computational meansExact vs approximate solution

Data structuresAlgorithm design technique

Design an algorithm

Prove correctness

Analyze the algorithm

Code the algorithm

Page 4: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Algorithms

• A sequence of unambiguous

instructions for solving a

problem, i.e. for obtaining the

required output for any

legitimate input in

a finite amount of time

Page 5: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Algorithm Characteristics

• Input : Zero or more quantities or externally supplied.

• Output: At least one quantity is produced.• Definiteness: Each instruction is clear and

unambiguous.• Finiteness: The algorithm should terminate

after a finite number of steps.• Effectiveness:

Page 6: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

How to Represent an Algorithm

Algorithm can be represented in 3 ways.1. In Natural Language (English etc…)2. Pseudo Code.3. Real Programming Language.

Popular representation is Pseudo Code.

Page 7: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Pseudo Code Convention for Algorithm

• Pseudo code consists of keywords and English-like phrases which specify the flow control.

• Pseudo code representation highlights the Computational aspects by abstracting the implementation details.

Page 8: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Sum of ‘n’ numbers

Algorithm in Natural Language

Step 1: Select n number.Step 2: Set sum S to Zero.Step 3: Repeat from first

number to nth number i.e S=S+A[i].

Step 4: Return sum (S).

Algorithm in Pseudo Code.

1. S<-02. for i<-1 to n3. S<-S+A[i]4. return S.

Page 9: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

gcd(m,n)

while n ≠0 dor ← m mod

n m ← n

n ← rreturn m

1. t ← min (m ,n)

2. if m % t = 0 goto 3,

else goto 4

3. if n % t = 0 return t,

else goto 4

4. t ← t - 15. goto 2

Algorithm-1 Algorithm-2

Page 10: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

General approaches to algorithm design

Divide and conquer Greedy method Dynamic ProgrammingBasic Search and Traversal TechniqueGraph TheoryLinear ProgrammingApproximation AlgorithmNP Problem

Page 11: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Some Applications

• Study problems these techniques can be applied to– sorting – data retrieval – network routing – Games– etc

Page 12: Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.

Exercises

• Write an algorithm for product of two matrix• Design an algorithm for finding square of

given number• Design an algorithm to find factorial of a

number