Struktur Data & Algoritme (Data Structures & Algorithms)

dokumen-dokumen yang mirip
Algoritme. Struktur Data & Algoritme (Data Structures & Algorithms) Expected Outcome. Analisa Algoritme: motivasi. Analisa Algoritme: what?

Struktur Data & Algoritme (Data Structures & Algorithms)

Struktur Data & Algoritma

ANALISA RUNNING TIME

Objectives. Struktur Data & Algoritme (Data Structures & Algorithms) Sort. Outline. Bubble Sort: idea. Bubble Sort. Sorting

Recursion Properties. Factorial: MaxSubseqSum Recursive version. MaxSubseqSum Recursive version

Struktur Data & Algoritme (Data Structures & Algorithms)

Objectives. Struktur Data & Algoritme (Data Structures & Algorithms) Outline. Apa itu Recursion? Recursion

Outline. Struktur Data & Algoritme (Data Structures & Algorithms) Pengantar. Definisi. 2-3 Trees

Algoritma dan Struktur Data

1/5. while and do Loops The remaining types of loops are while and do. As with for loops, while and do loops Praktikum Alpro Modul 3.

Design and Analysis of Algorithm

Struktur Data dan Algoritma

Adam Mukharil Bachtiar English Class Informatics Engineering Algorithms and Programming Searching

Struktur Data & Algoritme (Data Structures & Algorithms)

Struktur Data dan Algoritme. Struktur Data & Algoritme (Data Structures & Algorithms) Struktur Data dan Algoritme. Objectives.

Simple Sorting Techniques

Design and Analysis Algorithm

Pendahuluan. Ukuran input (input s size)

Pengantar Analisa Algoritma

Review. Struktur Data & Algoritme (Data Structures & Algorithms) Objectives. Outline. Stacks & Queues

Nama Soal Pembagian Ring Road Batas Waktu 1 detik Nama Berkas Ringroad[1..10].out Batas Memori 32 MB Tipe [output only] Sumber Brian Marshal

Rekursif. Proses yang memanggil dirinya sendiri. Merupakan suatu fungsi atau prosedur Terdapat suatu kondisi untuk berhenti.

Struktur Data dan Algoritma

SORTING (BAGIAN II) Proses kelima

Pemrograman Lanjut. Interface

Analisa dan Perancangan Algoritma. Ahmad Sabri, Dr Sesi 1: 9 Mei 2016

Sorting Algorithms. Algoritma dan Struktur Data. Sorting algorithms

Analisis Algoritma Bubble Sort

Arsitektur Komputer. Pertemuan ke-2 - Aritmatika Komputer >>> Sistem bilangan & Format Data - Perkembangan Perangkat Keras Komputer

Outline STRUKTUR DATA. VII. Sorting

Design and Analysis Algorithm. Ahmad Afif Supianto, S.Si., M.Kom. Pertemuan 04

Sorting. Struktur Data dan Algoritma. Suryana Setiawan, Ruli Manurung & Ade Azurat (acknowledgments: Denny) Fasilkom UI

ORGANISASI DAN ARSITEKTUR KOMPUTER

Struktur Data & Algoritme (Data Structures & Algorithms)

Rahasia Cermat & Mahir Menguasai Akuntansi Keuangan Menengah (Indonesian Edition)

ANALISIS ALGORITMA BINARY SEARCH

Program Attributes. Algorithm Analysis. Time Consumption. Algoritma

ANALISIS CAPAIAN OPTIMASI NILAI SUKU BUNGA BANK SENTRAL INDONESIA: SUATU PENGENALAN METODE BARU DALAM MENGANALISIS 47 VARIABEL EKONOMI UNTU

Organisasi Sistem Komputer

TEKNIK KOMPUTASI TEI 116/A. Jurusan Teknik Elektro dan Teknologi Informasi Universitas Gadjah Mada 2011

E VA D A E L U M M A H K H O I R, M. A B. P E R T E M U A N 2 A N A

