#include "segment.h"

char VERSION[] = "Ford-Fulkerson algorithm, with basic depth-first search";

arc* get_arc(graph* G, size_t from, size_t to)
{
    UNUSED(G);
    UNUSED(from);
    UNUSED(to);
    return NULL;
}

// Pred is the spanning tree computed during the search
// Seen is the bitmap with all visited nodes
int find_path(graph* G, size_t Pred[], char Seen[])
{
    // reset Pred and Seen
    for (size_t k = 0; k < G->nb_nodes; k++) {
        Seen[k] = 0;
        Pred[k] = NULL_NODE;
    }

    // initialize the todo queue
    stack* todo = new_stack();
    push_stack(todo, G->source);

    // ...
    // ...

    // free the todo queue
    free_stack(todo);
    return 0;
}

int find_bottleneck(graph* G, const size_t Pred[])
{
    UNUSED(G);
    UNUSED(Pred);
    return 0;
}

void augment_flow(graph* G, const size_t Pred[], int flow)
{
    UNUSED(G);
    UNUSED(Pred);
    UNUSED(flow);
}

int maxflow(graph* G, char Seen[])
{
    // allocate the Pred array
    size_t* Pred = malloc(G->nb_nodes * sizeof(size_t));

    int path_counter = 0;
    int flow_counter = 0;
    // while there are augmenting path, use them to increase the global flow
    // (Ford-Fulkerson algorithm)
    while (find_path(G, Pred, Seen)) {
        path_counter += 1;
        if (VERBOSITY == 3) {
            LOG(3, ".");
        }
        // find weight of augmenting path
        int flow = find_bottleneck(G, Pred);
        flow_counter += flow;
        LOG(4, ">>>> augmenting path #%i of weight %i\n", path_counter, flow);

        // update flow with augmenting path
        augment_flow(G, Pred, flow);
        LOG(4, "     current flow is %i\n", flow_counter);
    }
    if (VERBOSITY == 3) {
        LOG(3, "\n");
    }

    LOG(2, ">> %i augmenting path were found\n", path_counter);

    // deallocate Pred array
    free(Pred);
    return flow_counter;
}

int main_test(graph* G, size_t argc, char* argv[])
{
    // prevent unused parameter warning for empty function
    UNUSED(G);
    UNUSED(argc);
    UNUSED(argv);

    return 0;
}
// vim: textwidth=120 foldmethod=indent
