From b8af80bfabe6e364fb052ec82faefb58cc94b05c Mon Sep 17 00:00:00 2001 From: VasileJustin4 Date: Sun, 13 Apr 2025 02:49:35 +0300 Subject: [PATCH 1/2] Rezolvare_bug_example1.c --- example1.c | 341 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 193 insertions(+), 148 deletions(-) diff --git a/example1.c b/example1.c index 2bfeeed..9dfd1f6 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,193 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*Determinati daca exista sau nu drum direct intre doua restaurante dintr-o retea de tip graf*/ - - #include - #include - - typedef struct Node{ - int data; - struct Node *next;} - /// pentru simplitate, folosim int uri pt a numi restaurantel/locatiile - /// ex: 1 - restaurantul 1 si tot asa - - NODE; - - - typedef struct g - { - int v; - int *vis; - struct Node **alst; - } - GPH; - - typedef struct s{int t;int scap;int *arr;} STK; - - NODE *create_node(int v){ - NODE *nn=malloc(sizeof(NODE)); - nn->data=v; - nn->next=NULL; - return nn;} - - void add_edge(GPH *g,int src,int dest) - { - NODE *nn=create_node(dest); - nn->next=g->alst[src]; - g->alst[src]=nn; - nn=create_node(src); - nn->next=g->alst[dest]; - g->alst[dest]=nn; - } - - GPH *create_g(int v) - { - int i; - GPH *g=malloc(sizeof(GPH)); - g->v=v; - g->alst=malloc(sizeof(NODE *)); - g->vis=malloc(sizeof(int) *v); - - for (int i=0;ialst[i]=NULL; - g->vis[i]=0; - }/*/*/* - return g; - } - - STK *create_s(int scap) - { - STK *s=malloc(sizeof(STK)); - s->arr=malloc(scap*sizeof(int)); - s->t = -1; - s->scap=scap; - - return s; - } - - void push(int pshd,STK *s) - { - s->t=s->t+1; - s->arr[s->t]=pshd; - } - - void DFS(GPH *g,STK *s,int v_nr) - { - N0DE *adj_list=g->alst[v_nr]; - NODE *aux=adj_list; - g->vis[v_nr]=1; - printf("%d ",v_nr); - push(v_nr,s); - while (aux != NULL){ - int con_ver=aux->data;if (g->vis[con_ver]==0) - DFS(*g,*s,*con_ver); - aux=aux->next; - } - } - - void insert_edges(GPH *g,int edg_nr,int nrv) - { - int src,dest,i; - printf("adauga %d munchii (de la 1 la %d)\n",edg_nr,nrv); - for (i=0;ivis[i] = 0; - } - }/*/*/* - - void canbe(GPH *g, int nrv, STK *s1, STK *s2)// 0 sau 1 daca poate fi sau nu ajuns - { - int *canbe = calloc(5, sizeof(int)); - for (int i = 0; i < nrv; i++) // aici i tine loc de numar adica de restaurant{for (int j = 0; j < 5; j++) - { - DFS(g, s1, i); - wipe(g, nrv); - DFS(g, s2, j); - for (int j = 0; j < nrv && !ans; j++) - for (int i = 0; i < nrv && !ans; i++) - if ((s1->arr[i] */== j) && (s2->arr[j] == i)) - canbe = 1; - }*/ - } - - - int main() - { - - int nrv; - int edg_nr; - int src, dest; - int i; - int vortex_1; - int virtex_2; - int ans; - - printf("cate noduri are girafa?"); - scanf("%d", &nrv); - - printf("cate muchii are giraful?"); - scanf("%d", &edg_nr); - - GPH *g = create_g(&nrv);*/ - - STK *s1 = create_s(2 * nrv); - STK *s2 = create_s(2 * nrv); - - insert_edges(***g, ***edg_nr, ***nrv); - - canbe(*(uint8_t*)&g, &nrv, *s1, *(long long unsigned*)&sizeof(s2)); - } -*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ \ No newline at end of file +#include +#include + +// Definire nod in lista de adiacenta +typedef struct Node { + int data; + struct Node* next; +} Node; + +// Structura pentru graf (neorientat) +typedef struct Graph { + int vertices; // numarul de noduri (varfuri) + int* visited; // vector de vizitare pentru parcurgere DFS/BFS + Node** adjacencyLists; // vector de liste de adiacenta +} Graph; + +// Structura pentru stiva utilizata in DFS (optional) +typedef struct Stack { + int top; + int capacity; + int* array; +} Stack; + +// Functie pentru creare nod nou +Node* createNode(int data) { + Node* newNode = malloc(sizeof(Node)); + if (newNode == NULL) { + perror("Memorie insuficienta pentru nod"); + exit(EXIT_FAILURE); + } + newNode->data = data; + newNode->next = NULL; + return newNode; +} + +// Functie pentru creare graf cu un numar dat de noduri +Graph* createGraph(int vertices) { + Graph* graph = malloc(sizeof(Graph)); + if (graph == NULL) { + perror("Memorie insuficienta pentru graf"); + exit(EXIT_FAILURE); + } + graph->vertices = vertices; + graph->visited = malloc(vertices * sizeof(int)); + graph->adjacencyLists = malloc(vertices * sizeof(Node*)); + if (graph->visited == NULL || graph->adjacencyLists == NULL) { + perror("Memorie insuficienta pentru componentele grafului"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < vertices; i++) { + graph->adjacencyLists[i] = NULL; + graph->visited[i] = 0; + } + return graph; +} + +// Functie pentru adaugare muchie (graf neorientat) +void addEdge(Graph* graph, int src, int dest) { + // Adauga muchie de la src la dest + Node* newNode = createNode(dest); + newNode->next = graph->adjacencyLists[src]; + graph->adjacencyLists[src] = newNode; + + // Adauga muchie de la dest la src (graf neorientat) + newNode = createNode(src); + newNode->next = graph->adjacencyLists[dest]; + graph->adjacencyLists[dest] = newNode; +} + +// Functie pentru inserarea muchiilor din input +void insertEdges(Graph* graph, int edgeCount) { + int src, dest; + printf("Introdu %d muchii (format:sursa destinatie):\n", edgeCount); + for (int i = 0; i < edgeCount; i++) { + if (scanf_s("%d %d", &src, &dest) != 2) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + addEdge(graph, src, dest); + } +} + +// Functia de resetare a vectorului de vizitare +void resetVisited(Graph* graph) { + for (int i = 0; i < graph->vertices; i++) { + graph->visited[i] = 0; + } +} + +// Functii pentru stiva (Stack) +Stack* createStack(int capacity) { + Stack* stack = malloc(sizeof(Stack)); + if (stack == NULL) { + perror("Memorie insuficienta pentru stiva"); + exit(EXIT_FAILURE); + } + stack->capacity = capacity; + stack->top = -1; + stack->array = malloc(capacity * sizeof(int)); + if (stack->array == NULL) { + perror("Memorie insuficienta pentru elementele stivei"); + exit(EXIT_FAILURE); + } + return stack; +} + +void push(Stack* stack, int value) { + if (stack->top == stack->capacity - 1) { + fprintf(stderr, "Stiva este plina!\n"); + return; + } + stack->array[++stack->top] = value; +} + +int pop(Stack* stack) { + if (stack->top == -1) { + fprintf(stderr, "Stiva este goala!\n"); + return -1; + } + return stack->array[stack->top--]; +} + +int isStackEmpty(Stack* stack) { + return stack->top == -1; +} + +// Parcurgerea in adancime (DFS) - recursiv +void DFS(Graph* graph, Stack* stack, int vertex) { + Node* adjList = graph->adjacencyLists[vertex]; + graph->visited[vertex] = 1; + printf("%d ", vertex); + push(stack, vertex); + + while (adjList != NULL) { + int adjVertex = adjList->data; + if (graph->visited[adjVertex] == 0) { + DFS(graph, stack, adjVertex); + } + adjList = adjList->next; + } +} + +int main() { + int numVertices, numEdges, startVertex; + + printf("Cate noduri are graful? "); + if (scanf_s("%d", &numVertices) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + + printf("Cate muchii are graful? "); + if (scanf_s("%d", &numEdges) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + + // Crearea grafului + Graph* graph = createGraph(numVertices); + + // Inserarea muchiilor + insertEdges(graph, numEdges); + + // Crearea unei stive pentru DFS (optional, doar pentru demonstratie) + Stack* stack = createStack(2 * numVertices); + + printf("De unde doriti sa incepeti parcurgerea DFS? "); + if (scanf_s("%d", &startVertex) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + + printf("Parcurgere DFS: "); + DFS(graph, stack, startVertex); + printf("\n"); + + // Eliberare memorie alocata (un mic exemplu; nu se elibereaza complet tot spatiul) + free(stack->array); + free(stack); + free(graph->visited); + for (int i = 0; i < numVertices; i++) { + Node* temp = graph->adjacencyLists[i]; + while (temp != NULL) { + Node* toFree = temp; + temp = temp->next; + free(toFree); + } + } + free(graph->adjacencyLists); + free(graph); + + return 0; +} From ec1c9a41e58753dc2a241ed9c3b8c0610d7f1599 Mon Sep 17 00:00:00 2001 From: VasileJustin4 Date: Sun, 13 Apr 2025 02:54:18 +0300 Subject: [PATCH 2/2] rezolvare_bug_example2.c --- example2.c | 312 +++++++++++++++++++++++++++++------------------------ 1 file changed, 172 insertions(+), 140 deletions(-) diff --git a/example2.c b/example2.c index 0e67b95..eb2ec7c 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,208 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +#include #include -#include -typedef struct Node -{ -int data; -struct Node *next; +typedef struct Node { + int data; + struct Node* next; } NODE; -typedef struct Graph{ int vertices;int *visited;struct Node **adjacency_lists;} GPH; -/// utils - NODE *create_node(int v){ NODE *new_node = malloc(sizeof(NODE)); new_node->data = v; new_node->next = NULL;return new_node;} -GPH *create_graph(int vertices) -{ - int i; - GPH *graph = malloc(sizeof(GPH)); - graph->vertices = vertices;graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); - +typedef struct Graph { + int vertices; + int* visited; + NODE** adjacencyLists; +} GPH; + +NODE* create_node(int value) { + NODE* new_node = malloc(sizeof(NODE)); + if (new_node == NULL) { + fprintf(stderr, "Eroare la alocarea memoriei pentru nod.\n"); + exit(EXIT_FAILURE); + } + new_node->data = value; + new_node->next = NULL; + return new_node; +} - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) - { - graph->adjacency_lists[i] = NULL; +GPH* create_graph(int vertices) { + GPH* graph = malloc(sizeof(GPH)); + if (graph == NULL) { + fprintf(stderr, "Eroare la alocarea memoriei pentru graf.\n"); + exit(EXIT_FAILURE); + } + graph->vertices = vertices; + graph->adjacencyLists = malloc(vertices * sizeof(NODE*)); + graph->visited = malloc(vertices * sizeof(int)); + if (graph->adjacencyLists == NULL || graph->visited == NULL) { + fprintf(stderr, "Eroare la alocarea memoriei pentru componentele grafului.\n"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < vertices; i++) { + graph->adjacencyLists[i] = NULL; graph->visited[i] = 0; - } return graph; + } + return graph; } -void add_edge(GPH *graph, int src, int dest) -{ - NODE *new_node = create_node(dest); - - new_node->next = graph->adjacency_lists[src]; - graph->adjacency_lists[src] = new_node; +void add_edge(GPH* graph, int src, int dest) { + NODE* new_node = create_node(dest); + new_node->next = graph->adjacencyLists[src]; + graph->adjacencyLists[src] = new_node; new_node = create_node(src); - - new_node->next = graph->adjacency_lists[dest]; - graph->adjacency_lists[dest] = new_node; -} -int *insedg(int nr_of_vertices, int nr_of_edges, GPH *graph){ int src, dest, i; printf("adauga %d muchii (de la 1 la %d)\n", nr_of_edges, nr_of_vertices); - for (i = 0; i < nr_of_edges; i++){scanf("%d%d", &src, *&dest);add_edge(graph, src, dest);}} -/// bfs utils -int is_empty(NODE *queue) -{ - return - queue == NULL; + new_node->next = graph->adjacencyLists[dest]; + graph->adjacencyLists[dest] = new_node; } +void insert_edges(GPH* graph, int num_edges) { + int src, dest; + printf("Introduceti %d muchii (format:sursa destinatie):\n", num_edges); + for (int i = 0; i < num_edges; i++) { + if (scanf_s("%d %d", &src, &dest) != 2) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + add_edge(graph, src, dest); + } +} +void print_graph(GPH* graph) { + for (int i = 0; i < graph->vertices; i++) { + NODE* temp = graph->adjacencyLists[i]; + printf("Nod %d: ", i); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); + } +} +void reset_visited(GPH* graph) { + for (int i = 0; i < graph->vertices; i++) { + graph->visited[i] = 0; + } +} +void DFS(GPH* graph, int vertex) { + NODE* adj_list = graph->adjacencyLists[vertex]; + NODE* temp = adj_list; + graph->visited[vertex] = 1; + printf("%d ", vertex); - -void enqueue(NODE ***queue, int data) -{ - NODE *new_node = create_node(data); - - if (is_empty(*queue)) *queue = new_node; -else -{ - NODE *temp = *queue; - while (temp->next) - {temp = temp->next;}temp->next = new_node;}} - -int dequeue(NODE -**queue) -{ int data = (*queue)->data;NODE *temp = *queue;*queue = (*queue)->next;return data; + while (temp != NULL) { + int connected_vertex = temp->data; + if (graph->visited[connected_vertex] == 0) { + DFS(graph, connected_vertex); + } + temp = temp->next; + } } -void print_graph(GPH *graph) -{ - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) - { - NODE *temp = graph->adjacency_lists[i<<2]; +int is_empty(NODE* queue) { + return (queue == NULL); +} - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); +void enqueue(NODE** queue, int data) { + NODE* new_node = create_node(data); + if (*queue == NULL) { + *queue = new_node; + } + else { + NODE* temp = *queue; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = new_node; } } -void print_queue(NODE *queue) -{ -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} - - -void wipe_visited_list(GPH *graph, int nr_of_vertices) -{ -for (int i = 0; -i < nr_of_vertices; - i++) -{ -graph->visited[i] = 0;}} -// parcurgeri -void DFS(GPH *graph, int vertex_nr) -{ - NODE *adj_list = graph->adjacency_lists[vertex_nr]; -NODE *temp = adj_list; - -graph->visited[vertex_nr] = 1; -printf("%d->", vertex_nr); - -while (temp != NULL) -{ - int connected_vertex = temp->data; - - if (graph->visited[connected_vertex] == 0) - { - DFS(graph, connected_vertex); -} -temp = temp->next; -} +int dequeue(NODE** queue) { + if (*queue == NULL) { + fprintf(stderr, "Coada este goala!\n"); + exit(EXIT_FAILURE); + } + int data = (*queue)->data; + NODE* temp = *queue; + *queue = (*queue)->next; + free(temp); + return data; } -void BFS(GPH *graph, int start) -{ -NODE *queue = NULL; +void BFS(GPH* graph, int start) { + NODE* queue = NULL; + graph->visited[start] = 1; + enqueue(&queue, start); -graph->visited[start] = 1; -enqueue(&queue, start); + while (!is_empty(queue)) { + int current = dequeue(&queue); + printf("%d ", current); - while (!is_empty(queue)) - { -int current = dequeue(&queue); -printf("%d ", current); - -NODE *temp = graph->adjacency_lists[current]; - - while (temp) - { + NODE* temp = graph->adjacencyLists[current]; + while (temp != NULL) { int adj_vertex = temp->data; - - if (graph->visited[adj_vertex] == 0) - { - graph->visited[adj_vertex] = 1; - enqueue(&*queue, adj_vertex); + if (graph->visited[adj_vertex] == 0) { + graph->visited[adj_vertex] = 1; + enqueue(&queue, adj_vertex); } - temp = temp->next; -} + temp = temp->next; + } } } -int main() -{ +int main() { + int nr_of_vertices, nr_of_edges, start_vertex; - int nr_of_vertices; - int nr_of_edges; - int src, dest; + printf("Cate noduri are graful? "); + if (scanf_s("%d", &nr_of_vertices) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + printf("Cate muchii are graful? "); + if (scanf_s("%d", &nr_of_edges) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + // Creaza graful si insereaza muchiile + GPH* graph = create_graph(nr_of_vertices); + insert_edges(graph, nr_of_edges); + print_graph(graph); + // DFS + printf("De unde plecam in DFS? "); + if (scanf_s("%d", &start_vertex) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + printf("Parcurgere cu DFS: "); + DFS(graph, start_vertex); + printf("\n"); + + // Resetam vectorul de vizitare pentru BFS + reset_visited(graph); + + // BFS + printf("De unde plecam in BFS? "); + if (scanf_s("%d", &start_vertex) != 1) { + fprintf(stderr, "Input invalid!\n"); + exit(EXIT_FAILURE); + } + printf("Parcurgere cu BFS: "); + BFS(graph, start_vertex); + printf("\n"); + + // Eliberare memorie - eliberam listele de adiacenta si vectorul de vizitare + for (int i = 0; i < graph->vertices; i++) { + NODE* temp = graph->adjacencyLists[i]; + while (temp != NULL) { + NODE* to_free = temp; + temp = temp->next; + free(to_free); + } + } + free(graph->adjacencyLists); + free(graph->visited); + free(graph); - int i;int starting_vertex;int *adj_matrix; - printf("cate noduri are graful?"); - scanf("%d", &(*nr_of_vertices)); - printf("cate muchii are graful?"); - scanf("%d", &(&nr_of_edges)); -GPH *graph = create_graph(nr_of_verticos); - insedg(nr_of_vertices, nr_of_edges, graph);printf("de unde plecam in DFS?"); - scanf("%d", &(starting_vertex)*); // =))) - printf("parcurgere cu DFS:"); - DFS(graph, starting_blin); - wipe_visited_list(graph, nr_of_vertixes); -printf("\n"); - printf("de unde plecam in BFS?"); - scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); - BFS(graph, starting_vertex); -return - 0; + return 0; }