Commit cfe3d5f4 authored by Michael Banken's avatar Michael Banken

reorganized algorithms

parent 4c666498
......@@ -13,48 +13,75 @@ This is not true for the other direction, as removing unnecessary inclusions may
As a result of these observations, it is a strictly better option to always prioritize outer theories and since the runtime will already will already be dominating the time needed for a sort, we can do so with little additional cost (see \autoref{sec:runtime}).
\subsection{Theory Optimization}
Optimization of a theory itself happens in two separate passes. In the first pass superfluous inclusions are detected and eliminated. In the second pass cleans up any remaining simply redundant inclusions. Both of these algorithms can be found below.
Optimization of a theory itself happens in two separate passes. The first pass detects and eliminates superfluous inclusions. The second pass cleans up any remaining simply redundant inclusions.
The reason for separating these passes is to avoid the removal of simply redundant inclusions, that will later have to be reinserted when eliminating a partially superfluous inclusion. An example for such a hazard is the example of an overlap between the two types of candidates in \autoref{fig:redundantoverlap}
\subsubsection{First pass - Eliminating superfluous inclusions}
%TODO
For this algorithm it is not necessary to make a distinction between purely and partially superfluous inclusions, as the pure case is entirely subsumed by the partial case with an empty set of necessary inclusions.
\subsubsection{Second pass - Eliminating simply redundant inclusions}
The basic idea behind the second pass is to collect all those theories that are included indirectly by the current theory and throwing away all of those direct inclusions that are part of this set. This will leave us exactly with those inclusions that are not simply redundant, without changing the flattened theory graph.
Proof sketch:\\
Simply redundant inclusions are those, that are both directly and indirectly included.
We get rid of those direct inclusions that are also indirect, so clearly all simply redundant inclusions must be gone.
\subsection{Future Lite Code}
\label{sec:futurelite}
In \autoref{sec:viability} we already mentioned the problem of future lite code. The obvious method for aquiring the future lite code is traversing the entire theory graph and whenever we find a theory inclusion, we make a note for the included theory that the theory we are currently traversing is part of its future code. Unfortunately this requires traversing the graph in its entirety.
For the actual algorithm we skip this part and instead put create a map of used theories in the future code, since this is the part we actually require. Since this means potentially going over every theory for every theory (or at least reasonably close to it), our runtime worst case for this part is quadratic in the number of theories in the graph.
For the actual algorithm we skip this part and instead put create a map of used theories in the future code, since this is the part we actually require. Since this means potentially going over every theory for every theory (or at least reasonably close to it), our runtime worst case for this part is quadratic in the number of theories in the graph.
\section{Pseudo Code}
\subsection{Finding simply redundant inclusions}
\label{sec:alg_sri}
The following pseudo code is for finding simply redundant inclusions (see: \autoref{sec:redinc}).
\subsection{Optimizing graph}
The following code applies the earlier optimizations to the entire theory graph.
\begin{algorithm}[H]
\KwData{$theory$ = theory from our theory graph\\
pass changes that were already applied to the graph}
\KwResult{$redundantIncludes$ = set of simply redundant inclusions}
\label{alg:graph}
\KwData{theoryGraph = theory graph to be optimized}
\KwResult{replacements = map from theories to maps of theory inclusions to their replacement}
\Begin{
$subIncludes := \emptyset$\
$redundantIncludes := \emptyset$\;
\For{$i \leftarrow directIncludes(theory)$}{
$subIncludes := subIncludes \cup includes(i)$\;
}
\For{$i \leftarrow directIncludes(theory)$}{
\If{$i \in subIncludes$} {
$redundantIncludes := redundantIncludes \cup \{i\}$\;
}
}
\KwRet $redundantIncludes$\;
futureUses := empty map\;
replacements := empty map\;
theoryGraph := sort(theoryGraph)\;
theoryGraphRev := reverse(theoryGraph)\;
\For{theory $\leftarrow$ theoryGraph}{
add include $\rightarrow$ usedTheories(theory) $\cup$ futureUses(theory) to futureUses\;
add theory $\rightarrow$ optimize(theory, futureUses(theory)) to replacements\;
\For{include $\leftarrow$ includes(theory)}{
replace include $\rightarrow$ futureUses(include)$\cup$usedTheories(include)$\cup$futureUses(theory) in futureUses\;
}
\KwRet replacements \;
}
}
\caption{simplyRedundantIncludes(theory, futureUse)}
\caption{optimizeGraph(theorygraph)}
\end{algorithm}
The basic idea behind this algorithm is to collect all those theories that are included indirectly by the current theory and throwing away all of those direct inclusions that are part of this set. This will leave us exactly with those inclusions that are not simply redundant, without changing the flattened theory graph.
\subsection{Optimizing theory}
Proof:\\
Simply redundant inclusions are those, that are both directly and indirectly included.\\
We get rid of those direct inclusions that are also indirect, so clearly all simply redundant inclusions must be gone.\\
The following pseudo code combines the two earlier routines to apply optimizations to a given theory.
Since theories are partially ordered by inclusion and inclusion paths are of finite depth, all indirect inclusions must ultimately be the result of a direct inclusion, so all relevant direct inclusions are still in the set.
\begin{algorithm}[H]
\KwData{theory = theory from our theory graph\\
futureUse = set of theories used by theories including $theory$ in future lite code\\
pass changes that were already applied to the graph}
\KwResult{replacements = map from theory inclusions to their replacement}
\Begin{
replacements = superfluousIncludes(theory, futureUse)\;
\For{removal $\leftarrow$ redundantIncludes(theory, futureUse)}{
add removal $\rightarrow$ $\emptyset$ to replacements\;
}
\KwRet replacements\;
}
\caption{optimize(theory, futureUse)}
\end{algorithm}
\subsection{Finding superfluous inclusions}
\label{sec:alg_si}
......@@ -82,48 +109,28 @@ The following pseudo code is for finding superfluous inclusions (see: \autoref{s
Note that in \autoref{sec:superinc} we made a destinction between purely and partially superfluous inclusions. However we do not need to make this distinction while searching for them, as we can search for them by using the criteria for generally superfluous inclusion. Since they only differ in the set of inclusions that needs to be retained and we write that set in our result anyway, both can be accomplished by the same routine.
\subsection{Optimizing theory}
The following pseudo code combines the two earlier routines to apply optimizations to a given theory.
\subsection{Finding simply redundant inclusions}
\label{sec:alg_sri}
The following pseudo code is for finding simply redundant inclusions (see: \autoref{sec:redinc}).
\begin{algorithm}[H]
\KwData{theory = theory from our theory graph\\
futureUse = set of theories used by theories including $theory$ in future lite code\\
\KwData{$theory$ = theory from our theory graph\\
pass changes that were already applied to the graph}
\KwResult{replacements = map from theory inclusions to their replacement}
\Begin{
replacements = superfluousIncludes(theory, futureUse)\;
\For{removal $\leftarrow$ redundantIncludes(theory, futureUse)}{
add removal $\rightarrow$ $\emptyset$ to replacements\;
}
\KwRet replacements\;
}
\caption{optimize(theory, futureUse)}
\end{algorithm}
\subsection{Optimizing graph}
The following code applies the earlier optimizations to the entire theory graph.
\begin{algorithm}[H]
\label{alg:graph}
\KwData{theoryGraph = theory graph to be optimized}
\KwResult{replacements = map from theories to maps of theory inclusions to their replacement}
\KwResult{$redundantIncludes$ = set of simply redundant inclusions}
\Begin{
futureUses := empty map\;
replacements := empty map\;
theoryGraph := sort(theoryGraph)\;
theoryGraphRev := reverse(theoryGraph)\;
\For{theory $\leftarrow$ theoryGraph}{
add include $\rightarrow$ usedTheories(theory) $\cup$ futureUses(theory) to futureUses\;
add theory $\rightarrow$ optimize(theory, futureUses(theory)) to replacements\;
\For{include $\leftarrow$ includes(theory)}{
replace include $\rightarrow$ futureUses(include)$\cup$usedTheories(include)$\cup$futureUses(theory) in futureUses\;
}
\KwRet replacements \;
}
$subIncludes := \emptyset$\
$redundantIncludes := \emptyset$\;
\For{$i \leftarrow directIncludes(theory)$}{
$subIncludes := subIncludes \cup includes(i)$\;
}
\caption{optimizeGraph(theorygraph)}
\For{$i \leftarrow directIncludes(theory)$}{
\If{$i \in subIncludes$} {
$redundantIncludes := redundantIncludes \cup \{i\}$\;
}
}
\KwRet $redundantIncludes$\;
}
\caption{simplyRedundantIncludes(theory, futureUse)}
\end{algorithm}
\section{Complexity analysis}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment