From 4ff4be378ce213205326ee7688bf9494fd1fcf74 Mon Sep 17 00:00:00 2001 From: vacaruraisa Date: Sat, 12 Apr 2025 20:11:02 +0300 Subject: [PATCH] cod rezolvat --- example1.c | 327 +++++++++++++++++++++++++++++------------------------ example2.c | 228 +++++++++++++++++++------------------ 2 files changed, 299 insertions(+), 256 deletions(-) diff --git a/example1.c b/example1.c index 2bfeeed..e035cfe 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,179 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 + +/* Determinati daca exista sau nu drum direct intre doua restaurante dintr-o retea de tip graf */ +/* pentru simplitate, folosim int-uri pt a numi restaurantel/locatiile */ +/* ex: 1 - restaurantul 1 si tot asa */ + +#include +#include + +typedef struct Node +{ + int data; + struct Node *next; +} 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) +{ + GPH *g = malloc(sizeof(GPH)); + g->v = v; + g->alst = malloc(v * sizeof(NODE *)); + g->vis = malloc(v * sizeof(int)); + + for (int i = 0; i < v; i++) + { + g->alst[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) +{ + NODE *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; + printf("adauga %d munchii (de la 1 la %d)\n", edg_nr, nrv); + for (int i = 0; i < edg_nr; i++) + { + scanf("%d%d", &src, &dest); + add_edge(g, src, dest); + } +} + +void wipe(GPH *g, int nrv) +{ + for (int i = 0; i < nrv; i++) + { + g->vis[i] = 0; + } +} + +/* 0 sau 1 daca poate fi sau nu ajuns */ +int canbe_reached(GPH *g, int src, int dest) +{ + STK *temp_stack = create_s(g->v); + DFS(g, temp_stack, src); + + for (int i = 0; i <= temp_stack->t; i++) + { + if (temp_stack->arr[i] == dest) + { + free(temp_stack->arr); + free(temp_stack); + return 1; + } + } + + free(temp_stack->arr); + free(temp_stack); + return 0; +} + +int main() +{ + int nrv; + int edg_nr; + int src, dest; + int i; + int vortex_1; + int virtex_2; + int ans; + + printf("cate noduri are graful?"); // era graful nu girafa :) + scanf("%d", &nrv); + + printf("cate muchii are graful?"); + 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); + + printf("de la ce restaurant pornim?\n"); + scanf("%d", &vortex_1); + printf("si unde vrem sa ajungem?\n"); + scanf("%d", &virtex_2); + + wipe(g, nrv); + + ans = canbe_reached(g, vortex_1, virtex_2); + + if (ans == 1) + { + printf("Se poate ajunge de la %d la %d\n", vortex_1, virtex_2); + } + else + { + printf("Nu se poate ajunge de la %d la %d\n", vortex_1, virtex_2); + } + + return 0; +} diff --git a/example2.c b/example2.c index 0e67b95..0ce40b8 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,188 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +// parcurgerge graf cu DFS/BFS +#include #include -#include typedef struct Node { -int data; -struct Node *next; + 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) + +typedef struct Graph { - int i; - GPH *graph = malloc(sizeof(GPH)); - graph->vertices = vertices;graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + int vertices; + int *visited; + NODE **adjacency_lists; +} GPH; +/// creeaza un nod +NODE *create_node(int v) +{ + NODE *new_node = malloc(sizeof(NODE)); + new_node->data = v; + new_node->next = NULL; + return new_node; +} +/// creeaza graful +GPH *create_graph(int vertices) +{ + GPH *graph = malloc(sizeof(GPH)); + graph->vertices = vertices; + graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + graph->visited = calloc(vertices, sizeof(int)); - graph->visited = malloc(sizeof(int) * vertices); for (int i = 0; i < vertices; i++) { - graph->adjacency_lists[i] = NULL; - graph->visited[i] = 0; - } return graph; + graph->adjacency_lists[i] = NULL; + } + return graph; } + +/// adauga muchie 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; 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; -} - - - - - - -void enqueue(NODE ***queue, int data) +/// functie care citeste muchiile +void insedg(int nr_of_vertices, int nr_of_edges, GPH *graph) { - NODE *new_node = create_node(data); + int src, dest; + printf("adauga %d muchii (de la 1 la %d)\n", nr_of_edges, nr_of_vertices); + for (int i = 0; i < nr_of_edges; i++) + { + scanf("%d %d", &src, &dest); + add_edge(graph, src, dest); + } +} - if (is_empty(*queue)) *queue = new_node; -else +/// verificam daca coada e goala +int is_empty(NODE *queue) { - 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; + return queue == NULL; } -void print_graph(GPH *graph) +/// adauga in coada (folosim lista simpla) +void enqueue(NODE **queue, int data) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) + NODE *new_node = create_node(data); + if (*queue == NULL) { - NODE *temp = graph->adjacency_lists[i<<2]; - - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); + *queue = new_node; + } + else + { + NODE *temp = *queue; + while (temp->next) + temp = temp->next; + temp->next = new_node; } } -void print_queue(NODE *queue) +/// scoate din coada +int dequeue(NODE **queue) { -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} - + if (*queue == NULL) + return -1; + int data = (*queue)->data; + NODE *temp = *queue; + *queue = (*queue)->next; + free(temp); + return data; +} -void wipe_visited_list(GPH *graph, int nr_of_vertices) -{ -for (int i = 0; -i < nr_of_vertices; - i++) -{ -graph->visited[i] = 0;}} -// parcurgeri +/// DFS (depth-first search) 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); + NODE *adj_list = graph->adjacency_lists[vertex_nr]; + NODE *temp = adj_list; -while (temp != NULL) -{ - int connected_vertex = temp->data; + graph->visited[vertex_nr] = 1; + printf("%d->", vertex_nr); - if (graph->visited[connected_vertex] == 0) + while (temp != NULL) { - DFS(graph, connected_vertex); -} -temp = temp->next; -} + int connected_vertex = temp->data; + if (graph->visited[connected_vertex] == 0) + { + DFS(graph, connected_vertex); + } + temp = temp->next; + } } +/// BFS (breadth-first search) void BFS(GPH *graph, int start) { -NODE *queue = NULL; - -graph->visited[start] = 1; -enqueue(&queue, start); + NODE *queue = NULL; + graph->visited[start] = 1; + enqueue(&queue, start); while (!is_empty(queue)) { -int current = dequeue(&queue); -printf("%d ", current); - -NODE *temp = graph->adjacency_lists[current]; + int current = dequeue(&queue); + printf("%d ", current); - while (temp) - { + NODE *temp = graph->adjacency_lists[current]; + while (temp) + { int adj_vertex = temp->data; - if (graph->visited[adj_vertex] == 0) { - graph->visited[adj_vertex] = 1; - enqueue(&*queue, adj_vertex); + graph->visited[adj_vertex] = 1; + enqueue(&queue, adj_vertex); } - temp = temp->next; + temp = temp->next; + } + } } + +/// resetam vectorul de vizite +void wipe_visited_list(GPH *graph, int nr_of_vertices) +{ + for (int i = 0; i < nr_of_vertices; i++) + { + graph->visited[i] = 0; } } int main() { - int nr_of_vertices; int nr_of_edges; - int src, dest; + int starting_vertex; + int starting_blin; // =))) + 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_vertices); - 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:"); + insedg(nr_of_vertices, nr_of_edges, graph); + + printf("de unde plecam in DFS?"); + scanf("%d", &starting_blin); + printf("parcurgere cu DFS: "); DFS(graph, starting_blin); - wipe_visited_list(graph, nr_of_vertixes); -printf("\n"); + + wipe_visited_list(graph, nr_of_vertices); + printf("\n"); + printf("de unde plecam in BFS?"); scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); + printf("parcurgere cu BFS: "); BFS(graph, starting_vertex); -return - 0; + + return 0; }