0% found this document useful (0 votes)
25 views

KI141319 PAA2: Perancangan Dan Analisis Algoritma 2 Semester Genap 2015/2016

This document outlines the course plan for the Perancangan dan Analisis Algoritma 2 (Algorithm Design and Analysis 2) class. It discusses the following: - The class will meet 16 times over the semester with no more than 3 allowed absences. There are two sections that meet at different times. - Topics covered will include greedy algorithms, divide-and-conquer algorithms, and dynamic programming. - There will be 4 exams worth a total of 2/3 of the grade, with the other 1/3 coming from assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

KI141319 PAA2: Perancangan Dan Analisis Algoritma 2 Semester Genap 2015/2016

This document outlines the course plan for the Perancangan dan Analisis Algoritma 2 (Algorithm Design and Analysis 2) class. It discusses the following: - The class will meet 16 times over the semester with no more than 3 allowed absences. There are two sections that meet at different times. - Topics covered will include greedy algorithms, divide-and-conquer algorithms, and dynamic programming. - There will be 4 exams worth a total of 2/3 of the grade, with the other 1/3 coming from assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

KI141319 PAA2

Perancangan dan Analisis Algoritma 2


Semester Genap 2015/2016
Diana Purwitasari ([email protected])
Teknik Informatika, Institut Teknologi Sepuluh Nopember Surabaya
PAA2 A (Selasa 07.30-10.00) dan PAA2 D (Selasa 10.30-13.00)

Rencana Pembelajaran
Materi
(algorithmic
paradigms)
Greedy
Divide-and-conquer
Dynamic
Programming

Temu Muka

Nilai

16 kali
Tidak ada hari libur
nasional
Maksimal libur
pribadi 3x

4 kali
2 nilai dari ujian tulis
(koreksi bersama)

Algorithmic Paradigms
Greedy

Divide-and-Conquer (DC)

Build up a
solution
incrementally
(make a choice
that looks best at
the moment)
Get the optimal
solution of the
complete
problem

Divide: Break up a problem into


independent subproblems
Conquer: Recursively solve these
subproblems
Combine: Appropriately combine
solution to subproblems to form
solution to original problem

Dynamic
Programming (DP)
Break up a problem
into overlapping
subproblems
Build up solutions to
larger and larger
subproblems (Optimal
Substructure)

Fibonacci Problem -- DP
1. /* DP Memoization (Top Down)*/
2. int fib(int n){
3.
if (lookup[n] == NIL){
4.
if (n <= 1) lookup[n] = n;
5.
else
6.
lookup[n] = fib(n-1) + fib(n-2);
7.
}
8.
return lookup[n];
computed solutions
9. }

to subproblems are
stored in a table,
dont have to
recomputed

1. /* DP Tabulation (Bottom Up)*/


2. int fib(int n){
3.
int lookup[n+1]; int i;
4.
lookup[0]=0; lookup[1]=1;
5.
for (i = 2; i <= n; i++)
6.
lookup[i]=lookup[i-1]+lookup[i-2];
7.
return lookup[n];
8. }

1. /* simple recursive program */


2. int fib(int n){
3.
if ( n <= 1 )
4.
return n;
5.
return fib(n-1) + fib(n-2);
6. }

DC vs DP
Fibonacci Problem

Binary Search

Divide-and-conquer ??
Dynamic Programming ??

Divide-and-conquer ??
Dynamic Programming ??

Knapsack Problem (1)


Greedy
Build up a solution
incrementally
(make a choice
that looks best at
the moment)
Get the optimal
solution of the
complete problem

GOAL: maksimalkan berat

Knapsack Problem (2)


Greedy
Build up a solution
incrementally
(make a choice
that looks best at
the moment)
Get the optimal
solution of the
complete problem

0/1 Knapsack
vs
Fractional Knapsack
sort by:
weight or value or
value/weight or weight/value
GOAL: maksimalkan value
dengan constraint berat

Knapsack Problem (3)


Terdapat 4 benda a-b-c-d yang memiliki {size,value} sbb: a{2,3}, b{3,4}, c{4,5}, d{5,6}
Jika isi knapsack maksimal size = 5 maka benda yang akan diambil ???
Index (i,s), sortby
size

Maks size=0

Maks size=1

Maks size=2

Maks size=3

Maks size=4

Maks size=5

0 Benda (tdk ada)

Value=0

Value=0

Value=0

Value=0

Value=0

Value=0

Value=0, krn
value a=3

Value=0

Value=3

Value=3

Value=3

Value=3, size a
tdk mungkin >2

2 Benda (a/b)

Value=0

Value=0

Value=3, pilih
benda a

Value=4, benda
b lebih dipilih

Value=4, masih
memilih benda b

Value=7, pilih
benda a & b

3 Benda (a/b/c)

Value=0

Value=0

Value=3, ambil
pilihan
sebelumnya (a)

Value=4, ambil
pilihan
sebelumnya (b)

Value=5, benda
c lebih dipilih

Value=7, pilih
benda a & b

4 Benda (a/b/c/d)

Value=0

Value=0

Value=3, ambil
pilihan
sebelumnya (a)

Value=4, ambil
pilihan
sebelumnya (b)

Value=5, benda
c lebih dipilih

Value=7, pilih
benda a & b

1 Benda (a)

Knapsack Problem (4)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

sort by weight desc,


value desc
index-i v[i]
w[i]
// Input:
1
4
12
// Values (stored in array v)
2
10
4
// Weights (stored in array w)
3
2
2
// Number of distinct items (n) 4
2
1
// Knapsack capacity (W)
5
1
1
sort by v/w desc, value desc
index-i v[i]
w[i]
v/w
for j from 0 to W do:
1
10
4
2.50
m[0, j] := 0
2
2
1
2.00
3
2
2
1.00
for i from 1 to n do:
4
1
1
1.00
for j from 0 to W do:
5
4
12
0.33
if w[i-1] <= j then:
m[i, j] := max( m[i-1, j] , m[i-1, j-w[i-1]] + v[i-1] )
else:
m[i, j] := m[i-1, j]

You might also like