[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

595.0. "Topological sorting" by MODEL::YARBROUGH () Wed Oct 15 1986 18:25

Topological sorting is the process of putting objects in a consistent
[partial] ordering based on specified but usually weak constraints. For 
example,
	a<b, a<c ('<' means 'to the left of')
is a partial ordering which can be satisfied by either
	abc
or	acb
or, if another object d is involved, any of
	dabc
	acbd
	...
etc.

This concept is frequently used in practical ways: for example, any PERT
diagram is a set of nodes with connecting arcs that is usually displayed
as a set of boxes and lines that have been topologically sorted so that 
earlier boxes appear to the left, later to the right, and a great deal
of ambiguity, indecision and/or parallelism may be evident.

Ther is a very efficient algorithm for topological sorting in Knuth 'Art of 
Computer Programming', Vol I.

Now consider extending the concept to two dimensions: in addition to '<'
consider '^', meaning 'above'. E.g. a^b means
	a
	b
or, more generally, something like
	a
	  b

This concept can be used to resolve some of the ambiguity in 
two-dimensional layout of objects ordered by the '<' relationship.

What is needed is an algorithm to place objects in a two-dimensional 
lattice so that the specified relationships are satisfied. For example,
	a<b, a^b, b<c, c^a can be satisfied by

	..c
	a..
	.b.

Knuth's algorithm apparently does not extend well to two dimensions (or 
does it?) Any suggestions?
T.RTitleUserPersonal
Name
DateLines
595.1I'm confused...SSDEVO::LARYWed Oct 15 1986 20:017
Since the two partial orderings are on different coordinates, it would seem
that you could pick a set of real numbers {a1,b1,c1,...} that are topologically
sorted on < and another set {a2,b2,c2,...} that are topologically sorted on ^,
and the set of points {(a1,a2),(b1,b2),(c1,c2),...} are sorted on both.

This is easy enough that it probably isn't what you had in mind - how am I
misunderstanding the question?
595.2To clarifyMODEL::YARBROUGHThu Oct 16 1986 11:5521
I specified that the objects occupy a lattice, i.e. the coordinates are
integers. I did not make the goals clear, though. What I should have said
was that the x-y coordinates of each object should satisfy the conditions: 

	o All coordinates are non-negative integers

	o No two objects have the same coordinates

	o All the constraints are satisfied

	o All coordinates are minimal within the constraints

Note that there is still considerable latitude in the placement of 
unconstrained objects, w/r/t the order and position in which they may
appear. 

What I need, of course, is an algorithm to do this in two dimensions in
a reasonable span of time and space. As motivation, note that the algorithm 
given by Knuth for one dimension behaves as O(m+n) in time, where m is the
number of objects and n the number of constraints. I think it's also O(m+n)
in space. 
595.3Has it been done before?TOOK::APPELLOFCarl J. AppellofThu Oct 16 1986 12:352
    Was any of this sorting ever contained in Stan's GRAPHER programs?
    
595.4It is now...MODEL::YARBROUGHThu Oct 16 1986 17:326
>    Was any of this sorting ever contained in Stan's GRAPHER programs?
    
Yes, although it was not running when I copied the programs. I have the 
one-dimensional algorithm running, but nothing special beyond that.

Lynn Yarbrough
595.5CLT::GILBERTeager like a childFri Oct 17 1986 04:418
Richie Lary's solution solves the problem, except for:

>	o All coordinates are minimal within the constraints

which is ill-defined, anyway.  Once defined, it'll probably make
this an NP-complete problem, so don't bother.

Why not take Richie's solution, and just 'squish' it?
595.6Well, sorta...MODEL::YARBROUGHFri Oct 17 1986 12:1215
The condition 

	o All coordinates are minimal within the constraints

falls like the gentle rain out of the Knuth algorithm. I'd like to retain 
that property in a 2-D algorithm.

> Once defined, it'll probably make this an NP-complete problem, so don't
> bother. 

I can't imagine any reasonable algorithm that would behave worse than
O((m+n)^3). I'd like to get one that is O((m+n)*log(m)), at worst, and 
hopefully better.

However, I'm beginning to agree with you about the general method.
595.7I like this problemNOBUGS::AMARTINAlan H. MartinThu Oct 30 1986 15:4038
Re .0:

Did you intend to discard this solution to "a<b, a^b, b<c, c^a":

	.c
	a.
	.b

In other words, do you want to allow solutions which have distinct points
with some identical coordinates as long as they don't violate the ordering?

Re .3:

See also the "tsort" tool on Unix.

Re .5:

Unfortunately, the "solution" in .1 doesn't explain *how* to "pick"
an ordering for one coordinate which does not violate the constraints
on the other coordinate.  It describes many procedures, from an exhaustive
search by repeatedly sorting on one dimension and checking the other
one for legality, to any sort of incremental process which sorts both
dimensions at the same time.  I think that Richie understands the problem,
he just didn't go far enough with it to actually give someone a hint
of how to implement an algorithm to solve it.

There is a simple, subquadratic(?), solution to "integerizing" a bunch of
pairs of real coordinates.  Sort the pairs on one coordinate, replace that
coordinate with the position in the sorted array and then repeat the
process on the other coordinate.

Also, while you can efficiently take an integer lattice and trivially
squish it by removing all vacant "rows" and "columns", it would not
surprise me if there are sets of constraints which result in different 2-D
topological sorts which have different trivial squishes with different
sizes (product or sum of max row and column).  Can anyone come up with
an example of this?
				/AHM
595.8CLT::GILBERTeager like a childThu Oct 30 1986 20:4738
re .6

>	o All coordinates are minimal within the constraints

Huh?  Given {a<b, a<c}, the following topological ordering may be
produced by Kahn's algorithm: a, b, c.  The coordinate for 'c'
is not minimal.


re .5, .6:

Consider the relations: {a<b,a<c,a<d,a<e,a^b,a^c,a^d,a^e}.
The following minimizes the maximum x-coordinate:

	a .
	. b
	. c
	. d
	. e

And the following minimizes the maximum y-coordinate:

	a . . . .
	. b c d e

Clearly, the maximum x-coordinate and the maximum y-coordinate cannot
be simultaneously minimized, since 5 nodes cannot fit in a 2x2 square.


re .6:

The above example shows a case where simple 'squishing' won't produce a
solution that minimizes the product of the maximum row and column numbers.
Here *is* such a solution, with a product of 9:

	a . .
	. b c
	. d e