473,382 Members | 1,369 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,382 software developers and data experts.

Richard Heathfield's TSP permutation algorithm

Dear Sir I couldnt quite figure out wat your permute function does
exactly... could you please throw some light on it?
void Permute(char *Perm,
size_t n,
size_t unchanged)
{
size_t outer = 0;
size_t inner = 0;
int temp = 0;
if(unchanged < n)
{
for(outer = unchanged; outer < n; outer++)
{
temp = Perm[outer];
for(inner = outer; inner unchanged; inner--)
{
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;
Permute(Perm,
n,
unchanged + 1);
for(inner = unchanged; inner < outer; inner++)
{
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
}
}
else
{
printf("%s\n", Perm);
}

}
int main(int argc, char **argv)
{
char Input[256] = {0};
size_t len = 0;

if(argc 1)
{
len = strlen(argv[1]);
if(len sizeof Input - 1)
{
fprintf(stderr, "word too long for demo - truncating\n");
argv[1][sizeof Input - 1] = '\0';
}
strcpy(Input, argv[1]);
len = strlen(Input);
}
else
{
len = 3;
strcpy(Input, "ABC");
}
Permute(Input, len, 0);
return 0;

}

Thanking you
The whitehat

Sep 16 '06 #1
12 6579
wh*************@gmail.com said:
Dear Sir I couldnt quite figure out wat your permute function does
exactly... could you please throw some light on it?
It just permutes an array. If you want to turn it into a brute-forcer, I
suggest you hack it to accept a function that will be called whenever a
permutation is found.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 16 '06 #2
What does inner outer and unchanged do?

Sep 17 '06 #3
wh*************@gmail.com said:
What does inner outer and unchanged do?
Partition the array into two parts, an empty part, and the part you want to
permute.

| ABCDE

Move each element on the right-hand side to the left-hand side, in turn.

A | BCDE
B | CDEA
C | DEAB
D | EABC
E | ABCD

All you're really doing is rotating the array, yes?

For each of those partitioned array arrangements, we now have to solve the
problem of permuting the right-hand side, and incorporating the left-hand
side, ***unchanged***, into the final result.

Take A | BCDE, for example. We have 1 unchanged element, which will prefix
each solution, and an array, BCDE. We can solve this problem by recursing.
How? Well, like this...

Move each element on the right-hand side to the left-hand side, in turn.

AB | CDE
AC | DEB
AD | EBC
AE | BCD

All you're really doing is rotating the BCDE part of the array, yes?

For each of those partitioned array arrangements, we now have to solve the
problem of permuting the right-hand side, and incorporating the left-hand
side, ***unchanged***, into the final result.

Take AB | CDE, for example. We have 2 unchanged elements, which will prefix
each solution, and an array, CDE. We can solve this problem by recursing.
How? Well, like this...

Move each element on the right-hand side to the left-hand side, in turn.

ABC | DE
ABD | EC
ABE | CD

All you're really doing is rotating the CDE part of the array, yes?

For each of those partitioned array arrangements, we now have to solve the
problem of permuting the right-hand side, and incorporating the left-hand
side, ***unchanged***, into the final result.

Take ABC | DE, for example. We have 3 unchanged elements, which will prefix
each solution, and an array, DE. We can solve this problem by recursing.
How? Well, like this...

Move each element on the right-hand side to the left-hand side, in turn.

ABCD | E
ABCE | D

All you're really doing is rotating the DE part of the array, yes?

For each of those partitioned array arrangements, we now have to solve the
problem of permuting the right-hand side, and incorporating the left-hand
side, ***unchanged***, into the final result. But since each right-hand
side only has 1 element therein, there is only one permutation.

So for each of them we just display the result. ABCDE, and ABCED.

Then we drop out of this recursion, and continue with the next, and so on
until everything is done.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 17 '06 #4
Richard Heathfield wrote:
wh*************@gmail.com said:
>What does inner outer and unchanged do?

Partition the array into two parts, an empty part, and the part
you want to permute.

| ABCDE

Move each element on the right-hand side to the left-hand side,
in turn.

A | BCDE
B | CDEA
C | DEAB
D | EABC
E | ABCD

All you're really doing is rotating the array, yes?

For each of those partitioned array arrangements, we now have to
solve the problem of permuting the right-hand side, and incorporating
the left-hand side, ***unchanged***, into the final result.

Take A | BCDE, for example. We have 1 unchanged element, which will
prefix each solution, and an array, BCDE. We can solve this problem
by recursing. How? Well, like this...

Move each element on the right-hand side to the left-hand side,
in turn.

AB | CDE
AC | DEB
AD | EBC
AE | BCD

All you're really doing is rotating the BCDE part of the array, yes?

For each of those partitioned array arrangements, we now have to solve
the problem of permuting the right-hand side, and incorporating the
left-hand side, ***unchanged***, into the final result.

Take AB | CDE, for example. We have 2 unchanged elements, which will
prefix each solution, and an array, CDE. We can solve this problem
by recursing. How? Well, like this...

Move each element on the right-hand side to the left-hand side, in
turn.

ABC | DE
ABD | EC
ABE | CD

All you're really doing is rotating the CDE part of the array, yes?

For each of those partitioned array arrangements, we now have to
solve the problem of permuting the right-hand side, and incorporating
the left-hand side, ***unchanged***, into the final result.

Take ABC | DE, for example. We have 3 unchanged elements, which will
prefix each solution, and an array, DE. We can solve this problem by
recursing. How? Well, like this...

Move each element on the right-hand side to the left-hand side, in
turn.

ABCD | E
ABCE | D

All you're really doing is rotating the DE part of the array, yes?

For each of those partitioned array arrangements, we now have to
solve the problem of permuting the right-hand side, and
incorporating the left-hand side, ***unchanged***, into the final
result. But since each right-hand side only has 1 element therein,
there is only one permutation.

So for each of them we just display the result. ABCDE, and ABCED.

Then we drop out of this recursion, and continue with the next,
and so on until everything is done.
And that very nice description applies equally to my 'jumble'
routine, which I published here earlier as a complete program.
Jumble also allows you to suppress any output past a preselected
length value.

Richards description, and my lack of one, illustrates why I do not
make a good teacher. I am repeating my actual heart code for
illustration below:

/* exchange 0th and ith char in wd */
void trade(char *wd, unsigned int i)
{
char c;

c = *wd;
*wd = wd[i];
wd[i] = c;
} /* trade */

/* ------------------ */

/* Form all n char permutations of the characters in the
string wd of length lgh into outstring at index ix.
Output the results to stdout. */
void jumble(char *wd, unsigned int lgh,
unsigned int ix, /* output place to fill */
unsigned int n, /* max out places to fill */
char *outstring)
{
unsigned int i;

if (0 == n) {
outstring[ix] = '\0';
puts(outstring);
}
else
for (i = 0; i < lgh; i++) {
trade(wd, i); /* nop when (0 == i) */
outstring[ix] = *wd;
jumble(wd+1, lgh-1, ix+1, n-1, outstring);
trade(wd, i); /* restore the wd string */
}
} /* jumble */

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

--
Posted via a free Usenet account from http://www.teranews.com

Sep 17 '06 #5
WOW
Thanx a millions to Mr Richard Heathfield and to Mr CBFalconer
Now both your algorithms are clear, and even the ideas are clearer.

Thanx once again......

Sep 18 '06 #6
Just one last clarification:

for
A | BCDE
B | CDEA
inner is the right hand side of the bar |?
and outer is it the left hand side of the | bar?

The external most for loop does it permute only the first letter of the
combination??
if(unchanged < n)
{
for(outer = unchanged; outer < n; outer++)
{
temp = Perm[outer];
for(inner = outer; inner unchanged; inner--)
{
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;
Permute(Perm,
n,
unchanged + 1);
for(inner = unchanged; inner < outer; inner++)
{
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
}
}
else
{
printf("%s\n", Perm);
}

Sep 20 '06 #7
wh*************@gmail.com said:
Just one last clarification:

for
A | BCDE
B | CDEA
inner is the right hand side of the bar |?
and outer is it the left hand side of the | bar?
No. You leave everything to the left of the bar alone.
The external most for loop does it permute only the first letter of the
combination??
Nothing special about it at all. It just rotates the array and then recurses
to permute each rotation in turn.
if(unchanged < n)
Have we finished yet? No? Okay, let's rotate the array...
{
for(outer = unchanged; outer < n; outer++)
{
temp = Perm[outer];
for(inner = outer; inner unchanged; inner--)
{
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;
This bit changes, say, BCDE into EBCD
Permute(Perm,
n,
unchanged + 1);
This bit submits AEBCD to recursion.
for(inner = unchanged; inner < outer; inner++)
{
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
And this bit jiggles the array back into the right order so that the next
recursion level up from here works properly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 20 '06 #8
So actually what would be the appropriate name for outer and inner?

Sep 21 '06 #9
wh*************@gmail.com said:
So actually what would be the appropriate name for outer and inner?
In the absence of contextual information to the contrary, the most
appropriate name for outer is outer.

In the absence of contextual information to the contrary, the most
appropriate name for inner is inner.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 21 '06 #10
wh*************@gmail.com writes:
So actually what would be the appropriate name for outer and inner?
Please provide context. See <http://cfaj.freeshell.org/google/>.
(The bug in Google Groups has been corrected, so it's even easier to
get this right.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 21 '06 #11
Richard Heathfield wrote:
wh*************@gmail.com said:
>So actually what would be the appropriate name for outer and inner?

In the absence of contextual information to the contrary, the most
appropriate name for outer is outer.

In the absence of contextual information to the contrary, the most
appropriate name for inner is inner.
This is probably a discussion of naval strategy. In the UK, direct
the questions to the Admiralty. Try the First Sea Lord. In the
US, try the Department of the Navy.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #12
Sorry for the netiquette:
I meant
>size_t outer = 0;
size_t inner = 0;
what would be the approproate name for inner and outer?

This is probably a discussion of naval strategy. In the UK, direct
the questions to the Admiralty. Try the First Sea Lord. In the
US, try the Department of the Navy.
OMG Hahahahaaa nice one

Sep 22 '06 #13

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

Similar topics

4
by: m sergei | last post by:
I am not asking for code but wanted help with understanding the algorithm to permute all characters of a string. say string is "ABCD" I want to know the algorithm for finding all permutations of...
10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
27
by: onkar | last post by:
How to generate different permutations of n char array? ex : for n= 3, and basic string = abc bca cab bac cab ..... ... ..
6
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> ...
8
by: wazzup | last post by:
Create a program to display all of the permutations of a string of any length. A permutation is a re-arrangement of the order of letters. For example, the string "abc" has permutations "acb",...
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
7
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but...
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.