BPsite Forums
November 27, 2024, 06:04:39 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: BPSITE FOREVER!
 
   Home   Help Search Members Login Register  
Pages: 1 [2] 3 4
  Print  
Author Topic: The Gathering of Programmers  (Read 32517 times)
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #15 on: September 27, 2005, 04:01:42 AM »

I've worked it out with 4 and 5 cities.
but the problem is that I don't think that my technique works when there are more cities...
the only way for me to see if mine works is for someone to make the program as the instructor suggested (the brut force way), even if that person has to be me if everyone wants to be punks and not show in the forum <_<

My technique works by starting at one city and fallowing the shortest path to all adjacent cities.  Here’s the problem, it doesn't look far ahead, and there are paths that are shorter overall.  I tried to overcome some of this by starting anew at every city, but this only helps, not fixes the problem.

it looks like ripway is having major server trouble (the thing was totaly destroyed apparently) my image won't work till they fix their hardware
« Last Edit: September 27, 2005, 08:37:22 PM by smi256 » Logged

*was here
Mr.Slippery
Newbie
*
Offline Offline

Posts: 14



View Profile WWW
« Reply #16 on: September 27, 2005, 06:32:02 PM »

ADV-C:[/b][/size]
Traveling Salesman Problem:

Try these links:
 some have animations of Depth-First and Breadth-First algorithms:

http://www.cs.duke.edu/csed/jawaa/DFSanim.html
http://www.cs.duke.edu/csed/jawaa/BFSanim.html

http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search
http://en.wikipedia.org/wiki/Traveling_sal...alesman_problem

More links than I can deal with:
http://www.ing.unlp.edu.ar/cetad/mos/TSPBIB_home.html

Here's a Java algorithm:
http://www.pms.ifi.lmu.de/lehre/compgeomet...l#visualization


Dijkstra's algorithm (NOT the same as the Traveling Salesman prob):
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
http://www.cs.sunysb.edu/~skiena/combinato...s/dijkstra.html


--Slip
Logged
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #17 on: October 03, 2005, 12:01:30 AM »

For the “traveling salesman” problem are we to printout all duplicate paths?  For instance, if you have four cities and the shortest path is ABCD, should we also print ADCB?  It’s the same path, but traveling the other way around.

PS. Yes, this does indeed imply that my program works.  ( moral +10 ), ( moral -10 ) if otherwise.  >.>

PSS. Don’t forget to do the adaptation of the…umm…something that we already did…umm…were we to do the adaptation of the sort program(s) to make use of pointers instead of using array indices?

Should I post the code here?  Maybe more people will post or even signup.

[Edit]

Good lord…stupid factorial problem
I collected some data, number of cities versus the time it took for my program to complete.  I had only gotten five data points but I used Excel’s trend line thingy anyways, it told me that it would take 2.1 weeks for my program to find every single shortest path if there were 20 cities…I also saw that it would take 10 hours to find the shortest path with 15 cities; I figured that I could at least get another data point by the next morning.  However!! It’s now been 12 hours and my program is at almost 12% complete.  In which case, my logic dictates to me two things. One, that the trend line I used was wrong, either because I set it up incorrectly, or that I didn’t have enough data. Two, that the 2.1 weeks to search through 20 cities is much MUCH too short a time… <_<

I have now set my program's priority down to “below normal”.  This way I get to know how long it’ll take to complete, AND I get to use my computer.  Which doesn’t change the fact that it will take something like four and half days to complete…I guess that means it’ll take a total of 5 days to complete…O_o
 
« Last Edit: October 03, 2005, 07:34:05 PM by smi256 » Logged

*was here
Mr.Slippery
Newbie
*
Offline Offline

Posts: 14



View Profile WWW
« Reply #18 on: October 03, 2005, 08:32:30 PM »

Uhhh, that can't be right.

Weeks?? to solve a 20 city problem?

Is your computer "powered" by a treadwheel, revolving only by the sheer willpower of an elderly, arthritic hampster--as it negotiates the tricky wire slats with its custom-made walker??

