Done GA4 EX3
This commit is contained in:
parent
2d4e998383
commit
4c6e532b2c
2 changed files with 65 additions and 5 deletions
BIN
GA4/ga4.pdf
BIN
GA4/ga4.pdf
Binary file not shown.
70
GA4/ga4.tex
70
GA4/ga4.tex
|
@ -21,7 +21,7 @@
|
|||
columns=fullflexible,
|
||||
commentstyle=\color{gray},
|
||||
keywordstyle=\bfseries,
|
||||
keywords={,NIL,while,if,elif,else,continue,FUNCTION,return,for,from,to,TRUE,FALSE},
|
||||
keywords={,NIL,while,if,elif,else,continue,FUNCTION,return,for,from,to,TRUE,FALSE,not,},
|
||||
mathescape=true,
|
||||
aboveskip=2em,
|
||||
captionpos=b,
|
||||
|
@ -80,10 +80,14 @@ FUNCTION HELP-SETUP(G=(V,E), P, S, v):
|
|||
|
||||
\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 $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.
|
||||
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.
|
||||
|
||||
\section{Exercise 2}
|
||||
|
||||
|
@ -117,5 +121,61 @@ FUNCTION CONNECTED-COMPONENTS(G=(V,E)):
|
|||
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
|
||||
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.
|
||||
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.
|
||||
|
||||
|
||||
\end{document}
|
Reference in a new issue