• Tidak ada hasil yang ditemukan

Design and Analysis of Algorithm

N/A
N/A
Protected

Academic year: 2021

Membagikan "Design and Analysis of Algorithm"

Copied!
46
0
0

Teks penuh

(1)

Design and Analysis of Algorithm

Week 1: Introduction

Dr. Putu Harry Gunawan1

1Department of Computational Science School of Computing

(2)

Outline

1 Introduction

About this course Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(3)

Outline

1 Introduction

About this course

Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(4)

Goals

Sebelum UTS:

1 Mahasiswa mampu untuk mejelaskan kompleksitas waktu pada suatu

algoritma

2 Mahasiswa mampu menganalisis kompleksitas suatu algoritma.

Setelah UTS:

1 Mahasiswa mampu membedakan tipe dan karakteristik

masing-masing algoritma.

2 Mahasiswa mampu merancang suatu algoritma berdasarkan

contoh-contoh algoritma yang sudah diberikan.

(5)

References

(6)

Grading

TUGAS 15% KUIS 20% UTS 25% TUGAS BESAR 15% UAS 25%

NB: Bonus jika absensi mencapai 100%

(Membuat buku tugas yang akan dikumpul setiap pengumpulan tugas)

(7)

Outline

1 Introduction

About this course

Beginning of Algorithm

The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(8)

What is 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.

(9)

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.

Can be represented various forms Unambiguity/clearness

Effectiveness

Finiteness/termination Correctness

(10)

Example of computational domain

Statement of problem:

Input: A sequence of n numbers<a1,a2,· · · ,an>

Output: A reordering of the input sequence<a01,a20,· · ·,a0n>so that a0i ≤a0j wheneveri<j

Instance: The sequence<5,3,2,8,3>

Algorithms:

Selection sort Insertion sort Merge sort (many others)

(11)

Some well-known of computational domain

Sorting Searching

Shortest paths in a graph Minimum spanning tree Primality testing

Traveling salesman problem Knapsack problem

Chess

Towers of Hanoi Program termination

(12)

Basic Issues Related to Algorithms

How to design algorithms How to express algorithms Proving correctness

Efficiency (or complexity) analysis

Theoretical analysis Empirical analysis

Optimality

(13)

Algorithms and design strategies

Brute force

Divide and conquer Decrease and conquer Transform and conquer Greedy approach Dynamic programming

Backtracking and branch-and-bound Space and time tradeoffs

(14)

Analysis algorithms

How good is the algorithm?

Correctness Time efficiency Space efficiency

Does there exist a better algorithm?

Lower bounds Optimality

(15)

Euclid’s algorithm

Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand n

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

Euclids algorithm is based on repeated application of equality

gcd(m,n) =gcd(n,m mod n) until the second number becomes 0, which makes the problem trivial.

(16)

Euclid’s algorithm

Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand n

Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclids algorithm is based on repeated application of equality

gcd(m,n) =gcd(n,m mod n) until the second number becomes 0, which makes the problem trivial.

Example: gcd(60,24)

= gcd(24,12) = gcd(12,0) = 12

(17)

Euclid’s algorithm

Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand n

Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclids algorithm is based on repeated application of equality

gcd(m,n) =gcd(n,m mod n) until the second number becomes 0, which makes the problem trivial.

(18)

Euclid’s algorithm

Exercise: Make an algorithm for computing the gcd(m,n)!

Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value of 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 n6= 0 do

r ←m mod n m←n n ←r

return m

(19)

Euclid’s algorithm

Exercise: Make an algorithm for computing the gcd(m,n)! Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value of 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 n6= 0 do

r ←m mod n m←n n ←r

(20)

Another method to compute gcd

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

Is this slower than Euclids algorithm? How much slower?

O(n), if n≤m , vsO(logn)

(21)

Another method to compute gcd

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

Is this slower than Euclids algorithm? How much slower?

(22)

Another method to compute gcd

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

Is this slower than Euclids algorithm? How much slower?

O(n), if n≤m , vsO(logn)

(23)

Outline

1 Introduction

About this course Beginning of Algorithm

The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(24)

Efficiency

Pertimbangan Memilih algoritma : Kebenaran

Tepat guna (efektif)

output sesuai dengan inputnya sesuai dengan permasalahan

Kemudahan/ kesederhanaan

Untuk dipahami

Untuk diprogram (proses coding)

(25)

Efficiency

Pertimbangan Memilih algoritma : Kecepatan Algoritma:

Berkaitan dengan kecepatan eksekusi program Hemat Biaya:

(26)

Efficiency

Keempatnya sulit dicapai bersamaan, dan biasanya yang diutamakan adalah efisiensi (cepat dan hemat)

Analisis Algoritma : menganalisis efisiensi algoritma, yang mencakup :

efisiensi waktu (kecepatan)→banyaknya operasi yang dilakukan efisiensi memori→struktur data dan variabels yang digunakan

