2 views

Uploaded by Malathi Sankar

DAA-Unit-I

- KMS, Artificial Intelligence, & the Economist 20150509
- Computational discrete mathematics with Python
- Basic Inequalities and Diophantine Equations
- Greek Letters Used in Mathe
- MELJUN CORTES ALGORITHM Analysis of Algorithm Efficiency
- C Computer Programming Language Notes
- Shor Report
- Deswik White Paper Pseudoflow Explained
- Automatic Slide Generation System
- SEC-DID 1-Leng. C-2012.doc
- lec04
- Pset Advice Cs161
- engineering math
- Quants Basics
- Excel Version
- EX1-S14(1)
- Lecture 2
- 10-Gcd
- Ex-1-Solutions.pdf
- DSmT Based Scheduling Algorithm in Opportunistic Beamforming Systems

You are on page 1of 64

Algorithms

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.

Notion of algorithm

problem

algorithm

Algorithmic solution

PSEUDOCODE

Pseudocode (pronounced SOO-doh-kohd) is a detailed yet

readable description of what a computer program or algorithm

must do, expressed in a formally-styled natural language rather

than in a programming language.

developing a program.

provides programmers a detailed template for the next step of

writing code in a specific programming language.

Formatting and Conventions in Pseudocoding

its implementation in a programming language. Try to

indent at least four spaces.

The pseudocode entries are to be cryptic, AND

SHOULD NOT BE PROSE. NO SENTENCES.

No flower boxes in pseudocode.

Do not include data declarations in pseudocode.

Some Keywords That Should be Used

Do While...EndDo;

Do Until...Enddo;

Case...EndCase;

If...Endif;

Call ... with (parameters); Call; Return ....; Return;

When; Always use scope terminators for loops and

iteration.

Some Keywords …

As verbs, use the words

generate, Compute, Process,

Set, reset,

increment,

calculate,

add, sum, multiply, ...

print, display,

input, output, edit, test , etc.

Methods of finding GCD

M-1 M-3

M-2

Euclid’s Algorithm

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) = ?

equality

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

until the second number becomes 0, which makes the

problem

trivial.

Two descriptions of Euclid’s algorithm

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

Other methods for gcd(m,n) [cont.]

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 algorithm?

Sieve of Eratosthenes

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

Termination of Euclid’s Algorithm

The second number of the pair gets smaller with each

iteration and cannot become negative:

Indeed, the new value of n is r = m mod n, which is

always smaller than n.

Eventually, r becomes zero, and the algorithms stops.

Compute the GCD of 120 and 23.

Fundamentals of Algorithmic

Problem Solving

Fundamentals of Algorithmic Problem Solving

getting answers.

analyzing an algorithm

Algorithm Design & Analysis Process

Step 1: Understand the Problem

Before designing an algorithm - understand

completely the problem given.

questions if you have any doubts about the problem,

cases, and ask questions again if needed.

Step 1: Understand the Problem

An input to an algorithm specifies an instance of the

problem the algorithm solves.

instances the algorithm needs to handle.

inputs , but crashes on some boundary values.

works most of the time but one that works correctly for

all legitimate inputs.

Step 2: Ascertaining the capabilities of a

computational device

Algorithms designed to be executed on machines that

executes intstructions one after another are called

sequential algorithms.

execute operations concurrently are called parallel

algorithms.

Step 3: Choosing between Exact &

Approximate Problem Solving

Solving the problem exactly - Exact algorithms

Solving the problem approximately - Approximation

algorithms

Why approximation algorithms?

1. Problems cannot be solved exactly.

Eg. Extracting square roots, solving non-linear equations

2. Available exact algorithms are unacceptably slow because of

problem’s complexity

Eg. Traveling Salesman Problem

3. Approx. Algs can be a part of algorithms that solve the

problem exactly.

Step 4: Deciding on Appropriate Data

Structures

In the new world of object-oriented programming,

data structures remain important for both design and

analysis of algorithms.

now and concentrate on the algorithm side.

Step 5: Algorithm Design Techniques

An algorithm design technique (or “strategy” or

“paradigm”) is a general approach to solving problems

algorithmically that is applicable to a variety of

problems from different areas of computing.

Conquer

Importance:

1. Provide guidance for designing algorithms for new

problems.

2. To classify algorithms according to an underlying

design idea.

Step 6: Methods of Specifying

an Algorithm

Pseudocode, a mixture of a natural language and

programming language-like constructs.

