Design and Analysis Algorithm Drs. Achmad Ridok M.Kom Imam Cholissodin, S.Si., M.Kom M. Ali Fauzi, S.Kom., M.Kom. Ratih Kartika Dewi, ST, M.Kom Pertemuan 11
Insertion and Selection Sort Contents Decrease and Conquer 3 1 Insertion and Selection Sort 2 DFS and BFS 3 Binary Search Tree 4
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 dengan metode top down atau bottom up
Permasalahan eksponensial: Hitung xn Brute Force: Divide and Conquer: Decrease by one: Decrease by constant factor: n-1 multiplications T(n) = 2*T(n/2) + 1 = n-1 T(n) = T(n-1) + 1 = n-1 T(n) = T(n/a) + a-1 = (a-1) n = when a = 2
Insertion sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among the sorted A[0..n-2] Usually implemented bottom up (non-recursively) Example: Sort 6, 4, 1, 8, 5 6 | 4 1 8 5 4 6 | 1 8 5 1 4 6 | 8 5 1 4 6 8 | 5 1 4 5 6 8 Exercise :
Pseudo code Insertion sort
Kompleksitas waktu algoritma Insertion Sort: Penyelesaian: T(n) = cn + T(n – 1) = cn + { c ⋅ (n – 1) + T(n – 2) } = cn + c(n – 1) + { c ⋅ (n – 2) + T(n – 3) } = cn + c ⋅ (n – 1) + c ⋅ (n – 2) + {c(n – 3) + T(n – 4) } = ... = cn+c⋅(n–1)+c(n–2)+c(n–3)+...+c2+T(1) = c{ n + (n – 1) + (n – 2) + (n – 3) + ... + 2 } + a = c{ (n – 1)(n + 2)/2 } + a = cn2/2+cn/2 +(a–c) = O(n2)
Selection Sort Algoritma sorting Sorting perbandingan pada element Terbagi menjadi 2 : Sorted list Sisa himpunan yang belum tersorting
Selection sort
Misalkan tabel A berisi elemen-elemen berikut: Langkah-langkah pengurutan dengan Selection Sort:
Kompleksitas waktu algoritma: Penyelesaian (seperti pada Insertion Sort):
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 b e f c d g h DFS traversal stack: DFS tree: a ab abf abfe abg abgc abgcd abgcdh … 1 2 6 7 a b c d Red edges are tree edges and black edges are cross edges. e f g h 4 3 5 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
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 BFS traversal queue: BFS tree: a bef efg fg g ch hd d 1 2 6 8 a b c d Red edges are tree edges and black edges are cross edges. e f g h 3 4 5 7
Pseudo code BFS
Notes on BFS Asumsi: setiap simpul dapat membangkitkan b buah simpul baru. Misalkan solusi ditemukan pada aras/level ke-d Jumlah maksimum seluruh simpul: 1+b+b2 +b3 +...+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 4 8 s 5 7 3 6 9
Breadth First Search Shortest path from s Undiscovered Queue: s 1 Shortest path from s 2 2 4 8 s 5 7 3 6 9 Undiscovered Queue: s Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: s 2 Discovered Top of queue 1 2 4 8 s 5 7 3 3 6 9 1 Undiscovered Queue: s 2 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: s 2 3 Discovered Top of queue 1 2 4 8 s 5 5 7 1 3 6 9 1 Undiscovered Queue: s 2 3 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: s 2 3 5 Discovered 1 2 4 8 s 5 7 1 3 6 9 1 Undiscovered Queue: s 2 3 5 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 2 3 5 Discovered Top of queue 1 2 2 4 4 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 Discovered Top of queue Finished
Breadth First Search 5 already discovered: don't enqueue Undiscovered 1 2 2 4 8 5 already discovered: don't enqueue s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 2 3 5 4 Discovered 1 2 2 4 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 3 5 4 Discovered Top of queue 1 2 2 4 8 s 5 7 1 3 6 9 1 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 3 5 4 Discovered Top of queue 1 2 2 4 8 s 5 7 1 3 6 6 9 1 2 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 3 5 4 6 Discovered 1 2 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 3 5 4 6 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 5 4 6 Discovered Top of queue 1 2 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 5 4 6 Discovered Top of queue 1 2 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 4 6 Discovered Top of queue 1 2 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 4 6 Discovered Top of queue 1 2 3 2 4 8 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 4 6 8 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 8 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 6 8 Discovered Top of queue 1 2 3 2 4 8 s 5 7 7 1 3 3 6 9 1 2 Undiscovered Queue: 6 8 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 6 8 7 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 9 3 1 2 Undiscovered Queue: 6 8 7 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 6 8 7 9 Discovered 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 6 8 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 8 7 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 8 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 7 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 7 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 7 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 7 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: 9 Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 9 Discovered Top of queue Finished
Breadth First Search Undiscovered Queue: Discovered Top of queue 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 s 5 7 1 3 3 6 9 3 1 2 Level Graph
Latihan: Gunakan algoritma BFS dan DFS untuk menemukan pohon merentang (spanning tree) dari graf G di bawah ini jika traversalnya dimulai dari simpul k. 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 Not a binary search tree A binary search tree
Bagaimana spider menjelajahi (surfing) web? Halaman web dimodelkan sebagai graf berarah Simpul menyatakan halaman web (web page) Sisi menyatakan link ke halaman web Bagaimana teknik menjelajahi web? Secara DFS atau BFS Dimulai dari web page awal, lalu setiap link ditelusuri secara DFS sampai setiap web page tidak mengandung link. Pada setiap halaman web, informasi di dalamnya dianalisis dan disimpan di dalam basis data index.
Click to edit subtitle style Thank You !