Example #1
0
int bfs(int s,int a, struct vertex *adj) {
    int i, u, j, l, v, w, k;

    struct queue *queue = malloc(sizeof(struct queue));
    struct queue *queueHyphen = malloc(sizeof(struct queue));
    queueInit(queue);
    queueInit(queueHyphen);
#pragma omp parallel for shared(adj)
    for (i = 0; i < adj[s].indeg; i++) { //par begin //compare ok
        u = adj[s].ins[i].u;
        j = adj[s].ins[i].iuout;
        eliminate(adj, u, j);                       //compare ok
    } //par end                                     //compare ok
    l = 0;                                          //compare ok
    adj[s].traversal = a;                           //compare ok
    adj[s].distance = l;                            //compare ok //before was adj[s].distance = l //compare with php
    struct queueNode *queueNode1 = malloc(sizeof(struct queueNode));
    enque(s, queue, queueNode1);                               //todo
//    printf("l74:queue -- should've queued %i\n", s);
    queueReset(queueHyphen);                        //todo
//    printf("l76:queueHypen -- should've reset");
    while (true) {  //repeat 1 begin
        l = l + 1;                                  //compare ok
        while (true) {  //repeat 2 begin
            u = deque(queue);                      //todo
//            printf("l81:queue -- should've dequeued %i\n", u);
            while (adj[u].first < adj[u].outdeg) {  //compare ok
                i = adj[u].first;                   //compare ok
                v = adj[u].out[i];                  //compare ok
#pragma omp parallel for shared(adj)
                for (j = 0; j < adj[v].indeg; j++) {  //par begin //compare ok
                    w = adj[v].ins[j].u;
                    k = adj[v].ins[j].iuout;
                    eliminate(adj, w, k);           //compare ok
                } //par end                         //compare ok
                a = a + 1;                          //compare ok
                adj[v].traversal = a;               //compare ok
                adj[v].distance = l;                //compare ok
                adj[v].parent = u;                  //compare ok
//                printf(":%i parent of %i\n", u, v);  //comment me
                struct queueNode *queueNode2 = malloc(sizeof(struct queueNode));
                enque(v, queueHyphen, queueNode2);              //todo
//                printf("l103:queueHyphen -- should've queued %i\n", v);
            } //end while                           //compare ok
            if (isQueueEmpty(queue)) {  //until
                break;
            }
        } //repeat 2 end                            //compare ok
        *queue = *queueHyphen;
        queueInit(queueHyphen);
//        printf("l121:queue = queueHyphen\n");
        if (isQueueEmpty(queue)) {
            break;
        }
    } //end repeat 1                                //compare ok
    return EXIT_SUCCESS;
}
Example #2
0
int main()
{
    int i, j, k, nNodes, nEdges, initialNode, finalNode, nCritAreas, nCritPoints, criticPoint, *results, trash = 0;

    trash = scanf(" %d %d", &nNodes, &nEdges);

    graph.nNodes = nNodes;
    graph.neighbors = (int **) malloc(sizeof(int *) * nNodes);
    graph.nNeighbors = (int *) malloc(sizeof(int) * nNodes);
    graph.nodesInSearch = (int **) malloc(sizeof(int *) * nNodes);

    queueInit(nNodes);

    parent = (int *) malloc(sizeof(int) * nNodes);
    visited = (int *) malloc(sizeof(int) * nNodes);

    for(i=0; i<nNodes; i++) {
        graph.nNeighbors[i] = 0;
        graph.neighbors[i] = NULL;
        graph.nodesInSearch[i] = NULL;
        parent[i] = -1;
        visited[i] = -1;
    }

    for (i=0; i<nEdges; i++) {
        trash = scanf(" %d %d", &initialNode, &finalNode);

        graph.neighbors[initialNode] = realloc(graph.neighbors[initialNode],
            sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.neighbors[initialNode][graph.nNeighbors[initialNode]] = finalNode;

        graph.neighbors[finalNode] = realloc(graph.neighbors[finalNode],
            sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.neighbors[finalNode][graph.nNeighbors[finalNode]] = initialNode;

        graph.nodesInSearch[initialNode] = realloc(graph.nodesInSearch[initialNode],
                                                   sizeof(int) * (graph.nNeighbors[initialNode] + 1));
        graph.nodesInSearch[initialNode][graph.nNeighbors[initialNode]] = 0;


        graph.nodesInSearch[finalNode] = realloc(graph.nodesInSearch[finalNode],
                                                   sizeof(int) * (graph.nNeighbors[finalNode] + 1));
        graph.nodesInSearch[finalNode][graph.nNeighbors[finalNode]] = 0;

        graph.nNeighbors[finalNode]++;
        graph.nNeighbors[initialNode]++;
    }

    trash = scanf(" %d", &nCritAreas);

    results = (int *) malloc(sizeof(int) * nCritAreas);
    criticPoints = (int **) malloc (sizeof(int *) * nCritAreas);
    nCriticPoints = (int *) malloc(sizeof(int) * nCritAreas);

    for(i = 0; i < nCritAreas; i++) {
        trash = scanf(" %d", &nCritPoints);
        criticPoints[i] = (int *) malloc(sizeof(int) * nCritPoints);
        nCriticPoints[i] = nCritPoints;
        for(j = 0; j < nCritPoints; j++) {
            trash = scanf( " %d", &criticPoint);
            criticPoints[i][j] = criticPoint;
        }
    }

    for (k = 0; k < nCritAreas; k++) {
        results[k] = INT_MAX;
        for (i = 0; i < nCriticPoints[k]; i++) {
            for (j = i+1; j < nCriticPoints[k]; j++) {
                trash = 0;
                queueReset();
                trash = fordFulkerson(criticPoints[k][i], criticPoints[k][j]);
                results[k] = results[k] < trash ? results[k] : trash;
            }

        }
    }


    for (i = 0; i < nCritAreas; i++) {
        printf("%d\n", results[i]);
    }

    /* Frees */
    for (i=0; i<graph.nNodes; i++) {
        free(graph.neighbors[i]);
        free(graph.nodesInSearch[i]);
    }

    free(graph.neighbors);
    free(graph.nNeighbors);
    free(graph.nodesInSearch);
    free(parent);
    free(visited);
    queueFree();
    free(results);
    for (i = 0; i < nCritAreas; i++) { free(criticPoints[i]); }
    free(criticPoints);
    free(nCriticPoints);
    return 0;
}