Design and Analysis of Algorithm

Melakukan Operasi Logika

Easy & Simple - Web Programming: Belajar Pemprograman Website Secara Efektif dan Efisien (Indonesian Edition)

32-bit and 64-bit Windows: Frequently asked questions

KESASTRAAN MELAYU TIONGHOA DAN KEBANGSAAN INDONESIA: JILID 2 FROM KPG (KEPUSTAKAAN POPULER GRAMEDIA)

STRATEGI DIVIDE AND CONQUER

Keseimbangan Torsi Coulomb

Data Structures. Class 4 Arrays. Pengampu : TATI ERLINA, M.I.T. Copyright 2006 by The McGraw-Hill Companies, Inc. All rights reserved.

UJIAN AKHIR SEMESTER GANJIL 2005/2006 ALGORITMA & STRUKTUR DATA / CS2014

Algoritma Pencarian Blind. Breadth First Search Depth First Search

LINEAR PROGRAMMING-1

MANAJEMEN RISIKO 1 (INDONESIAN EDITION) BY IKATAN BANKIR INDONESIA

Statistik Bisnis 1. Week 9 Discrete Probability

Rekursif. Overview. Tujuan Instruksional

Membangun Menara karakter (Indonesian Edition)

Line VS Bezier Curve. Kurva Bezier. Other Curves. Drawing the Curve (1) Pertemuan: 06. Dosen Pembina Danang Junaedi Sriyani Violina IF-UTAMA 2

Practical Communication Skill: dalam Bisnis, Organisasi, dan Kehidupan (Indonesian Edition)

4. Seorang tukang sablon membuat nomor dada pada 100 kaos mulai dari nomor 1 sampai dengan nomor 251. Banyaknya angka 0 yang ia buat adalah...

SUKSES BERBISNIS DI INTERNET DALAM 29 HARI (INDONESIAN EDITION) BY SOKARTO SOKARTO

ABSTRACT. Key words: differential accounting, net present value method (NPV), payback period method, decision making. Universitas Kristen Maranatha

6 KERANJANG 7 LANGKAH API (INDONESIAN EDITION) BY LIM TUNG NING

SIMULASI MONTE CARLO RISK MANAGEMENT DEPARTMENT OF INDUSTRIAL ENGINEERING

SORTING DENGAN ALGORITMA DIVIDE AND CONQUER

Sistem Informasi. Soal Dengan 2 Bahasa: Bahasa Indonesia Dan Bahasa Inggris

Secuil Cerita tentang Facebook Hacker Cup 2012 Qualification Round [Part II]

Setelah mempelajari topik Analisis Algoritma di kuliah SDA, ada beberapa kompetensi yang perlu Anda kuasai:

Bab 2. Instructions: Bahasa dari Komputer

Sorting Algorithms. Buble Sort

ABSTRACT. Keyword: Algorithm, Depth First Search, Breadth First Search, backtracking, Maze, Rat Race, Web Peta. Universitas Kristen Maranatha

Data Structures. Class 5 Pointer. Copyright 2006 by The McGraw-Hill Companies, Inc. All rights reserved.

Analisis Kecepatan Sorting Dengan Notasi Big O

Problem 1. Implementasi Interface. Diberikan definisi interface BentukDuaDimensi berikut:

ANALISIS KINERJA MANAJEMEN (INDONESIAN EDITION) BY HERY HERY

Langkah Awal menuju Analisis Kompleksitas Algoritma

PAM 253 PERSAMAAN DIFERENSIAL BIASA Topik: Pendahuluan. Mahdhivan Syafwan

Relasi Negara & Agama: Redefinisi Diskursus Konstitusionalisme (Rangkaian Studi IMR)

DAFTAR LAMPIRAN. Wawancara Terhadap Bidang Standarisasi pada. LSP LSK TKI Sektor Tata Laksana Rumah Tangga : Ibu Etty. Meindrati

BAB III ANALISIS KOMPLEKSITAS ALGORITMA