collection of connected geometric shapes containing

descriptions of the algorithm’s steps.

Step 7: Proving an Algorithm’s Correctness

Prove algorithm’s correctness = prove that the

algorithm yields a required result for every legitimate

input in a finite amount of time.

be able to show that the error produced by the

algorithm does not exceed a predefined limit.

Step 8: Analyzing an Algorithm

1. Efficiency

Time efficiency indicates how fast the algorithm runs.

space efficiency indicates how much extra memory the

algorithm needs.

2. Simplicity

3. Generality

Design an algorithm for a problem posed in more

general terms.

Design an algorithm that can handle a range of inputs

that is natural for the problem at hand.

Step 9: Coding the algorithm

More than implementation

Important Problem Types

Important Problem Types

Sorting

Searching

String processing

Graph problems

Combinatorial problems

Geometric problems

Numerical problems

Sorting

The sorting problem asks us to rearrange the items of a

given list in ascending order.

we usually need to

sort lists of numbers,

characters from an alphabet,

character strings,

records similar to those maintained by schools about

their students,

libraries about their holdings,

companies about their employees.

Searching

The searching problem deals with finding a given

value, called a search key, in a given set (or a multiset,

which permits several elements to have the same

value).

String Processing

A string is a sequence of characters from an alphabet.

1. Text string – comprises letters, numbers, and

special characters

2. Bit string – comprises zeros and ones

3. Gene sequence

word in a text

Graph Problems

A graph can be thought of as a collection of points

called vertices, some of which are connected by line

segments called edges.

applications.

1. Graph traversal algorithms - How can one visit all

the points in a network?

2. Shortest-path algorithms - What is the best

Introduction route between two cities?

3. Topological sorting for graphs with directed edges

Combinatorial Problems

combinatorial problems: problems that ask (explicitly

or implicitly) to find a combinatorial object—such as

a permutation, a combination, or a subset—that

satisfies certain constraints and has some desired

property (e.g., maximizes a value or minimizes a

cost).

exactly in an acceptable amount of time.

Geometric Problems

Geometric algorithms deal with geometric objects such

as points, lines, and polygons.

2 class problems:

The closest pair problem: given n points in the plane,

find the closest pair among them.

convex polygon that would include all the points of a

given set. If

Convex hull problem

Numerical Problems

Numerical problems, another large special area of

applications, are problems that involve mathematical

objects of continuous nature: solving equations and

systems of equations, computing definite integrals,

evaluating functions, and so on.

Fundamentals of Analysis of

algorithm efficiency

Analysis of algorithms

Issues:

correctness

time efficiency

space efficiency

optimality

Approaches:

theoretical analysis

empirical analysis

Theoretical analysis of time efficiency

Time efficiency is analyzed by determining the number

of repetitions of the basic operation as a function of

input size

Basic operation: the operation that contributes the

most towards the running time of the algorithm

input size

T(n) ≈ copC(n)

running time execution time Number of times

for basic operation basic operation is

or cost executed

Input size and basic operation examples

Key comparison

in a list of n items items, i.e. n

Matrix dimensions or

Multiplication of Multiplication of

total number of

two matrices two numbers

elements

n’size = number of

Checking primality

digits (in binary Division

of a given integer n

representation)

Visiting a vertex

Typical graph #vertices and/or

or traversing an

problem edges

edge

Empirical analysis of time efficiency

or

Count actual number of basic operation’s executions

Efficiencies

Is its efficiency for the worst case input of size n, which

is an input of size n for which the algorithm runs the

longest among all possible inputs of that size

Cworst(n)

Best-case efficiency:

Is its efficiency for the worst case input of size n, which

is an input of size n for which the algorithm runs the

fastest among all possible inputs of that size

Cbest(n)

Amortized efficiency

It applies not to a single run of an

algorithm, but rather to a sequence of

operations performed on the same data

structure

Best-case, average-case, worst-case

For some algorithms, efficiency depends on form of input:

Worst case: Cworst(n) – maximum over inputs of size n

Best case: Cbest(n) – minimum over inputs of size n

Average case: Cavg(n) – “average” over inputs of size n

typical input

NOT the average of worst and best case

Expected number of basic operations considered as a

random variable under some assumption about the

probability distribution of all possible inputs. So, avg =

expected under uniform distribution.

Example: Sequential search

Worst case

n key comparisons

Best case

1 comparisons

Average case (n+1)/2, assuming K is in A

