1

I implemented the Edmonds–Karp algorithm in C++ and when I went to test it with a small graph, it didn't finish, but I can't identify where it got stuck. I usually use cout with something until I find where it prints infinitely, but I didn't work here. The program never print anything. I tried to put a cout as the first line of the program, but it doesn't print it. Anyone knows why it never print anything and/or have some tip as how I could identify the infinite loop?

The code, if someone wants to look, is:

int main ()
{
FILE *graph_file;
graph_file = fopen("grafo.txt", "r");
if (graph_file == NULL)
{
    cout << "Não foi possível abrir o arquivo 'grafo.txt'" << endl;
    exit(1);
}

char line[MAX_LINE];
char *line_split = NULL;

// pegar informações do grafo para armazenar o tamanho adequado dos vetores
fgets(line, MAX_LINE, graph_file);
while (feof(graph_file) == 0)
{
    if (line[0] == 'a') // é uma aresta
    {
        line_split = strtok(line, " "); // a

        line_split = strtok(NULL, " "); // vertice origem da aresta
        int origin = atoi(line_split);

        line_split = strtok(NULL, " "); // vertice destino da aresta
        int destiny = atoi(line_split);

        line_split = strtok(NULL, " "); // peso da atesta
        int capacity = atoi(line_split);

        insert_edge(origin, destiny, capacity);
    }
        fgets(line, MAX_LINE, graph_file);
}

//print_graph();

int origin = 1;
int destiny = 6;

calc_valid_path(origin, destiny);

while (precursor[destiny] != 0)
{
    int max_cap = get_path_max_cap(origin, destiny);

    int i = destiny;
    while (i != origin)
    {
        Edge *temp = get_edge(precursor[i], i);
        temp->flow = temp->flow + max_cap;

        temp = get_edge(i, precursor[i]);
        if (temp == NULL) // se a aresta inversa ainda não existe, cria
        {
            insert_edge (i, precursor[i], 0);
            temp = get_edge(i, precursor[i]);
        }
        temp->flow = temp->flow - max_cap;
        i = precursor[i];
    }

    calc_valid_path(origin, destiny);
}

cout << "Flow: " << get_flow(destiny);

return 0;
}
user2748531
  • 1,213
  • 4
  • 12
  • 21
  • 3
    Did you have a `endl` on your print? Typically, if you don't use `endl`, nothing will be printed, because it's all buffered in an output buffer until there is a `flush`. `endl` will flush the buffer. – Mats Petersson Sep 04 '13 at 21:32
  • Use a debugger. It will better than using cout! because if you add 1 or more cout... it's another program. – alexbuisson Sep 04 '13 at 21:44
  • I didn't know about the `endl` thing. It was the problem with `cout`. I think it was the first time I forget to put it. Sory for the stupid question. – user2748531 Sep 04 '13 at 21:45
  • I don't know how to use a debugger in linux... – user2748531 Sep 04 '13 at 21:46
  • 1
    @user2748531: `g++ -g yourfile.cpp -o yourfile`, `gdb ./yourfile` should get you to a debugger. If you type `run` in the debugger it will run your code. Press CTRL-C and it gets you back to the debugger, and you can see where you are with `stack` and use `print x` to view the variable x. – Mats Petersson Sep 04 '13 at 21:49
  • Thanks for the debugger tip. I will try it when I have a problem, now I have already identified that the loop was just a small stupid mistake. – user2748531 Sep 04 '13 at 22:06

1 Answers1

1

It sounds as if the output doesn't get flushed: std::cout is by default buffered and it sounds as if the buffer isn't filled. The easiest way to quickly fix this is to set the stream to always flush:

std::cout << std::unitbuf;

Often std::cerr is used for debugging instead as this stream as the std::ios_base::unitbuf set by default. Alternatively you can flush the stream explicitly:

std::cout << std::flush;

I didn't have a look at you algorithm, though.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380