You are on page 1of 32

Backtracking

Definisi
• Runut-balik (backtracking) adalah
algoritma yang berbasis pada DFS untuk
mencari solusi persoalan secara lebih
efisien.
• Runut-balik, yang merupakan perbaikan
dari algoritma brute-force, secara
sistematis mencari solusi persoalan di
antara semua kemungkinan solusi yang
ada.

• Dengan metode runut-balik, kita tidak perlu
memeriksa semua kemungkinan solusi yang
ada. Hanya pencarian yang mengarah ke solusi
saja yang selalu dipertimbangkan. Akibatnya,
waktu pencarian dapat dihemat.

• Saat ini algoritma runut-balik banyak diterapkan
untuk program games (seperti permainan tic-
tac-toe, menemukan jalan keluar dalam sebuah
labirin, catur, dll) dan masalah-masalah pada
bidang kecerdasan buatan (artificial
intelligence).

.Mencari jalan keluar di dalam labirin (Maze Problem).

. ke kiri (left). ke bawah (down). Sebuah langkah terdiri dari pergerakan satu unit sel pada arah tertentu. • Arah yang mungkin: ke atas (up). Penyelesaian dengan bactracking: • Bagi lintasan menjadi sederetan langkah. ke kanan (right).

Garis besar algoritma runut-baliknya: while belum sampai pada tujuan do if terdapat arah yang benar sedemikian sehingga kita belum pernah berpindah ke sel pada arah tersebut then pindah satu langkah ke arah tersebut else backtrack langkah sampai terdapat arah seperti yang disebutkan di atas endif endwhile .

• Bagaimana mengetahui langkah yang mana yang perlu dijejaki kembali? • Ada dua solusi untuk masalah ini: pertama. simpan semua langkah yang pernah dilakukan. gunakan rekursi (yang secara implisit menyimpan semua langkah). . atau kedua. • Rekursi adalah solusi yang lebih mudah.

down.function SolveMaze(input M : labirin)boolean { true jika solusi ditemukan. left = 3. down. arah) { backtrack } endif endfor return false { semua arah sudah dicoba. arah) { pindah satu langkah (satu sel) sesuai arah tersebut } if SolveMaze(M) then return true else unmove(M. maka kesimpulannya: tidak ada solusi } endif . right) do move(M. false jika tidak } Deklarasi arah : integer { up = 1. tetapi tetap buntu. right = 4 } Algoritma: if solusi sudah ditemukan then return true else for tiap arah gerakan (up. 2. left.

Runut-balik diperlihatkan dengan garis putus-putus. . in out Contoh runut-balik pada sebuah labirin.

.

Cm – Nonemtpy domain of possible values for each variable DV1. …..Constraint satisfaction problems • What is a CSP? – Finite set of variables V1. DV2. C2. V2. V1 ≠ V2 • A state is defined as an assignment of values to some or all variables. e. • Consistent assignment: assignment does not not violate the constraints.g. …. … DVn – Each constraint Ci limits the values that variables can take. Vn – Finite set of variables C1. .

• Applications: Scheduling the time of observations on the Hubble Space Telescope. • Some CSPs require a solution that maximizes an objective function. Constraint satisfaction problems • An assignment is complete when every value is mentioned. Floor planning. Cryptography . Map coloring. • A solution to a CSP is a complete assignment that satisfies all constraints.

• A solution to a CSP is a complete assignment that satisfies all constraints. Constraint satisfaction problems • An assignment is complete when every value is mentioned. • Some CSPs require a solution that maximizes an objective function. Cryptography . • Applications: Scheduling the time of observations on the Hubble Space Telescope. Floor planning. Map coloring.

SA.red). • E. NT.green. Q.g.…} .blue). WA  NT (if the language allows this) • E.green).(red.blue} • Constraints:adjacent regions must have different colors.NT)  {(red.g.(green. (WA. V. CSP example: map coloring • Variables: WA. T • Domains: Di={red. NSW.

V=red.T=green} . {WA=red.g.SA=blue.Q=red. e.CSP example: map coloring • Solutions are assignments satisfying all constraints.NSW=green.NT=green.

Backtracking search • Depth-first search • Chooses values for one variable at a time and backtracks when a variable has no legal values left to assign. 143) . • Uninformed algorithm – No good general performance (see table p.

Backtracking example .

Backtracking example .

Backtracking example .

Backtracking example .

Improving backtracking efficiency • Previous improvements  introduce heuristics • General-purpose methods can give huge gains in speed: – Which variable should be assigned next? – In what order should its values be tried? – Can we detect inevitable failure early? – Can we take advantage of problem structure? .

. Least constraining value • Least constraining value heuristic • Rule: given a variable choose the least constraining value i.e. the one that leaves the maximum flexibility for subsequent variable assignments.

• Terminate search when any variable has no legal values. . Forward checking • Can we detect inevitable failure early? – And avoid it later? • Forward checking idea: keep track of remaining legal values for unassigned variables.

Forward checking • Assign {WA=red} • Effects on other variables connected by constraints with WA – NT can no longer be red – SA can no longer be red .

why? . Forward checking • Assign {Q=green} • Effects on other variables connected by constraints with WA – NT can no longer be green – NSW can no longer be green – SA can no longer be green • MRV heuristic will automatically select NT and SA next.

Forward checking • If V is assigned blue • Effects on other variables connected by constraints with WA – SA is empty – NSW can no longer be blue • FC has detected that partial assignment is inconsistent with the constraints and backtracking can occur. .

– NT and SA cannot be blue! • Constraint propagation repeatedly enforces constraints locally . • FC checking propagates information from assigned to unassigned variables but • does not provide detection for all failures. Constraint propagation • Solving CSPs with combination of heuristics plus forward checking is more efficient • than either approach alone.

Arc consistency • X  Y is consistent iff for every value x of X there is some allowed y • SA  NSW is consistent iff SA=blue and NSW=red .

Arc consistency • X  Y is consistent iff for every value x of X there is some allowed y • NSW  SA is consistent iff NSW=red and SA=blue NSW=blue and SA=??? Arc can be made consistent by removing blue from NSW .

Arc consistency • Arc can be made consistent by removing blue from NSW • RECHECK neighbours !! – Remove red from V .

– Repeated until no inconsistency remains . Arc consistency • Arc can be made consistent by removing blue from NSW • RECHECK neighbours !! – Remove red from V • Arc consistency detects failure earlier than FC • Can be run as a preprocessor or after each assignment.

• end .