(or, maybe I should finish coding my program...)  Wink
--Slip :alien:  
Logged
Mr.Slippery
Newbie
*
Offline Offline

Posts: 14



View Profile WWW
« Reply #19 on: October 03, 2005, 10:34:09 PM »

Quote
I've worked it out with 4 and 5 cities.
but the problem is that I don't think that my technique works when there are more cities...
the only way for me to see if mine works is for someone to make the program as the instructor suggested (the brut force way), even if that person has to be me if everyone wants to be punks and not show in the forum <_<

My technique works by starting at one city and fallowing the shortest path to all adjacent cities.  Here’s the problem, it doesn't look far ahead, and there are paths that are shorter overall.  I tried to overcome some of this by starting anew at every city, but this only helps, not fixes the problem.
great graph, Smi!!

Looks vaguely familiar, as if in a dream, in a Dungeon, spoken in the foreboding voice of the D.ungeon M.aster...

Well, actually those Red paths are looked at, but not in that particular part of the graph. So C-D and C-E are either taken or looked at, but they were rejected at that particular point in the sequence.
I know what you're talking about, tho.
--Slip  :alien:  
Logged
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #20 on: October 03, 2005, 11:24:44 PM »

Quote
Well, actually those Red paths are looked at, but not in that particular part of the graph. So C-D and C-E are either taken or looked at, but they were rejected at that particular point in the sequence.
I know what you're talking about, tho.
--Slip  :alien:
True, but they were only looked at once.  I like graphs :hehe:

I recalculated things.  I found that it takes about 4.8E-6 seconds to make a complete path, which brings my 15 city test to 4.8 days to complete. <_<   That brings my 20 city test to 2,700 years. :blink:   However!!  I added a little bit of code saying, “if the current running total is more then the current shortest path, don’t continue down that path!”  Having said this, my 5 day runtime went down to 24 seconds!!! Shocked  Cheesy

There is no real way for me to calculate the amount of time my program will take to complete, but if my program goes at it’s current rate it’ll take about 9 hours. :cool: It finished in 55 minutes :cool:

Time to work on that other HW.  
Quote
yeah, increment pointers rather than array indices. Messy-er,
but gotta practice pointer stuff.
Just one of the search programs? :sheep:

[edit]
Speaking of which, Java HW is Page 283 #6.30.  Don’t forget your  Smiley/:(  faces!
« Last Edit: October 04, 2005, 12:03:15 AM by smi256 » Logged

*was here
Mr.Slippery
Newbie
*
Offline Offline

Posts: 14



View Profile WWW
« Reply #21 on: October 04, 2005, 02:13:51 AM »

Quote
I recalculated things.  I found that it takes about 4.8E-6 seconds to make a complete path, which brings my 15 city test to 4.8 days to complete. <_<   That brings my 20 city test to 2,700 years. :blink:   However!!  I added a little bit of code saying, “if the current running total is more then the current shortest path, don’t continue down that path!”  Having said this, my 5 day runtime went down to 24 seconds!!! Shocked  Cheesy

There is no real way for me to calculate the amount of time my program will take to complete, but if my program goes at it’s current rate it’ll take about 9 hours. :cool: It finished in 55 minutes :cool:

