Compare commits

...

1 commit

Author SHA1 Message Date
Claudio Maggioni
d7f1b286d0 Added GA4 EX1 2019-05-21 12:17:08 +02:00
2 changed files with 87 additions and 0 deletions

BIN
GA4/ga4.pdf Normal file

Binary file not shown.

87
GA4/ga4.tex Normal file
View file

@ -0,0 +1,87 @@
% vim: set ts=2 sw=2 tw=80 et:
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{listings}
\title{Graded Assignment 4 -- DSA}
\author{Claudio Maggioni}
\setlength{\parindent}{0cm}
% listings configuration
\lstset{
basicstyle=\small\ttfamily,
frame=shadowbox,
xleftmargin=10mm, % Move everything 10mm to the right
framexleftmargin=10mm, % Make the frame 10mm wider to the left
numbers=left, % Add numbers to the left of the code
rulesepcolor=\color{black},
columns=fullflexible,
commentstyle=\color{gray},
keywordstyle=\bfseries,
keywords={,NIL,while,if,elif,else,FUNCTION,return,for,from,to,TRUE,FALSE},
mathescape=true,
aboveskip=2em,
captionpos=b,
abovecaptionskip=1em,
belowcaptionskip=1em,
}
\begin{document}
\maketitle
\tableofcontents
\lstlistoflistings
\newpage
\section{Exercise 1}
\begin{lstlisting}[caption=Solution for exercise 1, label={lst:ex1}]
FUNCTION BEST-PATH(G=(V,E), v, w):
P[V(G)[0]] = NIL
for each vertex u $\in$ V(G):
prev_start[u] = NIL
prev_end[u] = NIL
prev_start[v] = START
prev_end[w] = END
HELP-SETUP(G, P, Adj[V(G)[0]], V(G)[0])
s = v
e = w
while prev_end[s] is NIL and prev_start[e] is NIL:
if P[s] is not NIL:
prev_start[P[s]] = s
s = P[s]
if P[e] is not NIL:
prev_end[P[e]] = e
e = P[e]
if prev_end[s] is not NIL:
n = s
else:
n = e
while s is not v:
s = prev_start[s]
prev_end[s] = P[s]
return prev_end
FUNCTION HELP-SETUP(G=(V,E), P, S, v):
for each vertex u $\in$ S:
P[u] = v
HELP-SETUP(G, P, Adj[u] \ v, u)
\end{lstlisting}
The $O(n)$ setup happens between line 2 and line 14. This is mainly needed to initialize some help arrays and define an arbitrary root
(and consequent parent relation) on the tree.
The rest of the algorithm walks the tree from the start to the root and from the end to the root concurrently, keeping track of the path taken and stopping when an edge was traversed by both walks. Then, the path memory to the start is reversed and inserted in the path memory for the end in order to obtain a mapping to the next node in the path from $v$ to $w$. The complexity of this step is $O(dist(v,w))$, since the number of traversed edges is at most two times the distance from $v$ to $w$, and the reversing operation at the end requires at most $dist(v,w)$ steps.
\end{document}