-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanuale fondamenti.tex
1618 lines (1448 loc) · 75.4 KB
/
manuale fondamenti.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% ATTENZIONE! Nel documento uso tikz, però sono ancora un nabbo e quindi potreste trovare porcherie
\documentclass[a4paper,oneside]{scrbook}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{xcolor}
\usepackage{cancel}
\usepackage{enumitem}
\usepackage{listings}
\hypersetup{
colorlinks,
citecolor=black,
filecolor=black,
linkcolor=black,
urlcolor=black
}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{tikz-qtree,tikz-qtree-compat}
\newcommand{\greenmark}{\quad \textcolor{green}{\checkmark}}
\newcommand{\blue}[1]{\textcolor{blue}{#1}}
\newcommand*{\circled}[2][]{\tikz[baseline=(C.base)]{\node[inner sep=0pt] (C) {\vphantom{1g}#2};\node[draw, circle, inner sep=2pt, yshift=1pt] at (C.center) {\vphantom{1g}};}}
%opening
\title{Manuale di sopravvivenza per l'esame Fondamenti di Informatica}
\subtitle{Una guida pratica per chi deve affrontare questo esame apparentemente insormontabile}
\author{Matteo Iervasi}
\date{Aprile 2018}
\begin{document}
\maketitle
\tableofcontents
\chapter*{Prefazione}
\addcontentsline{toc}{chapter}{Prefazione}
Questo documento ha lo scopo di dare un'idea al povero studente che deve affrontare il temibile esame di \textit{Fondamenti di Informatica} di come si affrontano gli esercizi.\\
\textbf{{\large NON}} tratterò l'aspetto teorico, per tanto assumo che si abbia già studiato (o almeno tentato di studiare) quella parte.
Mi rendo conto che la materia in questione sia piuttosto ostica, ma vi posso assicurare che una volta compresi i concetti base il resto verrà da se.
Ovviamente è \textbf{fondamentale} fare molti esercizi, in modo da verificare e consolidare l'apprendimento. Cercherò di essere il più chiaro possibile, ma non essendo mai stato bravo
a spiegare potrebbero esserci dei punti non chiari, per i quali chiedo scusa in anticipo.
Voglio ringraziare Jenny Bonato ed Elia Brentarolli per l'immensa pazienza che hanno avuto per insegnare a questo somaro le basi di questa materia, e Andrea Caucchiolo per avermi corretto un po' di errori.
Qualora dovreste trovare degli errori, scrivetemi a \href{mailto:[email protected]}{[email protected]}, oppure fate direttamente una ‘‘\textit{pull request}’’ nel repository GitHub.
\chapter{Linguaggi regolari}
Anche se molto spesso il primo esercizio non tratta un linguaggio regolare, è sempre utile sapere come si deve procedere.
Come dice il prof. Giacobazzi, la prima cosa da fare quando si osserva un linguaggio è capire \textit{intuitivamente} a che classe appartiene (regolare, CF, ecc.).
L'intuizione può essere allenata con la pratica, tuttavia esiste un trucco molto utile: se nel linguaggio è necessario ‘‘contare’’ in qualche modo qualcosa, allora \textbf{sicuramente}
questo linguaggio non sarà regolare. Dopo aver intuitivamente classificato il linguaggio bisogna procedere con la dimostrazione. Nel caso in cui il linguaggio sia \textbf{regolare}, bisogna costruire l'automa e dimostrarne la correttezza.
Ricordiamo che un'automa è scrivibile come una quintupla $<Q,\Sigma,\delta,q_0,F>$, dove:
\begin{itemize}
\item $Q$ è l'insieme degli stati
\item $\Sigma$ è l'alfabeto di input
\item $\delta:Q\times\Sigma\rightarrow Q$ è la funzione di transizione di uno stato
\item $q_0$ è lo stato iniziale
\item $F\subseteq Q$ è l'insieme degli stati finali
\end{itemize}
Prendiamo in esame il seguente linguaggio:
$$ L=\{\{0,1\}^* \text{ t.c. ci sono almeno due 0 consecutivi e non ci sono mai due 1 consecutivi}\} $$
Disegniamo l'automa corrispondente (non mi soffermo sul come farlo, questo è argomento del corso di Architettura degli Elaboratori):
\begin{center}
\begin{tikzpicture}[scale=0.2]
\tikzstyle{every node}+=[inner sep=0pt]
\draw [black] (10.3,-30.4) circle (3);
\draw (10.3,-30.4) node {$q_0$};
\draw [black] (23.6,-21.3) circle (3);
\draw (23.6,-21.3) node {$q_1$};
\draw [black] (23.6,-41) circle (3);
\draw (23.6,-41) node {$q_4$};
\draw [black] (44.1,-21.3) circle (3);
\draw (44.1,-21.3) node {$q_2$};
\draw [black] (44.1,-21.3) circle (2.4);
\draw [black] (44.1,-41.9) circle (3);
\draw (44.1,-41.9) node {$q_3$};
\draw [black] (44.1,-41.9) circle (2.4);
\draw [black] (34.5,-51.6) circle (3);
\draw (34.5,-51.6) node {$q_\bot$};
\draw [black] (12.78,-28.71) -- (21.12,-22.99);
\fill [black] (21.12,-22.99) -- (20.18,-23.03) -- (20.75,-23.86);
\draw (18.1,-26.35) node [below] {$0$};
\draw [black] (12.65,-32.27) -- (21.25,-39.13);
\fill [black] (21.25,-39.13) -- (20.94,-38.24) -- (20.32,-39.02);
\draw (15.79,-36.19) node [below] {$1$};
\draw [black] (22.648,-38.157) arc (-164.71686:-195.28314:26.582);
\fill [black] (22.65,-38.16) -- (22.92,-37.25) -- (21.95,-37.52);
\draw (21.21,-31.15) node [left] {$1$};
\draw [black] (24.462,-24.172) arc (13.77659:-13.77659:29.302);
\fill [black] (24.46,-24.17) -- (24.17,-25.07) -- (25.14,-24.83);
\draw (25.81,-31.15) node [right] {$0$};
\draw [black] (25.75,-43.09) -- (32.35,-49.51);
\fill [black] (32.35,-49.51) -- (32.12,-48.59) -- (31.43,-49.31);
\draw (27.88,-46.78) node [below] {$1$};
\draw [black] (26.6,-21.3) -- (41.1,-21.3);
\fill [black] (41.1,-21.3) -- (40.3,-20.8) -- (40.3,-21.8);
\draw (33.85,-21.8) node [below] {$0$};
\draw [black] (44.91,-18.423) arc (192.01279:-95.98721:2.25);
\draw (49.78,-15.79) node [above] {$0$};
\fill [black] (46.88,-20.19) -- (47.76,-20.52) -- (47.55,-19.54);
\draw [black] (45.211,-24.085) arc (18.18012:-18.18012:24.087);
\fill [black] (45.21,-39.12) -- (45.94,-38.51) -- (44.99,-38.2);
\draw (46.91,-31.6) node [right] {$1$};
\draw [black] (41.99,-44.03) -- (36.61,-49.47);
\fill [black] (36.61,-49.47) -- (37.53,-49.25) -- (36.82,-48.55);
\draw (38.78,-45.27) node [left] {$1$};
\draw [black] (43.177,-39.047) arc (-165.0509:-194.9491:28.868);
\fill [black] (43.18,-24.15) -- (42.49,-24.8) -- (43.45,-25.05);
\draw (41.7,-31.6) node [left] {$0$};
\end{tikzpicture}
\end{center}
Ora è necessario dimostrare la correttezza dell'automa.
Dobbiamo dimostrare che $x\in L \Leftrightarrow x\in L(n)$, tuttavia non dimostriamo in maniera diretta la doppia implicazione, ma dimostriamo separatamente le seguenti:
\begin{enumerate}
\item $x\in L \Rightarrow x \in L(m)$
\item $x\notin L \Rightarrow x \notin L(m)$
\end{enumerate}
\begin{enumerate}
\item $x\in L \Rightarrow x \in L(m)$\\
Innanzitutto ci troviamo un \textbf{caso base} utile per il \textbf{passo induttivo}. Qual'è la stringa più piccola $\in L$? È ‘‘$00$’’\\
\textbf{Passo base}\\
$ \delta(q_0,00)=q_2 \in F \greenmark $\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \in L \Rightarrow x\in L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$.\\
Visto che la funzione di transizione gode della proprietà della composizione, posso considerare $\delta(\delta(q_0,x),a)$.
Nel nostro caso però abbiamo due stati finali, $q_2$ e $q_3$, quindi dobbiamo guardare entrambi.
\begin{itemize}
\item $\delta(q_0,x)=q_2$\\
Se $a=0$ allora $\delta(q_2,0)=q_2 \in F \greenmark$\\
Se $a=1$ allora $\delta(q_2,1)=q_3 \in F \greenmark$
\item $\delta(q_0,x)=q_3$\\
Se $a=0$ allora $\delta(q_3,0)=q_2 \in F \greenmark$\\
Se $a=1$ allora $\delta(q_3,1)=q_\bot \notin F \greenmark$ (è giusto visto che in questo caso $y\notin L$, dato che ci sarebbero due 1 consecutivi)
\end{itemize}
\item $x\notin L \Rightarrow x \notin L(m)$\\
In questa caso dobbiamo trovare i casi che \textbf{NON} finiscono in stati finali.\\
\textbf{Passo base}\\
$x=0 \rightarrow \delta(q_0,0)=q_1 \notin F \text{ (non ho almeno due 0 consecutivi)} \greenmark$\\
$x=11 \rightarrow \delta(q_0,11)=q_\bot \notin F \text{ (ho due 1 consecutivi)} \greenmark$\\
$x=\varepsilon \rightarrow \delta(q_0,\varepsilon)=q_0 \notin F \greenmark$\\
$x=1 \rightarrow \delta(q_0,1)=q_4 \notin F \greenmark$\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \notin L \Rightarrow x\notin L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$. Ricordiamoci che possiamo usare $\delta(\delta(q_0,x),a)$.\\
In modo analogo alla precedente dimostrazione, dobbiamo considerare gli stati non finali.
\begin{itemize}
\item $\delta(q_0,x)=q_0$\\
Se $a=0$ allora $\delta(q_0,0)=q_1 \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_0,1)=q_4 \notin F \greenmark$
\item $\delta(q_0,x)=q_1$\\
Se $a=0$ allora $\delta(q_1,0)=q_2 \in F \greenmark$ (è giusto visto che in questo caso $y\in L$, dato che ci sono \textbf{almeno} due 0 consecutivi)\\
Se $a=1$ allora $\delta(q_1,1)=q_4 \notin F \greenmark$
\item $\delta(q_0,x)=q_4$\\
Se $a=0$ allora $\delta(q_4,0)=q_1 \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_4,1)=q_\bot \notin F \greenmark$
\item $\delta(q_0,x)=q_\bot$\\
Se $a=0$ allora $\delta(q_\bot,0)=q_\bot \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_\bot,1)=q_\bot \notin F \greenmark$
\end{itemize}
\end{enumerate}
Abbiamo quindi dimostrato la doppia implicazione e con essa la \textit{correttezza} del nostro automa.
Facciamo un altro esempio, prendiamo come linguaggio
$$ L=\{\{0,1\}^* \text{ t.c. gli 0 sono sempre a coppie}\} $$
Ecco l'automa:
\begin{center}
\begin{tikzpicture}[scale=0.2]
\tikzstyle{every node}+=[inner sep=0pt]
\draw [black] (20.9,-26.9) circle (3);
\draw (20.9,-26.9) node {$q_0$};
\draw [black] (20.9,-26.9) circle (2.4);
\draw [black] (36.2,-26.9) circle (3);
\draw (36.2,-26.9) node {$q_1$};
\draw [black] (50.6,-26.9) circle (3);
\draw (50.6,-26.9) node {$qb$};
\draw [black] (23.732,-25.918) arc (104.62203:75.37797:19.088);
\fill [black] (33.37,-25.92) -- (32.72,-25.23) -- (32.47,-26.2);
\draw (28.55,-24.8) node [above] {$0$};
\draw [black] (33.33,-27.765) arc (-77.20264:-102.79736:21.58);
\fill [black] (23.77,-27.77) -- (24.44,-28.43) -- (24.66,-27.46);
\draw (28.55,-28.8) node [below] {$0$};
\draw [black] (39.2,-26.9) -- (47.6,-26.9);
\fill [black] (47.6,-26.9) -- (46.8,-26.4) -- (46.8,-27.4);
\draw (43.4,-27.4) node [below] {$1$};
\draw [black] (53.28,-25.577) arc (144:-144:2.25);
\draw (57.85,-26.9) node [right] {$0,1$};
\fill [black] (53.28,-28.22) -- (53.63,-29.1) -- (54.22,-28.29);
\draw [black] (18.22,-28.223) arc (-36:-324:2.25);
\draw (13.65,-26.9) node [left] {$1$};
\fill [black] (18.22,-25.58) -- (17.87,-24.7) -- (17.28,-25.51);
\end{tikzpicture}
\end{center}
Dobbiamo dimostrare che $x\in L \Leftrightarrow x\in L(n)$:
\begin{enumerate}
\item $x\in L \Rightarrow x \in L(m)$
\item $x\notin L \Rightarrow x \notin L(m)$
\end{enumerate}
\begin{enumerate}
\item $x\in L \Rightarrow x \in L(m)$\\
\textbf{Passo base}\\
$ x=\varepsilon \rightarrow \delta(q_0,\varepsilon)=q_0 \in F \greenmark $\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \in L \Rightarrow x\in L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$.\\
Visto che la funzione di transizione gode della proprietà della composizione, posso considerare $\delta(\delta(q_0,x),a)$.\\
$\delta(q_0,x)=q_0$\\
Se $a=0$ allora $\delta(q_0,0)=q_1 \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_0,1)=q_0 \in F \greenmark$
\item $x\notin L \Rightarrow x \notin L(m)$\\
\textbf{Passo base}\\
$x=0 \rightarrow \delta(q_0,0)=q_1 \notin F \greenmark$\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \notin L \Rightarrow x\notin L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$. Ricordiamoci che possiamo usare $\delta(\delta(q_0,x),a)$.\\
In modo analogo alla precedente dimostrazione, dobbiamo considerare gli stati non finali.
\begin{itemize}
\item $\delta(q_0,x)=q_1$\\
Se $a=0$ allora $\delta(q_1,0)=q_0 \in F \greenmark$\\
Se $a=1$ allora $\delta(q_1,1)=q_\bot \notin F \greenmark$
\item $\delta(q_0,x)=q_\bot$\\
Se $a=0$ allora $\delta(q_\bot,0)=q_\bot \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_\bot,1)=q_\bot \notin F \greenmark$
\end{itemize}
\end{enumerate}
Abbiamo quindi dimostrato la doppia implicazione e con essa la \textit{correttezza} del nostro automa.
\chapter{Linguaggi liberi dal contesto e grammatiche}
Questa categoria è più rognosa di quella dei regolari, ma fortunatamente se dovesse capitarci non dobbiamo costruire un'automa. Dobbiamo però costruire la grammatica,
una procedura non sempre immediata purtroppo. Un utile trucco per riconoscere un linguaggio CF è immaginare un'automa con una pila: ad esempio se dobbiamo riconoscere il linguaggio
$0^n1^n$ il nostro automa mano a mano che legge gli $0$ li mette in una pila, dopodiché leggendo gli $1$ la scarica, e se a fine stringa la pila è vuota allora il linguaggio è riconosciuto.\\
\textit{Nota}: l'automa non è costretto a leggere linearmente la stringa, nell'esempio di prima possiamo anche leggere gli 1 a partire dal fondo, facendo valere la stessa regola dello
svuotamento della pila.
Prendiamo in esame il seguente linguaggio
$$L=\{x\in\{0,1\}^*\text{ t.c. ci siano tanti 0 quanti 1}\}$$
Intuitivamente notiamo che è necessario ‘‘contare’’ il numero di $0$ e far si che sia uguale al numero di $1$. Esempi di possibili combinazioni:
\begin{itemize}
\item $01$
\item $00001111$
\item $01010101$
\item $011010$
\end{itemize}
Notiamo che non è necessario che ci sia una determinata sequenza, basta solo che gli $0$ totali siano uguali agli $1$.
Pensandoci un po', viene naturale classificare questo linguaggio nella classe dei \textit{context free}.
Per dimostrarlo occorre prima applicare il cosiddetto \textbf{pumping lemma}, in modo da mostrare che non può essere regolare.
Successivamente dobbiamo scrivere una \textbf{grammatica} che genera il linguaggio e \textbf{dimostrarne la correttezza}.\\
\textbf{Fase 1: Pumping Lemma}\\
In questa fase ci basta prendere una stringa appartenente al linguaggio e dimostrare che ‘‘pompandone’’ una parte noi usciamo dal linguaggio.
Ricordiamoci che nell'applicare il pumping lemma dobbiamo sottostare a dei vincoli:
\begin{itemize}
\item $z=uvw$
\item $|z|\geq k$
\item $|uv|\leq k$
\item $|v|>0$
\item $\forall i \in \mathbb{N}.uv^iw\in L \text{ con }i\geq0$
\end{itemize}
Ovviamente non siamo stupidi, quindi scegliamo una stringa facile, in questo caso $z=0^n1^n$.
\begin{center}
\begin{tikzpicture}
% String rectangle
\draw (0,0.75) -- (0,1.25) node(0){};
\draw (0,0.75) -- (5,0.75) node[midway,above]{0};
\draw (5,0.75) -- (5,1.25) node(5){};
\draw (5,0.75) -- (10,0.75) node[midway,above]{1};
\draw (10,0.75) -- (10,1.25) node(10){};
% Bracket rectangle
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] (0.north east) -- (5.north west) node [midway, above=0.2cm] {n};
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] (5.north east) -- (10.north west) node [midway, above=0.2cm] {n};
% String division rectangle
\draw (0,0) -- (0,0.5);
\draw (0,0) -- (2.5,0) node[midway,above]{$u$};
\draw (2.5,0) -- (2.5,0.5);
\draw (2.5,0) -- (5,0) node[midway,above]{$v$};
\draw (5,0) -- (5,0.5);
\draw (5,0) -- (10,0) node[midway,above]{$w$};
\draw (10,0) -- (10,0.5);
\end{tikzpicture}
\end{center}
La stringa apparterrà al linguaggio solamente se $0^a1^b\in L \Leftrightarrow a=b$.\\
La nostra stringa $z$ sarà quindi composta da $0^{k-|v|}0^{i|v|}1^k$. Per soddisfare la condizione appena scritta sopra $k-|v|+|v|i=k$, quindi:\\
$-|v|+|v|i=0$\\
$|v|(i-1)=0$\\
Ma questo è un assurdo! Infatti $|v|$ non può essere uguale a $0$ per i vincoli del pumping lemma.
Vediamo che per $i=0 \wedge i\geq2$ usciamo dal linguaggio, quindi possiamo dire che è sicuramente non regolare. $\greenmark$\\
\textbf{Fase 2: Grammatica}\\
Una possibile grammatica per questo linguaggio è:
$$S \rightarrow S1S0S|S0S1S|\varepsilon$$
\textbf{Fase 3: Dimostrazione della grammatica}\\
{\LARGE ATTENZIONE! DIMOSTRAZIONE INCOMPLETA, NON HO VOGLIA DI FINIRLA PERCHÉ TROPPO LUNGA!}\\
Per dimostrare la correttezza della nostra grammatica, dobbiamo dimostrare la seguente condizione:
$$x\in L \Leftrightarrow S \Rightarrow_* x$$
Essendo una doppia implicazione, dimostreremo separatamente
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $, che si dimostra per \textit{induzione} sulla lunghezza della stringa
\item $ S \Rightarrow_i k \Rightarrow x \in L$, che si dimostra per \textit{induzione} sul numero di passi di derivazione
\end{enumerate}
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $\\
\textbf{Passo base}\\
$y=\varepsilon \qquad y \in L$ ed $\exists S\Rightarrow \varepsilon \greenmark $ (esiste una derivazione S che porta ad $\varepsilon$).\\
\textbf{Passo induttivo}\\
Consideriamo $y \in \Sigma^*$ tale che $|y|<n$ e supponiamo che $\forall y \in L \Rightarrow S \Rightarrow_* y$.\\
Allora prendiamo $|x|\geq n$ e sapendo che $y$ è composto come $y=0^k1^k$, $x$ sarà $0^m1^m$ con $m>k$. Procediamo prendendo $m=k+1$:
$$ x=0^{k+1}1^{k+1}$$
Sappiamo per ipotesi che esiste una derivazione $S\Rightarrow_i 0^k1^k$, quindi
$$\exists S \Rightarrow y \rightarrow \exists S \Rightarrow_i 0^k1^k$$
ma se esiste questa esiste anche la derivazione precedente, ovvero
$$\exists S \Rightarrow_{i-1} 0^{k-1}S1^{k-1} ...$$
essendo che la produzione $S\rightarrow 0S1$ appartiene all'insieme delle possibili produzioni, posso sostituire:
$$... \Rightarrow_i 0^{k-1}0S11^{k-1} \Rightarrow_{i+1} 0^k011^k = 0^{k+1}1^{k+1} = x \greenmark$$
In pratica siamo tornati indietro di un passo e abbiamo applicato una sostituzione che ci conducesse alla stringa $x$.\\
Ripetiamo lo stesso procedimento per $y=1^k0^k$ e $x=1^{k+1}0^{k+1}$:
$$\exists S \Rightarrow y \rightarrow \exists S \Rightarrow_i 1^k0^k \Rightarrow_{i-1} 1^{k-1}S0^{k-1}$$
$$\Rightarrow_i 1^{k-1}1S00^{k-1} \Rightarrow_{i+1} 1^k100^k = 1^{k+1}0^{k+1} = x \greenmark$$
\item $ S \Rightarrow_i k \Rightarrow x \in L$\\
\textbf{Passo base}\\
$S\rightarrow 01 \qquad x=01 \quad x \in L$\\
$S\rightarrow 10 \qquad x=10 \quad x \in L$\\
\textbf{Passo induttivo}\\
$\forall i \leq n \quad S \Rightarrow_i y \Rightarrow y \in L $ (in $i$ passi otteniamo una stringa $\in L$, per ogni $i\leq n$).
Sappiamo che $y$ sarà nella forma $0^k1^k$ visto che $\in L$, quindi $S \Rightarrow_i 0^k1^k$. Ma se esiste quella produzione, allora
esisterà anche quella precedente. Con lo stesso gioco che abbiamo applicato nella dimostrazione precedente, andiamo indietro per poi
andare avanti con una produzione:
$$S \Rightarrow_i 0^k1^k \Rightarrow \exists S \Rightarrow_{i-1} 0^{k-1}S1^{k-1}$$
$$\Rightarrow_i 0^{k-1}0S11^{k-1} \Rightarrow_{i+1} 0^k011^k = 0^{k+1}1^{k+1} \in L \greenmark$$
Per $y=1^k0^k$:
$$S \Rightarrow_i 1^k0^k \Rightarrow \exists S \Rightarrow_{i-1} 1^{k-1}S0^{k-1}$$
$$\Rightarrow_i 1^{k-1}1S00^{k-1} \Rightarrow_{i+1} 1^k100^k = 1^{k+1}0^{k+1} \in L \greenmark$$
In pratica mostriamo che andando avanti otteniamo una stringa che appartiene ancora al linguaggio.
\end{enumerate}
Facciamo un altro esempio, prendiamo come linguaggio
$$L={a^nb^n}$$
Sappiamo che è CF perché è necessario ‘‘tener conto’’ di quante $a$ sono state riconosciute per verificare che siano pari al numero di $b$.\\
\textbf{Fase 1: Pumping Lemma}\\
Scegliamo una stringa facile che rispetti i vincoli, come $a^nb^n$.
\begin{itemize}
\item $z=uvw$
\item $|z|\geq k$
\item $|uv|\leq k$
\item $|v|>0$
\item $\forall i \in \mathbb{N}.uv^iw\in L \text{ con }i\geq0$
\end{itemize}
L'unica suddivisione possibile è:
\begin{center}
\begin{tikzpicture}
% String rectangle
\draw (0,0.75) -- (0,1.25) node(0){};
\draw (0,0.75) -- (5,0.75) node[midway,above]{a};
\draw (5,0.75) -- (5,1.25) node(5){};
\draw (5,0.75) -- (10,0.75) node[midway,above]{b};
\draw (10,0.75) -- (10,1.25) node(10){};
% Bracket rectangle
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] (0.north east) -- (5.north west) node [midway, above=0.2cm] {n};
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] (5.north east) -- (10.north west) node [midway, above=0.2cm] {n};
% String division rectangle
\draw (0,0) -- (0,0.5);
\draw (0,0) -- (2.5,0) node[midway,above]{$u$};
\draw (2.5,0) -- (2.5,0.5);
\draw (2.5,0) -- (5,0) node[midway,above]{$v$};
\draw (5,0) -- (5,0.5);
\draw (5,0) -- (10,0) node[midway,above]{$w$};
\draw (10,0) -- (10,0.5);
\end{tikzpicture}
\end{center}
La stringa apparterrà al linguaggio solamente se $a^xb^y\in L \Leftrightarrow x=y$.\\
La nostra stringa $z$ sarà quindi composta da $a^{k-|v|}a^{i|v|}b^k$. Per soddisfare la condizione appena scritta sopra $k-|v|+|v|i=k$, quindi:\\
$-|v|+|v|i=0$\\
$|v|(i-1)=0$\\
Ma questo è un assurdo! Infatti $|v|$ non può essere uguale a $0$ per i vincoli del pumping lemma.
Vediamo che per $i=0 \wedge i\geq2$ usciamo dal linguaggio, quindi possiamo dire che è sicuramente non regolare.$\greenmark$\\
\textbf{Fase 2: Grammatica}\\
Una possibile grammatica per questo linguaggio è:
$$S \rightarrow aSb|\varepsilon$$
Mentre una possibile derivazione per questa grammatica è:
\begin{center}
\begin{tikzpicture}
\Tree
[.\blue{S}
[.a\blue{S}b
[.aa\blue{S}bb aabb ]
[.ab ]
]
[.$\varepsilon$ ]
]
\end{tikzpicture}
\end{center}
\textbf{Fase 3: Dimostrazione della grammatica}\\
Per dimostrare la correttezza della nostra grammatica, dobbiamo dimostrare la seguente condizione:
$$x\in L \Leftrightarrow S \Rightarrow_* x$$
Essendo una doppia implicazione, dimostreremo separatamente
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $, che si dimostra per \textit{induzione} sulla lunghezza della stringa
\item $ S \Rightarrow_i k \Rightarrow x \in L$, che si dimostra per \textit{induzione} sul numero di passi di derivazione
\end{enumerate}
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $\\
\textbf{Passi base}\\
$y=\varepsilon \qquad y \in L$ ed $\exists S\Rightarrow \varepsilon \greenmark$.\\
\textbf{Passo induttivo}\\
Consideriamo $y \in \Sigma^*$ tale che $|y|<n$ e supponiamo che $\forall y \in L \Rightarrow S \Rightarrow_* y$.\\
Allora prendiamo $|x|\geq n$ e sapendo che $y$ è composto come $y=a^kb^k$, $x$ sarà $x=a^mb^m$ con $m>k$. Procediamo prendendo $m=k+1$:
$$ x=a^{k+1}b^{k+1}$$
Sappiamo per ipotesi che esiste una derivazione $S\Rightarrow_i a^kb^k$, quindi
$$\exists S \Rightarrow y \rightarrow \exists S \Rightarrow_i a^kb^k$$
ma se esiste questa esiste anche la derivazione precedente, ovvero
$$\exists S \Rightarrow_{i-1} a^{k}Sb^{k} ...$$
essendo che la produzione $S\rightarrow aSb$ appartiene all'insieme delle possibili produzioni, posso sostituire:
$$... \Rightarrow_i a^{k}aSbb^{k} \Rightarrow_{i+1} a^kabb^k = a^{k+1}b^{k+1} = x \greenmark$$
In pratica siamo tornati indietro di un passo e abbiamo applicato una sostituzione che ci conducesse alla stringa $x$.
\item $ S \Rightarrow_i k \Rightarrow x \in L$\\
\textbf{Passo base}\\
$S\rightarrow \varepsilon \qquad x=\varepsilon \quad x \in L$\\
\textbf{Passo induttivo}\\
$\forall i \leq n \quad S \Rightarrow_i y \Rightarrow y \in L $ (in $i$ passi otteniamo una stringa $\in L$, per ogni $i\leq n$).
Sappiamo che $y$ sarà nella forma $a^kb^k$ visto che $\in L$, quindi $S \Rightarrow_i a^kb^k$. Ma se esiste quella produzione, allora
esisterà anche quella precedente. Con lo stesso gioco che abbiamo applicato nella dimostrazione precedente, andiamo indietro per poi
andare avanti con una produzione:
$$S \Rightarrow_i a^kb^k \Rightarrow \exists S \Rightarrow_{i-1} a^{k}Sb^{k}$$
$$\Rightarrow_i a^{k}aSbb^{k} \Rightarrow_{i+1} a^kabb^k = a^{k+1}b^{k+1} \in L \greenmark$$
\end{enumerate}
\chapter{Linguaggi non liberi dal contesto}
Quando un linguaggio richiede più di un singolo conteggio per essere riconosciuto, allora è molto probabile che non sia libero dal contesto.
In quel caso, si procede utilizzando il \textbf{pumping lemma} per i linguaggi CF, in modo da dimostrare la sua non appartenenza a questa classe.
A differenza del pumping lemma per i linguaggi regolari, le suddivisioni possibili sono molte di più, e bisogna dimostrare la fuoriuscita per ognuna di esse, quindi la rottura di palle è decisamente più elevata.
Prendiamo in esempio il linguaggio
$$L=\{a^nb^nc^n\}$$
Esso è chiaramente non-CF, infatti $|a|=|b|=|c|$ e quindi dobbiamo contare due volte, cosa non possibile in un'automa a pila.
Posto che $\forall z$ tale che $|z|>n$, dobbiamo soddisfare i seguenti vincoli:
\begin{itemize}
\item $\forall z = uvwxy$
\item $|z| \geq k$
\item $|vwx| \leq k$
\item $|vx|>0$
\item $\forall i. uv^iwx^iy \in L$
\end{itemize}
elenchiamo le diverse possibili suddivisioni:
\begin{center}
\begin{tikzpicture}
% Defining main coordinates
\coordinate (a0) at (0,0);
\coordinate (b0) at (4.5,0);
\coordinate (c0) at (9,0);
\coordinate (d0) at (13.5,0);
\coordinate (a1) at (0,0.5);
\coordinate (b1) at (4.5,0.5);
\coordinate (c1) at (9,0.5);
\coordinate (d1) at (13.5,0.5);
% Bracket rectangle
\def\y{0.5*10}
\draw [decorate, decoration={brace, amplitude=5pt}] ($(a1)+(0,\y)$.north east) -- ($(b1)+(0,\y)$.north west) node [midway, above=0.2cm] {n};
\draw [decorate, decoration={brace, amplitude=5pt}] ($(b1)+(0,\y)$.north east) -- ($(c1)+(0,\y)$.north west) node [midway, above=0.2cm] {n};
\draw [decorate, decoration={brace, amplitude=5pt}] ($(c1)+(0,\y)$.north east) -- ($(d1)+(0,\y)$.north west) node [midway, above=0.2cm] {n};
% Original string rectangle
% |_a_
\def\y{0.5*9}
\draw ($(a1)+(0,-0.2)$) -- ($(a1)+(0,\y)$) [];
\draw ($(a0)+(0,\y)$) -- ($(b0)+(0,\y)$) [] node[midway,above]{a};
% |_b_
\draw ($(b1)+(0,-0.2)$) -- ($(b1)+(0,\y)$) [];
\draw ($(b0)+(0,\y)$) -- ($(c0)+(0,\y)$) [] node[midway,above]{b};
% |_c
\draw ($(c1)+(0,-0.2)$) -- ($(c1)+(0,\y)$) [];
\draw ($(c0)+(0,\y)$) -- ($(d0)+(0,\y)$) [] node[midway,above]{c};
% |
\draw ($(d1)+(0,-0.2)$) -- ($(d1)+(0,\y)$) [];
\def\y{0.5*8}
\draw ($(a0)+(2.5,\y)$) node[]{$v$};
\draw ($(c0)+(0,\y)$) node[]{$|x|$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{1}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
% Possible divisions
\def\y{0.5*7}
\draw ($(b0)+(0,\y)$) node[]{$|v|$};
\draw ($(b0)+(2.5,\y)$) node[]{$x$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{2}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*6}
\draw ($(c0)+(-2.5,\y)$) node[]{$v$};
\draw ($(c0)+(0,\y)$) node[]{$|x|$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{3}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*5}
\draw ($(a0)+(2.5,\y)$) node[]{$vx$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{4}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*4}
\draw ($(b0)+(2.5,\y)$) node[]{$vx$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{5}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*3}
\draw ($(c0)+(2.5,\y)$) node[]{$vx$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{6}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*2}
\draw ($(b0)+(-0.2,\y)$) node[]{$v$};
\draw ($(b0)+(0.2,\y)$) node[]{$x$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{7}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5}
\draw ($(c0)+(-0.2,\y)$) node[]{$v$};
\draw ($(c0)+(0.2,\y)$) node[]{$x$};
\draw ($(a0)+(-0.2,\y)$) node[]{\circled{8}};
\draw ($(a0)+(0,\y-0.2)$) -- ($(d0)+(0,\y-0.2)$) [loosely dashed];
\end{tikzpicture}
\end{center}
Ora dimostriamo caso per caso che, ‘‘pompando’’, si esce dal linguaggio. Con un po' di pratica si arriva a capire il meccanismo generale.
\begin{enumerate}[label=\protect\circled{\arabic*}]
\item Caso invalido in quanto si rompe il vincolo $|vwx|\leq k$.
\item
Notiamo che si ha un'accavallamento, ovvero un caso nel quale o $v$ o $x$
stanno in mezzo alle suddivisioni (ad esempio $v$ è composta da un po' di $a$ e di~$b$).
È facile dimostrare come in questi casi il linguaggio ‘‘pompato’’ esca dal linguaggio originale.
La stringa sarà composta come:
\begin{equation*}
z=a^{k-\frac{|v|}{2}}(ab)^{|v|i}b^{k-\frac{|v|}{2}-|x|+i|x|}c^k
\end{equation*}
\textbf{N.B.}: $\frac{|v|}{2}$ è solo indicativo, non è che dobbiamo prendere quel pezzo per forza.
Ora abbiamo due casi:
\begin{itemize}
\item $|v|=0$\\
$a^kb^{k-|x|+i|x|}c^k$\\
$k+|x|(i-1)=k$\\
con $i=2 \rightarrow |x|=0 \quad \Rightarrow$ impossibile, in quanto si violerebbe il vincolo $|vx|>0$. \greenmark
\item $|v|>0$\\
$a^{k-\frac{|v|}{2}}(ab)^{|v|i}b^{k-\frac{|v|}{2}+|x|(i-1)}c^k$\\
con $i=2 \Rightarrow a^{k-\frac{|v|}{2}}ababb^{k-\frac{|v|}{2}+|x|}c^k \notin L$. \greenmark
\end{itemize}
\item
La dimostrazione è analoga al punto precedente.
\item
Ora $vx$ è tutto compreso in $a$. La stringa sarà quindi composta in questo modo:
\begin{equation*}
z=a^{k-|vx|+i|vx|}b^kc^k
\end{equation*}
Affinché sia valida, dobbiamo rispettare il seguente vincolo:
\begin{align*}
&k-|vx|+i|vx|=k\\
&|vx|(i-1)=0\\
&\text{con } i=2 \rightarrow |vx|=0 \quad \text{Impossibile} \greenmark
\end{align*}
\item
La dimostrazione è analoga al punto precedente.
\item
La dimostrazione è analoga al punto precedente.
\item
$v$ e $x$ si trovano in sottoparti separate.
La stringa è così composta:
\begin{equation*}
z=a^{k+|v|(i-1)}b^{k+|x|(i-1)}c^k
\end{equation*}
Quindi:
\begin{align*}
&k+|v|(i-1)+k+|x|(i-1)=2k\\
&(i-1)(|v|+|x|)=0\\
&\text{con } i=2 \rightarrow |v|+|x|=0 \quad \text{Impossibile} \greenmark
\end{align*}
\end{enumerate}
Abbiamo verificato per ogni caso che il linguaggio ‘‘pompato’’ esce dal linguaggio stesso, dimostrando quindi che non è CF. \greenmark
\chapter{Esercizi vari sui linguaggi}
Classificare le seguenti famiglie di linguaggi al variare di $n,m \in \mathbb{N}$ (N.B. $0\in\mathbb{N}$), motivando formalmente la risposta:
\begin{align*}
&A_{m,n}=\{ \sigma \in \{1,0\}^* | \text{ } \sigma = (1^{2n}001^m)^n \}\\
&B_m = \bigcup_{n\in\mathbb{N}} A_{m,n}\\
&C_n = \bigcup_{m\in\mathbb{N}} A_{m,n}
\end{align*}
Notiamo che il linguaggio, al variare di $m,n$, è finito, ad esempio:
\begin{align*}
&A_{0,0}=\varnothing\\
&A_{1,1}=11001\\
&A_{0,1}=1100\\
&A_{1,0}=\varnothing\\
&A_{2,1}=110011\\
&A_{2,2}=1111001111110011\\
&\dots
\end{align*}
Essendo i linguaggi finiti sono anche regolari.
\subsubsection{Unione in n}
Adesso guardiamo l'unione in $n$ del linguaggio $A_{m,n}$:
\begin{align*}
&A_{m,n}=\{ \sigma \in \{1,0\}^* | \text{ } \sigma = (1^{2n}001^m)^n \}\\
&B_m = \bigcup_{n\in\mathbb{N}} A_{m,n}
\end{align*}
\begin{itemize}
\item $B_0=(1^{2n}00)^n$
\item $B_1=(1^{2n}001)^n$
\item $B_2=(1^{2n}0011)^n$
\end{itemize}
Notiamo che, al variare di $m$, otteniamo sempre e comunque un linguaggio non CF.
Procediamo quindi con il pumping lemma per i linguaggi non CF.\\
\begin{itemize}
\item $\forall z = uvwxy$
\item $|z| \geq k$
\item $|vwx| \leq k$
\item $|vx|>0$
\item $uv^iwx^iy\in L$
\end{itemize}
Scegliamo una stringa, $z=(1^{2n}00)^n$, ed elenchiamo le possibili suddivisioni.
\begin{center}
\begin{tikzpicture}
% Defining main coordinates
\coordinate (a) at (0,0);
\coordinate (b) at (5,0);
\coordinate (c) at (6,0);
\coordinate (d) at (11,0);
\coordinate (e) at (12,0);
\coordinate (f) at (14,0);
% Bracket rectangle
\def\y{0.5*18}
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] ($(a)+(0,\y)$.north east) -- ($(b)+(0,\y)$.north west) node [midway, above=0.2cm] {$2k$};
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] ($(c)+(0,\y)$.north east) -- ($(d)+(0,\y)$.north west) node [midway, above=0.2cm] {$2k$};
\draw [decorate, decoration={brace, amplitude=5pt, raise=2.5pt}] ($(e)+(0,\y)$.north east) -- ($(f)+(0,\y)$.north west) node [midway, above=0.2cm] {$|z|-2(2k+2)$};
% Original string rectangle
\def\y{0.5*17}
\draw ($(a)+(0,-0.2)$) -- ($(a)+(0,\y+0.5)$) [];
\draw ($(a)+(0,\y)$) -- ($(b)+(0,\y)$) [] node[midway,above]{$1\dots1$};
\draw ($(b)+(0,-0.2)$) -- ($(b)+(0,\y+0.5)$) [];
\draw ($(b)+(0,\y)$) -- ($(c)+(0,\y)$) [] node[midway,above]{$00$};
\draw ($(c)+(0,-0.2)$) -- ($(c)+(0,\y+0.5)$) [];
\draw ($(c)+(0,\y)$) -- ($(d)+(0,\y)$) [] node[midway,above]{$1\dots1$};
\draw ($(d)+(0,-0.2)$) -- ($(d)+(0,\y+0.5)$) [];
\draw ($(d)+(0,\y)$) -- ($(e)+(0,\y)$) [] node[midway,above]{$00$};
\draw ($(e)+(0,-0.2)$) -- ($(e)+(0,\y+0.5)$) [];
\draw ($(e)+(0,\y)$) -- ($(f)+(0,\y)$) [] node[midway,above]{$\dots$};
% Possible divisions
\def\y{0.5*16}
\draw ($(a)+(2.5,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{1}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*15}
\draw ($(b)+(-0.1,\y)$) node[]{$v|x|$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{2}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*14}
\draw ($(b)+(0,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{3}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*13}
\draw ($(b)+(0.1,\y)$) node[]{$|v|x$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{4}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*12}
\draw ($(b)+(0.5,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{5}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*11}
\draw ($(c)+(-0.1,\y)$) node[]{$v|x|$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{6}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*10}
\draw ($(c)+(0,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{7}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*9}
\draw ($(c)+(0.1,\y)$) node[]{$|v|x$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{8}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*8}
\draw ($(c)+(2.5,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{9}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*7}
\draw ($(d)+(-0.1,\y)$) node[]{$v|x|$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{10}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*6}
\draw ($(d)+(0,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{11}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*5}
\draw ($(d)+(0.1,\y)$) node[]{$|v|x$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{12}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*4}
\draw ($(d)+(0.5,\y)$) node[]{$vx$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{13}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*3}
\draw ($(b)+(-0.2,\y)$) node[]{$v$};
\draw ($(c)+(0.2,\y)$) node[]{$x$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{14}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5*2}
\draw ($(b)+(0,\y)$) node[]{$|v|$};
\draw ($(c)+(0.2,\y)$) node[]{$x$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{15}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\def\y{0.5}
\draw ($(b)+(-0.2,\y)$) node[]{$v$};
\draw ($(c)+(0,\y)$) node[]{$|x|$};
\draw ($(a)+(-0.2,\y)$) node[]{\circled{16}};
\draw ($(a)+(0,\y-0.2)$) -- ($(e)+(0,\y-0.2)$) [loosely dashed];
\draw ($(b)$) node[]{$|v|$};
\draw ($(c)$) node[]{$|x|$};
\draw ($(a)+(-0.2,0)$) node[]{\circled{17}};
\draw ($(a)+(0,-0.2)$) -- ($(e)+(0,-0.2)$) [loosely dashed];
\end{tikzpicture}
\end{center}
Verifichiamo per ogni caso di violare le regole del pumping lemma, in modo da dimostrare che questo linguaggio non è CF.
\begin{enumerate}[label=\protect\circled{\arabic*}]
\item $z=1^{2k-|vx|+|vx|i}0^21^{2k}0^2$\\
Affinché sia valida, deve valere $2k+|vx|(i-1)=2k$, quindi $|vx|(i-1)=0$, che però per $i=2$ viola la condizione $|vx|>0$. \greenmark
\item $z=1^{2k-|v|+i|v|-\frac{|x|}{2}}(10)^{|x|i}0^{2-\frac{|x|}{2}}1^{2k}0^2$\\
Distinguiamo due casi:
\begin{itemize}
\item $|x|=0$\\
$1^{2k+|v|(i-1)}0^21^{2k}0^2 \Rightarrow 2k+|v|(i-1)=2k$\\
per $i \rightarrow 2 \Rightarrow |v|=0$ Impossibile \greenmark
\item $|x|>0$\\
$1^{2k+|v|(i-1)-\frac{|x|}{2}}(10)^{|x|i}0^{2-\frac{|x|}{2}}1^{2k}$\\
per $i \rightarrow 2 \Rightarrow 1^{2k+|v|-\frac{|x|}{2}}10100^{2-\frac{|x|}{2}}1^{2k} \notin L$
(questa suddivisione altera la struttura del linguaggio) \greenmark
\end{itemize}
\item $z=1^{2k+|v|(i-1)}0^{2+|x|(i-1)}1^{2k}0^2$\\
Affinché sia valida, deve valere $2k+|v|(i-1)+2+|x|(i-1)=2k+2$, quindi $|v|(i-1)+|x|(i-1)=0$,
che per $i \rightarrow 2 \Rightarrow |v|+|x|=0$ viola $|vx|>0$. \greenmark
\item La dimostrazione è analoga al punto 2.
\item $z=1^{2k}0^{2+|vx|(i-1)}1^{2k}0^2$\\
Affinché sia valida, deve valere $2+|vx|(i-1)=2$, che implicherebbe $|vx|=0$ per $i=2$, quindi non va bene. \greenmark
\item La dimostrazione è analoga al punto 2.
\item La dimostrazione è analoga al punto 3.
\item La dimostrazione è analoga al punto 2.
\item La dimostrazione è analoga al punto 1.
\item La dimostrazione è analoga al punto 2.
\item La dimostrazione è analoga al punto 3.
\item La dimostrazione è analoga al punto 2.
\item La dimostrazione è analoga al punto 5.
\item $z=1^{2k+|v|(i-1)}0^21^{2k+|x|(i-1)}0^2$\\
Affinché sia valida, deve valere $2k+|v|(i-1)+2k+|x|(i-1)=4k$, quindi per $i\rightarrow 2 \Rightarrow |v|+|x|=0$. Il problema è che andiamo a violare il vincolo $|vx|>0$, quindi non va bene. \greenmark
\item $z=1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}}1^{2k+|x|(i-1)}0^2$\\
Distinguiamo due casi:
\begin{itemize}
\item $|v|=0$\\
$1^{2k}0^{2}1^{2k+|x|(i-1)}0^2$\\
per $i \rightarrow 2 \Rightarrow |v|=0$ Impossibile \greenmark
\item $|v|>0$\\
$1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}}1^{2k+|x|(i-1)}0^2$\\
per $i \rightarrow 2 \Rightarrow 1^{2k-\frac{|v|}{2}}10100^{2-\frac{|v|}{2}}1^{2k+|x|}0^2 \notin L$ \greenmark
\end{itemize}
\item La dimostrazione è analoga al punto 15.
\item $z=1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}-\frac{|x|}{2}}(01)^{|x|i}1^{2k-\frac{|x|}{2}}0^2$\\
Distinguiamo i seguenti casi:
\begin{itemize}
\item $|v|=0$\\
$1^{2k}0^{2-\frac{|x|}{2}}(01)^{|x|i}1^{2k-\frac{|x|}{2}}0^2$
\begin{itemize}
\item $|x|=0$\\
Non possiamo, in quanto violeremmo $|vx|>0$.\greenmark
\item $|x|>0$\\
$1^{2k}0^{2-\frac{|x|}{2}}(01)^{|x|i}1^{2k-\frac{|x|}{2}}0^2$\\
per $i\rightarrow 2 \Rightarrow 1^{2k}0^{2-\frac{|x|}{2}}01011^{2k-\frac{|x|}{2}}0^2 \notin L$. \greenmark
\end{itemize}
\item $|v|>0$\\
$1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}-\frac{|x|}{2}}(01)^{|x|i}1^{2k-\frac{|x|}{2}}0^2$
\begin{itemize}
\item $|x|=0$\\
$1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}}1^{2k}0^2$\\
per $i \rightarrow 2 \Rightarrow 1^{2k-\frac{|v|}{2}}10100^{2-\frac{|v|}{2}}1^{2k}0^2 \notin L$. \greenmark
\item $|x|>0$\\
$1^{2k-\frac{|v|}{2}}(10)^{|v|i}0^{2-\frac{|v|}{2}-\frac{|x|}{2}}(01)^{|x|i}1^{2k-\frac{|x|}{2}}0^2$\\
per $i \rightarrow 2 \Rightarrow 1^{2k-\frac{|v|}{2}}10100^{2-\frac{|v|}{2}-\frac{|x|}{2}}01011^{2k-\frac{|x|}{2}}0^2 \notin L$. \greenmark
\end{itemize}
\end{itemize}
\end{enumerate}
Abbiamo dimostrato ogni caso, quindi il nostro linguaggio non è CF.
\subsubsection{Unione in m}
Per quanto riguarda l'unione in $m$ del linguaggio $A_{m,n}$:
\begin{align*}
&A_{m,n}=\{ \sigma \in \{1,0\}^* | \text{ } \sigma = (1^{2n}001^m)^n \}\\
&C_n = \bigcup_{m\in\mathbb{N}} A_{m,n}
\end{align*}
\begin{itemize}
\item $C_0=\varnothing$
\item $C_1=11001^m$
\item $C_2=(1111001^m)^2$
\item $C_{n>2}=(1^{2n}001^m)^n$
\end{itemize}
Notiamo che $C_0$ e $C_1$ sono linguaggi regolari, il primo è banale, il secondo ha questo automa:
\begin{center}
\begin{tikzpicture}[scale=0.2]
\tikzstyle{every node}+=[inner sep=0pt]
\draw [black] (10.1,-24.6) circle (3);
\draw (10.1,-24.6) node {$q_0$};
\draw [black] (20.6,-24.6) circle (3);
\draw (20.6,-24.6) node {$q_1$};
\draw [black] (31.1,-24.6) circle (3);
\draw (31.1,-24.6) node {$q_2$};
\draw [black] (42.2,-24.6) circle (3);
\draw (42.2,-24.6) node {$q_3$};
\draw [black] (53.5,-24.6) circle (3);
\draw (53.5,-24.6) node {$q_4$};
\draw [black] (53.5,-24.6) circle (2.4);
\draw [black] (34.8,-38.7) circle (3);
\draw (34.8,-38.7) node {$q\bot$};
\draw [black] (13.1,-24.6) -- (17.6,-24.6);
\fill [black] (17.6,-24.6) -- (16.8,-24.1) -- (16.8,-25.1);
\draw (15.35,-25.1) node [below] {$1$};
\draw [black] (23.6,-24.6) -- (28.1,-24.6);
\fill [black] (28.1,-24.6) -- (27.3,-24.1) -- (27.3,-25.1);
\draw (25.85,-25.1) node [below] {$1$};
\draw [black] (34.1,-24.6) -- (39.2,-24.6);
\fill [black] (39.2,-24.6) -- (38.4,-24.1) -- (38.4,-25.1);
\draw (36.65,-25.1) node [below] {$0$};
\draw [black] (45.2,-24.6) -- (50.5,-24.6);
\fill [black] (50.5,-24.6) -- (49.7,-24.1) -- (49.7,-25.1);
\draw (47.85,-25.1) node [below] {$0$};
\draw [black] (56.18,-23.277) arc (144:-144:2.25);
\draw (60.75,-24.6) node [right] {$1$};
\fill [black] (56.18,-25.92) -- (56.53,-26.8) -- (57.12,-25.99);
\draw [black] (51.1,-26.41) -- (37.2,-36.89);
\fill [black] (37.2,-36.89) -- (38.14,-36.81) -- (37.53,-36.01);
\draw (43,-31.15) node [above] {$0$};
\draw [black] (40.81,-27.26) -- (36.19,-36.04);
\fill [black] (36.19,-36.04) -- (37.01,-35.57) -- (36.12,-35.1);
\draw (37.82,-30.5) node [left] {$1$};
\draw [black] (31.86,-27.5) -- (34.04,-35.8);
\fill [black] (34.04,-35.8) -- (34.32,-34.9) -- (33.35,-35.15);
\draw (32.19,-32.14) node [left] {$1$};
\draw [black] (22.73,-26.71) -- (32.67,-36.59);
\fill [black] (32.67,-36.59) -- (32.46,-35.67) -- (31.75,-36.38);
\draw (26.53,-32.13) node [below] {$0$};
\draw [black] (12.71,-26.09) -- (32.19,-37.21);
\fill [black] (32.19,-37.21) -- (31.75,-36.38) -- (31.25,-37.25);
\draw (21.3,-32.15) node [below] {$0$};
\end{tikzpicture}
\end{center}
\begin{enumerate}
\item $x\in L \Rightarrow x \in L(m)$\\
\textbf{Passo base}\\
$ \delta(q_0,1100)=q_4 \in F \greenmark $\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \in L \Rightarrow x\in L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$.\\
Posto che $\delta(q_0,x)=q_4$:
\begin{itemize}
\item se $a=0$ allora $\delta(q_4,0)=q_\bot \notin F \greenmark$
\item se $a=1$ allora $\delta(q_4,1)=q_3 \in F \greenmark$
\end{itemize}
\item $x\notin L \Rightarrow x \notin L(m)$\\
\textbf{Passo base}\\
$x=0 \rightarrow \delta(q_0,0)=q_\bot \notin F \greenmark$\\
$x=1 \rightarrow \delta(q_0,1)=q_1 \notin F \greenmark$\\
$x=11 \rightarrow \delta(q_0,11)=q_2 \notin F \greenmark$\\
$x=10 \rightarrow \delta(q_0,10)=q_\bot \notin F \greenmark$\\
$x=111 \rightarrow \delta(q_0,111)=q_\bot \notin F \greenmark$\\
$x=110 \rightarrow \delta(q_0,110)=q_3 \notin F \greenmark$\\
$x=1101 \rightarrow \delta(q_0,1101)=q_\bot \notin F \greenmark$\\
\textbf{Passo induttivo}\\
Supponiamo che $\forall x \text{ t.c. } |x|=n \quad x \notin L \Rightarrow x\notin L(n)$.\\
Prendiamo allora una stringa $y$ t.c. $|y|>n$ e $y=xa \text{ con }a\in\varepsilon$. Ricordiamoci che possiamo usare $\delta(\delta(q_0,x),a)$.
\begin{itemize}
\item $\delta(q_0,x)=q_0$\\
Se $a=0$ allora $\delta(q_0,0)=q_\bot \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_0,1)=q_1 \notin F \greenmark$
\item $\delta(q_0,x)=q_1$\\
Se $a=0$ allora $\delta(q_1,0)=q_\bot \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_1,1)=q_2 \notin F \greenmark$
\item $\delta(q_0,x)=q_2$\\
Se $a=0$ allora $\delta(q_2,0)=q_3 \notin F \greenmark$\\
Se $a=1$ allora $\delta(q_2,1)=q_\bot \notin F \greenmark$
\item $\delta(q_0,x)=q_3$\\
Se $a=0$ allora $\delta(q_3,0)=q_4 \in F \greenmark$\\
Se $a=1$ allora $\delta(q_3,1)=q_\bot \notin F \greenmark$
\end{itemize}
\end{enumerate}
Se consideriamo invece $C_2=(1111001^m)^2$, notiamo che potrebbe facilmente trattarsi di un linguaggio CF. Procediamo quindi con il pumping lemma.
\begin{itemize}
\item $z=uvw$
\item $|z|\geq k$
\item $|uv|\leq k$
\item $|v|>0$
\item $\forall i \in \mathbb{N}.uv^iw\in L \text{ con }i\geq0$
\end{itemize}
La stringa sarà formata così: $1^40^21^{k+|v|(i-1)}1^40^21^k$.
Affinché appartenga al linguaggio, è necessario che $k+|v|(i-1)=k$, che per $i \rightarrow 2 \Rightarrow |v|=0$, che è impossibile. \greenmark\\
Una grammatica che genera questo linguaggio è fatta in questo modo:
\begin{align*}
&S \rightarrow 111100B\\
&B \rightarrow 1B1|111100
\end{align*}
Mentre una possibile derivazione per questa grammatica è:
\begin{center}
\begin{tikzpicture}
\Tree
[.\blue{S}
[.1^40^2\blue{B}
[.1^40^21\blue{B}1
[.1^40^211\blue{B}11
[.1^40^2111\blue{B}111
1^40^2\blue{1^3}1^40^2\blue{1^3}
]
]
]
]
]
\end{tikzpicture}
\end{center}
Dimostriamo la correttezza della grammatica.
Per dimostrare la correttezza della nostra grammatica, dobbiamo dimostrare la seguente condizione:
$$x\in L \Leftrightarrow S \Rightarrow_* x$$
Essendo una doppia implicazione, dimostreremo separatamente
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $, che si dimostra per \textit{induzione} sulla lunghezza della stringa
\item $ S \Rightarrow_i k \Rightarrow x \in L$, che si dimostra per \textit{induzione} sul numero di passi di derivazione
\end{enumerate}
\begin{enumerate}
\item $x\in L \Rightarrow S \Rightarrow_* x $\\
\textbf{Passo base}\\
$y=1^40^21^40^2 \qquad x \in L$ ed $\exists S\Rightarrow 1^40^21^40^2 \greenmark $\\
\textbf{Passo induttivo}\\
Consideriamo $y \in \varepsilon^*$ tale che $|y|<n$ e supponiamo che $\forall y \in L \Rightarrow S \Rightarrow_* y$.\\
Allora prendiamo $|x|\geq n$ e sapendo che $y$ è composto come $y=1^40^21^k1^40^21^k$, $x$ sarà $1^40^21^m1^40^21^m$ con $m>k$. Procediamo prendendo $m=k+1$:
$$ x=1^40^21^{k+1}1^40^21^{k+1}$$
Sappiamo per ipotesi che esiste una derivazione $S\Rightarrow_i 1^40^21^k1^40^21^k$, quindi
$$\exists S \Rightarrow y \rightarrow \exists S \Rightarrow_i 1^40^21^k1^40^21^k$$
ma se esiste questa esiste anche la derivazione precedente, ovvero
$$\exists S \Rightarrow_{i-1} 1^40^21^{k}B1^{k} \Rightarrow ...$$
essendo che la produzione $S\rightarrow 1B1$ appartiene all'insieme delle possibili produzioni, posso sostituire:
$$... \Rightarrow_i 1^40^21^{k+1}B1^{k+1} \Rightarrow_{i+1} 1^40^21^{k+1}1^40^21^{k+1} = x \greenmark$$