473,777 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm for sorting functional expressions

I am trying to write some code that will take a list of functional
expressions, and order them so that those with primitive terms appear
at the beginning of the list and those that are defined by other terms
appear last.

eg:
getSortedEquati ons(['b = w + z','a = z - y','w = 2*z + v','z = e +
f','y = p + l']) =
['w = 2*z + v', 'z = e + f', 'y = p + l', 'b = w + z', 'a = z - y']

It is easy enough to tokenise each of the equations and produce a list
like:
['b', ['w','z']], ['a': ['z','y']], ['w':'z','v'] , ['z', ['e','f']],
['y',['p','l']]

But I'd like to find an algorithm that can handle the sorting problem.

So I suspect that this is a common problem for those familiar with
partially ordered sets or directed graphs. I'm wondering if anyone else
is familiar with this problem and knows an efficient algorithm that
will solve it. It would be good if any such algorithm would be able to
check for circular definitions in the input.

Dec 4 '06 #1
5 1250
In <11************ **********@16g2 000cwy.googlegr oups.com>, chrisguest
wrote:
So I suspect that this is a common problem for those familiar with
partially ordered sets or directed graphs. I'm wondering if anyone else
is familiar with this problem and knows an efficient algorithm that
will solve it. It would be good if any such algorithm would be able to
check for circular definitions in the input.
You are looking for "topologica l sort". Feed a search engine with that
term. :-)

Ciao,
Marc 'BlackJack' Rintsch
Dec 4 '06 #2
<ch********@gma il.comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.com...
>I am trying to write some code that will take a list of functional
expressions, and order them so that those with primitive terms appear
at the beginning of the list and those that are defined by other terms
appear last.

eg:
getSortedEquati ons(['b = w + z','a = z - y','w = 2*z + v','z = e +
f','y = p + l']) =
['w = 2*z + v', 'z = e + f', 'y = p + l', 'b = w + z', 'a = z - y']

It is easy enough to tokenise each of the equations and produce a list
like:
['b', ['w','z']], ['a': ['z','y']], ['w':'z','v'] , ['z', ['e','f']],
['y',['p','l']]

But I'd like to find an algorithm that can handle the sorting problem.

So I suspect that this is a common problem for those familiar with
partially ordered sets or directed graphs.
Right.
I'm wondering if anyone else
is familiar with this problem and knows an efficient algorithm that
will solve it. It would be good if any such algorithm would be able to
check for circular definitions in the input.
Read http://en.wikipedia.org/wiki/Topological_sorting
or just search for "topologica l sorting" on the net.

Regards,
Christian
Dec 4 '06 #3
Marc 'BlackJack' Rintsch wrote:
>So I suspect that this is a common problem for those familiar with
partially ordered sets or directed graphs. I'm wondering if anyone else
is familiar with this problem and knows an efficient algorithm that
will solve it. It would be good if any such algorithm would be able to
check for circular definitions in the input.

You are looking for "topologica l sort". Feed a search engine with that
term. :-)
including "python" in the search request might be a good idea. here's a
topsort in Python:

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

(note that most code in that module is for analyzing cycles in case the
sort fails, not for the sort itself).

</F>

Dec 4 '06 #4
ch********@gmai l.com wrote:
I am trying to write some code that will take a list of functional
expressions, and order them so that those with primitive terms appear
at the beginning of the list and those that are defined by other terms
appear last.

eg:
getSortedEquati ons(['b = w + z','a = z - y','w = 2*z + v','z = e +
f','y = p + l']) =
['w = 2*z + v', 'z = e + f', 'y = p + l', 'b = w + z', 'a = z - y']

It is easy enough to tokenise each of the equations and produce a list
like:
['b', ['w','z']], ['a': ['z','y']], ['w':'z','v'] , ['z', ['e','f']],
['y',['p','l']]

But I'd like to find an algorithm that can handle the sorting problem.

So I suspect that this is a common problem for those familiar with
partially ordered sets or directed graphs. I'm wondering if anyone else
is familiar with this problem and knows an efficient algorithm that
will solve it. It would be good if any such algorithm would be able to
check for circular definitions in the input.
First, put them in a list:

L = [['b', ['w','z']], ['a', ['z','y']], ['w', 'z','v'], ['z',
['e','f']], ['y', ['p','l']]]

then sort the list:

def Compare(X, Y):
if X[0] in Y[1]:
return -1
elif Y[0] in X[1]:
return 1
else:
return 0

L.sort(cmp = Compare)

The result is:

[['z', ['e', 'f']], ['b', ['w', 'z']], ['y', ['p', 'l']], ['a', ['z',
'y']], ['w', 'z', 'v']]

It's left as an exercise for the reader as to how it works. :-)

Dec 4 '06 #5
MRAB wrote:
It's left as an exercise for the reader as to how it works. :-)
*if* it works, you mean. should the following script really print anything?

import random, sys

# input variables replaced with zeros
L = [
['b', ['w','z']],
['a', ['z','y']],
['w', ['z','0']],
['z', ['0','0']],
['y', ['0','0']]
]

def Compare(X, Y):
if X[0] in Y[1]:
return -1
elif Y[0] in X[1]:
return 1
else:
return 0

while 1:
random.shuffle( L)
L.sort(cmp=Comp are)
# check that all steps only use known values
i = set("0")
for d, s in L:
if s[0] not in i or s[1] not in i:
print L
print d, s, i
i.add(d)

</F>

Dec 5 '06 #6

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

Similar topics

1
7144
by: Shaunak Kashyap | last post by:
Does anyone know what sorting algorithm(s) -- quicksort, mergesort, radix sort, etc. -- does PHP use internally in its sort function?
16
2665
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
4
4285
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
60
4941
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
51
8653
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
0
13319
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the
30
5499
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
11
2291
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
2
1637
by: ShashiGowda | last post by:
I am writing a package manager and stuck unable to write the version sorting function the algorithm is here http://www.linux.gr/cgi-bin/man/man2html?deb-version+5 and all other info is also in it please tell me how to do lexical comparision in python it'll be cool if you just write the code!
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.