Design
and
Analysis
Algorithm
Pertemuan 11
Contents
Insertion and Selection Sort
2
Decrease and Conquer
31
DFS and BFS
33
Binary Search Tree
4
Decrease and Conquer
1.
Mengurangi permasalahan menjadi lebih kecil
pada permasalahan yang sama
2.
Selesaikan permasalahan yang lebih kecil
tersebut
3.
Kembangkan permasalahan yang lebih kecil
itu sehingga menyelesaikan permasalahan
sebenarnya
4.
Dapat dilakukan dengan dengan metode
top
down
atau
bottom up
Permasalahan eksponensial: Hitung x
n
Brute Force:
Divide and Conquer:
Decrease by one:
Decrease by
constant factor:
n-1 multiplications
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
5
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
= cn
2/2+cn/2 +(a–c)
= O(n
2)
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
a b
e f
c d
g h
DFS traversal stack:
DFS traversal stack: DFS tree:DFS tree:
a b
Red edges are tree
edges and black edges are cross edges.
1
2
3
4
5
6
7
1
2
3
4
5
6
7
1
2
4
5
6
3
7
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
BFS traversal
queue:
BFS tree:
BFS tree:
a b
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/level ke-d
Jumlah maksimum seluruh simpul:
1+b+b
2+b
3+...+b
d=(b
d+1–1)/(b–1)
T(n) = O(b
d)
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)
s
2
5
4
7
8
3 6 9
Breadth First Search
Top of queue
2
1
Shortest path
from s
Breadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queue4
2
Breadth First Search
Top of queueBreadth First Search
5 already discovered: don't enqueue
Undiscovered
Discovered
Finished Top of queue
Breadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
Top of queueBreadth First Search
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
A binary search tree Not a binary search tree