Types of formulas for basic operation’s count

Exact formula

e.g., C(n) = n(n-1)/2

multiplicative constant

e.g., C(n) ≈ 0.5 n2

multiplicative constant

e.g., C(n) ≈ cn2

Order of growth

Most important: Order of growth within a constant

multiple as n→∞

Example:

How much faster will algorithm run on computer that is

twice as fast?

input size?

Values of some important functions as n

Asymptotic Notations

O (Big-Oh)-notation

Ω (Big-Omega) -notation

Θ (Big-Theta) -notation

Asymptotic order of growth

A way of comparing functions that ignores constant factors

and small input sizes (because?)

g(n)

g(n)

g(n)

O-notation

denoted t(n) O(g(n)) is bounded above by some

constant multiple of g(n) for all large n, i.e., there exist

positive constant c and non-negative integer n0 such

that

f(n) ≤ c g(n) for every n ≥ n0

Big-oh

-notation

Formal definition

A function t(n) is said to be in (g(n)), denoted t(n)

(g(n)), if t(n) is bounded below by some constant

multiple of g(n) for all large n, i.e., if there exist some

positive constant c and some nonnegative integer n0

such that

t(n) cg(n) for all n n0

Big-omega

-notation

Formal definition

A function t(n) is said to be in (g(n)), denoted t(n)

(g(n)), if t(n) is bounded both above and below by

some positive constant multiples of g(n) for all large

n, i.e., if there exist some positive constant c1 and c2

and some nonnegative integer n0 such that

c2 g(n) t(n) c1 g(n) for all n n0

Big-theta

Theorem

If t1(n) O(g1(n)) and t2(n) O(g2(n)), then

t1(n) + t2(n) O(max{g1(n), g2(n)}).

The analogous assertions are true for the -notation and

-notation.

t1(n) c1*g1(n), for all n n1

t2(n) c2*g2(n), for all n n2

Define c3 = c1 + c2 and n3 = max{n1,n2}. Then

t1(n) + t2(n) c3*max{g1(n), g2(n)}, for all n n3

Some properties of asymptotic order of

growth

f(n) O(f(n))

f1(n) + f2(n) O(max{g1(n), g2(n)})

Establishing order of growth using

limits

lim T(n)/g(n) =

n→∞

∞ order of growth of T(n) > order of growth of g(n)

L’Hôpital’s rule and Stirling’s formula

L’Hôpital’s rule: If limn f(n) = limn g(n) = and

the derivatives f´, g´ exist, then

=

n g(n) n g ´(n)

Example: log n vs. n

Example: 2n vs. n!

Orders of growth of some important functions

All logarithmic functions loga n belong to the same class

(log n) no matter what the logarithm’s base a > 1 is

because

log a n log b n / log b a

All polynomials of the same degree k belong to the same class:

different a’s

order log n < order n (>0) < order an < order n! < order nn

Basic asymptotic efficiency classes

1 constant

log n logarithmic

n linear

n log n n-log-n

n2 quadratic

n3 cubic

2n exponential

n! factorial

- KMS, Artificial Intelligence, & the Economist 20150509Uploaded bythe1uploader
- Computational discrete mathematics with PythonUploaded bygxsteph
- Basic Inequalities and Diophantine EquationsUploaded byKhor Shi-Jie
- Greek Letters Used in MatheUploaded byndfx10
- MELJUN CORTES ALGORITHM Analysis of Algorithm EfficiencyUploaded byMELJUN CORTES, MBA,MPA
- Shor ReportUploaded bySubhadip17
- Deswik White Paper Pseudoflow ExplainedUploaded byKamara
- lec04Uploaded byShengFeng
- C Computer Programming Language NotesUploaded byAnna Castro
- Automatic Slide Generation SystemUploaded byIRJET Journal
- Pset Advice Cs161Uploaded bydts99
- engineering mathUploaded byselamitsp
- SEC-DID 1-Leng. C-2012.docUploaded byGaby Gonzalez
- Quants BasicsUploaded bydeepi
- Excel VersionUploaded byEren Võ
- EX1-S14(1)Uploaded byteddymax
- Lecture 2Uploaded byprateek
- 10-GcdUploaded byDewi Agust Soe
- Ex-1-Solutions.pdfUploaded byf.a.h
- DSmT Based Scheduling Algorithm in Opportunistic Beamforming SystemsUploaded byAnonymous 0U9j6BLllB
- Continued FractionsUploaded byc_s_rio
- BookUploaded byAmitoz Rathore
- Boolean Logic and EthernetUploaded byGalactic Goalie
- pajekUploaded byDiego Astorga
- BCA I Sem html practical fileUploaded byJay Suthar
- Data StructureUploaded bySrikanth Reddy G
- Jun 2011 Paper 2 SolutionUploaded byaflayers
- 4.Test Generation Comb 4Uploaded byRogerZhang
- ADA QBUploaded bySham Sundar
- calculation of sequent depth.pdfUploaded byKrunal Mahajan

- Internet and Web Technologies by Rahul Ranjan 39d4b9 (1)Uploaded byMalathi Sankar
- CS6551 CNUploaded byMalathi Sankar
- III B.Sc DCN NOTESUploaded byMalathi Sankar
- 238-IJCSE-07322-42.pdfUploaded byMalathi Sankar
- 2.docxUploaded byMalathi Sankar
- 1.Data Com & NwUploaded bySyed Jabeer
- C and Data Structure_101 13.pdfUploaded byMalathi Sankar
- MS WORD Lab ManualUploaded byMalathi Sankar
- BCA-124 Office Automation (W,E,P,A)Uploaded byRavi Prakash
- How a Computer WorksUploaded byMalathi Sankar
- All Programming NotesUploaded byMalathi Sankar
- c languageUploaded byR!0
- Software Engineering BcaUploaded byMalathi Sankar
- 04 pengurusan pejabatUploaded byhativern
- C SHARP LAB PRGUploaded byMalathi Sankar
- OAPaperIIYR2Uploaded byMalathi Sankar
- OM-02_BLOCK-1Uploaded byMalathi Sankar
- Office ManagementUploaded byMalathi Sankar
- An Introduction to C#Uploaded byMalathi Sankar
- Note OMUploaded byMalathi Sankar
- 1. Types of CommunicationsUploaded byMalathi Sankar
- II Year - DJA2E - Visual BasicUploaded byMalathi Sankar
- Php Source CodeUploaded byMalathi Sankar
- MisUploaded byMalathi Sankar
- PhpUploaded byEkansh Dwivedi
- PHP Part 1Uploaded byMalathi Sankar
- PHP OverviewUploaded byMalathi Sankar
- Data AdditionUploaded byMalathi Sankar

- pchardware-stadsdrtecUploaded byUganes Wari
- 2. Tìm kiếm cứu nạnUploaded byMinh Đức
- DAA-KitUploaded byCarlos Algalord
- fpga pptUploaded byPriya Darshini
- Falcon 222 E3 -Parts Catalogue (V6256706) on 27 Aug 13Uploaded bydiabalziab
- 6270 assi3(2)Uploaded byHeshan Abeydeera
- puzzle cube projectUploaded byapi-260953119
- Calalogue Mcb c60nUploaded byEko Parjono
- 20620041 Electrical MotorsUploaded byAbah Hafiz Hadif
- 10ns14Uploaded byCarlos Padilla
- Cv KorchinaUploaded byEko Priyanto
- Hydrofoil P72039Uploaded byPukhraj Daga
- Outlook Report - Supply Chain Management 2020 and BeyondUploaded byravithejahr
- Leetro LaserCut 6.1 ManualUploaded byNawres Arif
- ACCU2014_PeterSommerlad_C++14Uploaded byPratibha Gupta
- Ir Advance c7200 Series PDF Upload PartsUploaded bytroy2k0
- scjp_730Uploaded byเปรม ครับ
- PWM Signal With PIC 16F84Uploaded byASM_213
- 2100-td031_-en-pUploaded bycokicisne
- cs1541 HW4Uploaded byFei Kou
- Ssd2 Unit1 Computer SystemsUploaded byarman_9393
- Secondary Electron Emission by Bruce Darrow GaitherUploaded bybrucedar
- Asset Management - MindMAPUploaded byPessoa Dali
- chap06Uploaded byHidayah Hadi
- Strain QuizUploaded byLarete Paolo
- STM Experience ManualUploaded bygian matteo
- How to Add and Delete Users on Ubuntu 12How to Add and Delete Users on Ubuntu 12Uploaded byLohitRamakrishnaKotapati
- Boiler Survey ProcedureUploaded byMeghali Borle
- Mastering Set AnalysisUploaded byMadhavan Eyunni
- A Study on Retention Strategy With Reference to ESAB INDIA LTD,AmbatturUploaded byAntony Christopher