From 50c312b46a9821c6812e01245431bf1c87e6119b Mon Sep 17 00:00:00 2001 From: stefitp Date: Sat, 12 Apr 2025 22:54:48 +0300 Subject: [PATCH 1/2] Update example1.c --- example1.c | 317 ++++++++++++++++++++++++++++------------------------- 1 file changed, 169 insertions(+), 148 deletions(-) diff --git a/example1.c b/example1.c index 2bfeeed..d000eb2 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,169 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 + +// structura pentru un nod din lista de adiacenta +typedef struct node { + int destinatie; + struct node* next; +} node; + +// structura pentru graf +typedef struct graf { + int nr_noduri; + int* vizitat; + node** liste_adiacenta; +} graf; + +// structura pentru stiva +typedef struct stiva { + int varf; + int capacitate; + int* elemente; +} stiva; + +// creeaza un nod nou pentru lista de adiacenta +node* creeaza_nod(int destinatie) { + node* nod_nou = (node*)malloc(sizeof(node)); + nod_nou->destinatie = destinatie; + nod_nou->next = NULL; + return nod_nou; +} + +// creeaza un graf cu nr_noduri noduri +graf* creeaza_graf(int nr_noduri) { + graf* g = (graf*)malloc(sizeof(graf)); + g->nr_noduri = nr_noduri; + g->liste_adiacenta = (node**)malloc(nr_noduri * sizeof(node*)); + g->vizitat = (int*)calloc(nr_noduri, sizeof(int)); + + // initializeaza listele de adiacenta cu null + for (int i = 0; i < nr_noduri; i++) { + g->liste_adiacenta[i] = NULL; + } + return g; +} + +// adauga o muchie neorientata intre sursa si destinatie +void adauga_muchie(graf* g, int sursa, int destinatie) { + // adauga destinatie in lista de adiacenta a sursei + node* nod_nou = creeaza_nod(destinatie); + nod_nou->next = g->liste_adiacenta[sursa]; + g->liste_adiacenta[sursa] = nod_nou; + + // adauga sursa in lista de adiacenta a destinatiei (graf neorientat) + nod_nou = creeaza_nod(sursa); + nod_nou->next = g->liste_adiacenta[destinatie]; + g->liste_adiacenta[destinatie] = nod_nou; +} + +// creeaza o stiva cu capacitate data +stiva* creeaza_stiva(int capacitate) { + stiva* s = (stiva*)malloc(sizeof(stiva)); + s->varf = -1; + s->capacitate = capacitate; + s->elemente = (int*)malloc(capacitate * sizeof(int)); + return s; +} + +// adauga un element in stiva +void push(stiva* s, int valoare) { + if (s->varf < s->capacitate - 1) { + s->elemente[++s->varf] = valoare; + } +} + +// parcurgere dfs pentru a marca nodurile accesibile +void dfs(graf* g, stiva* s, int nod_start) { + g->vizitat[nod_start] = 1; + push(s, nod_start); + + // parcurge vecinii nodului curent + node* aux = g->liste_adiacenta[nod_start]; + while (aux != NULL) { + int vecin = aux->destinatie; + if (!g->vizitat[vecin]) { + dfs(g, s, vecin); + } + aux = aux->next; + } +} + +// reseteaza vectorul de noduri vizitate +void reseteaza_vizite(graf* g) { + for (int i = 0; i < g->nr_noduri; i++) { + g->vizitat[i] = 0; + } +} + +// citeste si adauga muchii in graf +void citeste_muchii(graf* g, int nr_muchii) { + printf("introdu %d muchii (sursa destinatie):\n", nr_muchii); + for (int i = 0; i < nr_muchii; i++) { + int sursa, destinatie; + scanf("%d %d", &sursa, &destinatie); + adauga_muchie(g, sursa, destinatie); + } +} + +// verifica daca exista drum intre nodul start si nodul destinatie +int exista_drum(graf* g, int start, int destinatie) { + stiva* s = creeaza_stiva(g->nr_noduri); + reseteaza_vizite(g); + dfs(g, s, start); + + int rezultat = g->vizitat[destinatie]; + + // elibereaza memoria stivei + free(s->elemente); + free(s); + return rezultat; +} + +// elibereaza memoria ocupata de graf +void elibereaza_graf(graf* g) { + for (int i = 0; i < g->nr_noduri; i++) { + node* aux = g->liste_adiacenta[i]; + while (aux != NULL) { + node* temp = aux; + aux = aux->next; + free(temp); + } + } + free(g->liste_adiacenta); + free(g->vizitat); + free(g); +} + +int main() { + int nr_noduri, nr_muchii; + + // citeste numarul de noduri + printf("cate noduri are graful? "); + scanf("%d", &nr_noduri); + + // citeste numarul de muchii + printf("cate muchii are graful? "); + scanf("%d", &nr_muchii); + + // creeaza graful + graf* g = creeaza_graf(nr_noduri); + + // citeste muchiile + citeste_muchii(g, nr_muchii); + + // verifica drum intre doua noduri + int nod_start, nod_destinatie; + printf("introdu nodurile pentru verificare (start destinatie): "); + scanf("%d %d", &nod_start, &nod_destinatie); + + // afiseaza rezultatul + if (exista_drum(g, nod_start, nod_destinatie)) { + printf("exista drum intre %d si %d\n", nod_start, nod_destinatie); + } else { + printf("nu exista drum intre %d si %d\n", nod_start, nod_destinatie); + } + + // elibereaza memoria + elibereaza_graf(g); + return 0; +} From c2576beba4d22a8d082fad211b125a6820658c31 Mon Sep 17 00:00:00 2001 From: stefitp Date: Sat, 12 Apr 2025 22:55:37 +0300 Subject: [PATCH 2/2] Update example2.c --- example2.c | 262 ++++++++++++++++++++++++++--------------------------- 1 file changed, 130 insertions(+), 132 deletions(-) diff --git a/example2.c b/example2.c index 0e67b95..24e8b23 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,174 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +#include #include -#include -typedef struct Node -{ -int data; -struct Node *next; +// structura pentru nod din lista de adiacenta +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 *)); +// structura pentru graf +typedef struct Graph { + int vertices; + int *visited; + NODE **adjacency_lists; +} GPH; + +// creeaza un nod nou +NODE *create_node(int v) { + NODE *new_node = malloc(sizeof(NODE)); + new_node->data = v; + new_node->next = NULL; + return new_node; +} +// creeaza un graf cu un numar dat de noduri +GPH *create_graph(int vertices) { + GPH *graph = malloc(sizeof(GPH)); + graph->vertices = vertices; + graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + graph->visited = malloc(vertices * sizeof(int)); - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) - { - graph->adjacency_lists[i] = NULL; + for (int i = 0; i < vertices; i++) { + graph->adjacency_lists[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); +// adauga o muchie neorientata intre doua noduri +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; -} - - - - +// insereaza muchii in graf pe baza inputului +void insert_edges(int nr_of_vertices, int nr_of_edges, GPH *graph) { + int src, dest; + printf("adauga %d muchii (de la 0 la %d):\n", nr_of_edges, nr_of_vertices - 1); + for (int i = 0; i < nr_of_edges; i++) { + scanf("%d %d", &src, &dest); + add_edge(graph, src, dest); + } +} +// verifica daca coada e goala +int is_empty(NODE *queue) { + return queue == NULL; +} -void enqueue(NODE ***queue, int data) -{ +// adauga un element la finalul cozii +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 != NULL) { + temp = temp->next; + } + temp->next = new_node; + } +} - if (is_empty(*queue)) *queue = new_node; -else -{ +// scoate si returneaza primul element din coada +int dequeue(NODE **queue) { + int data = (*queue)->data; 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; + *queue = (*queue)->next; + free(temp); + return data; } -void print_graph(GPH *graph) -{ - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) - { - NODE *temp = graph->adjacency_lists[i<<2]; - - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); +// reseteaza vectorul de noduri vizitate +void wipe_visited_list(GPH *graph, int nr_of_vertices) { + for (int i = 0; i < nr_of_vertices; i++) { + graph->visited[i] = 0; } } -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; -} +// parcurgere in adancime (dfs) +void DFS(GPH *graph, int vertex_nr) { + NODE *temp = graph->adjacency_lists[vertex_nr]; + 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; + } } -void BFS(GPH *graph, int start) -{ -NODE *queue = NULL; - -graph->visited[start] = 1; -enqueue(&queue, start); - - while (!is_empty(queue)) - { -int current = dequeue(&queue); -printf("%d ", current); +// parcurgere in latime (bfs) +void BFS(GPH *graph, int start) { + NODE *queue = NULL; + graph->visited[start] = 1; + enqueue(&queue, start); -NODE *temp = graph->adjacency_lists[current]; + while (!is_empty(queue)) { + int current = dequeue(&queue); + printf("%d ", current); - while (temp) - { + NODE *temp = graph->adjacency_lists[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; + } + } } + +// afiseaza lista de adiacenta a grafului +void print_graph(GPH *graph) { + for (int i = 0; i < graph->vertices; i++) { + NODE *temp = graph->adjacency_lists[i]; + printf("nodul %d: ", i); + while (temp) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); } } -int main() -{ +int main() { + int nr_of_vertices, nr_of_edges, starting_vertex; - int nr_of_vertices; - int nr_of_edges; - int src, dest; + 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); + insert_edges(nr_of_vertices, nr_of_edges, 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?"); + printf("de unde plecam in dfs? "); scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); + printf("parcurgere cu dfs: "); + DFS(graph, starting_vertex); + + wipe_visited_list(graph, nr_of_vertices); + + printf("\n"); + + printf("de unde plecam in bfs? "); + scanf("%d", &starting_vertex); + printf("parcurgere cu bfs: "); BFS(graph, starting_vertex); -return - 0; + + printf("\n"); + return 0; }