1

So i think I am on the right track using the branch and bound method to solve my heuristic solution to the traveling problem, however, I get a segmentation fault in my "least" function, but I am still having a hard time wrapping my head around the algorithm. Any help fixing this would be much appreciated. Basically the requirements for the function (because my main function looks funny) are that the program should take up to 150 cities, and if it reads under 60 seconds I get bonus points ( which means I don't fail) the input file will look like(for example): c 1 c 2 a 1 2 300 Where 'c' means "create a city", and 'a' means "add a boundary." which is why my program is set up the way it is.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int cost=0;
int main(){
int numcity ,a[151][151],visited[151],worthless,city1,city2,distance;
char citystring[2];
  a[1][1]=0;
  while(scanf("%s",citystring) != EOF){
  if(strcmp(citystring, "c") == 0){
    scanf("%d", &worthless);
    numcity++;
    }
  else if(strcmp(citystring, "a") == 0){
    scanf("%d %d %d",&city1,&city2,&distance);
    a[city1][city2] = distance;
    a[city2][city1] = distance;
    }
  }
  mincost(1,numcity,a,visited);
  printf("The minimum cost tour is %d",cost);

  return 0;
}

int mincost(int city,int n,int a[151][151],int visited[151]){
int i,ncity;
  visited[city]=1;
  printf("%d->",city);
  ncity=least(city,n,a,visited);
  if(ncity==999999){
    ncity=1;
    printf("%d",ncity);
    cost+=a[city][ncity];
  }
  mincost(ncity,n,a,visited);
}

int least(int c,int n,int a[151][151],int visited[10]){
int i,nc=999999,min=999999,kmin;
  for(i=1;i<=n;i++){
    if((a[c][i]!=0)&&(visited[i]==0)){
      if(a[c][i]<min){
        min=a[i][1]+a[c][i];
        kmin=a[c][i];
        nc=i;
      }
    }
  }
  if(min!=999999)
    cost+=kmin;
    return nc;
}
rpax
  • 4,468
  • 7
  • 33
  • 57
Dillon Stout
  • 19
  • 1
  • 2
  • Your code definitely has a couple of problems. First mincost recurses without any way to get out (that is why you are crashing now). second, as Jim mentions, your for loop in least() starts at 1. There may be others, but just tackle them one at a time, and add as much printf tracing as you need to. You'll get it. – Joshua Clayton Apr 05 '13 at 23:46

2 Answers2

0

your program core dumps on the last line of mincost(), which is a recursive call to mincost().

the reason for the crash is you are blowing (overflowing) the stack.

You have the recursive equivalent of an endless loop.

When should mincost return?

Joshua Clayton
  • 1,669
  • 18
  • 29
0

C arrays are 0-indexed, but your index goes from 1..n instead of 0..(n-1) ... is that why you they are declared 151 instead of 150?

But your crash is probably due to mincost calling itself unconditionally. You say that you think you're on the right track but are having trouble wrapping your head around the algorithm ... I think those statements are inconsistent. You should be sure that you understand the algorithm before attmepting to code it.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66