Genetic algorithm implementation in c/c++

balbeer's Avatar, Join Date: Nov 2011
Newbie Member
How to implement a c/c++ code to find shortest path using genetic algorithm.

Can anyone help me out?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Where are you stuck?
0
balbeer's Avatar, Join Date: Nov 2011
Newbie Member
I am unable to save different possible paths.what type of data structure i should use to save different paths..as i have to further use these paths..
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You could use a tree structure, or just save everything locally and recurse. Depends if you're using a depth-first or breadth-first algorithm.
0
balbeer's Avatar, Join Date: Nov 2011
Newbie Member
Code:
#include<stdio.h>
#include<conio.h>

static struct {
  int value1;
  int value2;
  int used;
} data[] = {
  { 1, 2 },
  { 1, 6 },
  { 2, 1 },
  { 2, 3 },
  { 3, 2 },
  { 3, 4 },
  { 3, 5 },
  { 4, 3 },
  { 4, 5 },
  { 4, 8 },
  {5,3},
  {5,4},
  {5,6},
  {5,12},
  {6,1},
  {6,5},
  {7,1},{7,11},{8,4},{8,9},{8,10},{9,8},{9,14},{9,15},{10,8},{10,12},
  //{10,16},{11,7},{11,12},{12,5},{12,10},{12,11},{12,13},{13,12},{13,16},{14,9},{14,16},{15,9},{15,16},{16,10},{16,13},{16,14},{16,15}
};

enum { DATA_SIZE = sizeof data / sizeof *data };

static int output[DATA_SIZE];

int traverse(int from, int to, int depth) {
  output[depth++] = from;
  int len;
  int i;
  if (from == to) {
    for (i = 0; i < depth; i++) {
      if (i) {
        printf("-");
      }
      printf("%d", output[i]);
      if(len>depth){
      len=depth;
      //len=len+1;
  }    
    }
    len=depth;
    printf("\n");
    
  } else {
    for (i = 0; i < DATA_SIZE; i++) {
      if (!data[i].used) {
        data[i].used = 1;

        if (from == data[i].value1) {
          traverse(data[i].value2, to, depth);
        } else if (from == data[i].value2) {
          traverse(data[i].value1, to, depth);
        }

        data[i].used = 0;
      }
    }
  }
  return len;
}

int main() {
  int len=traverse(9, 4, 0);
  printf("Shortest length path :%d",len);
  getch();
}

how can i save the paths in an array so that i can use them further..?

Last edited by shabbir; 8Nov2011 at 07:19.. Reason: Code blocks