Fixed exercise 4 GA4

This commit is contained in:
Claudio Maggioni 2019-05-27 10:48:38 +02:00
parent f1e8517d9b
commit e1cb103be5
2 changed files with 13 additions and 18 deletions

Binary file not shown.

View file

@ -5,6 +5,7 @@
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{hyperref}
\title{Graded Assignment 4 -- DSA}
\author{Claudio Maggioni}
@ -194,12 +195,14 @@ edge in the minimal spanning tree. The other parameters must be given as describ
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:
while s $\neq$ w:
edge_w = weight($\textit{edge}$ (s, P[s]))
if edge_w > c:
return FALSE
s = P[s]
edge2_w = weight($\textit{edge}$ (s, w))
return c > edge1_w $\land$ c > edge2_w
return TRUE
FUNCTION DEFINE-PARENT(G=(V,E), P, S, v):
for each vertex u $\in$ S:
@ -208,27 +211,19 @@ FUNCTION DEFINE-PARENT(G=(V,E), P, S, v):
\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
the root. Then, this relation is used to define the path between $v$ and $w$, and the weights of every edge in the path from $v$ to
$w$ are memorized in \texttt{edge\_w} and compared and checked against the weight of $(v, w)$. If we find an edge in the path with
weight bigger than $(v, w)$ we can then replace that edge with $(v, w)$, so we return \texttt{FLASE}\textit{ (sic)}
\footnote{\url{https://medium.com/@DanielC7/dbf8773df767}} since we the old MST is not minimal anymore.
The complexity of this 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{delete edge where IS-MST-MINIMAL stopped from T}$
$\textit{add (v, w) to T}$
\end{lstlisting}