70
Introduction to Design & Analysis of Algorithms Matakuliah Desain & Analisis Algoritma (CS 3024) ZK Abdurahman Baizal STT Telkom Bandung

Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

Embed Size (px)

Citation preview

Page 1: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

Introduction to Design & Analysis of Algorithms

Matakuliah Desain & Analisis Algoritma(CS 3024)

ZK Abdurahman BaizalSTT Telkom Bandung

Page 2: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 2

Algorithm is a sequence of un ambiguous instructions for solving a problem (Levitin)

Problem

Algorithm

Computerinput output

Page 3: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 3

Isi dari algoritma :AssignmentSubroutine callSequencePencabanganLooping

Page 4: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 4

Fundamentals of Algorithmic Problem Solving

Understand the Problem

Decide on : Computational means, exact vsapproximate solving, data structures,algorithmsdesign techniques

Design algorithm

Prove correctness

Analyze the algorithm

Code the algorithm

Page 5: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 5

Important Problem Types

SortingTo rearrange the items of given list in

ascending/descending orderAda Stable sorting algorithm and ada Not

Stable sorting algorithm

Page 6: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 6

Important Problem Types (Cont)

SearchingDeals with finding a given value, called a

search key, in a given setSequencial search, binary search, dll

Page 7: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 7

Important Problem Types (cont)

String ProcessingString : a sequence of characters (Levitin)Particular problem : searching for a given word

in a text → string matchingGraph Problems

Graph : a collection of point (vertex), some of which are connected by line segments (edge)

Page 8: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 8

Important Problem Types (cont)

Graph bisa sebagai pemodelan daritransportation and communicaton networks, project schedulling, games, dsb

Page 9: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 9

Important Problem Types (cont)

Combinatorial ProblemsProblem ask to find a combinatorial objectCombinatorial objects typically grows

extremely fast with problem’s sizeThere no known algorithms for solving most

such problems in acceptable amount of timeexception : shortest-path problem

(diantaranya)Contoh : graph coloring problem, traveling

salesman problem, knapsack problems, dll

Page 10: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 10

Important Problem Types (cont)

Geometric ProblemsGeometric algorithms deal with geometric

objects such as points, lines and polygons. Applications : computer graphics, robotics, dsbClassic problems of computational geometry:

The closest pair problem and convex hull problem

Page 11: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 11

Important Problem Types (cont)

Numerical ProblemsProblems that involve mathematical objects of

continuous natureExample : solving equations and systems of

equations, computing definite integrals, evaluating functions, dsbMajority can be solved only approximatelyApplication : information storage, information

retrieval, transportation through networks, dsb

Page 12: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 12

Introduction to Software Engineering Concepts

Page 13: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 13

Top Down Programming by Stepwise Refinement

In this chapter, we will concern with small programThe term programming in the small refers to the activities that center on the creation of small program The term programming in the large refers to activities that center on the creation and modification of large software systems

Page 14: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 14

Top Down Programming by Stepwise Refinement

One of the helpful methods for developing small programs is called top down programming by stepwise refinement

We begin at the topmost conceptual level, by imagining a general, and we sketch the rough program strategy in outline form before choosing any particular low level data or algorithmIn stepwise of modifications, we refine the program strategy supplying more detail, until low-level details are completely filled in

Page 15: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 15

Top Down Programming

Consider the following example of a scratch-off lottery ticket

Page 16: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 16

Top Down Programming

We buy ticket with an opaque coating to scratch off. When we remove the coating, six numbers representing dollar amounts become visible, and we can hand in the ticket and receive the largest dollar amount repeated three or more times.If no amount is repeated three or more times, we don’t win anythingWe will write a C program for this case

Page 17: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 17

Process of Top Down Programming

One possible solution follows the theme, “make a table and search it to find the solution”

Page 18: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 18

Process of Top Down Programming

Refinement stepsMaking program strategy describing top level goals

[ Program Strategy 1]

Page 19: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 19

Process of Top Down ProgrammingRefinement steps of Program Strategy 1: Get six inputs from user

We can use an array that can store six integers

[ Program 1.1 ]

Page 20: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 20

Process of Top Down Programming

There are two alternatives :We choose the data structure for the table before designing the strategy for the algorithmWe design the algorithm before choosing the details of the data structure → postponing the choice the details of data representations

Let’s we try the second idea !

Page 21: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 21

Process of Top Down ProgrammingRefinement steps of Program Strategy 1: Make a Table of amounts and repetitions

We use abstract table operations

[ Program Strategy 1.2]

unwritten function

Page 22: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 22

The activity of creating program strategies at the top level leads to the creation of function calls, whose lower level function definitions have not yet be written → Top Down Method

We first write the definitions of functions before using calls on them inside other higher level functions → Bottom Up Method

Page 23: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 23

Process of Top Down Programming

Abstract Program Strategy for function InsertAmount on on Program Strategy 1.2

[ Program Strategy 1.2.1]

Page 24: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 24

Process of Top Down ProgrammingRefinement steps of Program Strategy 1: Print largest amount repeated three or more times

[ Program Strategy 1.3 ]

