-1

I am doing an assignment for class that is supposed to be coded a very specific way. Here is the goal:

Goal: To implement Dijkstra’s algorithm for single source shortest path problem for a given directed graph using an adjacency matrix representation of the graph.

I am using Makefile to compile the code. Input will be taken via the terminal/console ./a7 < datafile. Here is an example of the input:

6 11 0
0 1 50
0 2 10
0 4 45
1 2 15
1 4 10
2 0 20
2 3 15
3 1 20
3 4 35
4 3 30
5 4 03

And the expected output:

0 2 10
0 3 25
0 1 45
0 4 45
0 5 INF (no path)

Here is the code:

#include <limits.h> /* for INT_MAX */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define N 10 /* max matrix size is 10 x 10 */
#define INF INT_MAX /* infinity? */

int main(int argc, char **argv) {
  void getdata(int amtrx[][N], int *sz, int *stv);
  void dijkstras(int amtrx[][N], int src);

  if (argc < 2) {
    printf("Missing Filename.\n");
    return(1);
  } else if (argc > 2) {
    printf("Too many arguments.\n");
    return(1);
  }
  FILE *f = fopen(argv[1], "r");

  int amtrx[N][N], *sz, *stv;
  stv = malloc(sizeof(int));
  sz = malloc(sizeof(int));
  getdata(amtrx, sz, stv);

  dijkstras(amtrx, *stv);

  fclose(f);
  return(0);
}

void getdata(int amtrx[][N], int *sz, int *stv) {
  int i, j, nsz, nedg, fr, to, vtx, wt;
  scanf("%d %d %d", &nsz, &nedg, &vtx);
  for(i = 0; i < nsz; i++)
    for(j = 0; j < nsz; j++)
      amtrx[i][j] = INF;
  for(i = 0; i < nedg; i++) {
    scanf("%d %d %d", &fr, &to, &wt);
    amtrx[fr][to] = wt;
    amtrx[to][fr] = wt;
  }
  *sz = nsz;
  *stv = vtx;
}

void dijkstras(int amtrx[][N], int src) {
  void printpaths(int amtrx[][N]);
  int mindistance(int dist[], bool sptSet[]);

  int dist[N];
  bool sptSet[N];
  int i, count, v, u;

  for (i = 0; i < N; i++)
    dist[i] = INF, sptSet[i] = false;

  dist[src] = 0;

  for (count = 0; count < N - 1; count++) {
    u = mindistance(dist, sptSet);

    sptSet[u] = true;

    for (v = 0; v < N; v++)
      if (!sptSet[v] && amtrx[u][v] && dist[u] != INF && dist[u] + amtrx[u][v] < dist[v]) {
        dist[v] = dist[u] + amtrx[u][v];
      }
    }
    printpaths(amtrx);
}

int mindistance(int dist[], bool sptSet[]) {
  int min = INF, min_index;
  int v;

  for (v = 0; v < N; v++)
    if (sptSet[v] == false && dist[v] <= min)
      min = dist[v], min_index = v;

  return min_index;
}

void printpaths(int amtrx[N][N]) {
  //to be written
}

So far it just runs forever and doesn't print anything. I suspect maybe an infinite loop but I can't see where it's happening. I tried to run in GDB and it just says Starting program: /a7 datafile and goes no where. I also would like help making sure my algorithm is correct, but I can save that for another question if need be. Thanks in advance!

Helana Brock
  • 45
  • 15

1 Answers1

2

To find out what is wrong with help of gdb you usually need to run program in step mode to see where and why it hangs or loops.

  1. Ensure that you compiled your program with debug symbols (if you are using gcc or similar compiler then -g option is needed).
  2. To avoid going over whole program in step mode you can just hit Ctrl-C in gdb at the time you think your program hanged and then enter command bt. It will show you where your program is. You can then enter command cont, and break program execution after some time to see if you are in different place, or in the same...
  3. If you still don't know what is the reason of your problem after step 2, you can kill program and set up break point before the place you have identified as being the one in which your program hangs/loops --- read gdb documentation about break to see how to set up breakpoint.

Here is link for gdb tutorial hosted at CMU: https://www.cs.cmu.edu/~gilpin/tutorial/

Slimak
  • 1,396
  • 1
  • 8
  • 17
  • Thank you so much for your answer! I have gotten around the problem another way but I will definitely keep this in mind! I figured out I need to use files another way, one that does't even involve opening them?! I will be posting another question once I am able to, so keep an eye out for it :) – Helana Brock Apr 21 '20 at 21:56
  • Is "_use a debugger_" really an answer? – ad absurdum Apr 21 '20 at 21:58
  • The question could be interpreted in many ways. Of course I could answer "you need to replace scanf with fscanf", but I think that in this case giving a generic way of finding why program does not do what it should was fitting much better the SO and the header of the question. – Slimak Apr 21 '20 at 22:02
  • 1
    @Slimak -- I don't think that replacing `scanf()` with `fscanf()` would help, anyway; OP appears to have redirected input from a file to `stdin` with `./a7 < datafile`. – ad absurdum Apr 21 '20 at 22:17
  • @exnihilo --- I missed ```<``` --- maybe because when I tested the program it correctly errored when not given file name as the only argument instead of hanging which seems (the hanging) to be the reason of asking the question. – Slimak Apr 21 '20 at 22:47