Added GA4 EX2
This commit is contained in:
parent
d7f1b286d0
commit
2d4e998383
2 changed files with 36 additions and 2 deletions
BIN
GA4/ga4.pdf
BIN
GA4/ga4.pdf
Binary file not shown.
38
GA4/ga4.tex
38
GA4/ga4.tex
|
@ -21,7 +21,7 @@
|
||||||
columns=fullflexible,
|
columns=fullflexible,
|
||||||
commentstyle=\color{gray},
|
commentstyle=\color{gray},
|
||||||
keywordstyle=\bfseries,
|
keywordstyle=\bfseries,
|
||||||
keywords={,NIL,while,if,elif,else,FUNCTION,return,for,from,to,TRUE,FALSE},
|
keywords={,NIL,while,if,elif,else,continue,FUNCTION,return,for,from,to,TRUE,FALSE},
|
||||||
mathescape=true,
|
mathescape=true,
|
||||||
aboveskip=2em,
|
aboveskip=2em,
|
||||||
captionpos=b,
|
captionpos=b,
|
||||||
|
@ -76,7 +76,7 @@ FUNCTION BEST-PATH(G=(V,E), v, w):
|
||||||
FUNCTION HELP-SETUP(G=(V,E), P, S, v):
|
FUNCTION HELP-SETUP(G=(V,E), P, S, v):
|
||||||
for each vertex u $\in$ S:
|
for each vertex u $\in$ S:
|
||||||
P[u] = v
|
P[u] = v
|
||||||
HELP-SETUP(G, P, Adj[u] \ v, u)
|
HELP-SETUP(G, P, Adj[u] \ {v}, u)
|
||||||
|
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
|
@ -84,4 +84,38 @@ The $O(n)$ setup happens between line 2 and line 14. This is mainly needed to in
|
||||||
(and consequent parent relation) on the tree.
|
(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}
|
||||||
|
|
||||||
|
\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
|
||||||
|
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.
|
||||||
\end{document}
|
\end{document}
|
Reference in a new issue