m 01 introduction 2
Post on 15-Jul-2016
13 Views
Preview:
DESCRIPTION
TRANSCRIPT
Introduction to Design & Analysis of Algorithms
Matakuliah Desain & Analisis Algoritma(CS 3024)
ZK Abdurahman BaizalSTT Telkom Bandung
ZKA-STT Telkom Bandung 2
Algorithm is a sequence of un ambiguous instructions for solving a problem (Levitin)
Problem
Algorithm
Computerinput output
ZKA-STT Telkom Bandung 3
Isi dari algoritma :AssignmentSubroutine callSequencePencabanganLooping
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
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
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
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)
ZKA-STT Telkom Bandung 8
Important Problem Types (cont)
Graph bisa sebagai pemodelan daritransportation and communicaton networks, project schedulling, games, dsb
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
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
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
ZKA-STT Telkom Bandung 12
Introduction to Software Engineering Concepts
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
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
ZKA-STT Telkom Bandung 15
Top Down Programming
Consider the following example of a scratch-off lottery ticket
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
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”
ZKA-STT Telkom Bandung 18
Process of Top Down Programming
Refinement stepsMaking program strategy describing top level goals
[ Program Strategy 1]
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 ]
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 !
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
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
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]
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 ]
ZKA-STT Telkom Bandung 25
Choosing Data structure for the table
typedef struct {int Amount;int Repetitions;
} RowStruct;
RowStruct Table[6];
ZKA-STT Telkom Bandung 26
Choosing Data structure for the table
Refinement steps of Program Strategy 1.2
[ Program 1.2.2 ]
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 ]
ZKA-STT Telkom Bandung 28
Choosing Data structure for the tableRefinement steps of Program Strategy 1.3
[ Program 1.3.1 ]
ZKA-STT Telkom Bandung 29
Choosing Data structure for the tableRefinement steps of Program Strategy 1
ZKA-STT Telkom Bandung 30
Entire Program for Finding a Lottery Winner
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”
ZKA-STT Telkom Bandung 32
Proving Programs Correct
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}
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]}
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
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*/
ZKA-STT Telkom Bandung 37
Finding the position of the largest item
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
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
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
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 :
ZKA-STT Telkom Bandung 42
Finishing the proof
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)
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.
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]
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
ZKA-STT Telkom Bandung 47
A subtle bug
Incomplete assertions !
Is this program a correct sorting algorithm?
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?
ZKA-STT Telkom Bandung 49
Transforming & Optimizing Programs
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
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
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
ZKA-STT Telkom Bandung 53
Recursion elimination
Tail recursion
[Program A]
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
ZKA-STT Telkom Bandung 55
Tail Recursion Elimination
[Program Transformation B]
ZKA-STT Telkom Bandung 56
Tail Recursion EliminationSelection Sorting after Tail Recursion Elimination
[Program C]
ZKA-STT Telkom Bandung 57
Tail Recursion Elimination
Selection Sorting after Useless Assignment Elimination
ZKA-STT Telkom Bandung 58
Iterative vs Recursive running time
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);
}}
ZKA-STT Telkom Bandung 60
Algorithm Efficiency
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)
ZKA-STT Telkom Bandung 62
Efisiensi Algoritma
Kecepatan AlgoritmaBerkaitan dengan kecepatan eksekusi program
Hemat Biayamengacu pada kebutuhan memory
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
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.
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?
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
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
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
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
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
top related