Time to work on that other HW. 
[Just one of the search programs? :sheep:

Speaking of which, Java HW is Page 283 #6.30.  Don’t forget your  Smiley/:(  faces!
Smi,
Werry impwessive.
That's a nice optimization.
What is the final time for 20-cities?

Yeah, I believe that the pointer-array stuff applies to all 3 sort methods. (hah, since I put all 3 sort functions into 1 prog, that'll save me some time. (and I need time, all the time!)

We are all just  :sheep: in the world.

Except for me, of course. I'm an  :alien:
--Slip
Logged
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #22 on: October 04, 2005, 07:08:18 AM »

Damn, I can’t seem to get a counter working.
I have it set up to count the number of paths that have been ‘searched’
When a path has reached full circle then, paths++
Depth is the number of cities that have already been added to the current path.
When the current path length is greater then the shortest path then, paths+= factorial (number_of_cities-Depth-1);
When I go to print the percentage then, printf("%3.2f%c ",((float)paths*100/TotPaths),37);
I sometimes get a number that’s too small, and other times one that’s too big… I don’t know why is doesn’t work. Sad

Btw, my program now takes 30minutes to find the shortest path with 20 cities. Cheesy  
Logged

*was here
Guest
Guest
« Reply #23 on: October 04, 2005, 09:04:14 AM »

Quote
Btw, my program now takes 30minutes to find the shortest path with 20 cities. Cheesy
That's a bit better than 2,700 years, ya' think?

I guess, you'll have to rename your program now to PizzaDriver.exe
(30 minutes or it's FREE!)

--Slip  :alien:

p.s. at least your program runs.
Logged
Guest
Guest
« Reply #24 on: October 04, 2005, 09:15:30 AM »

Quote
--Slip  :alien:

p.s. at least your program runs.
Could you perhaps loan me your hamster? Sad
--Slip  :alien:  
Logged
Guest
Guest
« Reply #25 on: October 04, 2005, 02:51:12 PM »


Hi Peter,

It is the first time I read and knew how to post reply here. Mine travelling salesman seems to be good to me. He works fast, but sometimes, he gives me the negative tour.   :rolleyes: My point is that: can we wrok on the same program? That case, more people are interested in signing up. As you know, people here seem to be busy and stress all the time with their living. You just say that you have problem and people cannot have a look at your code, they cannot say anything. Maybe they can tell you that theirs work better or worse.

Anyway, you are working hard and doing good job. Smiley

Thank you for the link.
 
Logged
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #26 on: October 04, 2005, 09:02:48 PM »

Quote
/*******************************************
 * Name:              Peter Kaminsky
 * Course:            Adv. C
 * Assingment:        Traveling Salesman
 * Compiler:          Div-C++
*******************************************/

#include <stdio.h>
#include <math.h>
#include <time.h>

//#define PERCENT  //doesn't work corrently
#define SPIN       //doesn't refresh enough when there are too many cities
void starter(int array[][], int size_of_array, long long int TotPaths);
void depth  (int array[][], int size_of_array, intCol, intTot, char string[],
                            char Min_Path[], int *Min_Length,
                            long long int TotPaths, int Depth);
long long factorial (long long n);
FILE *f;
int step=1;

main()
char meh;
/*
   #define ASIZE 5
   int a[][ASIZE]={ { 0,29, 5,20,15},
                    {29, 0,42,24,35},
                    { 5,42, 0, 8,38},
                    {20,24, 8, 0, 4},
                    {15,35,38, 4, 0} };
*/
/*
   #define ASIZE 10
   int a[][ASIZE]={ { 0,30,39,24,11,27,30,26, 3,31},
                    {30, 0,11, 3,36,32,20, 1,48,50},
                    {39,11, 0, 7,41,50,13,34, 9,12},
                    {24, 3, 7, 0,31,31,46,24,25,23},
                    {11,36,41,31, 0,29,12,10,28,34},
                    {27,32,50,31,29, 0,47,15, 8,42},
                    {30,20,13,46,12,47, 0, 2,22, 3},
                    {26, 1,34,24,10,15, 2, 0,10,10},
                    { 3,48, 9,25,28, 8,22,10, 0,20},
                    {31,50,12,23,34,42, 3,10,20, 0} };
*/
/*
   #define ASIZE 13  //has two paths!
   int a[][ASIZE]={ { 0,37, 6, 7,14,32,34,37,31, 7,42, 8,14},
                    {37, 0,35,14,46,47, 6,44, 3,17,28,39, 8},
                    { 6,35, 0,14,41,23,12,35, 3,28,25,45,50},
                    { 7,14,14, 0,33,21,24,27,23,16,29, 2,45},
                    {14,46,41,33, 0, 7,46,42,14,19,43, 7, 3},
                    {32,47,23,21, 7, 0,25,40,37,36,26,48,44},
                    {34, 6,12,24,46,25, 0, 7,44,31,23, 6,33},
                    {37,44,35,27,42,40, 7, 0,10,29,25,44, 7},
                    {31, 3, 3,23,14,37,44,10, 0,35,34, 8,10},
                    { 7,17,28,16,19,36,31,29,35, 0,14,25,11},
                    {42,28,25,29,43,26,23,25,34,14, 0,19,47},
                    { 8,39,45, 2, 7,48, 6,44, 8,25,19, 0,39},
                    {14, 8,50,45, 3,44,33, 7,10,11,47,39, 0} };
*/


   #define ASIZE 15
   int a[][ASIZE]={ { 0,12,38,44,47,23,33,13,13,39,50,33, 7,20,29},
                    {12, 0,31,27,21, 6,10,36,19,40,13,12, 1, 2,25},
                    {38,31, 0,39,14,39,32,23,47,25,10, 7,40, 4,25},
                    {44,27,39, 0,12,50,12, 9,35,38,19,42, 8, 7, 6},
                    {47,21,14,12, 0,11,32,23,29,42,29,26,48,25,25},
                    {23, 6,39,50,11, 0,28,46,10,44,40,40, 4,16,19},
                    {33,10,32,12,32,28, 0,20, 6,35,42, 6,37, 3,29},
                    {13,36,23, 9,23,46,20, 0,48,19,45,28,27, 9, 3},
                    {13,19,47,35,29,10, 6,48, 0,36, 4,29,19,16,22},
                    {39,40,25,38,42,44,35,19,36, 0,14,18,40, 4,36},
                    {50,13,10,19,29,40,42,45, 4,14, 0, 9,18, 6,32},
                    {33,12, 7,42,26,40, 6,28,29,18, 9, 0,36, 4,33},
                    { 7, 1,40, 8,48, 4,37,27,19,40,18,36, 0,46, 2},
                    {20, 2, 4, 7,25,16, 3, 9,16, 4, 6, 4,46, 0,25},
                    {29,25,25, 6,25,19,29, 3,22,36,32,33, 2,25, 0} };

/*
   #define ASIZE 20
   int a[][ASIZE]={ { 0,12,40,37,18,15, 5,17,49,11, 4,40,18,46,37,25,14,40,30,44},
                    {12, 0,32, 5,47,48,21,29,45,31,16, 4,44,22,31,18,47,14,50,24},
                    {40,32, 0,11,37,41,49,14,29,41,23,20,42,41,14,46,47,28,41,19},
                    {37, 5,11, 0,37,30,40,47,19, 1,21,40, 6,12, 6,42,14,24,13, 7},
                    {18,47,37,37, 0,29,21, 8,34, 9,18,37, 1,42,45, 2,48,48,11,25},
                    {15,48,41,30,29, 0,20,38,33,22,12,11,47,14,26, 9,22, 2, 7,27},
                    { 5,21,49,40,21,20, 0, 4,42,39,13,21,19,11,38,47,13,20,45,43},
                    {17,29,14,47, 8,38, 4, 0,21,38,36,43,30,47,49,35,27,20,47,20},
                    {49,45,29,19,34,33,42,21, 0,23, 5,27, 3,23,14, 7,18,44,49,25},
                    {11,31,41, 1, 9,22,39,38,23, 0, 6, 9,16,24,29, 6,11,37, 3, 4},
                    { 4,16,23,21,18,12,13,36, 5, 6, 0,27,28,39,33,50,45,50,14,35},
                    {40, 4,20,40,37,11,21,43,27, 9,27, 0, 3, 7,29, 9,36,33,11,48},
                    {18,44,42, 6, 1,47,19,30, 3,16,28, 3, 0,40,19, 9,23,18,49,22},
                    {46,22,41,12,42,14,11,47,23,24,39, 7,40, 0,33,39, 4, 7,22,35},
                    {37,31,14, 6,45,26,38,49,14,29,33,29,19,33, 0,12,48, 7,44,17},
                    {25,18,46,42, 2, 9,47,35, 7, 6,50, 9, 9,39,12, 0, 6, 1,36,22},
                    {14,47,47,14,48,22,13,27,18,11,45,36,23, 4,48, 6, 0,34,31,34},
                    {40,14,28,24,48, 2,20,20,44,37,50,33,18, 7, 7, 1,34, 0,16, 3},
                    {30,50,41,13,11, 7,45,47,49, 3,14,11,49,22,44,36,31,16, 0,34},
                    {44,24,19, 7,25,27,43,20,25, 4,35,48,22,35,17,22,34, 3,34, 0} };
*/

   int i, j, size=sqrt((sizeof(a)/sizeof(int)));
   printf("Cities: %d\n",size);
   printf("Segments between cities: %d\n",((size*size-size)/2)); //cities choose 2
   long long int TotPaths=factorial(size-1);
   printf("Posible paths: %.0f\n", (float)TotPaths);
   long int startT=clock(); //start timer
   starter(a,size,TotPaths);//call function
   long int stopT=clock();  //stop timer
   printf("%c\nSearch Time: %.2f Seconds",13,(float)(stopT-startT)/CLK_TCK);
   printf("\n\nPress a key to close this window.");
   meh=getch();
}//end main()

void starter(int a[][], int size, long long TotPaths)
int minlen=0;
   char minpath[100]={};
   depth  (a, size, 0, 0, "A", minpath, &minlen, TotPaths, 0);
   printf("%cThe shortest path has a length of %d\n",13,minlen); //Print the Total Length
   printf("All of the shortest (directional) paths are:\n\n");
   /*Print the Paths*/
   f=fopen("minTemp.txt","r");
   while(fgets(minpath,size,f)!=NULL)
      fputs(minpath,stdout);
   fclose(f);
}//end starter()

void depth (int a[][ASIZE], int size,   int Old_Col, intTot, char Old_Str[],
            char minpath[], int *minlen, long long int TotPaths, int Depth)
int i, j, row=Old_Col, col, stop=1;
   static long long int paths=0;
   char string[100]={}, sub[0]={};  /**  create a new private string for the path  **/
   strcpy(string,Old_Str);          /**  copy the old path to the new string       **/
   sprintf(sub,"%c",Old_Col+65);    /**  find new city letter                      **/
   if(strrchr(string,sub[0])==NULL) /**  don't double-up on the print command      **/
      strcat(string,sub);           /**  add the new city to the new string        **/
   a[Old_Col][Old_Col]=-1;                //came from this city, can't go back to it
/************************************//// start end-of-the-line block
/**/   for(i=0; i<size; i++)              //check for the end-of-the-line
/**/      if(a==0)
/**/         stop=0;
/**/   if(stop==1)                        //if this IS the end-of-the-line
/**/   {  Tot+=a[0][Old_Col];             //go back to city 'A'
          #ifdef PERCENT                  //switch the printing of the % on/off
/**/      paths++;                        //the number of paths already taken
/**/      printf("%c%3.2f%c  ",13,((float)paths*100/TotPaths),37);
          #endif          //there is something wrong with paths count
          #ifdef SPIN                     //switch the spining thing on/off
/**/      switch(step)                    //because... I can
/**/      {  case 1: { printf("%c\\",8); step=2; break; }// this needs to go more often,
/**/         case 2: { printf("%c|",8);  step=3; break; }// when there are more cities
/**/         case 3: { printf("%c/",8);  step=4; break; }// it doesn't get
/**/         case 4: { printf("%c-",8);  step=1; break; }// triggered enough
/**/      }
          #endif
/**/      if( (Tot==*minlen) )                    //store current "minpath" to a file
/**/      {  f=fopen("minTemp.txt","a");          //and other "minlen" paths
/**/         fprintf(f,"%s\n",string);            //
/**/         fclose(f);                           //when there is a new "minpath"
/**/      }                                       //start the file over again
/**/      if( ((*minlen==0) || (Tot<*minlen)) )   //
/**/      {  *minlen=Tot;                         //at the very end,
/**/         strcpy(minpath,string);              //print the real shortest paths
/**/         f=fopen("minTemp.txt","w");          //from the file
/**/         fprintf(f,"%s\n",string);            //
/**/         fclose(f);                           //
/**/      }                               
/**/   }
/************************************//// end end-of-the-line block
   for(col=0; col<size; col++)            //step through all unvisited cities
   {  if( a[col][col]!=-1 )               //continue if the current length is shorter
      {  if(((Tot+a[row][col])<*minlen) || *minlen==0)
         {  depth(a,size,col,Tot+a[row][col],string,minpath,minlen,TotPaths,++Depth);
            a[col][col]=0;                //this city is now back 'open'
            Depth--;                      //came back a level
         }
         #ifdef PERCENT                   //switch the printing of the % on/off
         else  //something is wrong with paths count
         {  paths+=( factorial(size-Depth-1) ); //this should be the number of paths that
         }     //would have been searched through but weren't because the current path
         #endif//was longer then the current "shortest path"
      }
   }
}//end depth()