Langkah Awal menuju Analisis Kompleksitas Algoritma

Informatics Class A UISI CALCULUS I WEEK 2 DAY 2

1-x. dimana dan dihubungkan oleh teorema Pythagoras.

Dynamic Programming Matakuliah Desain & Analisis Algoritma (CS 3024) ZK Abdurahman Baizal STT Telkom Bandung

Membangun Menara karakter (Indonesian Edition)

Simulation. Prepared by Akhid Yulianto, SE, MSC (Log) Based on Anderson, Sweeney, and Williams Thomson ΤΜ /South-Western Slide

TIF APPLIED MATH 1 (MATEMATIKA TERAPAN 1) Week 3 SET THEORY (Continued)

Virtual Memory Ch. 9. Virtual Memory. SISTIM OPERASI (Operating System) IKI Johny Moningka

Struktur Data & Algoritma

Analisa Algoritma. Konsep Algoritma

Kompleksitas Algoritma

ADLN-Perpustakaan Universitas Airlangga

ABSTRAK. Kata Kunci: optimasi, pengiriman, pengepakan barang, pivoting, algoritma best-fit decreasing. Universitas Kristen Maranatha

GROWTH AND UNDERINVESTMENT PROGRAM STUDI ADMINISTRASI BISNIS UNIVERSITAS BRAWIJAYA

ADDING RTGS BENEFICIARY FOR CHECKER MAKER SYSTEM

Logika dan Algoritma Yuni Dwi Astuti, ST 2

MODULE 1 GRADE XI VARIATION OF EXPRESSIONS

MANAJEMEN PROYEK LANJUT

The Top 10 Leadership Principles. Maximize Your Leadership Potential

Transkripsi:

Struktur Data & Algoritme (Data Structures & Algorithms) Analisa Algoritme Denny (denny@cs.ui.ac.id) Suryana Setiawan (setiawan@cs.ui.ac.id) Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2004/2005 Version 2.0 - Internal Use Only Algoritme al go rithm n. 1 Math. a) any systematic method of solving a certain kind of problem b) the repetitive calculations used in finding the greatest common divisor of two numbers (called in full Euclidean algorithm) 2 Comput. a predetermined set of instructions for solving a specific problem in a limited number of steps Suatu set instruksi yang harus diikuti oleh computer untuk memecahkan suatu masalah. Program harus berhenti dalam batas waktu yang wajar (reasonable) Tidak terikat pada programming language atau bahkan paradigma pemrograman (mis. Procedural vs Object-Oriented) SDA/ALG/V2.0/2 1

Analisa Algoritme: motivasi perlu diketahui berapa banyak resource (time & space) yang diperlukan oleh sebuah algoritme Menggunakan teknik-teknik untuk mengurangi waktu yang dibutuhkan oleh sebuah algoritme SDA/ALG/V2.0/3 Expected Outcome mahasiswa dapat memperkirakan waktu yang dibutuhkan sebuah algoritme mahasiswa memahami teknik-teknik yang secara drastis menurunkan running time mahasiswa memahami kerangka matematik yang menggambarkan running time SDA/ALG/V2.0/4 2

Outline Apa itu analisa algoritme?- what Bagaimana cara untuk analisa/mengukur? - how Notasi Big-Oh Case Study: The Maximum Contiguous Subsequence Sum Problem Algorithm 1: A cubic algorithm Algorithm 2: A quadratic algorithm Algorithm 3: An N log N algorithm Algorithm 4: A linear algorithm SDA/ALG/V2.0/5 Analisa Algoritme: what? Mengukur jumlah sumber daya (time dan space) yang diperlukan oleh sebuah algoritme Waktu yang diperlukan (running time) oleh sebuah algoritme cenderung tergantung pada jumlah input yang diproses. Running time dari sebuah algoritme adalah fungsi dari jumlah inputnya Selalu tidak terikat pada platform (mesin + OS), bahasa pemrograman, kualitas kompilator atau bahkan paradigma pemrograman (mis. Procedural vs Object-Oriented) SDA/ALG/V2.0/6 3