(27)

Efficiency

Algoritma yang bagus adalah algoritma yang efisien

Algoritma yang efisien ialah algoritma yang meminimumkan kebutuhan waktu dan ruang/memori.

Kebutuhan waktu dan ruang suatu algoritma bergantung pada ukuran masukan (n), yang menyatakan jumlah data yang diproses.

(28)

Efficiency

Question:

Mana yang lebih baik : menggunakan algoritma yang waktu eksekusinya cepat dengan komputer standard atau menggunakan algoritma yang waktunya tidak cepat tetapi dengan komputer yang cepat?

¡Watch Video¿

(29)

Efficiency

Misal dipunyai :

Algoritma dengan waktu eksekusi dalam orde 2n Sebuah komputer dengan kecepatan 10−4×2n

n= 10→1/10 detik

n= 20→2 menit

n= 30→lebih dari satu hari

(30)

Efficiency

Misal dipunyai :

Algoritma dengan waktu eksekusi dalam orde 2n Sebuah komputer dengan kecepatan 10−6x2n

n= 45→1 tahun

(31)

Efficiency

Misal dipunyai :

Algoritma dengan waktu eksekusi dalam orde n3

Sebuah komputer dengan kecepatan 10−4xn3 n= 900→1 hari

(32)

Efficiency

Misal dipunyai :

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

(33)

Outline

1 Introduction

About this course Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(34)

Rules

Make a group

Each group consists of 5 or more peoples Choose a leader

Prepare the answer sheet

(35)

Outline

1 Introduction

About this course Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(36)

Problem 1

Buatlah algoritma untuk membuat Jus Nanas!

Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure

(37)

Problem 1

Buatlah algoritma untuk membuat kopi!

Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure

(38)

Problem 2

Buatlah algoritma untuk membuat Sambal Balado!

Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure

(39)

Problem 2

Buatlah algoritma untuk membuat Lumpia Basah!

Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure

(40)

Outline

1 Introduction

About this course Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(41)

Problem 3

Given the following algorithm:

(42)

Outline

1 Introduction

About this course Beginning of Algorithm The efficiency of algorithm

2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks

(43)

Problem 4

Design a algorithm that reads as its inputs the (x,y) coordinates of the endpoints of two line segments P1,Q1 and P2,Q2 and determines

(44)

Problem 5

Design an algorithm for the following problem: Given a set ofn points in the Cartesian plane, determine whether all of them lie on the same circumference.

(45)

Homeworks

Compare two algorithms of shorting problem, give an analysis and a comment for the each algorithms.

Compare two algorithms of searching problem, give an analysis and a comment for the each algorithms.

Prove the following relations

1 1 + 2 + 3 +· · ·+n=n(n+1)

2

2 1 + 3 + 5 +· · ·+ (2n1) =n2 3 ifn∈ Z andn0, thenPn

i=0i·i! = (n+ 1)!−1

Compute the following sums.

1 Pn+1 i=3 1 2 Pn+1 i=3 i 3 Pn−1 i=0 i(i+ 1) 4 Pn i=03j+1 5 Pn i=1 Pn j=1ij

(46)

The end of week 1

Thank you for your attention!

Gambar

Figure : Main reference.

Referensi

Dokumen terkait

Acara dihadiri oleh Dekan FKG UNAIR dan disaksikan oleh seluruh Civitas akademika, Dalam sambutannya Dekan menegaskan walau RSGM bersifat mandiri, namun FKG UNAIR akan

Berdasarkan hasil uji hipotesis pada dimensi setiap aspek keterampilan psikologis yang ditinjau dari posisi dapat diketahui bahwa kesimpulan dari uji perbedaan setiap dimensi

Pemerintah Amerika Serikat telah melakukan penggunaan wacana ancaman dan bahaya dalam berbagai kasus : “ancaman merah” penduduk asli Indian-Amerika yang mengancam

Hasil dari penelitian ini adalah pemberitaan di Jawa Pos mengenai Kekacauan Pelaksanaan Ujian Nasional SMA 2013 di Jawa Post Periode 16 – 19 April 2013 masih

PROGRAM STUDI PENDIDIKAN MATEMATIKA FAKULTAS KEGURUAN DAN ILMU PENDIDIKAN UNIVERSITAS MUHAMMADIYAH SURABAYA 2016..

Dengan demikian, kesimpulan dari hasil penelitian ini adalah terdapat hubungan korelasi yang sangat rendah antara kompetensi manajerial kepala sekolah dengan mutu

a) Investasi mudharabah setelsah di kurangi penyisihan kerugian investasi. b) Pengembalian investasi mudharabah diakui sebagai keuntungan atau kerugian.. 3)

Pada dasarnya penyebab kebisingan pada mesin TFO adalah mesin rusak dan material input,dan kemudian dilakukan analisis kebisingan dengan menggunakan Fault Tree