Done GA4
This commit is contained in:
parent
4c6e532b2c
commit
f1e8517d9b
2 changed files with 65 additions and 9 deletions
BIN
GA4/ga4.pdf
BIN
GA4/ga4.pdf
Binary file not shown.
70
GA4/ga4.tex
70
GA4/ga4.tex
|
@ -46,8 +46,8 @@ FUNCTION BEST-PATH(G=(V,E), v, w):
|
|||
prev_start[u] = NIL
|
||||
prev_end[u] = NIL
|
||||
|
||||
prev_start[v] = START
|
||||
prev_end[w] = END
|
||||
prev_start[v] = START $(\textit{non-NIL})$
|
||||
prev_end[w] = END $(\textit{non-NIL})$
|
||||
|
||||
HELP-SETUP(G, P, Adj[V(G)[0]], V(G)[0])
|
||||
|
||||
|
@ -71,7 +71,12 @@ FUNCTION BEST-PATH(G=(V,E), v, w):
|
|||
s = prev_start[s]
|
||||
prev_end[s] = P[s]
|
||||
|
||||
return prev_end
|
||||
s = v
|
||||
while s != w:
|
||||
print(s)
|
||||
s = prev_end[s]
|
||||
if v != w:
|
||||
print(w)
|
||||
|
||||
FUNCTION HELP-SETUP(G=(V,E), P, S, v):
|
||||
for each vertex u $\in$ S:
|
||||
|
@ -85,9 +90,10 @@ 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.
|
||||
path memory for the end in order to obtain a mapping to the next node in the path from $v$ to $w$. This mapping is then printed.
|
||||
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, as the printing operation.
|
||||
|
||||
\section{Exercise 2}
|
||||
|
||||
|
@ -134,7 +140,7 @@ first place, so $o$ does not have to be defined for this case.
|
|||
FUNCTION OBSERVATION-HOLDS(G=(V,E), o):
|
||||
for each vertex u $\in$ V(G):
|
||||
color[u] = WHITE
|
||||
species[u] = Nil
|
||||
species[u] = NIL
|
||||
|
||||
for each vertex u $\in$ V(G):
|
||||
if color[u] $\neq$ WHITE:
|
||||
|
@ -177,5 +183,55 @@ Note that if the observation graph $G$ is composed by more than one connected co
|
|||
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.
|
||||
|
||||
\section{Exercise 4}
|
||||
|
||||
\subsection{Point 1}
|
||||
|
||||
We assume the minimum spanning tree $T$ is represented as an adjacency-mapped graph. \textit{weight} is the weight mapping for every
|
||||
edge in the minimal spanning tree. The other parameters must be given as described in the assignment.
|
||||
|
||||
\begin{lstlisting}[caption=Solution for exercise 4 point 1, label={lst:ex4p1}]
|
||||
FUNCTION IS-MST-MINIMAL(T=(V,E), weight, v, w, c):
|
||||
P[w] = NIL
|
||||
DEFINE-PARENT(T, P, Adj[w], w)
|
||||
edge1_w = weight($\textit{edge}$ (v, P[v]))
|
||||
s = v
|
||||
while P[s] $\neq$ w:
|
||||
s = P[s]
|
||||
edge2_w = weight($\textit{edge}$ (s, w))
|
||||
return c > edge1_w $\land$ c > edge2_w
|
||||
|
||||
FUNCTION DEFINE-PARENT(G=(V,E), P, S, v):
|
||||
for each vertex u $\in$ S:
|
||||
P[u] = v
|
||||
DEFINE-PARENT(G, P, Adj[u] \ {v}, u)
|
||||
\end{lstlisting}
|
||||
|
||||
The algorithm works by walking the entire tree with \texttt{DEFINE-PARENT} in order to define a parent relation considering $w$ as
|
||||
the root. Then, this relation is used to define the path between $v$ and $w$, and the weights of the
|
||||
first outgoing edge from $v$ and the final edge to $w$ are memorized in \texttt{edge1\_w} and \texttt{edge2\_w}.
|
||||
The complexity of this step is $O(|V|)$ since the total number of edges in a tree is linearly dependent to the number
|
||||
of vertices (i.e.: $|E_T| = |V| - 1$).
|
||||
|
||||
Note that if $v$ and $w$ are adjacent then these two values are the same, but this does not compromise the algorithm.
|
||||
|
||||
Finally, we check if $(v, w)$ is the the edge with highest weight with respect to \textit{edge1} and \textit{edge2}. If this is the
|
||||
case, then replacing \textit{edge1} or \textit{edge2} with $(v, w)$ would not give a spanning tree with minimum total weight, and
|
||||
thus we return \texttt{FALSE}. Otherwise, the opposite is true and we return \texttt{TRUE}. Note that this step is constant, so the
|
||||
total complexity is $O(|V|)$.
|
||||
|
||||
\subsection{Point 2}
|
||||
|
||||
\begin{lstlisting}[caption=Solution for exercise 4 point 2, label={lst:ex4p1}]
|
||||
FUNCTION MAKE-MST-MINIMAL(T=(V,E), weight, v, w, c):
|
||||
if not IS-MST-MINIMAL(T, weight, v, w, c):
|
||||
if edge1_w < edge2_w:
|
||||
$\textit{delete edge1 from T}$
|
||||
else:
|
||||
$\textit{delete edge2 from T}$
|
||||
$\textit{add (v, w) to T}$
|
||||
\end{lstlisting}
|
||||
|
||||
For what said before, this algorithm updates $T$ to a valid MST and runs in $O(|V_T|)$ which is always $< O(|E|)$.
|
||||
|
||||
\end{document}
|
Reference in a new issue