Analisa Algoritme: how? Bagaimana jika kita menggunakan jam? Jumlah waktu yang digunakan bervariasi tergantung pada beberapa faktor lain: kecepatan mesin, sistem operasi (multi-tasking), kualitas kompiler, dan bahasa pemrograman. Sehingga kurang memberikan gambaran yang tepat tentang algoritme Wall-Clock time CPU time Process 1: Process 2: Process 3: Idle: Wall-Clock time SDA/ALG/V2.0/7 Analisa Algoritme: how? Notasi O (sering disebut sebagai notasi big-oh ) Digunakan sebagai bahasa untuk membahas efisiensi dari sebuah algoritme: log n, linier, n log n, n 2, n 3,... Dari hasil run-time, dapat kita buat grafik dari waktu eksekusi dan jumlah data. SDA/ALG/V2.0/8 4

Analisa Algoritme: how? Contoh: Mencari elemen terkecil dalam sebuah array Algoritme: sequential scan / linear search Orde-nya: O(n) linear public static int smallest (int a[]) { // assert (length of array > 0); int elementerkecil = a[0]; for (ii = 1; ii < a.length; ii++) { if (a[ii] < elementerkecil) { elementerkecil = a[ii]; return elementerkecil ; SDA/ALG/V2.0/9 Analisa Algoritme: how? running time worst case average case best case input size n SDA/ALG/V2.0/10 5

Big Oh: contoh Sebuah fungsi kubic adalah sebuah fungsi yang suku dominannya (dominant term) adalah sebuah konstan dikalikan dengan n 3. Contoh: 10 n 3 + n 2 + 40 n + 80 n 3 + 1000 n 2 + 40 n + 80 n 3 + 10000 SDA/ALG/V2.0/11 Dominant Term Mengapa hanya suku yang memiliki pangkat tertinggi/dominan saja yang diperhatikan? Untuk n yang besar, suku dominan lebih mengindikasikan perilaku dari algoritme. Untuk n yang kecil, suku dominan tidak selalu mengindikasikan perilakunya, tetapi program dengan input kecil umumnya berjalan sangat cepat sehingga kita tidak perlu perhatikan. SDA/ALG/V2.0/12 6

Big Oh: issues Apakah fungsi linier selalu lebih kecil dari fungsi kubic? Untuk n yang kecil, bisa saja fungsi linier > fungsi kubic Tetapi untuk n yang besar, fungsi kubic > fungsi linier Contoh: f(n) = 10 n 3 + 20 n + 10 g(n) = 10000 n + 10000 n = 10, f(n) = 10.210, g(n) = 110.000 f(n) < g(n) n = 100, f(n) = 10.002.010, g(n) = 1.010.000 f(n) > g(n) Mengapa nilai konstan/koefesien pada setiap suku tidak diperhatikan? Nilai konstan/koefesien tidak berarti pada mesin yang berbeda Big Oh digunakan untuk merepresentasikan laju pertumbuhan (growth rate) SDA/ALG/V2.0/13 Orde Fungsi Running-Time Function Name c Constant log N Logarithmic log 2 N Log-squared N Linear N log N N log N N 2 Quadratic N 3 Cubic 2 N Exponential N! Factorial SDA/ALG/V2.0/14 7

Fungsi Running-Time Untuk input yang sedikit, beberapa fungsi lebih cepat dibandingkan dengan yang lain. SDA/ALG/V2.0/15 Fungsi Running-Time (2) Untuk input yang besar, beberapa fungsi runningtime sangat lambat - tidak berguna. SDA/ALG/V2.0/16 8

Contoh Algoritme Mencari dua titik yang memiliki jarak terpendek dalam sebuah bidang (koordinat X-Y) Masalah dalam komputer grafis. Algoritme brute force: hitung jarak dari semua pasangan titik cari jarak yang terpendek Jika jumlah titik adalah n, maka jumlah semua pasangan adalah n * (n - 1) / 2 Orde-nya: O(n 2 ) - kuadratik Ada solusi yang O(n log n) bahkan O(n) dengan cara algoritme lain. SDA/ALG/V2.0/17 Contoh Algoritme Tentukan apakah ada tiga titik dalam sebuah bidang yang segaris (colinier). Algoritme brute force: periksa semua pasangan titik yang terdiri dari 3 titik. Jumlah pasangan: n * (n - 1) * (n - 2) / 6 Orde-nya: O(n 3 ) - kubik. Sangat tidak berguna untuk 10.000 titik. Ada algoritme yang kuadratik. SDA/ALG/V2.0/18 9

Studi Kasus Mengamati sebuah masalah dengan beberapa solusi. Masalah Maximum Contiguous Subsequence Sum Diberikan (angka integer negatif dimungkinkan) A 1, A 2,, A N, cari nilai maksimum dari (A i + A i+1 + + A j ). maximum contiguous subsequence sum adalah nol jika semua integer adalah negatif. Contoh (maximum subsequences digarisbawahi) -2, 11, -4, 13, -4, 2 1, -3, 4, -2, -1, 6 SDA/ALG/V2.0/19 Brute Force Algorithm (1) Algoritme: Hitung jumlah dari semua sub-sequence yang mungkin Cari nilai maksimumnya Contoh: jumlah subsequence (start, end) (0, 0), (0,1), (0,2),, (0,5) (1,1), (1,2),, (1, 5) (5,5) 0-2 1 11 2-4 3 13 4-4 5 2 SDA/ALG/V2.0/20 10

Brute Force Algorithm (2) public static int maxsubsum1( int [] A ) { int maxsum = 0; for(int ii = 0; ii < A.length; ii++) { for( int jj = ii; jj < A.length; jj++) { int thissum= 0; for(int kk = ii; kk <= jj; kk++) thissum += A[kk]; if( thissum > maxsum ) maxsum = thissum; return maxsum ; -2 11-4 13-4 2 ii jj SDA/ALG/V2.0/21 Analysis Loop of size N inside of loop of size N inside of loop of size N means O(N 3 ), or cubic algorithm. Slight over-estimate that results from some loops being of size less than N is not important. SDA/ALG/V2.0/22 11

Actual Running Time For N = 100, actual time is 0.47 seconds on a particular computer. Can use this to estimate time for larger inputs: T(N) = cn 3 T(10N) = c(10n) 3 = 1000cN 3 = 1000T(N) Inputs size increases by a factor of 10 means that running time increases by a factor of 1,000. For N = 1000, estimate an actual time of 470 seconds. (Actual was 449 seconds). For N = 10,000, estimate 449000 seconds (6 days). SDA/ALG/V2.0/23 How To Improve? Remove a loop; not always possible. Here it is: innermost loop is unnecessary because it throws away information. thissum for next jj is easily obtained from old value of thissum: Need A ii + A ii+1 + + A jj-1 + A jj Just computed A ii +A ii+1 + + A jj-1 What we need is what we just computed + A jj In other words: sum (ii, jj) = sum (ii, jj - 1) + A jj SDA/ALG/V2.0/24 12

The Better Algorithm public static int maxsubsum2( int [ ] A ) { int maxsum = 0; for( int ii = 0; ii < A.length; ii++ ) { int thissum = 0; for( int jj = ii; jj < A.length; jj++ ) { thissum += A[jj]; if( thissum > maxsum ) maxsum = thissum; return maxsum ; SDA/ALG/V2.0/25 Analysis Same logic as before: now the running time is quadratic, or O(N 2 ) As we will see, this algorithm is still usable for inputs in the tens of thousands. Recall that the cubic algorithm was not practical for this amount of input. SDA/ALG/V2.0/26 13

Actual running time For N = 100, actual time is 0.011 seconds on the same particular computer. Can use this to estimate time for larger inputs: T(N) = cn 2 T(10N) = c(10n) 2 = 100cN 2 = 100T(N) Inputs size increases by a factor of 10 means that running time increases by a factor of 100. For N = 1000, estimate a running time of 1.11 seconds. (Actual was 1.12 seconds). For N = 10,000, estimate 111 seconds (= actual). SDA/ALG/V2.0/27 Recursive Algorithm Use a divide-and-conquer approach. Membagi (divide) permasalahan ke dalam bagian yang lebih kecil Menyelesaikan (conquer) masalah per bagian secara recursive Menggabung penyelesaian per bagian menjadi solusi masalah awal SDA/ALG/V2.0/28 14

Recursive Algorithm (2) The maximum subsequence either lies entirely in the first half lies entirely in the second half starts somewhere in the first half, goes to the last element in the first half, continues at the first element in the second half, ends somewhere in the second half. Compute all three possibilities, and use the maximum. First two possibilities easily computed recursively. SDA/ALG/V2.0/29 Computing the Third Case Easily done with two loops; see the code For maximum sum that starts in the first half and extends to the last element in the first half, use a right-to-left scan starting at the last element in the first half. For the other maximum sum, do a left-to-right scan, starting at the first element in the first half. 4-3 5-2 -1 2 6-2 4* 0 3-2 -1 1 7* 5 SDA/ALG/V2.0/30 15

Recursion version static private int maxsumrec (int[] a, int left, int right) { int center = (left + right) / 2; if(left == right) { // Base case return a[left] > 0? a[left] : 0; int maxleftsum = maxsumrec (a, left, center); int maxrightsum = maxsumrec (a, center+1, right); for(int ii = center; ii >= left; ii--) { leftbordersum += a[ii]; if(leftbordersum > maxleftbordersum) maxleftbordersum = leftbordersum;... SDA/ALG/V2.0/31 Recursion Version... for(int jj = center + 1; jj <= right; jj++) { rightbordersum += a[jj]; if(rightbordersum > maxrightbordersum) maxrightbordersum = rightbordersum; return max3 (maxleftsum, maxrightsum, maxleftbordersum + maxrightbodersum); // publicly visible routine (a.k.a driver function) static public int maxsubsum (int [] a) { return maxsumrec (a, 0, a.length-1); SDA/ALG/V2.0/32 16

Coding Details The code is more involved Make sure you have a base case that handles zeroelement arrays. Use a public static driver with a private recursive routine. Recursion rules: Have a base case Make progress to the base case Assume the recursive calls work Avoid computing the same solution twice SDA/ALG/V2.0/33 Analysis Let T( N ) = the time for an algorithm to solve a problem of size N. Then T( 1 ) = 1 (1 will be the quantum time unit; remember that constants don't matter). T( N ) = 2 T( N / 2 ) + N Two recursive calls, each of size N / 2. The time to solve each recursive call is T( N / 2 ) by the above definition Case three takes O( N ) time; we use N, because we will throw out the constants eventually. SDA/ALG/V2.0/34 17

Bottom Line T(1) = 1 = 1 * 1 T(2) = 2 * T(1) + 2 = 4 = 2 * 2 T(4) = 2 * T(2) + 4 = 12 = 4 * 3 T(8) = 2 * T(4) + 8 = 32 = 8 * 4 T(16) = 2 * T(8) + 16 = 80 = 16 * 5 T(32) = 2 * T(16) + 32 = 192 = 32 * 6 T(64) = 2 * T(32) + 64 = 448 = 64 * 7 T(N) = N(1 + log N) = N + N log N = O(N log N) SDA/ALG/V2.0/35 N log N Any recursive algorithm that solves two half-sized problems and does linear non-recursive work to combine/split these solutions will always take O( N log N ) time because the above analysis will always hold. This is a very significant improvement over quadratic. It is still not as good as O( N ), but is not that far away either. There is a linear-time algorithm for this problem. The running time is clear, but the correctness is non-trivial. SDA/ALG/V2.0/36 18

Linear Algorithms Linear algorithm would be best. Remember: linear means O( N ). Running time is proportional to amount of input. Hard to do better for an algorithm. If input increases by a factor of ten, then so does running time. SDA/ALG/V2.0/37 Idea Let A i,j be any sequence with S i,j < 0. If q > j, then A i,q is not the maximum contiguous subsequence. Proof: S i,q = S i,j + S j+1,q S i,j < 0 S i,q < S j+1,q SDA/ALG/V2.0/38 19

The Code - version 1 static public int maximumsubsequencesum3 (int a[]) { int maxsum = 0; int thissum = 0; for (int jj = 0; jj < a.length; jj++) { thissum += a[jj]; if (thissum > maxsum) { maxsum = thissum; else if (thissum < 0) { thissum = 0; return maxsum; SDA/ALG/V2.0/39 The Code - version 2 static public int maximumsubsequencesum3b (int data[]) { int thissum = 0, maxsum = 0; for (int ii = 0; ii < data.length; ii++) { thissum = Max(0, data[ii] + thissum); maxsum = Max(thisSum, maxsum); return maxsum; SDA/ALG/V2.0/40 20

Running Time SDA/ALG/V2.0/41 Running Time: on different machines Cubic Algorithm on Alpha 21164 at 533 Mhz using C compiler Linear Algorithm on Radio Shack TRS-80 Model III (a 1980 personal computer with a Z-80 processor running at 2.03 Mhz) using interpreted Basic n Alpha 21164A, C compiled, Cubic Algorithm TRS-80, Basic interpreted, Linear Algorithm 10 0.6 microsecs 200 milisecs 100 0.6 milisecs 2.0 secs 1,000 0.6 secs 20 secs 10,000 10 mins 3.2 mins 100,000 7 days 32 mins 1,000,000 19 yrs 5.4 hrs SDA/ALG/V2.0/42 21

Running Time: moral story Even the most clever programming tricks cannot make an inefficient algorithm fast. Before we waste effort attempting to optimize code, we need to optimize the algorithm. SDA/ALG/V2.0/43 The Logarithm Algorithm Formal Definition of Logarithm For any B, N > 0, log B N = K, if B K = N. If (the base) B is omitted, it defaults to 2 in computer science. Examples: log 32 = 5 (because 2 5 = 32) log 1024 = 10 log 1048576 = 20 log 1 billion = about 30 The logarithm grows much more slowly than N, and slower than the square root of N. SDA/ALG/V2.0/44 22

Examples of the Logarithm BITS IN A BINARY NUMBER How many bits are required to represent N consecutive integers? REPEATED DOUBLING Starting from X = 1, how many times should X be doubled before it is at least as large as N? REPEATED HALVING Starting from X = N, if N is repeatedly halved, how many iterations must be applied to make N smaller than or equal to 1 (Halving rounds up). Answer to all of the above is log N (rounded up). SDA/ALG/V2.0/45 Why log N? B bits represents 2 B integers. Thus 2 B is at least as big as N, so B is at least log N. Since B must be an integer, round up if needed. Same logic for the other examples. SDA/ALG/V2.0/46 23

Repeated Halving Principle An algorithm is O( log N ) if it takes constant time to reduce the problem size by a constant fraction (which is usually 1/2). Reason: there will be log N iterations of constant work. SDA/ALG/V2.0/47 Linear Search Given an integer X and an array A, return the position of X in A or an indication that it is not present. If X occurs more than once, return any occurrence. The array A is not altered. If input array is not sorted, solution is to use a linear search. Running times: Unsuccessful search: O( N ); every item is examined Successful search: Worst case: O( N ); every item is examined Average case: O( N ); half the items are examined Can we do better if we know the array is sorted? SDA/ALG/V2.0/48 24

Binary Search Yes! Use a binary search. Look in the middle Case 1: If X is less than the item in the middle, then look in the sub-array to the left of the middle Case 2: If X is greater than the item in the middle, then look in the sub-array to the right of the middle Case 3: If X is equal to the item in the middle, then we have a match Base Case: If the sub-array is empty, X is not found. This is logarithmic by the repeated halving principle. The first binary search was published in 1946. The first published binary search without bugs did not appear until 1962. SDA/ALG/V2.0/49 /** Performs the standard binary search using two comparisons per level. @param a the array @param x the key @exception ItemNotFound if appropriate. @return index where item is found. */ public static int binarysearch (Comparable [ ] a, Comparable x ) throws ItemNotFound { int low = 0; int high = a.length - 1; int mid; while( low <= high ) { mid = (low + high) / 2; if (a[mid].compareto (x) < 0) { low = mid + 1; else if (a[mid].compareto (x) > 0) { high = mid - 1; else { return mid; throw new ItemNotFound( "BinarySearch fails" ); SDA/ALG/V2.0/50 25

Binary Search Can do one comparison per iteration instead of two by changing the base case. Save the value returned by a[mid].compareto (x) Average case and worst case in revised algorithm are identical. 1 + log N comparisons (rounded down to the nearest integer) are used. Example: If N = 1,000,000, then 20 element comparisons are used. Sequential search would be 25,000 times more costly on average. SDA/ALG/V2.0/51 Big-Oh Rules (1) Mathematical expression relative rates of growth DEFINITION: (Big-Oh) T(N) = O(F(N)) if there are positive constants c and N 0 such that T(N) c F(N) when N N 0. DEFINITION: (Big-Omega) T(N) = Ω(F(N)) if there are constants c and N 0 such that T(N) c F(N) when N N 0. DEFINITION: (Big-Theta) T(N) = Θ(F(N)) if and only if T(N) = O(F(N)) and T(N) = Ω(F(N)). DEFINITION: (Little-Oh) T(N) = o(f(n)) if and only if T(N) = O(F(N)) and T(N) Θ(F(N)). SDA/ALG/V2.0/52 26

Big-Oh Rules (2) Meanings of the various growth functions T(N) = O(F(N)) T(N) = Ω(F(N)) T(N) = Θ(F(N)) T(N) = o(f(n)) Jika ada lebih dari satu parameter, maka aturan tersebut berlaku untuk setiap parameter. 4 n log(m) + 50 n 2 + 500 m + 1853 O(n log(m) + n 2 + m) 4 m log(m) + 50 n 2 + 500 m + 853 O(m log(m) + n 2 ) Growth of T(N) is growth of F(N) Growth of T(N) is growth of F(N) Growth of T(N) is = growth of F(N) Growth of T(N) is < growth of F(N) SDA/ALG/V2.0/53 Big-Oh Rules (3) Jika ada lebih dari satu parameter, maka aturan tersebut berlaku untuk setiap parameter. 4 n log(m) + 50 n 2 + 500 m + 1853 O(n log(m) + n 2 + m) 4 m log(m) + 50 n 2 + 500 m + 853 O(m log(m) + n 2 ) SDA/ALG/V2.0/54 27

Summary more data means the program takes more time Big-Oh tidak dapat digunakan untuk N yang kecil SDA/ALG/V2.0/55 Exercises Urutkan fungsi berikut berdasarkan laju pertumbuhan (growth rate) N, N, N 1.5, N 2, N log N, N log log N, N log 2 N, N log (N 2 ), 2/N, 2 N, 2 N/2, 37, N 3, N 2 log N A 5 + B 5 + C 5 + D 5 + E 5 = F 5 0 < A B C D E F 75 hanya memiliki satu solusi. berapa? SDA/ALG/V2.0/56 28

Further Reading Chapter 5 http://telaga.cs.ui.ac.id/webkuliah/iki101 00/resources/AlgDesignManual/INDEX.HTM SDA/ALG/V2.0/57 What s Next Sorting SDA/ALG/V2.0/58 29