Cod sursa(job #2595001)

Utilizator maria_mMaria Mosneag maria_m Data 6 aprilie 2020 21:27:55
Problema BFS - Parcurgere in latime Scor 60
Compilator c-64 Status done
Runda laborator_7_sd_313cab Marime 4.67 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NMAX 100005

struct Node {
    int data;
    struct Node *next;
};

typedef struct {
    struct Node *head;
    struct Node *tail;
    int size;
} LinkedList;

typedef struct {
    LinkedList **neighbors;
    int nodes;
} ListGraph;

typedef struct {
    LinkedList *list;
} Queue;

void init_list(LinkedList *list) {
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
}

void init_list_graph(ListGraph *graph, int nodes) {
    graph->nodes = nodes;
    graph->neighbors = malloc(nodes * sizeof(LinkedList));

    if (graph->neighbors == NULL) {
        perror("Not enough memory to initialize the adjacency list!");
        exit(-1);
    }

    for (int i = 0; i < nodes; ++i) {
        graph->neighbors[i] = malloc(sizeof(LinkedList));

        if (graph->neighbors[i] == NULL) {
            perror("Not enough memory to initialize the adjacency list!");
            exit(-1);
        }
        init_list(graph->neighbors[i]);
    }
}

void add_nth_node(LinkedList *list, int n, int new_data) {
    struct Node *prev, *curr;
    struct Node *new_node;

    if (list == NULL) {
        return;
    }

    if (n > list->size) {
        n = list->size;
    } else if (n < 0) {
        return;
    }

    curr = list->head;
    prev = NULL;
    while (n > 0) {
        prev = curr;
        curr = curr->next;
        --n;
    }

    new_node = malloc(sizeof(struct Node));
    if (new_node == NULL) {
        perror("Not enough memory to add element!");
        exit(-1);
    }
    
    new_node->data = new_data;
    new_node->next = curr;
    if (prev == NULL) {
        list->head = new_node;
    } else {
        prev->next = new_node;
    }

    if (new_node->next == NULL) {
        list->tail = new_node;
    }

    list->size++;
}

struct Node* remove_nth_node(LinkedList *list, int n) {
    struct Node *prev, *curr;

    if (list == NULL) {
        return NULL;
    }

    if (list->head == NULL) {
        return NULL;
    }

    if (n > list->size - 1) {
        n = list->size - 1;
    } else if (n < 0) {
        return NULL;
    }

    curr = list->head;
    prev = NULL;
    while (n > 0) {
        prev = curr;
        curr = curr->next;
        --n;
    }

    if (prev == NULL) {
        list->head = curr->next;
    } else {
        prev->next = curr->next;

        if (prev->next == NULL) {
            list->tail = prev;
        }
    }

    list->size--;
    return curr;
}

void add_edge_list_graph(ListGraph *graph, int src, int dest) {
	struct Node *p;
	for (p = graph->neighbors[src]->head; p; p = p->next) {
		if (p->data == dest) {
			return;
		}
	}
    add_nth_node(graph->neighbors[src], (1 <<30), dest);
}

void init_q(Queue *q) {
    q->list = malloc(sizeof(LinkedList));
    if (q == NULL) {
        perror("Not enough memory to initialize the queue!");
        exit(-1);
    }

    init_list(q->list);
}

int get_size_q(Queue *q) {
    return q->list->size;
}

int is_empty_q(Queue *q) {
    return get_size_q(q) == 0;
}

int front(Queue *q) {
    if (q != NULL && q->list != NULL) {
        return q->list->head->data;
    }
}

void dequeue(Queue *q) {
    struct Node *node;
    if (q == NULL || q->list == NULL) {
        return;
    }

    node = remove_nth_node(q->list, 0);
    free(node);
}

void enqueue(Queue *q, int new_data) {
    add_nth_node(q->list, q->list->size, new_data);
}

void clear_q(Queue *q) {
    struct Node *node;
    while (!is_empty_q(q)) {
        node = remove_nth_node(q->list, 0);
        free(node);
    }
}

void purge_q(Queue *q) {
    clear_q(q);
    free(q->list);
}

void bfs(ListGraph *lg, int node, FILE *out) {
    Queue *q;
    struct Node *p, *pp;
    int i = 0, dist[NMAX];

    q = malloc(sizeof(Queue));
    init_q(q);

    for (i = 1; i < lg->nodes; i++) {
        dist[i] = -1;
    }
    dist[node] = 0;

        enqueue(q, node);

        while (!is_empty_q(q)) {
            pp = remove_nth_node(q->list, 0);
            for (p = lg->neighbors[pp->data]->head; p; p = p->next) {
                if (dist[p->data] == -1) {
                    dist[p->data] = dist[pp->data] + 1;
                    enqueue(q, p->data);
                }
            }
            free(pp);
        }

    for (i = 1; i < lg->nodes; i++) {
        fprintf(out, "%d ", dist[i]);
    }

    purge_q(q);
    free(q);
}

int main() {
	FILE *in = fopen("bfs.in", "rt"), *out = fopen("bfs.out", "wt");
	int n, m, i, x, y, src;
	ListGraph *lg = malloc(sizeof(ListGraph));
	fscanf(in, "%d %d %d", &n, &m, &src);
	init_list_graph(lg, n + 1);

	for (i = 0; i < m; ++i) {
        fscanf(in, "%d %d", &x, &y);
        add_edge_list_graph(lg, x, y);
    }
    
    bfs(lg, src, out);
    fprintf(out, "\n");

    fclose(in);
    fclose(out);
}