720 likes | 1.02k Views
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
E N D
Design and Analysis of AlgorithmDecrease and Conquer Algorithm Aryo Pinandito, ST, M.MT – PTIIK UniversitasBrawijaya
Decrease and Conquer • Mengurangipermasalahanmenjadilebihkecilpadapermasalahan yang sama • Selesaikanpermasalahan yang lebihkeciltersebut • Kembangkanpermasalahan yang lebihkecilitusehinggamenyelesaikanpermasalahansebenarnya • Dapatdilakukandengandenganmetode top down atau bottom up
Variasi decrease and conquer • Mengurangidengansebuah constant (Decrease by a constant) • Ukurandarimasalahdikurangidenganfaktorkonstanta yang sama – biasanyadikurangisebanyakduapadasetiapiterasi • Polapenguranganukuranmasalahbervariasi/berbedaantaraiterasisatudengan yang lainnya
Decrease and Conquer • Decrease by a constant (usually by 1): • insertion sort • graph traversal algorithms (DFS and BFS) • topological sorting • algorithms for generating permutations, subsets • Decrease by a constant factor (usually by half) • binary search and bisection method • exponentiation by squaring • multiplication à la russe • Variable-size decrease • Euclid's algorithm • Selection by partition • Nim-like games • Biasanyamenggunakanalgoritmarekursif.
PermasalahanEksponensial • Permasalahan: Hitungxn • 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) loga n = log2 n when a = 2
Sorting in Decrease and Conquer Insertion Sort, Selection Sort
Insertion Sort dengan Divide and Conquer ProsedurMergedapatdigantidenganprosedurpenyisipansebuahelemen padatabel yang sudahterurut (lihatalgoritmaInsertion Sortversiiteratif).
Searching Depth-First Search (DFS) Breadth-First Search (BFS) Binary Search Tree (BST)
Depth-First Search (DFS) • Mengunjungi vertex-vertex padagrafikdenganselalubergerakdari vertex yang terakhirdikunjungike vertex yang belumdikunjungi, lakukan backtrack apabilatidakada vertex tetangga yang belumdikunjungi. • Rekursifataumenggunakan stack • Vertex di-push ke stack ketikadicapaiuntukpertamakalinya • Sebuah vertex di-pop off ataudilepasdari stack ketika vertex tersebutmerupakan vertex akhir (ketikatidakada vertex tetangga yang belumdikunjungi) • "Redraws" ataugambarulanggrafikdalambentuksepertipohon (dengan edges pohondan back edges untukgrafiktakberarah/undirected graph)
a b c d e f g h Red edges are tree edges and black edges are back edges. Example: DFS traversal of undirected graph DFS traversal stack: DFS tree: a ab abf abfe abf ab abg abgc abgcd abgcdh abgcd … 1 2 6 7 a b c d e f g h 4 3 5 8
1 • DFS : 1, 2, 4, 8, 5, 6, 3, 7 2 3 4 5 6 7 8
1 • DFS : 1, 2, 3, 6, 8, 4, 5, 7 2 3 4 5 6 7 8
1 • DFS : 1, 2, 5, 8, 9, 6, 3, 7, 4 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 grafikdenganberpindahkesemua vertex tetanggadari vertex yang terakhirdikunjungi • BFS menggunakan queue • Miripdengan level ke level daripohonmerentang • "Redraws"ataugambarulanggrafikdalambentuksepertipohon (dengan edges pohondan back edges untukgrafiktakberarah/undirected graph)
a b c d e f g h Red edges are tree edges and black edges are cross edges. 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 e f g h 3 4 5 7
Notes on BFS • Asumsi: setiapsimpuldapatmembangkitkan b buahsimpulbaru. • Misalkansolusiditemukanpadaaraske-d • Jumlahmaksimumseluruhsimpul: 1+b+b2 +b3 +...+bd =(bd+1 –1)/(b–1) T(n) = O(bd) • Kompleksitasruangalgoritma BFS = samadengankompleksitaswaktunya, karenasemuasimpuldaundaripohonharusdisimpan di dalammemoriselama proses pencarian.
Breadth First Search (grafik berarah) 2 4 8 s 5 7 3 6 9
Breadth First Search 1 2 2 4 8 Shortest pathfrom s 0 s 5 7 3 6 9 Undiscovered Queue: s Discovered Top of queue Finished
Breadth First Search 1 2 4 8 0 s 5 7 3 3 6 9 1 Undiscovered Queue: s 2 Discovered Top of queue Finished
Breadth First Search 1 2 4 8 0 5 s 5 7 1 3 6 9 1 Undiscovered Queue: s 2 3 Discovered Top of queue Finished
Breadth First Search 1 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 5 already discovered:don't enqueue 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 2 3 5 4 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 6 9 1 2 Undiscovered Queue: 3 5 4 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 3 5 4 6 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 5 4 6 Discovered Top of queue Finished
Breadth First Search 1 2 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 6 9 1 2 Undiscovered Queue: 4 6 8 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 7 s 5 7 1 3 3 6 9 1 2 Undiscovered Queue: 6 8 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 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 1 2 3 2 4 8 0 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 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 8 7 9 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished
Breadth First Search 1 2 3 2 4 8 0 s 5 7 1 3 3 6 9 3 1 2 Undiscovered Queue: 7 9 Discovered Top of queue Finished