473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting with only a partial order definition

I have a list of items and a "rule" for ordering them.

Unfortunately, the rule is not complete so it won't define the correct
order for any two items in that list.

In other words, if I pick two random items from the list I may or may
not have a rule that dictates the order of those two items. The rule
could be "implicit" in that I got rules for other items, for instance:

[a, b, c, d]
a < b
b < c

If I now pick a and c out of the list I would not know wether a or c
comes first, unless I grab the rules for a<b and b<c and imply that a<c
from those two.

As such, there could be potentially many correct results from ordering
such a list. (d is unspecified above so any position for d is actually
legal)

Is there anything in Python that will help me sort a list with something
like this ?

For instance, given the following list of items:

items = ["a", "b", "c", "d"]

and the following two rules:

"a" comes before "d"
"d" comes before "b"

then the following is a list of correct results (could be more though):

["a", "d", "b", "c"]
["a", "c", "d", "b"]
["a", "d", "c", "b"]
....

Note that this is an arbitrary example. The real list is a list of lists
containing database rows, ie. something like this:

[[10, "Column 2", "Column 3"],
[15, "Column 2", "Column 3"],
...]

If there isn't anything built in, does anyone have an idea about how to
go about creating such an ordering function ? Tweaking a cmp-like
function to return 0 for "undefined" didn't seem to work so there must
be a slightly more intelligent solution than that. Perhaps the rules
would have to be checked in a specific order.

Doing a combinatorial solution and checking the rules aren't really an
option as on last count there was close to 20K rows.

There's also a lot of rules (also coming from a database). I was
thinking I could do a sort based on one of the rules and use a stable
sort for the following rules but doing that sometimes rearranged the
list so that it was now incorrect going by the previous rules.

Here's my initial test for doing a cmp-like solution:

order = set([("a", "d"), ("d", "b")])
def my_cmp(a, b):
if (a, b) in order:
print a, "<", b
return -1
elif (b, a) in order:
print a, "<", b
return +1
else:
print a, "?", b
return 0

items = ["c", "b", "a", "d"]
items.sort(cmp=my_cmp)
print items

This prints out:

b ? c
a ? b
d < a
['c', 'b', 'a', 'd']

Which is obviously incorrect.

Any help or pointers would be most welcome.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Oct 27 '05 #1
7 3635
Lasse Vågsæther Karlsen wrote:
I have a list of items and a "rule" for ordering them.

Unfortunately, the rule is not complete so it won't define the correct
order for any two items in that list.
This is called a "partial ordering".

[...] If there isn't anything built in, does anyone have an idea about how to
go about creating such an ordering function ? Tweaking a cmp-like
function to return 0 for "undefined" didn't seem to work so there must
be a slightly more intelligent solution than that. Perhaps the rules
would have to be checked in a specific order.


The usual tools to deal with partial orderings are directed acyclic graphs,
and "topological sorting". Try Googling the terms along with "Python".
--
--Bryan
Oct 27 '05 #2
Lasse Vågsæther Karlsen <la***@vkarlsen.no> writes:
I have a list of items and a "rule" for ordering them.

Unfortunately, the rule is not complete so it won't define the correct
order for any two items in that list.

In other words, if I pick two random items from the list I may or may
not have a rule that dictates the order of those two items. The rule
could be "implicit" in that I got rules for other items, for instance:


That's called topological sorting and any reference on graph
algorithms will describe how to do it. I don't know of Python code
offhand but it's easy to write.

http://en.wikipedia.org/wiki/Topological_sorting

gives a straightforward linear-time algorithm.
Oct 27 '05 #3
Paul Rubin wrote:
Lasse Vågsæther Karlsen <la***@vkarlsen.no> writes:
I have a list of items and a "rule" for ordering them.

Unfortunately, the rule is not complete so it won't define the correct
order for any two items in that list.

In other words, if I pick two random items from the list I may or may
not have a rule that dictates the order of those two items. The rule
could be "implicit" in that I got rules for other items, for instance:

That's called topological sorting and any reference on graph
algorithms will describe how to do it. I don't know of Python code
offhand but it's easy to write.

http://en.wikipedia.org/wiki/Topological_sorting

gives a straightforward linear-time algorithm.


Thank you both.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Oct 27 '05 #4
Bryan Olson wrote:
The usual tools to deal with partial orderings are directed acyclic graphs,
and "topological sorting". Try Googling the terms along with "Python".


here's a rather powerful timbot implementation:

http://mail.python.org/pipermail/pyt...ly/006625.html

</F>

Oct 27 '05 #5
Lasse Vågsæther Karlsen wrote:
Paul Rubin wrote:
Lasse Vågsæther Karlsen <la***@vkarlsen.no> writes:
I have a list of items and a "rule" for ordering them.

<snip>

Ok, managed to implement the algorithm. Might not be the optimal
solution (memory and speed-wise) but it worked and doesn't take too long
to run either so I'm going to stick with it.

I have a different question though, along the same lines, but one that
doesn't need a solution, just want to know if something like it exists.

A while back I had an application that had a lot of items that it needed
to order. The problem, however, was that the rules was not defined at
all. Instead it was opted for a visual solution where the user would be
presented with images and had to rearrange them in the order that was
necessary. The application was one I helped build for a friend which
combined images from several cameras and allowed him to sort them
according to the contents so that he could get a timeline formed.

The date/time stamps on the cameras was not directly usable for various
reasons so the visual ordering was what we ended up on. For instance,
two of the cameras was not digital ones so they had no timestamp except
for the one provided by the scanning software.

In that application we talked about presenting the user with two and two
images and he just had to click on the image that came first. The
problem with this was to try to present the "right" images to the user
so that he had to minimize the number of clicks.

In other words, try to pick nodes in the graph and ask the user to
provide the direction of the edge, and the "picking algorithm" would
work in such a way that the number of edges would be minimized.

Not sure if I'm explaining it correctly.

The solution we ended up with was to present the user with all the
images in one big timeline and just let him drag them around. This worked.

What I was wondering about is if there is an algorithm that would do
what I want? Ie. help me pick the nodes so as to minimize the number of
edges. Obviously the answer to the first pair of nodes will influence
which nodes will be subsequently picked, so each answer would stear the
algorithm in a way, not just go into the final problem.

If anyone got the name of such an algorithm or something I would like to
look at it at least. Application is built and deployed so I'm not
looking for a solution to implement.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Oct 27 '05 #6
Lasse Vågsæther Karlsen <la***@vkarlsen.no> writes:
In that application we talked about presenting the user with two and
two images and he just had to click on the image that came first. The
problem with this was to try to present the "right" images to the user
so that he had to minimize the number of clicks.


If you're trying to sort those pictures chronologically, there is a
total ordering and the best you can do in general is a standard
O(N log N) sorting algorithm. If you've got good timestamps on some
of the pics, so you want to minimize comparisons involving pics with
no timestamps, hmm, maybe some optimizations are possible.
Oct 27 '05 #7
On Thursday 27 October 2005 11:08, Lasse Vågsæther Karlsen wrote:
What I was wondering about is if there is an algorithm that would do
what I want? Ie. help me pick the nodes so as to minimize the number of
edges.


To rephrase your question, you want a sorting algorithm that minimises the
number of comparisons (because a comparison involves asking a human), and
which takes advantage of any pre-existing rough ordering.

You need timsort - the algorithm behind python lists sort() method.

--
Toby Dickenson
Oct 27 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
1
by: LoopyNZ | last post by:
Hi there, I have an IIF expression that is returning a 0 (exact match), 1 (partial match), or 2 (no match) based on comparing text fields (see below). However, when I try to sort the results...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.