On Wed, 30 Jan 2019 06:55:56 -0800 (PST)
Jim Newton <
jimka...@gmail.com> wrote:
> I have three questions about topological sorting with respect to computing the
> class precedence list in CLOS.
>
> In AMOP and in the implementation of closette.lisp, there is a comment on the function
> std-tie-breaker-rule, which says:
> (There's a lemma that shows that this rule yields a unique result.)
>
> Q1) Which lemma is this? Does anyone know?
Not me. But the reasoning is too simple to count as a separate lemma unless
they have in mind something more general.
> It is not obvious to me why the tie breaker restriction in the CL spec, guarantees a
> unique result.
>
> For example, it says when topologically sorting the classes, you must always choose the
> "minimum" element when there is only one, but when there are several minimum element you
> choose the one whose which has a subclass farthest the the right in the list computed
> so far.
"Minimal" would be a better term. A partially ordered set can have at most 1
minimum element but can have several minimal ones.
> Q2) What happens if there are two such classes which both have this right-most
> element as subclass. Or why can't this ever happen?
"4.3.5.1 Topological Sorting" says "direct subclass". To show that there
can't be 2 or more , we'll conclude a contradiction. So assume that in the
course of the algorithm , the remaining set S has distinct classes T1 and T2
such that they have as direct subclass a class T which exists in the
precedence list produced so far and that , according to the remaining set R
[which is originally defined in 4.3.5] , T1 and T2 do not have a predecessor
i.e. there is no class T3 such that { <T3 , T1> is in R or <T3 , T2> is in
R }. Let C_1 , ... , C_n be the direct superclasses of T in the order they
were given in the DEFCLASS form for T. Then there are integers i and j such
that C_i = T1 and C_j = T2 .Without loss of generality we can assume that i
< j .By the way the set R is defined , we know that at the start of the
algorithm it contained the pairs <T , C_1> , ... , <C_{n-1} , C_n> . Also by
the way the set S_T is defined , we know that at the start of the algorithm
it contained all superclasses of T so in particular C_1 , ... , C_n .Since at
the present state of execution , C_j does not have a predecessor in R , at
some point in the execution <C_{j-1} , C_j> was removed from R and by the
definition of the algorithm this could only have happened if C_{j-1} was
removed from S. From this follows that even earlier in the algorithm C_{j-1}
did not have a predecessor in R so C_{j-2} was removed from S even earlier.
Proceeding inductively it follows that C_i = T1 was removed from S earlier
in the algorithm which is a contradiction.
> The comment in closette.lisp claims this is "THE" standard algorithm for
> topological sort. However, the standard algorithm taught to our students is
> a DFS algorithm which numbers the elements as the recursion backtracks.
> This is also the algorithm explained in "Introduction to Algorithms" by Thomas Cormen.
>
> Q3) are these two algorithms equivalent? Can the DFS algorithm also use a tie-breaker
> function such as the one described in the CL specification? If so, what is the "list so far"?
What "the standard algorithm" means is anyone's guess but in general there is
more than one way to extend a partial ordering to a total one. So for example
the ordering
A
/ \
/ \
B C
\ /
\ /
D
has exactly 2 extensions : D < B < C < A and D < C < B < A .So there can't
be a unique algorithm. Whether some algorithm "stands out" enough to be
called "the standard algorithm" , I don't know.
I note also that it would make it easier to reason about the way these things
work in CL if it specified in the definition of DEFCLASS that "If in the list
of superclasses of the class being defined there exist superclasses C1 and C2
such that C1 = C2 or C1 is a superclass of C2 , an error shall be signalled"
and I don't think we would lose expressive power.
--
The report on "Death Star security failures" is finally out :
<
c84729c3-22a1-46c0...@googlegroups.com>