Algorithms and Algorithm Analysis The “fun” stuff.

13
Algorithms and Algorithms and Algorithm Analysis Algorithm Analysis The “fun” stuff The “fun” stuff

Transcript of Algorithms and Algorithm Analysis The “fun” stuff.

Algorithms and Algorithm Algorithms and Algorithm AnalysisAnalysisThe “fun” stuffThe “fun” stuff

AlgorithmsAlgorithms

What is an algorithm?What is an algorithm? A finite set of steps that specify a A finite set of steps that specify a

sequence of operations to be carried out in sequence of operations to be carried out in order to solve a specific problem.order to solve a specific problem.

Properties of AlgorithmsProperties of Algorithms

1.1. FinitenessFiniteness

2.2. Absence of AmbiguityAbsence of Ambiguity

3.3. Definition of SequenceDefinition of Sequence

4.4. FeasibilityFeasibility

5.5. InputInput

6.6. OutputOutput

What is Programming?What is Programming?

Phases of ProgrammingPhases of Programming1.1. DesignDesign

2.2. ImplementationImplementation

3.3. TestingTesting

4.4. RepeatingRepeating

Algorithm vs. ProgramsAlgorithm vs. Programs

A computer program is one concrete A computer program is one concrete implementation of an algorithm using a implementation of an algorithm using a particular computer languageparticular computer language

The design phase should produce an The design phase should produce an algorithmalgorithm

The implementation phase should produce The implementation phase should produce a programa program

The design phase is typically much longer The design phase is typically much longer than the programming phasethan the programming phase

Algorithm PerformanceAlgorithm Performance

It would be great if you could code up an It would be great if you could code up an algorithm and then run it with a bunch of algorithm and then run it with a bunch of input.input.

Take a look at the clock and decide how Take a look at the clock and decide how well it ranwell it ran

There are many problems with this There are many problems with this approach and it suffices to say that this is approach and it suffices to say that this is badbad

A better wayA better way

What is often done is to approximate or What is often done is to approximate or estimate the performance of an algorithmestimate the performance of an algorithm

Estimation is an important skill to learn Estimation is an important skill to learn and to useand to use

Question: How many hotdogs tall is the Question: How many hotdogs tall is the Empire State Building?Empire State Building?

ExampleExample

Question: How many Question: How many hotdogs tall is the hotdogs tall is the Empire State Empire State Building?Building?

Answer: The ESB is Answer: The ESB is 1250 feet tall.1250 feet tall.

Assuming that a Assuming that a hotdog is 6 inches hotdog is 6 inches from end to end, you from end to end, you would need, 1250 * 2 would need, 1250 * 2 = 2500 hotdogs.= 2500 hotdogs.

Complexity AnalysisComplexity Analysis

An objective way to evaluate the cost of an An objective way to evaluate the cost of an algorithm or code section. algorithm or code section.

The cost is computed in terms of space or time, The cost is computed in terms of space or time, usuallyusually

The goal is to have a meaningful way to The goal is to have a meaningful way to compare algorithms based on a common compare algorithms based on a common measure.measure.

Complexity analysis has two phases,Complexity analysis has two phases, Algorithm analysisAlgorithm analysis Complexity analysisComplexity analysis

Algorithm AnalysisAlgorithm Analysis

Algorithm analysis requires a set of rules to Algorithm analysis requires a set of rules to determine how operations are to be counted.determine how operations are to be counted.

There is no generally accepted set of rules for There is no generally accepted set of rules for algorithm analysis.algorithm analysis.

In some cases, an exact count of operations is In some cases, an exact count of operations is desired; in other cases, a general approximation desired; in other cases, a general approximation is sufficient.is sufficient.

The rules presented that follow are typical of The rules presented that follow are typical of those intended to produce an exact count of those intended to produce an exact count of operations.operations.

RulesRules

1.1.We assume an arbitrary time unit.We assume an arbitrary time unit.

2.2.Execution of one of the following operations Execution of one of the following operations takes time 1:takes time 1:1.1. assignment operationassignment operation

2.2. single I/O operationssingle I/O operations

3.3. single Boolean operations, numeric comparisonssingle Boolean operations, numeric comparisons

4.4. single arithmetic operationssingle arithmetic operations

5.5. function returnfunction return

6.6. array index operations, pointer dereferencesarray index operations, pointer dereferences

More RulesMore Rules

3.3. Running time of a selection statement (if, switch) is the Running time of a selection statement (if, switch) is the time for the condition evaluation + the maximum of the time for the condition evaluation + the maximum of the running times for the individual clauses in the running times for the individual clauses in the selection.selection.

4.4. Loop execution time is the sum, over the number of Loop execution time is the sum, over the number of times the loop is executed, of the body time + time for times the loop is executed, of the body time + time for the loop check and update operations, + time for the the loop check and update operations, + time for the loop setup.loop setup.

† † Always assume that the loop executes the maximum number of Always assume that the loop executes the maximum number of iterations possibleiterations possible

5.5. Running time of a function call is 1 for setup + the time Running time of a function call is 1 for setup + the time for any parameter calculations + the time required for for any parameter calculations + the time required for the execution of the function body.the execution of the function body.

ExampleExample

Sum = 0;Sum = 0;In >> Value;In >> Value;while ( In ) while ( In ) {{

if ( Value < 0 ) if ( Value < 0 ) {{

Sum = -Sum;Sum = -Sum;Sum = Sum + Value;Sum = Sum + Value;

}else {}else {Sum = Sum + Value;Sum = Sum + Value;

}}In >> Value;In >> Value;

}}