Cod sursa(job #2594894)

Utilizator ionut_cristian.tacuTacu Ionut Cristian ionut_cristian.tacu Data 6 aprilie 2020 19:04:27
Problema Sortare topologica Scor 0
Compilator c-64 Status done
Runda laborator_7_sd_313cab Marime 5.87 kb


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
    void *data; /* lista ...*/
    struct 
    Node *next;
} Node;

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

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

void add_nth_node(LinkedList *list, int n, void *new_data) {
    Node *prev, *curr;
    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(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) {
        /* Adica n == 0. */
        list->head = new_node;
    } else {
        prev->next = new_node;
    }

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

    list->size++;
}

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

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

    if (list->head == NULL) { /* Lista este goala. */
        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) {
        /* Adica n == 0. */
        list->head = curr->next;
    } else {
        prev->next = curr->next;

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

    list->size--;
    return curr;
}

int get_size(LinkedList *list) {
    if (list == NULL) {
        return -1;
    }

    return list->size;
}

void free_list(LinkedList **pp_list) {
    struct Node *currNode;

    if (pp_list == NULL || *pp_list == NULL) {
        return;
    }

    while(get_size(*pp_list) > 0) {
        currNode = remove_nth_node(*pp_list, 0);
        free(currNode);
    }

    free(*pp_list);
    *pp_list = NULL;
}

typedef struct {
    LinkedList **neighbors;       /* Listele de adiacenta ale grafului */
    int nodes;                    /* Numarul de noduri din graf. */
} ListGraph;

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

    if (graph->neighbors == NULL) {
        perror("Not enough memory to initialize the lsacency 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 lsacency list!");
            exit(-1);
        }
        init_list(graph->neighbors[i]);
    }
}

void add_edge_list_graph(ListGraph *graph, int src, int *dest) {
    add_nth_node(graph->neighbors[src], (1 <<30), dest);
}

int has_edge_list_graph(ListGraph *graph, int src, int dest) {
    Node *head = graph->neighbors[src]->head;
    int crt_node;

    while (head != NULL) {
        crt_node = *(int *)head->data;

        if (crt_node == dest) {
            return 1;
        }
        head = head->next;
    }

    return 0;
}

LinkedList* get_neighbours_list_graph(ListGraph *graph, int node) {
    return graph->neighbors[node];
}

void remove_edge_list_graph(ListGraph *graph, int src, int dest) {
    Node *head = graph->neighbors[src]->head;
    int node_index = 0;
    int crt_node = 0;

    while (head != NULL) {
        crt_node = *(int *)head->data;

        if (crt_node == dest) {
            remove_nth_node(graph->neighbors[src], node_index);
            return;
        }
        head = head->next;
        ++node_index;
    }
}

void clear_list_graph(ListGraph *graph) {
    for (int i = 0; i < graph->nodes; ++i) {
        free_list(&graph->neighbors[i]);
    }
    free(graph->neighbors);
}

#define MAX_NODES 100000

void dfs_list_graph(ListGraph *lg, int node, int *visited, int *t_fin, int *time, int *cnt) {
    LinkedList *ls;
    Node *curr;
    int next_node;
 
    visited[node] = 1;
    (*time)++;
    ls = get_neighbours_list_graph(lg, node);
    curr = ls->head;
 
    while (curr)    // la dfs nu e scos decat t_descop, in rest e la fel
    {
        next_node = *(int*)curr->data;
        if (!visited[next_node] && has_edge_list_graph(lg, node, next_node))
        {
            dfs_list_graph(lg, next_node, visited, t_fin, time, cnt);
        }
        curr = curr->next;
    }
    t_fin[(*cnt)] = node;
    (*cnt)++;
}

void dfs_topo_sort(ListGraph *lg, int *t_fin, FILE *out) {
    int i, *visited, time = 0, cnt = 0;
    visited = malloc((lg->nodes)*sizeof(int));
    for(i=0; i<lg->nodes; i++) {
    	visited[i] = 0;   // toti nevizitati
    }

    for(i=0; i<lg->nodes; i++) {
    	if(!visited[i]) {
    		dfs_list_graph(lg, i, visited, t_fin, &time, &cnt);
    	}
    }

    for (i=lg->nodes-1; i > 0; i--) {
        fprintf(out, "%d ", t_fin[i]+1);
    }
    free(visited);
}

int main() {
    FILE *in, *out;
    char input[] = "sortaret.in", output[] = "sortaret.out";
    int nodes, edges, source, *t_fin;
    int nodes_index[MAX_NODES];
    int x[MAX_NODES], y[MAX_NODES];
    ListGraph *lg = malloc(sizeof(ListGraph));
    in = fopen(input, "r");
    out = fopen(output, "w");
    fscanf(in, "%d %d", &nodes, &edges);
    init_list_graph(lg, nodes);
    t_fin = malloc(nodes*sizeof(int));
    for (int i = 0; i < nodes; ++i) {
        nodes_index[i] = i;
    }
    for (int i = 0; i < edges; ++i) {
        fscanf(in, "%d %d", &x[i], &y[i]);
        x[i]--; y[i]--;
        add_edge_list_graph(lg, x[i], &y[i]);
    }
    dfs_topo_sort(lg, t_fin, out);

    clear_list_graph(lg);
    free(lg); free(t_fin);
    fclose(in); fclose(out);
    return 0;
}