Design and Analysis of Algorithm Decrease and Conquer








































































- Slides: 72

Design and Analysis of Algorithm Decrease and Conquer Algorithm Aryo Pinandito, ST, M. MT – PTIIK Universitas Brawijaya

Decrease and Conquer � Mengurangi permasalahan menjadi lebih kecil pada permasalahan yang sama � Selesaikan permasalahan yang lebih kecil tersebut � Kembangkan permasalahan yang lebih kecil itu sehingga menyelesaikan permasalahan sebenarnya � Dapat dilakukan dengan metode top down atau bottom up

Variasi decrease and conquer � Mengurangi constant) dengan sebuah constant (Decrease by a � Ukuran dari masalah dikurangi dengan faktor konstanta yang sama – biasanya dikurangi sebanyak dua pada setiap iterasi � Pola pengurangan ukuran masalah bervariasi/berbeda antara iterasi satu dengan yang lainnya

Decrease and Conquer � Decrease by a constant (usually by 1): � � � Decrease by a constant factor (usually by half) � � binary search and bisection method exponentiation by squaring multiplication à la russe Variable-size decrease � � insertion sort graph traversal algorithms (DFS and BFS) topological sorting algorithms for generating permutations, subsets Euclid's algorithm Selection by partition Nim-like games Biasanya menggunakan algoritma rekursif.

Permasalahan Eksponensial � Permasalahan: � Brute Force: � Divide Hitung xn n-1 multiplications and conquer: T(n) = 2 * T(n/2) + 1 = n-1 � Decrease by one: � Decrease by constant factor: T(n) = T(n-1) + 1 = n-1 T(n) = T(n/a) + a-1 = (a-1) loga n = log 2 n when a = 2

Sorting in Decrease and Conquer Insertion Sort, Selection Sort

Insertion Sort

Insertion Sort dengan Divide and Conquer Prosedur Merge dapat diganti dengan prosedur penyisipan sebuah elemen pada tabel yang sudah terurut (lihat algoritma Insertion Sort versi iteratif).

Divide, Conquer, and Solve

Merge

Kompleksitas Insertion Sort

Selection Sort

Pseudocode Selection Sort

Contoh Selection Sort

Kompleksitas Selection Sort

Searching Depth-First Search (DFS) Breadth-First Search (BFS) Binary Search Tree (BST)

Depth-First Search (DFS) � Mengunjungi vertex-vertex pada grafik dengan selalu bergerak dari vertex yang terakhir dikunjungi ke vertex yang belum dikunjungi, lakukan backtrack apabila tidak ada vertex tetangga yang belum dikunjungi. � Rekursif atau menggunakan stack Vertex di-push ke stack ketika dicapai untuk pertama kalinya � Sebuah vertex di-pop off atau dilepas dari stack ketika vertex tersebut merupakan vertex akhir (ketika tidak ada vertex tetangga yang belum dikunjungi) � � "Redraws" atau gambar ulang grafik dalam bentuk seperti pohon (dengan edges pohon dan back edges untuk grafik tak berarah/undirected graph)

Pseudo code DFS

Example: DFS traversal of undirected graph a b c d e f g h DFS traversal stack: a ab abfe abf ab abgcdh abgcd … DFS tree: 1 2 6 7 a b c d e f g h 4 3 5 8 Red edges are tree edges and black edges are back edges.

1 2 4 3 5 6 7 8 � DFS : 1, 2, 4, 8, 5, 6, 3, 7

1 2 4 3 5 6 7 8 � DFS : 1, 2, 3, 6, 8, 4, 5, 7

1 2 5 8 3 6 4 7 9 � DFS : 1, 2, 5, 8, 9, 6, 3, 7, 4

Notes on DFS � Time complexity of DFS is O(|V|). Why? � each edge (u, v) is explored exactly once, � All steps are constant time.

Breadth-first search (BFS) � Mengunjungi vertex-vertex grafik dengan berpindah ke semua vertex tetangga dari vertex yang terakhir dikunjungi � BFS menggunakan queue � Mirip dengan level ke level dari pohon merentang � "Redraws" atau gambar ulang grafik dalam bentuk seperti pohon (dengan edges pohon dan back edges untuk grafik tak berarah/undirected graph)

Example of BFS traversal of undirected graph a b c d e f g h BFS traversal queue: a bef efg fg g ch hd d BFS tree: 1 2 6 8 a b c d e f g h 3 4 5 7 Red edges are tree edges and black edges are cross edges.

Pseudo code BFS

Notes on BFS � Asumsi: setiap simpul dapat membangkitkan b buah simpul baru. � Misalkan solusi ditemukan pada aras ke-d � Jumlah maksimum seluruh simpul: 1+b+b 2 +b 3 +. . . +bd =(bd+1 – 1)/(b– 1) T(n) = O(bd) � Kompleksitas ruang algoritma BFS = sama dengan kompleksitas waktunya, karena semua simpul daun dari pohon harus disimpan di dalam memori selama proses pencarian.

Breadth First Search (grafik berarah) 2 s 5 3 28 4 8 7 6 9

Breadth First Search 1 Shortest path from s 0 2 s 4 5 3 7 6 Undiscovered Discovered Top of queue 29 Finished 8 Queue: s 9

Breadth First Search 1 2 0 s 4 5 3 7 6 1 Undiscovered Discovered Top of queue 30 Finished 8 Queue: s 2 9

Breadth First Search 1 2 0 4 5 s 8 7 1 3 6 1 Undiscovered Discovered Top of queue 31 Finished Queue: s 2 3 9

Breadth First Search 1 2 0 4 s 5 8 7 1 3 6 1 Undiscovered Discovered Top of queue 32 Finished Queue: 2 3 5 9

Breadth First Search 1 2 0 2 4 s 5 8 7 1 3 6 1 Undiscovered Discovered Top of queue 33 Finished Queue: 2 3 5 9

Breadth First Search 1 2 0 2 4 s 5 already discovered: 7 don't enqueue 5 1 3 8 6 1 Undiscovered Discovered Top of queue 34 Finished Queue: 2 3 5 4 9

Breadth First Search 1 2 0 2 4 s 5 8 7 1 3 6 1 Undiscovered Discovered Top of queue 35 Finished Queue: 2 3 5 4 9

Breadth First Search 1 2 0 2 4 s 5 8 7 1 3 6 1 Undiscovered Discovered Top of queue 36 Finished Queue: 3 5 4 9

Breadth First Search 1 2 2 0 4 s 5 8 7 1 6 3 1 2 Undiscovered Discovered Top of queue 37 Finished Queue: 3 5 4 9

Breadth First Search 1 2 2 0 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 38 Finished Queue: 3 5 4 6 9

Breadth First Search 1 2 2 0 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 39 Finished Queue: 5 4 6 9

Breadth First Search 1 2 2 0 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 40 Finished Queue: 5 4 6 9

Breadth First Search 1 2 2 0 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 41 Finished Queue: 4 6 9

Breadth First Search 1 2 2 0 3 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 42 Finished Queue: 4 6 9

Breadth First Search 1 2 2 0 3 4 s 5 8 7 1 3 1 6 2 Undiscovered Discovered Top of queue 43 Finished Queue: 4 6 8 9

Breadth First Search 1 2 2 0 3 4 s 7 5 1 3 3 1 6 2 Undiscovered Discovered Top of queue 44 8 Finished Queue: 6 8 9

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 45 8 Finished Queue: 6 8 7 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 46 8 Finished Queue: 6 8 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 47 8 Finished Queue: 8 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 48 8 Finished Queue: 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 49 8 Finished Queue: 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 50 8 Finished Queue: 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 51 8 Finished Queue: 7 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 52 8 Finished Queue: 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 53 8 Finished Queue: 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 54 8 Finished Queue: 9 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Undiscovered Discovered Top of queue 55 8 Finished Queue: 9 3

Breadth First Search 1 2 2 0 3 4 s 5 7 1 3 3 1 6 2 Level Graph 56 8 9 3

1 2 4 3 5 6 8 � BFS : 1, 2, 3, 4, 5, 6, 7, 8 7

1 2 4 3 5 6 8 � BFS : 1, 2, 3, 4, 5, 6, 7, 8 7

1 2 5 8 � BFS 3 6 7 9 : 1, 2, 3, 4, 5, 6, 7, 8, 9 4

� Latihan: Gunakan algoritma BFS dan DFS untuk menemukan pohon merentang (spanning tree) dari graf G di bawah ini jika traversalnya dimulai dari simpul e. Dalam menjawab soal ini, perlihatkan traversal BFS/DFS sebagai pohon berakar dengan e sebagai akarnya.

Binary Search Tree � Several algorithms on BST requires recursive processing of just one of its subtrees, e. g. , � Searching � Insertion of a new key � Finding the smallest (or the largest) key <k k >k

Binary Search Tree A binary search tree Not a binary search tree


Searching (Find) � Find X: return a pointer to the node that has key X, or NULL if there is no such node Binary. Node * Binary. Search. Tree: : Find(const float &x, Binary. Node *t) const { if (t == NULL) return NULL; else if (x < t->element) return Find(x, t->left); else if (t->element < x) return Find(x, t->right); else return t; // match } � Time complexity: O(height of the tree)

Algoritma BST � Algorithm BST(x, v) // Searches for node with key equal to v // in BST rooted at node x if x = NIL return -1 else if v = K(x) return x else if v < K(x) return BST(left(x), v) else return BST(right(x), v) � Efficiency worst case: C(n) = n average case: C(n) ≈ 2 ln n ≈ 1. 39 log 2 n if the BST was built from n random keys and v is chosen randomly.

Aplikasi DFS dan BFS � Search Engine (Google, Yahoo!). � Komponen search engine: 1. 2. 3. Web Spider : program penjelajah web (web surfer) Index: database yang menyimpan kata-kata penting pada setiap halaman web Query: pencarian berdasarkan string yang dimasukkan oleh pengguna (end-user) � Secara periodik (setiap jam atau setiap hari), spider menjejalahi internet untuk mengunjungi halaman web, mengambil kata-kata penting di dalam web, dan menyimpannya di dalam index. � Query dilakukan terhadap index, bukan terhadap website yang aktual.

Search Engine Google

Hasil Pencarian Search Engine Google

Bagaimana Spider Menjelajahi Web? � Halaman web dimodelkan sebagai graf berarah � Simpul menyatakan halaman web (web page) � Sisi menyatakan link ke halaman web � Bagaimana BFS � Dimulai teknik menjelajahi web? Secara DFS atau dari web page awal, lalu setiap link ditelusuri secara DFS sampai setiap web page tidak mengandung link. � Pada setiap halaman web yang ditemukan, informasi di dalamnya dianalisis dan disimpan di dalam database index.

Perbedaan BSF dan DSF BSF Mengunjungi vertex-vertex grafik dengan berpindah ke semua vertex tetangga dari vertex yang terakhir dikunjungi � BFS menggunakan queue � Mirip dengan level ke level dari pohon merentang � DSF �Mengunjungi vertex-vertex pada grafik dengan selalu bergerak dari vertex yang terakhir dikunjungi ke vertex yang belum dikunjungi. Lakukan backtrack apabila tidak ada vertex tetangga yang belum dikunjungi. �Rekursif stack � � Vertex di-push ke stack ketika dicapai untuk pertama kalinya Vertex di-pop off dari stack ketika vertex tersebut merupakan vertex akhir (tidak ada vertex tetangga yang belum dikunjungi)

Questions?

감사합니 Grazias Kiitos 다Danke Gratias ﺷﻜﺮﺎﹰ Terima Kasih 谢谢 Merci ������� Thank You ありがとうございます