long long factorial(long long n)          //a "long long" O_o? crazy
if(n==1)
      return(1);
   else
      return(n * factorial(n-1));
}
« Last Edit: October 04, 2005, 09:13:03 PM by smi256 » Logged

*was here
Mr.Slippery
Newbie
*
Offline Offline

Posts: 14



View Profile WWW
« Reply #27 on: October 04, 2005, 11:15:13 PM »

Hmmm, Smi.

Looks complicated.   :unsure:

Here are some more thoughts (to further complicated/simplify the tour logic):

The algorithm should have some penalty in not going in a straight line (or large circle).
That is, there should be some bias given to paths that are
approximately in the same direction (in successive moves). I don't
know how to do it yet, but I think it involves taking the cosine or
sine of two successive paths and encoding them into a matrix. Then,
not only the length of the nearest city but it's straightness along
your current path helps the salesman decide where to go.

In the ideal case, you would start in a straight line and move from
one end to the other. In another case, all cities are arranged in a
circle with very little arc (sine penalty) between cities.

--Slip  :alien:
Logged
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #28 on: October 04, 2005, 11:24:56 PM »

:miffed: I don't understand Sad  
Logged

*was here
smi256
Hero Member
*****
Offline Offline

Posts: 2287



View Profile
« Reply #29 on: October 16, 2005, 12:23:36 AM »

I think this is a crap problem, not just because it’s very poorly worded, it’s just lame.
Quote
7.10) Sales Commissions
Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, or a total of $650. Write an application (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
     a) $200-299
     b) $300-399
     c) $400-499
     d) $500-599
     e) $600-699
     f) $700-799
     g) $800-899
     h) $900-999
     i) $1000 and over
Summarize the results in tabular format.

The part that I have a problem with is the,
“Write an application that determines how many of the salespeople earned salaries in each of the following ranges:”

I’m thinking it should read,
“Write an application that determines how much a salesperson would earn with a gross sale in each of the following ranges:”

Or even to the effect of,
“Write an application that determines how much a salesperson’s gross sale was, based on their salary based on the following ranges:”

I’m either reading it wrong or there is a certain amount of ambiguity here <_<

[edit/add]

It was suggested to me that the user is supposed to enter the gross sales for each salesperson (however many the user chooses to enter).  When the user is done, print a table of how many salespeople fall into a particular bin (the ranges given in the problem)

Ya… I think that makes more sense :sheep:
« Last Edit: October 16, 2005, 02:27:40 AM by smi256 » Logged

*was here
Pages: 1 [2] 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!