This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
DSA/GA4/ga4.tex

181 lines
6.2 KiB
TeX
Raw Normal View History

2019-05-21 10:15:37 +00:00
% 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,
2019-05-22 18:58:22 +00:00
keywords={,NIL,while,if,elif,else,continue,FUNCTION,return,for,from,to,TRUE,FALSE,not,},
2019-05-21 10:15:37 +00:00
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
2019-05-21 18:27:30 +00:00
HELP-SETUP(G, P, Adj[u] \ {v}, u)
2019-05-21 10:15:37 +00:00
\end{lstlisting}
2019-05-22 18:58:22 +00:00
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.
2019-05-21 10:15:37 +00:00
2019-05-22 18:58:22 +00:00
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.
2019-05-21 18:27:30 +00:00
\section{Exercise 2}
\begin{lstlisting}[caption=Solution for exercise 2, label={lst:ex2}]
FUNCTION CONNECTED-COMPONENTS(G=(V,E)):
for each vertex u $\in$ V(G):
color[u] = WHITE
c = 0
for each vertex s $\in$ V(G):
if color[u] $\neq$ WHITE:
continue
color[s] = GRAY
Q = $\emptyset$
c = c + 1
ENQUEUE(Q, s)
while Q $\neq \emptyset$:
u = DEQUEUE(Q)
for each v $\in$ Adj[u]:
if color[v] == WHITE:
color[v] = GRAY
ENQUEUE(Q, v)
color[u] = BLACK
return c
\end{lstlisting}
The algorithm is simply a modified color-only version of BFS with an extra iteration: using every vertex in the graph as a starting
node. If the node was already visited, the color makes this iteration over all vertexes skip to the next vertex. The complexity of
this algorithm is $O(|V| + |E|)$ like BFS, since the iterative application to BFS over every connected component will cover every edge
2019-05-22 18:58:22 +00:00
and node of the graph exactly once, and the extra check for nodes being which is just another $O(|V|)$ cost, which can be ignored.
\section{Exercise 3}
Assume data is provided in Graph-like form $G=(V,E)$ where $V$ is the set of butterflies, $E$ is the set of edges, and a relation
$o : E \to \textsc{true, false}$ where \textsc{false} means ``same'' and \textsc{true} means ``different'' to determine the the type
of observation. Ambiguous observations are not included in $E(G)$ in the
first place, so $o$ does not have to be defined for this case.
\begin{lstlisting}[caption=Solution for exercise 3, label={lst:ex3}]
FUNCTION OBSERVATION-HOLDS(G=(V,E), o):
for each vertex u $\in$ V(G):
color[u] = WHITE
species[u] = Nil
for each vertex u $\in$ V(G):
if color[u] $\neq$ WHITE:
continue
species[u] = FALSE
if not DFS-CHECK-OBSERVATION(species, o, v):
return FALSE
return TRUE
FUNCTION DFS-CHECK-OBSERVATION(species, o, v):
color[u] = GREY
for each vertex v $\in$ Adj[u]:
if color[v] == WHITE:
species[v] = species[u] XOR o($\textit{edge}$ (u, v))
if not DFS-CHECK-OBSERVATION(species, o, v):
return FALSE
else:
if species[u] $\neq$ species[v] XOR o($\textit{edge}$ (v, u)) $\lor$
species[v] $\neq$ species[u] XOR o($\textit{edge}$ (u, v)):
return FALSE
color[u] = BLACK
return TRUE
\end{lstlisting}
The code given traverses $G$ using a modified DFS by first assigning an arbitrary species (\texttt{FALSE}) to the first
vertex encountered, then by computing the species of the subsequent nodes encountered using the observation mapping $o : E \to
\textsc{true, false}$. A XOR is used to assign the opposite species (flip the species[\ldots] bit) to the newly discovered node
if $o(\textsc{current node, new node})$ is ``different'', and to assign the same species if the observation is ``same''
\footnote{\textsc{true, false} mean ``different'' and ``same'' when they are result of $o(\ldots)$. When assigning to
\texttt{species[\ldots]}, they represent an arbitrary assignment of the two species of butterfly.}.
Paths between already visited vertexes (non-white vertexes) are checked in order to find inconsistencies. Both edge traversal
directions are checked in order to handle cases where observation are directed (i.e. $o(\textit{edge} (v, u)) \neq
o(\textit{edge} (v, u)))$. If an inconsistency is found, we return \texttt{FALSE}, otherwise we return true.
Note that if the observation graph $G$ is composed by more than one connected component assigning an arbitrary species to the first
vertex encountered in each connected component does not compromise the solutions, since we are not asked to find the correct species
assignment but we are just asked to find inconsistencies.
2019-05-21 10:15:37 +00:00
\end{document}