Design and Analysis of Algorithm Decrease and Conquer Algorithm

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

description

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 - PowerPoint PPT Presentation

Transcript of Design and Analysis of Algorithm Decrease and Conquer Algorithm

Page 1: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Design and Analysis of AlgorithmDecrease and Conquer Algorithm

Aryo Pinandito, ST, M.MT – PTIIK Universitas Brawijaya

Page 2: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

Page 3: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Variasi decrease and conquer Mengurangi dengan sebuah constant

(Decrease by a constant)

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

Page 4: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

Biasanya menggunakan algoritma rekursif.

Page 5: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Permasalahan Eksponensial Permasalahan: 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) loga n

= log2 n when a = 2

Page 6: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Sorting in Decrease and Conquer

Insertion Sort, Selection Sort

Page 7: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Insertion Sort

Page 8: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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).

Page 9: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Divide, Conquer, and Solve

Page 10: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Merge

Page 11: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Kompleksitas Insertion Sort

Page 12: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Selection Sort

Page 13: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Pseudocode Selection Sort

Page 14: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Contoh Selection Sort

Page 15: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Kompleksitas Selection Sort

Page 16: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Searching

Depth-First Search (DFS)Breadth-First Search (BFS)

Binary Search Tree (BST)

Page 17: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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)

Page 18: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Pseudo code DFS

Page 19: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Example: DFS traversal of undirected graph

a b

e f

c d

g h

DFS traversal stack: DFS tree:

a b

e f

c d

g h

aababfabfeabfababgabgcabgcdabgcdhabgcd… Red edges are tree edges and black edges are back edges.

1 2

54

6

3

7

8

Page 20: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 3

4 5 6 7

8

Page 21: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 3

4 5 6 7

8

Page 22: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 4

5 6

3

7

8 9

Page 23: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

Page 24: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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)

Page 25: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Example of BFS traversal of undirected graph

BFS traversal queue:

a b

e f

c d

g h

BFS tree:

a b

e f

c d

g h

abefefgfggchhdd

Red edges are tree edges and black edges are cross edges.

1

3

2 6

4 75

8

Page 26: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Pseudo code BFS

Page 27: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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+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.

Page 28: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search (grafik berarah)

28

s

2

5

4

7

8

3 6 9

Page 29: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

29

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: s Top of queue

21

Shortest pathfrom s

Page 30: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

30

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: s 2 Top of queue

3

1

1

Page 31: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

31

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: s 2 3 Top of queue

5

1

1

1

Page 32: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

32

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 2 3 5 Top of queue

1

1

1

Page 33: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

33

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 2 3 5 Top of queue

41

1

1

2

Page 34: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

34

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 2 3 5 4 Top of queue

1

1

1

2

5 already discovered:don't enqueue

Page 35: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

35

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 2 3 5 4 Top of queue

1

1

1

2

Page 36: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

36

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 3 5 4 Top of queue

1

1

1

2

Page 37: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

37

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 3 5 4 Top of queue

1

1

1

2

6

2

Page 38: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

38

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 3 5 4 6 Top of queue

1

1

1

2

2

Page 39: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

39

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 5 4 6 Top of queue

1

1

1

2

2

Page 40: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

40

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 5 4 6 Top of queue

1

1

1

2

2

Page 41: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

41

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 4 6 Top of queue

1

1

1

2

2

Page 42: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

42

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 4 6 Top of queue

1

1

1

2

2

83

Page 43: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

43

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 4 6 8 Top of queue

1

1

1

2

2

3

Page 44: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

44

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 6 8 Top of queue

1

1

1

2

2

3

73

Page 45: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

45

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 6 8 7 Top of queue

1

1

1

2

2

3

9

3

3

Page 46: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

46

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 6 8 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 47: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

47

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 8 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 48: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

48

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 49: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

49

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 50: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

50

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 51: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

51

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 7 9 Top of queue

1

1

1

2

2

3

3

3

Page 52: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

52

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 9 Top of queue

1

1

1

2

2

3

3

3

Page 53: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

53

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 9 Top of queue

1

1

1

2

2

3

3

3

Page 54: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

54

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: 9 Top of queue

1

1

1

2

2

3

3

3

Page 55: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

55

s

2

5

4

7

8

3 6 9

0

UndiscoveredDiscovered

Finished

Queue: Top of queue

1

1

1

2

2

3

3

3

Page 56: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Breadth First Search

56

s

2

5

4

7

8

3 6 9

0

Level Graph

1

1

1

2

2

3

3

3

Page 57: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 3

4 5 6 7

8

Page 58: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 3

4 5 6 7

8

Page 59: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

1

2 4

5 6

3

7

8 9

Page 60: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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.

Page 61: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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

Page 62: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Binary Search Tree

A binary search tree Not a binary search tree

Page 63: Design and Analysis of Algorithm Decrease and  Conquer Algorithm
Page 64: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Searching (Find) Find X: return a pointer to the node that has

key X, or NULL if there is no such node

Time complexity: O(height of the tree)

BinaryNode * BinarySearchTree::Find(const float &x, BinaryNode *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}

Page 65: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

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) ≈ 2ln n ≈ 1.39log2 n if the BST was built from n random keys and v is chosen randomly.

Page 66: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Aplikasi DFS dan BFS Search Engine (Google, Yahoo!). Komponen search engine:

1. Web Spider : program penjelajah web (web surfer)2. Index: database yang menyimpan kata-kata penting pada

setiap halaman web 3. Query: pencarian berdasarkan string yang dimasukkan

oleh pengguna (end-user)

Secara periodik (setiap jam atau setiap hari), spider menjejalahi internet untuk mengunjungi halaman-halaman web, mengambil kata-kata penting di dalam web, dan menyimpannya di dalam index.

Query dilakukan terhadap index, bukan terhadap website yang aktual.

Page 67: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Search Engine Google

Page 68: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Hasil Pencarian Search Engine Google

Page 69: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Bagaimana Spider Menjelajahi 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 yang ditemukan, informasi di dalamnya dianalisis dan disimpan di dalam database index.

Page 70: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Perbedaan BSF dan DSFBSF DSF 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

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)

Page 71: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Questions?

Page 72: Design and Analysis of Algorithm Decrease and  Conquer Algorithm

Terima KasihThank

You

Danke Gratias

Merci

ありがとうございます

감사합니다 Kiitos

谢谢ًشكرا

Grazias

धन्यवाद