Page 25: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 25

Choosing Data structure for the table

typedef struct {int Amount;int Repetitions;

} RowStruct;

RowStruct Table[6];

Page 26: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 26

Choosing Data structure for the table

Refinement steps of Program Strategy 1.2

[ Program 1.2.2 ]

Page 27: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 27

Refinement steps of Program Strategy 1.2.1: Details of Program Strategy 1.2.1

[ Program 1.2.1.1 ]

Page 28: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 28

Choosing Data structure for the tableRefinement steps of Program Strategy 1.3

[ Program 1.3.1 ]

Page 29: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 29

Choosing Data structure for the tableRefinement steps of Program Strategy 1

Page 30: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 30

Entire Program for Finding a Lottery Winner

Page 31: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 31

• Another possible solution for Finding a Lottery Winner follows the theme : “Sort the input into descending order and find the first run of three repeated amounts”

Page 32: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 32

Proving Programs Correct

Page 33: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 33

Proving Programs Correct

How to prove that a program does what it is intended to do

Providing assertions:Precondition Initial statePostcondition Final state

{precondition} P{postcondition}

Page 34: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 34

Example: SelectionSort(A,m,n)

By applying the pattern in the case of SelectionSort:

{m ≤ n}SelectionSort(A,m,n){A[m]≥A[m+1] ≥… ≥A[n]}

Page 35: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 35

Proving correctness of SelectionSort

1. Place additional assertions inside the text of the SelectionSort program

2. Construct proofs of correctness of the subroutines used by SelectionSort

Page 36: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 36

First, Proving FindMax

The pre and postcondition are:

{m<n} /*precondition*/

j=FindMax(A,m,n);

{A[j]≥A[m:n]} /*postcondition*/

Page 37: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 37

Finding the position of the largest item

Page 38: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 38

Mathematical Induction

ith tripStatement i++i=m+1j=m

Statement if (A[i]>A[j]) j=iA[j]≥A[m:m+1] A[j] ≥A[m:i]

If (m<n) and (i=m+1), then (i≤n)∴ Loop invariant A[j]≥A[m:i] Λ (i≤n)

true

Page 39: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 39

Mathematical Induction

(i+1)st tripTest the while-condition (i!=n), suppose it is still true, so we have two facts:

(i ≠ n)A[j]≥A[m:i] Λ (i≤n)

→ by combining those two facts, the loop invariant assertionwill yield : A[j]≥A[m:i] Λ (i<n)

after i++ is executed, another assertion:A[j]≥A[m:i-1] Λ (i≤n) must hold.

The statement if (A[i]>A[j]) j=i; is executed, the loop invariant assertion: A[j]≥A[m:i] Λ (i≤n) still holds true

Page 40: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 40

Mathematical Induction

The principle of proving correctness program using induction:

If it’s true on any given trip, it will remain true on the next trip.

Consequently,It’s true on every trip through the loop

Page 41: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 41

Finishing the proof

We are now ready to go back and prove that SelectionSort(A,m,n) sorts A[m:n], using the fact we just establish that j=FindMax(A,m,n)

We can add assertions in SelectionSort(A,m,n) as follows :

Page 42: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 42

Finishing the proof

Page 43: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 43

Outer Subproblem

Assume condition (m<n) is trueFunction FinMax(A,m,n) is called, so we have assertion A[MaxPosition] ≥ A[m:n] (line 14)

Meaning A[MaxPosition] is the largest item in the subarrayA[m:n]

Statement exchange A[m] – A[maxPosition]A[m] = subarray’s first itemA[MaxPosition] = subarray’s largest itemAfter axchange, A[m] is the largest item in A[m:n]

→ so we have assertion A[m] ≥ A[m:n] (line 19)

Page 44: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 44

Smaller subproblems

Recursive call SelectionSort(A,m+1,n)Effect : rearranging the items in A[m+1:n] into decreasing order so we have assertion A[m+1]≥ A[m+2]≥… ≥A[n] (line 21)So we have 2 facts :

A[m] ≥ A[m+1]A[m+1]≥ A[m+2]≥… ≥A[n]

→ yields : A[m] ≥ A[m+1]≥ A[m+2]≥… ≥A[n] (line 27)if condition (m<n) on line 10 was false, combining the falsehood (m<n) with precondition (m≤n) on line 4, implies (m=n) must have been true.A[m:m] only have 1 item, so there are no items to sort.

Page 45: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 45

Finishing the proof

This completes our proof that SelectionSort correctly rearranges the items in A[m:n] into decending order :

A[m] ≥ A[m+1]≥ A[m+2]≥… ≥A[n]

Page 46: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 46

Recursion Induction

We used a second form of induction in constructing the proof of SelectionSort. This sometimes called RECURSION INDUCTIONThis Method is applied by assuming that recursive calls on smaller-sized subproblem within the text of the main outer recursive function, correctly solve the smaller subproblem

Page 47: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 47

A subtle bug

Incomplete assertions !

Is this program a correct sorting algorithm?

Page 48: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 48

Review questions

