[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Jun 30 17:31:39 1994 
Subject:A Standard Graph Labelling Algorithm 
From: Robin Popplestone  
Volume-ID:940701.03 

POP-11 properties give a rather nice direct encoding of the following graph
labelling algorithm.

=============================================================================
/*
TopologicalSort.p                       M.Das and Robin Popplestone JUN 94

The TopologicalSort procedure takes two inputs - v0 and Adj.
    - v0 is the source node of the graph
    - Adj(w) is a function which returns the adjacent nodes of
      the given node w.
The routine returns the property "label" which gives the
numbering of the nodes after topological sorting.

See "Combinatorial Algorithms", by Reingold, Nievergelt and Deo,
                        page 331&332

*/

lib int_parameters;

define TopologicalSort (v0,Adj)->label;
    lvars v0,Adj,
          label = newassoc ([]);
    lvars i     = 0;
    lvars j     = pop_max_int;
    lvars num   = newassoc ([]);

    define TS(v);
        lvars w;
        i + 1 -> i;
        i -> num(v);
        for w in Adj(v) do
            unless num(w) then TS(w);
            elseunless label(w) then
                mishap('Cycle detected... ',[^v]);
            endunless;
        endfor;
        j-1 -> j;
        j -> label(v);
    enddefine;

    TS (v0);

    appproperty (label, procedure(v, j);
                        j - pop_max_int + i + 1 -> label(v) endprocedure);
enddefine;

example TopologicalSort;

vars G   = [ [A [B C D]] [B [C G]] [C [G E]] [D [E F]] [E [G F]] [G [F]] ],
vars Adj = newproperty (G,32,[],"tmparg");

vars P = TopologicalSort("A", Adj);
syssort(datalist(P) ,procedure(x,y); alphabefore(hd(x),hd(y)) endprocedure) =>
** [[A 1] [B 3] [C 4] [D 2] [E 5] [F 7] [G 6]]


endexample