algorithm introduction

25
Introduction to Algorithms By Ajay Rawat

Upload: ajay-rawat

Post on 27-Dec-2015

15 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Algorihm Lectures

TRANSCRIPT

Introduction to Algorithms

By

Ajay Rawat

Algorithm • Set of well defined rule for solving some computational

problem. E.g – Sorting order list

– Shortest path

– Scheduling task with deadlines

• Why algorithm – Algorithm and DS is imp to work in the field of computer science.

• E.g. Routing communication network use classical shortest path algorithm.

• Effectiveness of Public key cryptography relies on number theoretic algorithm.

• Computer graphics need the computational primitive supplied by geometric algorithm.

• Database indices rely on balanced search tree data structure.

• Computational biology use dynamic programming algorithm to measure genome

• And so on…..

Algorithm – Key role in modern technology innovation

• Search engine use algorithm efficiently to compute the relevance of various web pages to its given search.

• Page rank algorithm used by Google.

– Outside the computer science domain • Quantum computation

• Economic market fluctuation

• Evolution can be seen as search algorithm

Study Algorithm • For computer professional.

– Practical standpoint: • Know standard set of algorithm from different area of computing.

• Design new algorithm and analyze their efficiency.

– Theoretical standpoint: • Algorithmics (study) recognized as cornerstone of CS.

• For non computer professional – Computer program would not exist w/o algorithm and

computer application become indispensable in almost all aspect of our professional and personal lives.

– Useful in developing analytical skill.

• Algorithms are solution to problems - not answers but precisely defined procedures for getting answers

Algorithm • An algorithm is a sequence of unambiguous instructions

for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

problem

algorithm

computer input output

Questions • Difference between algorithm and program.

• Why we need algorithm instead of a program or direct implementation.

• Different types of problem:

– Exact solution

– Approximation solution

– Optimization solution

• Why we need different algorithm for a same problem.

• Why to analyze algorithm.

Greatest Common Divisor • Problem: Find gcd(m,n), the greatest common divisor of

two nonnegative, not both zero integers m and n

• Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?

• Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12

Two descriptions of Euclid’s algorithm Step 1 If n = 0, return m and stop; otherwise go to Step 2

Step 2 Divide m by n and assign the value fo the remainder to r

Step 3 Assign the value of n to m and the value of r to n. Go to Step 1.

while n ≠ 0 do

r ← m mod n

m← n

n ← r

return m

Other methods for computing gcd(m,n) Consecutive integer checking algorithm

Step 1 Assign the value of min{m,n} to t

Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4

Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4

Step 4 Decrease t by 1 and go to Step 2

• Does not work correctly when one of its input is 0.

• Illustrate why it is so imp to specify the range of algorithm’s inputs explicitly.

gcd(m,n)

while n ≠0 do r ← m mod n m ← n n ← r

return 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 - 1

5. goto 2

Algorithm-1 Algorithm-2

Middle-school procedure Step 1 Find the prime factorization of m

Step 2 Find the prime factorization of n

Step 3 Find all the common prime factors

Step 4 Compute the product of all the common prime factors and return it as gcd(m,n)

Is this an legitimate algorithm?

Middle-school procedure • Prime factorization steps are not defined

unambiguously.

• Step 3 is not defined clearly (how to find common element is two sorted list).

Sieve of Eratosthenes Problem: Algorithm for generating consecutive primes no not

exceeding any given integer n. Input: Integer n ≥ 2 Output: List of primes less than or equal to n for p ← 2 to n do A[p] ← p

for p ← 2 to √n do if A[p] 0 //p hasn’t been previously eliminated from the list

j ← p* p while j ≤ n do

A[j] ← 0 //mark element as eliminated

j ← j + p Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

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

15

Understand the problem

Decide on: computational means,

Exact vs approximate solution,

Data structures,

Algorithm design technique

Design an algorithm

Prove correctness

Analyze the algorithm

Code the algorithm

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.

• We can consider algorithms to be

procedural solutions to problems

Sequence Of Steps In Designing And Analyzing An Algorithm

• Understanding the Problem • Ascertaining the Capabilities of the Computational

Device (RAM / Parellal) • Choosing between Exact and Approximate Problem

Solving (square root, can be slow) • Designing an Algorithm and Data Structures • Methods of Specifying an Algorithm • Proving an Algorithm’s Correctness (Mathematical

induction) • Analyzing an Algorithm (Time/Space) • Coding an Algorithm

Important Problem Types

• Sorting

• Searching

• String processing

• Graph problems – TSP , Graph Coloring problem

• Combinatorial problems

• Geometric problems – Closest pair problem, convex-hull problem

• Numerical problems – Definite integral, solving equation, evaluating function

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: steps are sufficiently simple and basic

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.

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.

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<-0

2. for i<-1 to n

3. S<-S+A[i]

4. return S.

General approaches to algorithm design

Divide and conquer

Greedy method

Dynamic Programming

Basic Search and Traversal Technique

Graph Theory

Linear Programming

Approximation Algorithm

NP Problem

Some Applications

• Study problems these techniques can be applied to – sorting

– data retrieval

– network routing

– Games

– etc

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