title: Using BFS to compute distance between a source and a vertex
body: <p>I am trying to use adjacency list to compute the distance from a source vertex to the other vertices. I am using a queue to accomplish this however I get the distance of each vertex besides the source as -1, but I am not sure why this is happening </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "input_error.h"
  #define VertexToSearch 1
  
  typedef struct edge {
      int vertexIndex;
      struct edge *edgePtr;
  } edge;
  
  typedef struct vertex {
      int vertexKey;
      struct edge *edgePtr;
      int visited;
      int distance;
  } vertex;
  
  typedef struct queue {
      struct vertex v;
      struct queue* next;
  }queue;
  
  int vertexCount = 0;
  struct vertex graph[];
  void load_file(char*);
  void insertEdge(int, int, struct vertex[]);
  void InsertVertex(int, struct vertex[]);
  void printGraph();
  void bfs();
  void print_distances();
  queue* enqueue(queue*,vertex );
  vertex dequeue(queue*);
  
  enum error program_error;
  int count;
  
  int main(int argc, char** argv) {
      load_file(argv[1]);
      printGraph();
      bfs();
      print_distances();
      return 0;
  }
  
  void load_file(char* filename) {
  
      int vertex1;
      int vertex2;
      FILE* file = fopen(filename, "r");
  
      if (file == NULL) {
          printf("%s did not open\n", filename);
          program_error = FILE_FAILED_TO_OPEN;
          exit(program_error);
      }
  
      fscanf(file, "%d", &amp;count);
      graph[count];
      for (int i = 0; i &lt; count; i++) {
          InsertVertex(i + 1, graph);
      }
      for (int i = 0; i &lt; count; i++) {
          fscanf(file, "\n(%d,%d)", &amp;vertex1, &amp;vertex2);
          insertEdge(vertex1, vertex2, graph);
      }
      fclose(file);
  }
  
  void InsertVertex(int vertexKey, struct vertex graph[]) {
      graph[vertexCount].vertexKey = vertexKey;
      graph[vertexCount].edgePtr = NULL;
      graph[vertexCount].visited = 0;
      graph[vertexCount].distance = -1;
      vertexCount++;
  }
  
  void insertEdge(int vertex1, int vertex2, struct vertex graph[]) {
      struct edge *e, *e1, *e2;
      e = graph[vertex1 - 1].edgePtr;
      while (e &amp;&amp; e-&gt;edgePtr) {
          e = e-&gt;edgePtr;
      }
      e1 = (struct edge *) malloc(sizeof (*e1));
      e1-&gt;vertexIndex = vertex2;
      e1-&gt;edgePtr = NULL;
      if (e)
          e-&gt;edgePtr = e1;
      else
          graph[vertex1 - 1].edgePtr = e1;
  
      e = graph[vertex2 - 1].edgePtr;
      while (e &amp;&amp; e-&gt;edgePtr) {
          e = e-&gt;edgePtr;
      }
      e2 = (struct edge *) malloc(sizeof (*e2));
      e2-&gt;vertexIndex = vertex1;
      e2-&gt;edgePtr = NULL;
      if (e)
          e-&gt;edgePtr = e2;
      else
          graph[vertex2 - 1].edgePtr = e2;
  }
  
  void printGraph() {
      int i;
      struct edge *e;
      for (i = 0; i &lt; vertexCount; i++) {
          printf("%d(%d)", i + 1, graph[i].vertexKey);
          e = graph[i].edgePtr;
          while (e) {
              printf("-&gt;%d", e-&gt;vertexIndex);
              e = e-&gt;edgePtr;
          }
          printf("\n");
      }
  }
  
  void bfs() {
      graph[0].distance = 0;
      queue* q = NULL;
     q = enqueue(q,graph[0]);
      while(q-&gt;next != NULL){
          vertex u = dequeue(q);
          while(u.edgePtr != NULL){
             if(graph[u.edgePtr-&gt;vertexIndex -1 ].distance == -1){
                graph[u.edgePtr-&gt;vertexIndex -1 ].distance = u.distance + 1;
                enqueue(q, graph[u.edgePtr-&gt;vertexIndex -1 ]);
             } 
             u.edgePtr = u.edgePtr-&gt;edgePtr;
          }
      }
  
  }
  
  void print_distances() {
      for (int i = 0; i &lt; count; i++) {
          printf("%d %d\n", i + 1, graph[i].distance);
      }
  }
  
  queue* enqueue(queue* q,vertex v) {
      queue* new = malloc(sizeof (queue));
      new-&gt;next = NULL; 
      new-&gt;v = v;
      if (q == NULL) {
          q = malloc(sizeof(queue));
          q = new;
      } else {
          while (q-&gt;next != NULL) {
              q = q-&gt;next;
          }
          //add new node at the end
          q-&gt;next = new;
  
      }
      return q;
  }
  
  vertex dequeue(queue* q) {
      vertex v;
      queue* tempPtr;
      tempPtr = q; //makes temp the address of the node to be deleted
      v = tempPtr-&gt;v;
      q = q-&gt;next; //sets the new head as the address of the next node
  
      return v;
  } 
  </code></pre>