Push Down Automata
Pertemuan 11
Dosen Pembina :
Danang Junaedi
Pendahuluan
• Latar belakang munculnya konsep PDA
[1 & 3]– Terdapat context-free languages yang tidak regular,
contoh
• {0n1n| 0=<n} Is not regular
• {0n1n | 0≤n≤k, for some fixed k} Is regular, for any fixed k
– Finite Automota tidak bisa mengenal semua
context-free languages Finite Automata memiliki memory
yang terbatas
– A DFA can “remember” only a finite amount of
information, whereas a PDA can “remember” an infinite
amount of (certain types of) information
2 IF-UTAMA
Perbedaan FA dan PDA
[7]
• Finite Automata
• Push Down Atomata
Pushdown Automaton
[3]
• Definisi
[3]– A PDA is an NFA-ε with a stack.
– Transitions are modified to accommodate stack operations
•
A pushdown automaton (PDA) is an abstract model
machine similar to the FSA
•
It has a finite set of states. However, in addition, it has a
pushdown stack. Moves of the PDA are as follows:
1.
An input symbol is read and the top symbol on the stack is
read.
2.
Based on both inputs, the machine enters a new state and
writes zero or more symbols onto the pushdown stack.
3.
Acceptance of a string occurs if the stack is ever empty.
(Alternatively, acceptance can be if the PDA is in a final state.
Both models can be shown to be equivalent.)
5 IF-UTAMA
Power of PDAs
[3]
• PDAs are more powerful than FSAs.
• a
nb
n, which cannot be recognized by an FSA, can easily
be recognized by the PDA.
• Stack all a symbols and, for each b, pop an a off the
stack.
• If the end of input is reached at the same time that the
stack becomes empty, the string is accepted.
• It is less clear that the languages accepted by
• PDAs are equivalent to the context-free languages.
6 IF-UTAMA
PDAs to produce derivation
strings
[3]
•
Given some BNF (context free grammar). Produce the
leftmost derivation of a string using a PDA:
1.
If the top of the stack is a terminal symbol, compare it to the
next input symbol; pop it off the stack if the same. It is an error
if the symbols do not match.
2.
If the top of the stack is a nonterminal symbol X, replace X on
the stack with some string α, where α is the right hand side of
some production X→ α.
•
This PDA now simulates the leftmost derivation for
some context-free grammar.
•
This construction actually develops a nondeterministic
PDA that is equivalent to the corresponding BNF
grammar. (i.e., step 2 may have multiple options.)
7 IF-UTAMA
NDPDAs are different from
DPDAs
[3]
•
What is the relationship between deterministic PDAs and
nondeterministic PDAs?
• Consider the set of palindromes, strings reading the same forward and backward, generated by the grammar
X → 0X0 | 1X1 | 2
• We can recognize such strings by a deterministic PDA:
1. Stack all 0s and 1s as read. 2. Enter a new state upon reading a 2.
3. Compare each new input to the top of stack, and pop stack.
• However, consider the following set of palindromes: X → 0X0 | 1X1 | 0 | 1
• In this case, we never know where the middle of the string is. To recognize these palindromes, the automaton must guess where the middle of the string is (i.e., is nondeterministic).
8 IF-UTAMA
Pendahuluan
• Mekanisme kerja PDA
[4]– Memiliki tempat penyimpanan
yang tidak terbatas berupa
stack
/tumpukan
– Stack kumpulan dari
elemen-elemen yg sejenis dengan sifat
pengambilan dan penambahan
melalui suatu tempat yg disebut
top of stack
(puncak stack)
– Sistem pengaturan LIFO (Last In
First Out
)
– Operasi pop Pengambilan
elemen dari stack
– Operasi push memasukkan
elemen ke dalam stack
9 IF-UTAMA
Model Fisik
[9]
•
Keterangan
1. Pita input berisi deretan simbol
yang akan diproses.
2. Tumpukan dapat diisi oleh
simbol-simbol yang disusun
secara LIFO (Last In First Out)
3. Pita input bergerak satu arah.
4. Pada saat awal, head berada
tepat di atas simbol pertama,
Stack
berisi simbol awal Stack.
5. Status PDA dapat
berubah-ubah sesuai dengan simbol
input
dan simbol teratas yang
terdapat dalam Stack.
10 IF-UTAMA
Formal Definition of a PDA
[6]
• A pushdown automaton (PDA) is a seven-tuple:
M = (Q, Σ, Г, δ, S, Z
0
, F)
Q
A finite set of states
Σ
A finite set of input alphabet
Г
A finite set of stack alphabet
S
The initial/starting state, S is in Q
Z
0A starting stack symbol, is in Г
F
A set of final/accepting states, which is
a subset of Q
δ
A transition function
Formal Definition of a PDA
[9]
P = (Q, Σ
Σ
Σ, Γ
Σ
Γ
Γ, δ
Γ
δδ
δ, S, Z
0, F)
di mana
P = nama PDA.
Q = himpunan berhingga dari status PDA. Σ = alfabet input.
Γ = himpunan simbol yang boleh terdapat pada Stack. δ = himpunan transisi status.
S= status awal.
Z0= simbol pertama yang terdapat pada Stack F = himpunan status akhir.
Transisi status (δ) dinyatakan dalam bentuk δ(qi, a, X) = {(qj, γ)} ; qi, qj ∈ Q ; a = Σ ; X = Γ ; γ = Γ*
• Deskripsi sesaat : (q, aw, αααα), di mana
q = status mesin pada saat itu.
a = simbol yang sedang terbaca oleh head w = deretan simbol input yang belum terbaca. α = deretan simbol dalam Stack. TOS ada di paling kiri.
• Contoh : (q1, 0101, ABB)
Jika deskripsi pada suatu saat adalah (q1, 0101, ABB) dan terdapat aturan transisi berbentuk δ(q1, 0, A) = {(q2, CA), maka deskripsi mesin berubah menjadi (q2, 101, CABB), ditulis (q1, 0101, ABB) |- (q2, 101, CABB).
Transition function
[6]
• δ(q
0
, 0 , Z
0
)=(q
1
, 0Z
0
)
• If next state is still q
0
, this means the PDA has
not reached the middle of the input string
Z Z00 0 0 String String
γ=0
Z
0
Next state Next state Start state Start state Input Input symbol symbol Stack Stack symbol symbol 13 IF-UTAMA• For the language {x | x = a
nb
n, n ≥0 and n in {a,b}*}
M
1= (Q, Σ, Г, δ, S, Z
0, F)
Q={q
0, q
1, q
2, q
3};
Σ
={a,b};
Г
={a,$}
S={q
0};
Z
0={$};
F={q
3}final state atau {} isi stack kosong
δ
:
(1) δ(q
0, a, $) = {(q
1, a$)}
(2) δ(q
1, a, a) = {(q
1, aa)}
(3) δ(q
1, b, a) = {(q
2, ε)}
(4) δ(q
2, b, a) = {(q
2, ε)}
(5) δ(q
2, ε, $) = {(q
3, ε)}
Example 1 - 1
14 IF-UTAMA • Example Computation:(1) δ(q
0, a, $) = {(q
1, a$)}
(2) δ(q
1, a, a) = {(q
1, aa)}
(3) δ(q
1, b, a) = {(q
2, ε)}
(4) δ(q
2, b, a) = {(q
2, ε)}
(5) δ(q
2, ε, $) = {(q
3, ε)}
Rule Applied State Input Stack Rules Applicable
- q0 aabb $ (1) (1) q1 abb a$ (2) (2) q1 bb aa$ (3) (3) q2 b εa$ (4) (4) q2 ε ε$ (5) (5) q3 ε ε
-• Karena state akhir ada di q3 maka string aabb diterima
Example 1 - 2
15 IF-UTAMA • Example Computation:(1) δ(q
0, a, $) = {(q
1, a$)}
(2) δ(q
1, a, a) = {(q
1, aa)}
(3) δ(q
1, b, a) = {(q
2, ε)}
(4) δ(q
2, b, a) = {(q
2, ε)}
(5) δ(q
2, ε, $) = {(q
3, ε)}
Rule Applied State Input Stack Rules Applicable
- q0 abb $ (1)
(1) q1 bb a$ (3)
(3) q2 b ε$
-• Karena state akhir bukan di q3 maka string abb ditolak
Example 1 - 3
16 IF-UTAMA
• For the language {x | x = wcwrand w in {0,1}*} M = ({q1, q2}, {0, 1, c}, {R, B, G}, δ, q1, R, q2) δ: (1) δ(q 1, 0, R) = {(q1, BR)} (9) δ(q1, 1, R) = {(q1, GR)} (2) δ(q1, 0, B) = {(q1, BB)} (10) δ(q1, 1, B) = {(q1, GB)} (3) δ(q1, 0, G) = {(q1, BG)} (11) δ(q1, 1, G) = {(q1, GG)} (4) δ(q1, c, R) = {(q2, R)} (12) δ(q2, 1, G) = {(q2, ε)} (5) δ(q 1, c, B) = {(q2, B)} (6) δ(q1, c, G) = {(q2, G)} (7) δ(q2, 0, B) = {(q2, ε)} (8) δ(q2, ε, R) = {(q2, ε)} • Notes:
– Rule #8 is used to pop the final stack symbol off at the end of a computation.
Example 2 - 1
[8]
17 IF-UTAMA • Example Computation: (1) δ(q1, 0, R) = {(q1, BR)} (7) δ(q2, 0, B) = {(q2, ε)} (2) δ(q 1, 0, B) = {(q1, BB)} (8) δ(q2, ε, R) = {(q2, ε)} (3) δ(q 1, 0, G) = {(q1, BG)} (9) δ(q1, 1, R) = {(q1, GR)} (4) δ(q1, c, R) = {(q2, R)} (10) δ(q1, 1, B) = {(q1, GB)} (5) δ(q1, c, B) = {(q2, B)} (11) δ(q1, 1, G) = {(q1, GG)} (6) δ(q1, c, G) = {(q2, G)} (12) δ(q2, 1, G) = {(q2, ε)}Rule Applied State Input Stack Rules Applicable
- q1 01c10 R (1) (1) q1 1c10 BR (10) (10) q1 c10 GBR (6) (6) q2 10 GBR (12) (12) q2 0 εBR (7) (7) q2 ε εR (8) (8) q2 ε ε
-• Karena state akhir ada di q2 maka string 01c10 diterima
Example 2 - 2
[8]
18 IF-UTAMA • Example Computation: (1) δ(q 1, 0, R) = {(q1, BR)} (9) δ(q1, 1, R) = {(q1, GR)} (2) δ(q 1, 0, B) = {(q1, BB)} (10) δ(q1, 1, B) = {(q1, GB)} (3) δ(q 1, 0, G) = {(q1, BG)} (11) δ(q1, 1, G) = {(q1, GG)} (4) δ(q 1, c, R) = {(q2, R)} (12) δ(q2, 1, G) = {(q2, ε)} (5) δ(q 1, c, B) = {(q2, B)} (6) δ(q 1, c, G) = {(q2, G)} (7) δ(q 2, 0, B) = {(q2, ε)} (8) δ(q 2, ε, R) = {(q2, ε)}Rule Applied State Input Stack Rules Applicable - q1 1c1 R (9)
(9) q1 c1 GR (6)
(6) q2 1 GR (12)
(12) q2 ε εR (8)
(8) q2 ε ε
-• Karena state akhir ada di q2 maka string 1c1 diterima
Example 2 - 3
[8]
Jenis-Jenis PDA
1. Ditinjau dari stack
– Non Extended PDA δ
δδ
δ(qi, a, αααα) = {(qj, γγγγ)} ; qi, qj qj ∈ Q ; a ∈ Σ ; α, γ ∈ Γ* , dimana
• α adalah satu simbol teratas dalam stack. • γ adalah deretan simbol yang menggantikan α Contoh : δ(q1, c, G) = {(q2, G)}
– Extended PDA δ
δδ
δ(qi, a, αααα) = {(qj, γγγγ)} ; qi, qj ∈ Q ; a ∈ Σ ; α, γ ∈ Γ* , dimana
• α adalah deretan simbol teratas dalam stack. • γ adalah deretan simbol yang menggantikan α Contoh : δ(q1, a, bSb) = {(q2, S)}
2. Ditinjau dari fungsi transisi
– Deterministik PDA δ δδ δ(q, a, ααα) = {(p, γγγγ)}α contoh: δ(q0, a, a) = {(q1, ε)} – Nondeterministik PDA δ δδ δ(q0, a, αααα) = {(p1, γγγγ1), (p2, γγγγ2), (p3, γγγγ3), …} contoh: δ(q0, a, a) = {(q0, aa), (q1, ε)}
Studi Kasus
1. PDA yang dapat mengenali kalimat 0n1n, n = 1, 2, 3, …
P1 = (Q, Σ, Γ, δ, S, Z0, F) ; Q = {q0, q1} ; Σ = {0, 1}; ΓΓΓ = {0, 1, Z} ; S = qΓ 0; Z0= Z ; F = {} ; δ sebagai berikut : (1) δ(q0, 0, Z) = {(q0, 0Z)} (4) δ(q1, 1, 0) = {(q1, ε)} (2) δ(q0, 0, 0) = {(q0, 00)} (5) δ(q1, ε, Z) = {(q1, ε)} (3) δ(q0, 1, 0) = {(q1, ε)}
Jika diberikan string input 00110101, bagaimanakah perubahan deskripsi sesaat dan komputasinya ? Termasuk ke dalam jenis PDA apakah P1?Jelaskan alasannya! 2. PDA P2= (Q, Σ, Γ, δ, S, Z0, F) Q = {q, p} ; Σ = {a, b} ; Γ = {a, b, S, Z} ; S = q ; Z0= Z ; F = {p} ; δ sebagai berikut : (1) δ(q, a, b) = {(q, bS)} (6) δ(q, a, S) = {(q, aS)} (11) δ(q, a, bSb) = {(q, S)} (2) δ(q, b, b) = {(q, bb)} (7) δ(q, b, Z) = {(q, bZ)} (12) δ(q, ε, aZ) = {(p, ε)} (3) δ(q, b, bS) = {(q, a)} (8) δ(q, b, a) = {(q, ba)} (13) δ(q, b, S) = {(q, bS)} (4) δ(q, a, Z) = {(q, aZ)} (9) δ(q, b, ba) = {(q, aS)} (14) δ(q, ε, bSb) = {(q, S)} (5) δ(q, a, a) = {(q, aa)} (10) δ(q, a, aSa) = {(q, S)} (15) δ(q, ε, aSa) = {(q, S)}
Jika diberikan input bbaabb, bagaimanakah perubahan deskripsi sesaat dan komputasinya? Termasuk ke dalam jenis PDA apakah P2?Jelaskan alasannya! 21 IF-UTAMA
Studi Kasus
[9]
3.
PDA P
3= (Q, Σ, Γ, δ, S, Z
0, F)
Q = {q
0, q
1, q
2} ; Σ = {a, b} ; Γ = {a, b, Z} ; S=q
0; Z
0= Z ; F = {q2} ;
δ sebagai berikut :
a. δ(q0, a, Z) = {(q0, aZ)} b. δ(q0, b, Z) = {(q0, bZ)} c. δ(q0, a, a) = {(q0, aa), (q1, ε)} d. δ(q0, a, b) = {(q0, ab)} e. δ(v, b, a) = {(q0, ba)} f. δ(q0, b, b) = {(q0, bb), (q1, ε)} g. δ(q1, a, a) = {(q1, ε)} h. δ(q1, b, b) = {(q1, ε)} i. δ(q1, ε, Z) = {(q2, ε)}Bagaimanakah perubahan deskripsi sesaat dan komputasinya
jika diberi input ababab? Termasuk ke dalam jenis PDA apakah
P3?Jelaskan alasannya
22
Konstruksi Push Down Automata
Pertemuan 12
Dosen Pembina :
Danang Junaedi
Cara Mengkonstruksi PDA
[9]
•
Analisis skenario kerja PDA
– Buat skenario kerja PDA
– Definisi status-status yang diperlukan
– Definisikan PDA
•
Analisis tata bahasa Berdasarkan Tata
Bahasa Bebas Konteks
24 IF-UTAMA
Analisis skenario kerja PDA
[9]
•
Contoh : PDA yang mengenali 0
i1
2i– Buat skenario kerja PDA
1. Jika saat awal, head membaca 0, TOS = Z, maka mesin telah membaca 0, simpan 00 ke dalam stack.
2. Jika mesin telah membaca 0, head membaca 0, TOS = 0, maka mesin telah membaca 0, simpan 00 ke dalam stack.
3. Jika mesin telah membaca 0, head membaca 1, TOS = 0, maka mesin telah membaca 1, ambil simbol teratas dari stack.
4. Jika mesin telah membaca 1, head membaca 1, TOS = 0, maka mesin telah membaca 1, ambil simbol teratas dari stack.
5. Jika mesin telah membaca 1, input sudah habis, TOS = Z, maka mesin telah membaca 1, ambil simbol teratas dari stack, head tidak bergerak.
– Definisi status-status yang diperlukan
• q0= saat awal
• q1= mesin telah membaca 0
• q2 = mesin telah membaca 1
25 IF-UTAMA
Analisis skenario kerja PDA
[9]
•
Contoh : PDA yang mengenali 0
i1
2i(Lanjutan)
– Definisikan PDA
P4 = (Q, Σ, Γ, δ, S, Z0, F)
• Q = himpunan status = {q0, q1, q2}
• Σ = himpunan simbol-simbol yang dibaca oleh head = {0, 1} • Γ = himpunan simbol - simbol yang disimpan ke dalam stack = {0,Z} • S = q0
• Z0= Z
• F = {}
• Terjemahkan skenario kerja ke dalam fungsi transisi 1. δ(q0, 0, Z) = {(q1, 00Z)} 2. δ(q1, 0, 0) = {(q1, 000)} 3. δ(q1, 1, 0) = {(q2, ε)} 4. δ(q2, 1, 0) = {(q2, ε)} 5. δ(q2, ε, Z) = {(q2, ε)} 26 IF-UTAMA
Analisis tata bahasa Berdasarkan
Tata Bahasa Bebas Konteks
[9]•
Jika terdapat tata bahasa bebas konteks G = (Vn, Vt, P, S), maka
PDA yang mengenali bahasa L(G) adalah
– P = (Q, Σ, Γ, δ, S, Z0, F) ; Q = {q} ;
– Σ = Vt ; Γ = (Vn U Vt) ; S = q ; Z0= S ; F = {} dan – δ sebagai berikut :
1. Jika A α anggota dari P, maka δ(q, ε, A) mengandung (q, α) 2. δ(q, a, a) = {(q, ε)} untuk seluruh a ∈ Vt
•
Contoh: Tata bahasa bebas konteks untuk {0
n1
2n| n > 0}
G = (Vn, Vt, P, S); Vn = {X} ; Vt = {0, 1} ; S = S ;
P : {
X 0X11 X 011}
–
Maka
P5= (Q, Σ, Γ, δ, q0, Z0, F) ; Q = {q} ; Σ = {0, 1} ; Γ = {X, 0, 1} ; S = q;
Z0= S ; F = {} dan δ sebagai berikut : 1. δ(q, ε, X) = {(q, 0X11)} … ketentuan 1 2. δ(q, ε, X) = {(q, 011)} … ketentuan 1 3. δ(q, 0, 0) = {(q, ε)} … ketentuan 2 4. δ(q, 1, 1) = {(q, ε)} … ketentuan 2
Studi Kasus
[9]
1. Buktikanlah bahwa kalimat 001111 diterima oleh P
4dan
P
52. Buatlah PDA diterministik P
6yang dapat mengenali
bahasa {0
n1
n| n > 0} U {1
n0
n| n > 0}
3. Buatlah PDA P
7yang dapat mengenali bahasa L(G), di
mana G adalah sebagai berikut :
G = (Vn, Vt, P, S) ; Vn = {N, D} ; Vt = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} ;
S = N ; P sebagai berikut :
•
N D
•
N ND
•
D
0 | 1 | 2 | 3P
7= (Q, Σ, Γ, δ, q
0, Z
0, F) ; Q = {q0, q1} ;
Σ = {0, 1}; ΓΓΓΓ = {0, 1, Z} ; q0= q0 ; Z0= Z ; F = {} ;4. Buatlah PDA P
8yang mampu mengenali bahasa
Referensi
1. http://www.dit.hcmut.edu.vn/~tru/AUTOMATA/chapter7.ppt, Tanggal Akses : 11 April 2009
2. http://www.cs.fit.edu/~dmitra/FormaLang/PushdownAutomata.ppt, Tanggal Akses 11 April 2009
3. http://www.cs.nctu.edu.tw/~lwhsu/course/pl/slides/PZ03A.ppt, Tanggal Akses : 11 April 2009
4. http://semadionline.baliseven.com/materi/push_Down.ppt, Tanggal Akses : 8 April 2009
5. http://www.cs.unm.edu/~joel/cs401/Pushdown%20Automaton.ppt, Tanggal Akses 11 April 2009
6. http://profile.iiita.ac.in/IIT2006110/Documents/fat/pushdown%20automata%20 230307%201800.ppt, Tanggal Akses : 11 April 2009
7. http://www.csc.lsu.edu/~busch/courses/theorycomp/fall2008/slides/PDA.ppt, Tanggal Akses : 11 April 2009
8. http://www.cs.fit.edu/~dmitra/FormaLang/PushdownAutomata.ppt, Tanggal Akses : 11 April 2009
9. Roni Djuliawan, M.T., “Diktat & Handout Kuliah Teori Bahasa & Otomata”, Teknik Informatika – Universitas Widyatama, 2003
29 IF-UTAMA