commit e1af0af7280ca6beb9d9582fd0e8387a8b0eb744 Author: xfarrow Date: Mon Aug 14 14:08:19 2023 +0200 Add files via upload diff --git a/algorithms-and-data-structures/Algoritmo di Kadane/Algoritmo di Kadane.latex b/algorithms-and-data-structures/Algoritmo di Kadane/Algoritmo di Kadane.latex new file mode 100644 index 0000000..0519d72 --- /dev/null +++ b/algorithms-and-data-structures/Algoritmo di Kadane/Algoritmo di Kadane.latex @@ -0,0 +1,136 @@ +\documentclass{article} + +% Language setting +% Replace `english' with e.g. `spanish' to change the document language +\usepackage[italian]{babel} + +% Set page size and margins +% Replace `letterpaper' with `a4paper' for UK/EU standard size +\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry} + +% Useful packages +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{graphicx} +\usepackage{listings} +\usepackage[colorlinks=true, allcolors=blue]{hyperref} + +\title{Algoritmo di Kadane} +\author{Alessandro Ferro} + +\begin{document} +\maketitle + +\begin{abstract} +Si vuole dimostrare la correttezza dell'Algoritmo di Kadane, in particolare la versione che ammette l'esistenza di una sotto-sequenza vuota (ovvero l'algoritmo non restituirà mai un valore inferiore a 0). +\end{abstract} + +\section{Teorema} +Per dimostrare la correttezza dell'algoritmo, enunciamo il seguente teorema: + + +Fissati $i,r \in \mathbb{N} $, se $\forall j $ compreso nell'intervallo $ i \leq j < r $ si verifica che + +\begin{itemize} +\item $SUM(i,j) \geq 0 $ +\item $SUM(i,r) < 0 $ +\end{itemize} + +$\implies$ + +\begin{enumerate} +\item $SUM(p,q) \leq SUM(i,q) \ \forall p,q $ comprese nell'intervallo $i\leq p \leq q \leq r$ +\item $SUM(p,r+k) < SUM(r+1, r+k) \ \forall p$ comprese nell'intervallo $i \leq p \leq r$ con $k \geq 1$ +\end{enumerate} + +\subsection{Dimostrazione informale} +Dimostriamo dapprima il punto $1$:\\ +Tutte le sequenze $(i,i),(i+1),(i+2),...,(i,q)$ hanno somma non negativa per ipotesi (in quanto $q$ è nell'intervallo $i \leq q < r$), pertanto è evidente che una somma che ha più termini ($SUM(i,q)$) sia maggiore o uguale di una somma che ne ha di meno o in egual misura ($SUM(p,q)$).\\ + +Adesso dimostriamo il punto $2$:\\ +Tramite una più formale dimostrazione scopriremo che $SUM(p,r) < 0$. Avremo quindi che\\ $SUM(p,r+k) = SUM(p,r) + SUM(r+1,r+k) \implies SUM(p,r+k) < SUM(r+1,r+k)$. + +\subsection{Dimostrazione} +Dimostrazione punto $1$:\\ +Se $p=i$ allora si ha banalmente $SUM(p,q) = SUM(i,q)$. Dimostriamo allora quando $i SUM(i,y)$, la sotto-sequenza localmente massima potrebbe essere $(x,y)$, e dunque sarebbe necessario valutarne il valore della somma. +\item Grazie al punto $2$ possiamo dire che è inutile analizzare le sequenze $(x,r+k)$ con $x$ nell'intervallo $[i,r]$ perché più avanti si troverà sicuramente una sottosequenza di somma maggiore. +\end{itemize} + +Dunque, se abbiamo analizzato le sequenze $(i,i),(i,i+1),(i,i+2),...,(i,r)$ sarà inutile analizzare una sequenza $(x,y)$ con $x$ compreso nell'intervallo $i < x \leq r$, perché comunque preso $y$, sarà inutile la sua analisi. + +\newpage + +\section{Algoritmo} +\begin{lstlisting} +KadaneAlgorithm(Array v){ + n = v.Length(); + max_sum = 0; + local_sum = 0; + + for(i = 0 ; i < n ; i++){ + local_sum = local_sum + v[i]; + if( local_sum < 0 ){ + local_sum = 0; + } + else if( local_sum > max_sum ){ + max_sum = local_sum; + } + } + + return max_sum; +} +\end{lstlisting} +\subsection{Spiegazione} +L'algoritmo ha una complessità $T(n) = \Theta(n)$ in quanto non genera tutte le sottosequenze possibili (altrimenti il tempo sarebbe stato un $\Omega(n^2)$).\\ + +Infatti se trova che $SUM(i,r) < 0$ resetta $sum$ a 0 e riparte iniziando a sommare dalla cella $r+1$ in avanti, in virtù del fatto che generare qualsiasi sotto-sequenza $(x,y)$ con $i 0, n_1 > 0 \ : \ \forall n \geq n_1, \ h'(n) \leq c_1 h(n) $ +\\ +$\exists c_2 > 0, n_2 > 0 \ : \ \forall n \geq n_2, \ g'(n) \leq c_2 g(n) $ +\\ +Where $h'(n)$ and $g'(n)$ are the anonymous functions contained in $O(h(n))$ and $O(g(n))$. +\\ +Let $n_3 = MAX(n_1,n_2)$, then +$$h'(n) + g'(n) \leq c_1h(n) + c_2g(n) \ \forall n \geq n_3$$ +Let $c_3 = MAX(c_1,c_2)$, then +$$h'(n) + g'(n) \leq c_1h(n) + c_2g(n) \leq c_3(h(n) + g(n)) \ \forall n \geq n_3$$ +$$\implies h'(n) + g'(n) \leq c_3(h(n) + g(n)) \ \forall n \geq n_3$$ +which means that $h'(n) + g'(n) \in O(h(n)+g(n))$ +\\ +$\square$ + +\section{Notes} +Most books do an abuse of notation, reporting this theorem as +$$\sum \Theta(f(n)) = \Theta(\sum f(n))$$ + +\end{document} diff --git a/algorithms-and-data-structures/Linearity Property applied to convergent series/Linearity_Property_applied_to_convergent_series.pdf b/algorithms-and-data-structures/Linearity Property applied to convergent series/Linearity_Property_applied_to_convergent_series.pdf new file mode 100644 index 0000000..6d39b11 Binary files /dev/null and b/algorithms-and-data-structures/Linearity Property applied to convergent series/Linearity_Property_applied_to_convergent_series.pdf differ diff --git a/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum number of simple paths generable from a graph.latex b/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum number of simple paths generable from a graph.latex new file mode 100644 index 0000000..268f8ac --- /dev/null +++ b/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum number of simple paths generable from a graph.latex @@ -0,0 +1,42 @@ +\documentclass{article} + +% Language setting +% Replace `english' with e.g. `spanish' to change the document language +\usepackage[english]{babel} + +% Set page size and margins +% Replace `letterpaper' with`a4paper' for UK/EU standard size +\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry} + +% Useful packages +\usepackage{amsmath} +\usepackage{graphicx} +\usepackage[colorlinks=true, allcolors=blue]{hyperref} + +\title{Maximum number of simple paths generable from a graph} +\author{Alessandro Ferro} + +\begin{document} +\maketitle + +\begin{abstract} +I want to give an exact formula that represents the maximum number of simple paths generable from a graph +\end{abstract} + +\section{Formula} + +Let $ G = \langle V,E\rangle $ be a directed graph.\ +The maximum number of simple paths generable from $G$ is +$$\sum_{i=0}^{|V|}\frac{|V|!}{(|V|-i)!}$$ + +\subsection{Corollary} +Let $ G = \langle V,E\rangle $ be an undirected graph.\ +The maximum number of simple paths generable from $G$ is +$$\sum_{i=0}^{|V|}{\frac{|V|!}{i! * (|V|-i)!}}$$ + +\section{Explaination} + +The formula is derived from the number of possible permutations from a set whose cardinality is $|V|$ into a set whose cardinality is $i$.\newline +If $S$ is a sequence $\langle s_1, s_2, ... , s_i \rangle $ then we can generate $\frac{|V|!}{(|V|-i)!}$ different simple paths. Then we shall sum the number of possible simple paths for any sequence whose length is $0$ to $|V|$. + +\end{document} \ No newline at end of file diff --git a/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum_number_of_simple_paths_in_a_graph.pdf b/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum_number_of_simple_paths_in_a_graph.pdf new file mode 100644 index 0000000..575770c Binary files /dev/null and b/algorithms-and-data-structures/Maximum number of simple paths generable from a graph/Maximum_number_of_simple_paths_in_a_graph.pdf differ diff --git a/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying Mathematical Notations in Recurrence Equations.latex b/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying Mathematical Notations in Recurrence Equations.latex new file mode 100644 index 0000000..e41a94a --- /dev/null +++ b/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying Mathematical Notations in Recurrence Equations.latex @@ -0,0 +1,77 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage{amsmath} +\title{Simplifying Mathematical Notations in Recurrence Equations} +\author{Alessandro Ferro} +\date{April 2022} + +\begin{document} + +\maketitle + +\section{Introduction} +In the book "Introduction to Algorithms" written by CLRS, section 4.4, the following recurrence equation is given: + + \begin{equation} + T(n)= + \begin{cases} + \Theta(1), & \text{if}\ n=1 \\ + 3T(\lfloor n/4 \rfloor) + \Theta(n^2), & \text{otherwise} + \end{cases} + \end{equation} + + which later on was simplified as + + \begin{equation} + T(n)= + \begin{cases} + \Theta(1), & \text{if}\ n=1 \\ + 3T(\lfloor n/4 \rfloor) + cn^2, & \text{otherwise} + \end{cases} + \end{equation} + + we will argue that even if $\Theta(n^2)$ is a set, and $c*n^2$ is a scalar and they generally cannot be swapped one with the other, in this case it can be done and will help the equation to be more tractable. + +\section{Proof} +Let's consider the general case only, so we have +\begin{gather*} + 3T(\lfloor n/4 \rfloor) + \Theta(n^2) +\end{gather*} + +which expanded is +\begin{gather*} + 3T(\lfloor n/4 \rfloor) + f(n) \ \text{where} \ f(n) \in \Theta(n^2) +\end{gather*} + +$f(n) \in \Theta(n^2)$ means that + +$\exists \ c_1>0,c_2>0,n_0>0$ such that $c_1*n^2 \leq f(n) \leq c_2*n^2 \ \forall n \geq n_0$ +\\ \\ +So we have +\begin{gather*} + 3T(\lfloor n/4 \rfloor) + c_1 * n^2 \leq 3T(\lfloor n/4 \rfloor) + f(n) \leq 3T(\lfloor n/4 \rfloor) + c_2 * n^2 \ \forall n \geq n_0 + \\ + = + \\ + \underbrace{3T(\lfloor n/4 \rfloor) + c_1 * n^2}_{\alpha} \leq T(n) \leq \underbrace{3T(\lfloor n/4 \rfloor) + c_2 * n^2}_{\beta} \ \forall n \geq n_0 +\end{gather*} +So if we resolve $\alpha$ only, we'll have a lower bound for $T(n)$. If we resolve $\beta$ only, we'll have an upper bound for $T(n)$. + + +But $\alpha$ and $\beta$ are asymptotically equivalent, that means that $T(n)$ is asymptotycally equivalent to $\alpha$ and $\beta$ too. + +So we just need to resolve either $\alpha$ or $\beta$ to obtain the asymptotic value of $T(n)$, so we can just resolve for a generic $c$ : + + \begin{equation} + T(n)= + \begin{cases} + \Theta(1), & \text{if}\ n=1 \\ + 3T(\lfloor n/4 \rfloor) + cn^2, & \text{otherwise} + \end{cases} + \end{equation} + + since using a generic constant won't change the asymptotic value. + +\section{Conclusions} +The concept described in this document and the relative proof can be easily extended to any similar recurrence, and generalized. +\end{document} diff --git a/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying_Mathematical_Notations_in_Recurrence_Equations.pdf b/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying_Mathematical_Notations_in_Recurrence_Equations.pdf new file mode 100644 index 0000000..3e59e40 Binary files /dev/null and b/algorithms-and-data-structures/Simplifying Mathematical Notations in Recurrence Equations/Simplifying_Mathematical_Notations_in_Recurrence_Equations.pdf differ