1. What is an assertion inside a program ?2. What is a precondition ?3. What is a postcondition ?4. What is a correctness proof for a

program?

Page 49: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 49

Transforming & Optimizing Programs

Page 50: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 50

Transforming & Optimizing Programs

Transformation is applied in order to improve the program efficiency→ maksudnya improve dalam running time (secara real time) dan real memory, bukan improve dari sisikompleksitas

Program Transformations exchange the laws that permit us to replace one form of a program with an equivalent form

Page 51: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 51

Transforming & Optimizing Programs

Example:If condition (i<n) is simpler than (i <= n && i != n)Polynomial ((x + 3)*x + 5)*x + 2 is simpler than x*x*x + 3*x*x + 5*x + 2

Page 52: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 52

Recursion elimination

Recursion elimination is converting a recursive program to an equivalent iterative program.

Transferring the parametersMaking callReturning from the call

Page 53: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 53

Recursion elimination

Tail recursion

[Program A]

Page 54: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 54

Recursion elimination

Tail recursion elimination transformation converts the if-statement into a while loop and replace the tail recursion with assignment statements that assign the actual parameter expressions to be values of the formal parameters of the function

Page 55: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 55

Tail Recursion Elimination

[Program Transformation B]

Page 56: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 56

Tail Recursion EliminationSelection Sorting after Tail Recursion Elimination

[Program C]

Page 57: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 57

Tail Recursion Elimination

Selection Sorting after Useless Assignment Elimination

Page 58: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 58

Iterative vs Recursive running time

Page 59: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 59

Exercises Eliminate the tail recursion from the following program

void RevString(char *S,int m,int n){

char c;

if (m<n){c=S[m];S[m]=S[n];S[n]=c;RevString(S,m+1,n-1);

}}

Page 60: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 60

Algorithm Efficiency

Page 61: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 61

Efisiensi Algoritma

Pertimbangan Memilih algoritma :Kebenaran

Tepat guna (efektif)output sesuai dengan inputnyasesuai dengan permasalahan

Kemudahan/ kesederhanaanUntuk dipahamiUntuk diprogram (proses coding)

Page 62: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 62

Efisiensi Algoritma

Kecepatan AlgoritmaBerkaitan dengan kecepatan eksekusi program

Hemat Biayamengacu pada kebutuhan memory

Page 63: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 63

Efisiensi Algoritma

Keempatnya sulit dicapai bersamaan, dan biasanyayang diutamakan adalah efisiensi (cepat dan hemat)Analisis Algoritma : menganalisis efisiensi algoritma, yang mencakup :

efisiensi waktu (kecepatan) → banyaknya operasiyang dilakukanefisiensi memori → struktur data dan variabels yang digunakan

Page 64: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 64

Efisiensi Algoritma

Algoritma yang bagus adalah algoritmayang efisienAlgoritma yang efisien ialah algoritmayang meminimumkan kebutuhan waktudan ruang/memori. Kebutuhan waktu dan ruang suatualgoritma bergantung pada ukuranmasukan (n), yang menyatakan jumlahdata yang diproses.

Page 65: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 65

Efisiensi Algoritma

Mana yang lebih baik : menggunakanalgoritma yang waktu eksekusinya cepatdengan komputer standard ataumenggunakan algoritma yang waktunyatidak cepat tetapi dengan komputer yang cepat?

Page 66: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 66

Efisiensi Algoritma

Misal dipunyai :Algoritma dengan waktu eksekusi dalam orde2n

Sebuah komputer dengan kecepatan 10-4 x 2n

n=10 → 1/10 detikn=20 → 2 menitn=30 → lebih dari satu harin=38 → 1 tahun

Page 67: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 67

Efisiensi Algoritma

Misal dipunyai :Algoritma dengan waktu eksekusi dalam orde2n

Sebuah komputer dengan kecepatan 10-6 x 2n

n=45 → 1 tahun

Page 68: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 68

Efisiensi Algoritma

Misal dipunyai :Algoritma dengan waktu eksekusi dalam orden3

Sebuah komputer dengan kecepatan 10-4 x n3

n=900 → 1 harin=6800 → 1 tahun

Page 69: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 69

Mengapa kita memerlukan algoritma yang efisien? Lihat grafik di bawah ini.

105 15 20 25 30 35 40

Ukuran masukan

10

102

103

104

105

11 detik

1 menit

1 jam

1 hari

Wak

tu k

ompu

tasi

(dal

am d

etik

)

10-1

10-4 x 2n

10-6 x n3

10-6 x 2n

10-4 x n3

Page 70: Introduction to Design & Analysis of Algorithmsphg-simulation-laboratory.com/.../uploads/2016/01/M01Introduction2.pdf · Introduction to Design & Analysis of Algorithms Matakuliah

ZKA-STT Telkom Bandung 70

Reference

Munir,Rinaldi. Diktat Strategi Algoritmik . Departemen Teknik Informatika, ITBLevitin, Anany. Introduction to the design and analysis of algorithm. Addison WesleyStandish, Thomas A. Data structures, Algorithms, & Software Principles in C. Addison wesley publishing company. 1995