-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlg.tex
executable file
·1378 lines (1024 loc) · 37 KB
/
Alg.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
% !TeX spellcheck = en_US
\documentclass[10pt,a4paper,twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{graphicx}
\usepackage[cm]{fullpage}
\usepackage{color}
\usepackage{enumerate}
\usepackage[linesnumbered,lined,german]{algorithm2e}
% Used for indicator function
\usepackage{bbm}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyfoot[C]{\thepage/\pageref{LastPage}}
\setlength{\parindent}{0in}
\newtheorem{definition}{Definition}[section]
\newcommand\pro{\item[$+$]}
\newcommand\con{\item[$-$]}
\usepackage{enumitem}
\usepackage[a4paper,
bindingoffset=0.2in,
left=0.4in,
right=0.6in,
top=0.5in,
bottom=0.5in,
footskip=.25in]{geometry}
\setlength\itemsep{-1em}
\usepackage{minted}
\usepackage{lipsum}
\setminted[c++]{ %
linenos=true, % Line numbers
autogobble=true, % Automatically remove common white space
frame=lines,
framesep=2mm,
fontsize=\footnotesize
}
\makeatletter
\newenvironment{code}
{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
\def\FV@BProcessLine##1{%
\hbox{%
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\FancyVerbFormatLine{##1}%
}%
}%
\VerbatimEnvironment
\setbox\z@=\hbox\bgroup
\begin{minted}{c++}}
{\end{minted}\egroup
\leavevmode\vbox{\hrule\kern2mm\box\z@\kern2mm\hrule}}
\makeatother
\begin{document}
\begin{center}
\textbf{Summary\\ Algorithms\\}
\mbox{}\\
\textit{FTP\_Alg}
\vspace{10pt}
\vspace{10pt}
HS 2021
\end{center}
\section{Begriffe}
\begin{itemize}
\item Stable Algorithms: Other keys are sorted as well
\item Intern Algorithms: Whole data is available at once
\item Extern Algorithms: Data only in batches available
\end{itemize}
\section{Algorithm Analysis}
\subsection{Asymptotic Notation}
\subsubsection{$\Theta$-Notation}
"average case"
\begin{center}
\includegraphics[width=6cm]{images/asymptotische-notation-theta.png}
\end{center}
\[
\begin{array}{rcl}
\Theta(g(n)) & = & \{f(n):\exists c_1,c_2,n_0>0 \\
& & \text{ mit }0\leq c_1g(n)\leq f(n)\leq c_2g(n) \\
& & \text{ für alle }n\geq n_0\}
\end{array}
\]
\subsubsection{$\mathcal{O}$-Notation}
"worst case"
\begin{center}
\includegraphics[width=6cm]{images/asymptotische-notation-bigoh.png}
\end{center}
\[
\begin{array}{rcl}
\mathcal{O}(g(n)) & = & \{f(n):\exists c,n_0>0 \\
& & \text{ mit }0\leq f(n)\leq cg(n) \\
& & \text{ für alle }n\geq n_0\}
\end{array}
\]
\subsubsection{$\Omega$-Notation}
"best case"
\begin{center}
\includegraphics[width=6cm]{images/asymptotische-notation-omega.png}
\end{center}
\[
\begin{array}{rcl}
\Omega(g(n)) & = & \{f(n):\exists c,n_0>0 \\
& & \text{ mit }0\leq cg(n)\leq f(n) \\
& & \text{ für alle }n\geq n_0\}
\end{array}
\]
\begin{center}
\includegraphics[width=8cm]{images/runetimes}
\end{center}
\begin{itemize}
\item $n!$ - factorial runtime
\item $2^n$ - exponential runtime
\item $n^2$ - polynomial runtime
\item $n \cdot log(n)$ - super-linear runtime
\item $n$ - linear runtime
\item $log(n)$ - logarithmic runtime
\item $1$ - constant runtime
\end{itemize}
\subsubsection{Examples}
$T(n) = \mathcal{O}(n \cdot log(n)^2)$
\begin{center}
\includegraphics[width=0.45\textwidth]{images/example_alg_analysis1}
\end{center}
\subsection{Master Theorem}
The master theorem allows to analyze the runtime complexity of recursive algorithms.
Let $a \geq 1, b > 1$ be constants, $f(n)$ be a function and $T(n)$ defined by the following recurrence\\
$$
T(n)=a T(n / b)+f(n)
$$
where $\frac{n}{b}$ is interpretex as either $\lfloor n / b\rfloor$ or $\lceil n / b\rceil$. Then
\begin{enumerate}
\item $T(n)=\Theta\left(n^{\log _{b} a}\right) \text { if } f(n)=O \left(n^{\log _{b} a-\epsilon}\right) $ \\ $\text { for some } \epsilon>0 $
\item $T(n)=\Theta\left(n^{\log _{b} a} \ln n\right) \text { if } f(n)=\Theta\left(n^{\log _{b} a}\right)$
\item $T(n)=\Theta(f(n)) \text { if } f(n)=\Omega\left(n^{\log _{b} a+\epsilon}\right)$ \\ $ \text { for some } \epsilon>0, \text { and if } a \cdot f(n / b) \leq c \cdot f(n) $ \\ $\text{ for all } n \geq n_{0}, \text { where } c \text { and } n_{0} \text { are positive } \\ \text { constant with } $
\end{enumerate}
$$
\begin{array}{l}T(n)=a \cdot T\left(\frac{n}{b}\right)+\mathcal{O}{(n^{d})} \text { where } \begin{array}{r}a>0 \\ b>1 \\ d>0\end{array} \\
T(n)=\left\{\begin{array}{ll}\mathcal{O}\left(n^{d}\right)
& \text { if } d > \log _{b}(a)\\
\mathcal{O}\left(n^{d} \cdot \log (n)\right) & \text { if } d=\log _{b}(a) \\
\mathcal{O}\left(n^{\log_{b}(a)}\right) & \text { if } d<\log _{b}(a)\end{array}\right.\end{array}
$$
Example:\\
$T(n) = 4 \cdot T(\frac{n}{2}) + \mathcal{O}(n^1)$\\
$1 = d < \log_{2}(4) = 2 \rightarrow T(n) = \mathcal{O}(n^{log_{b}(a)}) = \mathcal{O}(n^2)$
\section{Datenstrukturen}
\subsection{Array}
\begin{center}
\includegraphics[width=8cm]{images/array1}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/array_operations}
\end{center}
\subsection{Stack}
Der Stack besteht aus den Elementen $S[1..S.top]$.
\begin{center}
\includegraphics[width=10cm]{images/stack-layout}
\end{center}
\subsubsection{Operationen}
\begin{itemize}
\item \textbf{PUSH(S,x):} Platziert das Element x auf dem Stack.
\item \textbf{POP(S):} Entfernt das oberste Element vom Stack.
\item \textbf{STACK-EMPTY(S):} Liefert true, wenn der Stack leer ist.
\end{itemize}
\textbf{Bemerkungen}
\begin{itemize}
\item Alle drei Operationen haben eine Laufzeit von $\mathcal{O}(1)$.
\end{itemize}
\subsubsection{Implementation}
\begin{center}
\includegraphics[width=4.5cm]{images/stack-implementation.png}
\end{center}
\subsection{Queue}
Eine Queue verfügt über einen Kopf (Head) und ein Ende (Tail). Neue Elemente werden am Ende angefügt. Das nächste Element wird jeweils vom Kopf entfernt.
\begin{center}
\includegraphics[width=6cm]{images/queue-layout.png}
\end{center}
\subsubsection{Operationen $\mathcal{O}(1)$}
\begin{itemize}
\item \textbf{ENQUEUE(Q,x):} Fügt das Element x an das Ende der Queue an.
\item \textbf{DEQUEUE(Q):} Entfernt das nächste Element von der Kopfposition.
\end{itemize}
\subsubsection{Implementation}
\begin{center}
\includegraphics[width=6cm]{images/queue-implementation.png}
\end{center}
%\subsection{Linked List (optional)}
%Eine Linked List ist eine \emph{geordnete Liste} von Objekten, in der jedes Objekt einen Zeiger auf das nächste Objekt enthält.
%
%\begin{center}
%\includegraphics[width=8.5cm]{linkedlist.png}
%\end{center}
%
%\textbf{Bemerkungen}
%\begin{itemize}
%\item Der \emph{prev}-Zeiger des ersten Objekts und der \emph{next}-Zeiger des letzten Objekts in der Linked List sind jeweils \emph{NIL}.
%\end{itemize}
\subsubsection{Operationen}
\begin{itemize}
\item \textbf{INSERT(L,x):} Fügt Element x am Anfang der Liste L ein. Die Laufzeit ist $\mathcal{O}(1)$.
\item \textbf{DELETE(L,x):} Löscht ein Element aus der Liste L. Als Input dient ein Zeiger auf das Objekt. Die Laufzeit beträgt $\mathcal{O}(1)$. Falls zuerst noch der Zeiger ermittelt werden muss, beträgt die Laufzeit $\mathcal{O}(n)$, da zuerst noch SEARCH ausgeführt werden muss.
\item \textbf{SEARCH(L,k):} Findet das erste Element mit Key k in der Linked List L. SEARCH(L,4) liefert für obiges Bild einen Zeiger auf das dritte Objekt in der Liste. Die Laufzeit beträgt $\Theta(n)$ im schlechtesten Fall.
\end{itemize}
\subsubsection{Implementation}
\begin{center}
\includegraphics[width=5cm]{images/linkedlist-insert.png}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/linkedlist-delete.png}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/linkedlist-search.png}
\end{center}
\section{Binary Search Tree (BST)}
\begin{itemize}
\item Each Node is greater than every node in its left subtree
\item Each Node is less than every node in its right subtree
\end{itemize}
\begin{center}
\includegraphics[width=9cm]{images/bst1}
\end{center}
\subsection{Operations}
\begin{itemize}
\item x.p means parent node of x
\item h means height of the tree
\end{itemize}
\textbf{Tree Minimum $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=4cm]{images/tree-minimum}
\end{center}
\textbf{Tree Maximum $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=5cm]{images/tree-maximum}
\end{center}
\textbf{Tree Successor $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=5cm]{images/tree-successor}
\end{center}
\textbf{Tree Predecessor $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=5cm]{images/tree-predecessor}
\end{center}
\textbf{Inorder Tree Walk $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=5cm]{images/inorder-tree-walk}
\end{center}
\textbf{Insert Node$\mathcal{O}(?)$}
A new node is always inserted as a leaf.
\begin{center}
\includegraphics[width=6cm]{images/tree-insert}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/example-insert-tree}
\end{center}
\textbf{Tree Search $\mathcal{O}(h)$}
\begin{center}
\includegraphics[width=6cm]{images/tree-search}
\end{center}
Alternative:
\begin{center}
\includegraphics[width=6cm]{images/tree-search2}
\end{center}
\textbf{Tree Delete $\mathcal{O}(?)$}
\begin{center}
\includegraphics[width=6cm]{images/tree-delete}
\end{center}
Transplant: Replace one subtree without updating
\begin{center}
\includegraphics[width=6cm]{images/transplant-tree}
\end{center}
\begin{center}
\includegraphics[width=10cm]{images/tree-delete-example}
\end{center}
"Replacement with inorder successor".\\
\textbf{Range Searching}
Nachfolgend ein Beispiel einer Range Query im Bereich $[18:77]$. Zurückgegeben werden müssen die dunkelgrau markierten Knoten plus der Knoten $u$.
\begin{center}
\includegraphics[width=8cm]{images/range-search-example}
\end{center}
Grobbeschreibung:
\begin{enumerate}
\item Finde den Knoten $v_{split}$.
\item Ausgehend von $v_{split}$, folge dem Pfad von $x$. Bei jedem Knoten, bei dem die Suche links weitergeht, rapportiere den rechten Teilbaum.
\item Ausgehend von $v_{split}$, folge dem Pfad von $x'$. Bei jedem Knoten, bei dem die Suche rechts weitergeht, rapportiere den linken Teilbaum.
\end{enumerate}
\begin{center}
\includegraphics[width=9cm]{images/rangequery}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/find-split-node}
\end{center}
\begin{center}
\includegraphics[width=4cm]{images/find-split-node-example}
\end{center}
\textbf{Komplexitätsanalyse}
Der Baum benötigt $\mathcal{O}(n)$ Platz und kann in $\mathcal{O}(n\log{n})$ Zeit konstruiert werden ($n$ Elemente, ein Element muss den Baum mit Höhe $\log{n}$ von der Wurzel bis zu den Blättern durchlaufen). Bei der Abfragezeit müssen die Anzahl zu rapportierenden Knoten $k$ mitberücksichtigt werden (outputsensitiver Algorithmus). \verb+ReportSubTree+ benötigt $\mathcal{O}(k)$ Zeit. Die restlichen Knoten, die besucht werden, befinden sich auf dem Pfad von $x$ oder $x'$. Die Pfadlänge beträgt jeweils $\mathcal{O}(\log{n})$. Die gesamte Abfragezeit beträgt somit $\mathcal{O}(\log{n}+k)$.
\section{KD-Trees}
\subsection{Build KD-Tree $\mathcal{O}(n \cdot \log(n))$}
\begin{center}
\includegraphics[width=8cm]{images/example-build-kdtree}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/pseudo-kdtree}
\end{center}
\textbf{Example}\\
$P = \{(1,3),(12,1),(4,5),(5,4),(10,11),(8,2),(2,7)\}$
\begin{center}
\includegraphics[width=9cm]{images/kd-tree-example1}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/kd-tree-example2}
\end{center}
\subsection{Query KD-Tree $\mathcal{O}(\sqrt{n}+k)$}
\begin{center}
\includegraphics[width=9cm]{images/query-kdtree-example}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/search-kd-tree}
\end{center}
\begin{center}
\includegraphics[width=4cm]{images/sarch-kd-tree-scheme}
\end{center}
\section{Sorting Algorithms}
\subsection{Overview}$$
\begin{array}{l|ll}\text { Algorithm } & \begin{array}{l}\text { Worst-case } \\ \text { running time }\end{array} & \begin{array}{l}\text { Average-case } \\ \text { running time }\end{array} \\ \hline \text { Insertion sort } & \Theta\left(n^{2}\right) & \Theta\left(n^{2}\right) \\ \text { Merge sort } & \Theta(n \lg n) & \Theta(n \lg n) \\ \text { Heapsort } & O(n \lg n) & - \\ \text { Quicksort } & \Theta\left(n^{2}\right) & \Theta(n \lg n) \quad \\ \text { Counting sort } & \Theta(k+n) & \Theta(k+n) \\ \text { Radix sort } & \Theta(d(n+k)) & \Theta(d(n+k)) \\ \text { Bucket sort } & \Theta\left(n^{2}\right) & \Theta(n) \quad \end{array}
$$
\subsection{Insertion Sort $\mathcal{O}(n^2)$}
\begin{center}
\includegraphics[width=9cm]{images/insertion-sort}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/insertion-sort-example}
\end{center}
\subsection{Merge Sort}
\begin{itemize}
\pro $\mathcal{O}(n \cdot \log(n))$
\con Does not sort in place
\end{itemize}
\begin{center}
\includegraphics[width=8cm]{images/merge-sort}
\end{center}
\textbf{Divide}
\begin{center}
\includegraphics[width=6cm]{images/merge-sort-divide}
\end{center}
\textbf{Merge}
\begin{center}
\includegraphics[width=10cm]{images/merge-sort-merge}
\end{center}
\textbf{Example}
\begin{center}
\includegraphics[width=10cm]{images/mergesort-example1}
\end{center}
\begin{center}
\includegraphics[width=9.5cm]{images/mergesort-example2}
\end{center}
\textbf{Runetime Complexity}
Using the master theorem: In Mergeseort $a=b=2$ and $f(n)=D(n)+C(n)=\Theta(n)$. Thus the running time is $\Theta(n \cdot \log(n))$
$$
T(n)=\left\{\begin{array}{ll}\Theta(1) & \text { if } n \leq c \\ a T(n / b)+D(n)+C(n) & \text { otherwise }\end{array}\right.
$$
\subsection{Heap Sort}
\begin{itemize}
\pro $\mathcal{O}(n \cdot \log(n))$
\con Sorts in place (no overhead)
\end{itemize}
Fill the data into a tree and create a Max-Heap.
\begin{center}
\includegraphics[width=9cm]{images/build-max-heap}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/max-heapify}
\end{center}
\newpage
\textbf{Example Max-Heap}
\begin{center}
\includegraphics[width=9cm]{images/max-heapify-example}
\end{center}
\textbf{Example Heapsort}\\
Build Heap-Tree and Maxify:
\begin{center}
\includegraphics[width=9cm]{images/heapsort-example1}
\end{center}
\newpage
Sort the Maxified-Tree:
\begin{center}
\includegraphics[width=9.5cm]{images/heapsort-example2}
\end{center}
\subsection{Quicksort}
\begin{itemize}
\item $\mathcal{O}(n^2)$
\item $\Theta(n \cdot log(n))$
\end{itemize}
\begin{center}
\includegraphics[width=9cm]{images/quick-sort-split}
\end{center}
\begin{center}
\includegraphics[width=6cm]{images/runtime-quicksort}
\end{center}
\begin{center}
\includegraphics[width=7cm]{images/quicksort-example}
\end{center}
$$
\begin{array}{l}\text
{ Worst-case: } T(n)=T(n-1)+T(0)+\Theta(n)\\ \rightarrow T(n)=\Theta\left(n^{2}\right) \\ \text
{ Best - case: } T(n)=2 T\left(\frac{n}{2}\right)+\theta(n)\\ \rightarrow T(n)=O(n \cdot \log (n)) \\ \text { Average-case: } T(n) = (0)+\Theta\left(\frac{n-1}{2}-1\right)+\Theta\left(\frac{n-1}{2}\right)\\=\Theta(n)\end{array}
$$
\subsection{Counting Sort}
\begin{center}
\includegraphics[width=9cm]{images/counting-sort}
\end{center}
\begin{center}
\includegraphics[width=9.5cm]{images/counting-sort-example}
\end{center}
\section{Sweep Algorithms}
\subsection{Segment Direction}
\begin{center}
\includegraphics[width=9cm]{images/segment-direction}
\end{center}
\begin{itemize}
\item a) $\operatorname{cross}\left(\overrightarrow{p_{0} p_{1}}, \overrightarrow{p_{0} p_{2}}\right)>0$
\item b) $\operatorname{cross}\left(\overrightarrow{p_{0} p_{1}}, \overrightarrow{p_{0} p_{2}}\right)<0$
\item if $\operatorname{cross}\left(\overrightarrow{p_{0} p_{1}}, \overrightarrow{p_{0} p_{2}}\right)=0$ $\rightarrow$ collinear, because spanned area is zero
\end{itemize}
\subsection{Two Line Intersection}
4 points are given, we want to check whether the two segment intersect each other.
\begin{center}
\includegraphics[width=8cm]{images/segment-intersection1}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/segment-intersection2}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/line-intersection-examplea}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/line-intersection-exampleb}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/line-intersection-examplec}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/line-intersection-exampled}
\end{center}
\begin{enumerate}[label=(\alph*)]
\item We have ($d_{1}<0$ and $d_{2}>0$) and ($d_{3} > 0$ and $d_{4} < 0$) $\rightarrow$ segments intersect
\item We have ($d_{1}<0$ and $d_{2}<0$) $\rightarrow$ segments do not intersect
\item We have $d_{3}=0$ and ON-SEGMENT($p_{1}$, $p_{2}$, $p_{3}$) = true $\rightarrow$ not a intersection but on segment
\item We have $d_{3}=0$ and ON-SEGMENT($p_{1}$, $p_{2}$, $p_{3}$) = false $\rightarrow$ not a intersection and neither on segment
\end{enumerate}
\subsection{Any Pair Intersection}
\begin{center}
\includegraphics[width=9cm]{images/any-intersection}
\end{center}
\textbf{Event Points}\\
The event points will be sorted according to the x-coordinate. If two or more endpoints have the same x-coordinate, we break ties by putting before left endpoints than right endpoints. For tails we put before endpoints with smaller y-coordinate.
\begin{center}
\includegraphics[width=9cm]{images/endpoints-example}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/pseudo-code-sweetlines}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/operation-sweep-lines}
\end{center}
\section{Vektor-Operationen}
\subsection{Betrag eines Vektors}
Der Betrag eines Vektors $\vec{a}$, $|\vec{a}|$, entspricht der Länge des Vektors (des Pfeils).
\vspace{10pt}
Im zweidimensionalen Raum:
\[
|\vec{a}|=\sqrt{a_1^2+a_2^2}
\]
Im dreidimensionalen Raum:
\[
|\vec{a}|=\sqrt{a_1^2+a_2^2+a_3^2}
\]
\subsection{Skalarprodukt (dot product)}
Das Skalarprodukt ordnet zwei Vektoren $\vec{a}$ und $\vec{b}$ eine Zahl (Skalar) zu. Die Zahl entspricht dem Betrag des Vektors $\vec{b}_{\vec{a}}$.
\begin{center}
\includegraphics[width=3cm]{images/skalarprodukt.png}
\end{center}
\vspace{10pt}
Geometrische Interpretation:
\begin{itemize}
\item \textbf{Parallelität:} Zwei Vektoren sind parallel, wenn das Skalarprodukt der beiden Vektoren gleich dem Produkt der beiden Beträge ist, also $\vec{a}\cdot\vec{b}=|\vec{a}||\vec{b}|$.
\item \textbf{Orthogonalität:} Zwei Vektoren sind genau dann orthogonal, wenn ihr Skalarprodukt 0 ist.
\item \textbf{Spitzer Winkel:} Wenn das Skalarprodukt $>0$ ist, ist $\phi$ ein spitzer Winkel ($<90^\circ$).
\item \textbf{Stumpfer Winkel:} Wenn das Skalarprodukt $<0$ ist, ist $\phi$ ein stumpfer Winkel ($>90^\circ$).
\end{itemize}
Berechnung mit Vektorkomponenten:
\[
\vec{x}\cdot\vec{y}:=\sum_{i=1}^{n}x_iy_i=x_1y_1+x_2y_2+...+x_ny_n
\]
Berechnung mit Betrag und Cosinus:
\[
\vec{x}\cdot\vec{y}=|\vec{x}||\vec{y}|\cos\measuredangle(\vec{x},\vec{y})
\]
\textbf{Bemerkungen}
\begin{itemize}
\item Das Skalarprodukt ist für beliebige $n$-dimensionale Vektoren definiert.
\end{itemize}
\subsection{Kreuzprodukt / Vektorprodukt}
Das Kreuzprodukt $\vec{a}\times\vec{b}$ von zwei Vektoren $\vec{a}$ und $\vec{b}$ im dreidimensionalen Anschauungsraum ist ein Vektor, der orthogonal zu $\vec{a}$ und $\vec{b}$, und damit zu der von $\vec{a}$ und $\vec{b}$ aufgespannten Ebene ist.
\begin{center}
\includegraphics[width=3cm]{images/vectorproduct.png}
\end{center}
Geometrische Interpretation:
\begin{itemize}
\item \textbf{Fläche:} Der Betrag des Kreuzprodukts, $|\vec{a}\times\vec{b}|$, gibt die Fläche des von $\vec{a}$ und $\vec{b}$ aufgespannten Parallelogramms an.
\item \textbf{Normale einer Ebene:} Das Kreuzprodukt entspricht dem Normalenvektor der Ebene.
\end{itemize}
Berechnung mit Vektorkomponenten:
\[
\vec{a}\times\vec{b}=
\left(
\begin{array}{c}
a_1 \\
a_2 \\
a_3
\end{array}
\right)
\times
\left(
\begin{array}{c}
b_1 \\
b_2 \\
b_3
\end{array}
\right)
=
\left(
\begin{array}{c}
a_2b_3-a_3b_2 \\
a_3b_1-a_1b_3 \\
a_1b_2-a_2b_1
\end{array}
\right)
\]
Berechnung mit Winkel:
\[
\vec{a}\times\vec{b}=|\vec{a}||\vec{b}|\sin\theta\vec{n}
\]
\textbf{Bemerkungen}
\begin{itemize}
\item Das Kreuzprodukt ist nur für den dreidimensionalen Raum definiert.
\end{itemize}
\newpage
\section{Introduction to Meta-Heuristics}
\subsection{Greedy Algorithm}
Algorithm that finds the optimal choice for the current state/location. Does not provide the best solution, but a reasonable good one.
\subsection{NP-Problems}
\begin{center}
\includegraphics[width=7cm]{images/np-problems}
\end{center}
\textbf{P Problems}\\
Problems that can be solved in polynomial time e.g. sorting, shortest path $\mathcal{O}(n^2)$
\textbf{NP}\\
The given solution can be verified in polynomial time. No polynomial algorithm, so far, is known to solve this.
\textbf{NP Complete}\\
These are the problems which are both NP and NP-Hard. That means, if we can solve these problems, we can solve any other NP problem and the solutions to these problems can be verified in polynomial time.\\
Example: TSP $\mathcal{O}(2^n)$
\subsection{Graphs}
\begin{center}
\includegraphics[width=6cm]{images/graphs}
\end{center}
\textbf{Representation methods}\\
\begin{itemize}
\item Adjacency Matrix
\begin{center}
\includegraphics[width=4cm]{images/adjacency-matrix-example}
\end{center}
\item Adjacency List: for each vertex the list of its neighbors\\
\quad 1: 2, 3, 4\\
2: 3\\
3: 2\\
4: 4\\
5: 2
\item Vertex List\\
Number of vertices; Number of edges;\\
For each vertex: number and list of neighbors\\
Example: 5, 7, 3, 2, 3, 4, 1, 3, 1, 2, 1, 4, 1, 2
\item Edge List\\
Number of vertices; Number of edges;\\
For each edge: start and end vertex\\
Example: 5, 7, 1, 2, 1, 3, 1, 4, 2, 3, 3, 2, 4, 4, 5, 2
\end{itemize}
\subsection{Problems}
\subsubsection{TSP}
\begin{itemize}
\item Tours with $c_{1}$ as first city: $\mathcal{O}(n!)$
\item NP-hard
\item exponential runtime $\mathcal{O}(2^n)$
\item Describtion: $\min \left(\sum_{i=1}^{n-1} d_{p i} p_{i+1}+d_{p n} p 1\right)$
\end{itemize}
\subsubsection{Scheduling}
\subsubsection{Vertex Colering}
Assignment of labels to each vertex such that no edge connects two identically colered vertices. Minimum vertex coloring means, to minimize the number of the assign colors.
\begin{center}
\includegraphics[width=5cm]{images/vertex-colering-example}
\end{center}
\textbf{First Fitting Color}
\begin{center}
\includegraphics[width=8cm]{images/first-fitting-color}
\end{center}
\begin{center}
\includegraphics[width=9.5cm]{images/first-fitting-color-example}
\end{center}
Decreasing Degree:
\begin{center}
\includegraphics[width=9cm]{images/decreasing-degree}
\end{center}
\begin{center}
\includegraphics[width=9cm]{images/degree-of-saturation}
\end{center}
\subsubsection{Capacitated Vehicle Routing Problem (CVRP)}
\begin{center}
\includegraphics[width=9cm]{images/cvrp}
\end{center}
Input:
\begin{itemize}
\item n customers and 1 depot
\item $q_{i}$: quantity ordered by customer i
\item Distances $d:{ij}$ between each pair of customers and distances between depot and each customer
\item Q: vehicle capacity
\end{itemize}
A Feasible Solution is a set of tours such that
\begin{itemize}
\item Each tour starts from and comes back to the depot
\item Each customer appears exactly once in the set of tours
\item Each customer receives her ordered quantity
\item The sum of quantities on any tour $\leq$ Q
\end{itemize}
Objective: Minimize the total travel length performed by the vehicle
\subsubsection{Quadratic Assignment Problem (QAP)}
Given sets of n facilities (also called activities) and n locations, the flows between facilities and the distances between locations, the objective of the Quadratic Assignment Problem is to assign each facility to a location in such a way as to minimize the total transportation cost.
Applications:
\begin{itemize}
\item Number of connections between electronic modules + Possible positions for the placement of the modules
\item Number of passengers that must change flights + Positions of the gates at an airport
\item Frequency of consecutive appearance of two letters in a language + Time between typing of two successive keys on a keyboard
\item Meeting frequency of two employees + Time for walking from one desk to the other
\end{itemize}
$$
\begin{array}{l}\qquad \min z=\sum_{i, j=1}^{n} \sum_{h, k=1}^{n} d_{i h} f_{j k} X_{i j} X_{h k} \\ \text { Subject to } \\ \sum_{i=1}^{n} X_{i j}=1 \quad \text { for } j=1,2,3, \ldots, n \\ \sum_{j=1}^{n} X_{i j}=1 \quad \text { for } i=1,2,3, \ldots, n \\ X_{i j} \in(0,1) \quad \text { for } i, j=1,2,3, \ldots, n \\ \text { Where } d_{i h} \in D \text { distance matrix and } f_{i k} \in F \text { flow matrix. }\end{array}
$$
\subsubsection{Steiner Tree}
A Steiner Tree is a NP-hard problem.
\begin{center}
\includegraphics[width=9cm]{images/steiner-tree}
\end{center}
\subsection{Shortest-Path-Problem}
Find the shortest length from s to j for each vertex.
\subsubsection{Dijkstra's Algorithm}
We consider a weighted directes graph, where each weight is non-negative.
\begin{center}
\includegraphics[width=5cm]{images/init-single-source}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/relax}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/dijkstra}
\end{center}
\textbf{Example 1}
\begin{center}
\includegraphics[width=9cm]{images/dijkstra-example_1}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/dijkstra-example_2}
\end{center}
\begin{center}
\includegraphics[width=5cm]{images/dijkstra-example_3}
\end{center}
d
\begin{center}
\includegraphics[width=9cm]{images/dijkstra-example_4}
\end{center}
\textbf{Example 2}
\begin{center}
\includegraphics[width=8cm]{images/dijkstra-example2}
\end{center}
\begin{center}
\includegraphics[width=8cm]{images/dijkstra-example22}
\end{center}
\subsection{Minimum Spanning Tree (MST)}
\subsubsection{Kruskal-Algorithm $\mathcal{O}(n)$}
\begin{enumerate}
\item Sort the list according to the costs
\item Iterate through the list form cheapest to most expensive edge and add the edges to the MST when they do not generate a circle
\end{enumerate}
\begin{center}
\includegraphics[width=9cm]{images/kruskal}
\end{center}
\subsubsection{Prim-Algorithm $\mathcal{O}(n)$}
Start with a vertex and add always the cheapest edge to the MST.
\begin{center}
\includegraphics[width=9cm]{images/prim}
\end{center}
\subsubsection{Building a TSP-Tour from MST}
\begin{center}
\includegraphics[width=7.5cm]{images/mst_for_tsp}
\end{center}
\newpage
\section{Meta-Heuristics}
\subsection{Meta-Heuristic Approach}
\begin{enumerate}
\item Use a \textbf{Constructive Method} to create an initial solution (e.g. Nearest Neighbour for TSP)
\item Use a \textbf{Local Improvement Method} to improve the solution (e.g. 2-opt for TSP)
\item Apply \textbf{Selection Rule} to decide which improvement move is taken (e.g. Best Improving Move)
\item \textbf{Randomized Methods} (e.g. Simulated Annealing) explore a larger search space than pure greedy methods
\end{enumerate}
\subsection{Constructive Methods - \textit{Finding a first reasonable good solution}}
\subsubsection{Random Sampling/Building}
\begin{itemize}
\pro simple method
\pro works for almost all problems
\pro easy to implement
\con bad solution quality
\end{itemize}
\subsubsection{Greedy construction}
\begin{itemize}
\con Too short-sighted
\con A good choice at a step can be globally disastrous
\end{itemize}
\begin{center}
\includegraphics[width=9cm]{images/creedy-construction}
\end{center}
\textbf{Nearest Neightbor}
\begin{center}
\includegraphics[width=9cm]{images/nearest-neighbor}
\end{center}
\textbf{Best Global Edge}
\begin{center}
\includegraphics[width=9cm]{images/best-global-edge}
\end{center}
\newpage
\textbf{Maximum Regret}
\begin{center}
\includegraphics[width=9cm]{images/maximum-regret}
\end{center}
\textbf{Random Best Insertion}
\begin{center}
\includegraphics[width=9.5cm]{images/random-best-insertion}
\end{center}
\textbf{Most Distant Best Insertion}
\begin{center}
\includegraphics[width=9cm]{images/most-distant-best-insertion}
\end{center}
\textbf{Nearest Best Insertion}