From b2b74666f9a6c7af3564e2fe4729b331d32e781b Mon Sep 17 00:00:00 2001 From: biancamaria21 Date: Sat, 12 Apr 2025 16:14:09 +0300 Subject: [PATCH 1/2] Update example2.c --- example2.c | 248 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 146 insertions(+), 102 deletions(-) diff --git a/example2.c b/example2.c index 0e67b95..6682a91 100644 --- a/example2.c +++ b/example2.c @@ -3,31 +3,47 @@ //Imi cer scuze in avans #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; + +typedef struct Graph +{ + int vertices; + int *visited; + 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;} + +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 *)); - - - + + graph->vertices = vertices; + graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); graph->visited = malloc(sizeof(int) * vertices); + for (int i = 0; i < vertices; i++) { - graph->adjacency_lists[i] = NULL; + graph->adjacency_lists[i] = NULL; graph->visited[i] = 0; } return graph; } + void add_edge(GPH *graph, int src, int dest) { NODE *new_node = create_node(dest); @@ -40,110 +56,131 @@ void add_edge(GPH *graph, int src, int dest) 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);}} + +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; + return queue == NULL; } - - - - - - -void enqueue(NODE ***queue, int data) +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;}} + if (*queue == NULL) + *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; +int dequeue(NODE **queue) +{ + int data = (*queue)->data; + NODE *temp = *queue; + + *queue = (*queue)->next; + free(temp); + return data; } void print_graph(GPH *graph) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) + int i; + for (i = 0; i < graph->vertices; i++) { - NODE *temp = graph->adjacency_lists[i<<2]; - - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); + NODE *temp = graph->adjacency_lists[i]; + while (temp) + { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); } } void print_queue(NODE *queue) { -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} - + + while (queue != NULL) + { + printf("%d ", queue->data); + queue = queue->next; + } + +} void wipe_visited_list(GPH *graph, int nr_of_vertices) { -for (int i = 0; -i < nr_of_vertices; - i++) -{ -graph->visited[i] = 0;}} + 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; -} + 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; + } } void BFS(GPH *graph, int start) { -NODE *queue = NULL; - -graph->visited[start] = 1; -enqueue(&queue, start); - - while (!is_empty(queue)) + 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]; + + while (temp) { -int current = dequeue(&queue); -printf("%d ", current); - -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); - } - temp = temp->next; -} + int adj_vertex = temp->data; + + if (graph->visited[adj_vertex] == 0) + { + graph->visited[adj_vertex] = 1; + enqueue(&queue, adj_vertex); + } + temp = temp->next; } + } } int main() @@ -152,25 +189,32 @@ int main() int nr_of_vertices; int nr_of_edges; int src, dest; - - - - 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?"); + 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_vertices); + 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_vertex); + 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; } From 081a246ff8f6b0a3ff9e680e99b0915df5fa9ce9 Mon Sep 17 00:00:00 2001 From: biancamaria21 Date: Sat, 12 Apr 2025 20:33:56 +0300 Subject: [PATCH 2/2] Update example1.c am corectat cateva greseli de sintaxa si functii si am curatat codul --- example1.c | 285 +++++++++++++++++++++++++---------------------------- 1 file changed, 137 insertions(+), 148 deletions(-) diff --git a/example1.c b/example1.c index 2bfeeed..f1a9844 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,137 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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*/ + +#include +#include + +typedef struct Node +{ + int data; + struct Node *next; +}NODE; +/// pentru simplitate, folosim int uri pt a numi restaurantel/locatiile +/// ex: 1 - restaurantul 1 si tot asa +typedef struct graph +{ + int v; + int *vis; + NODE **alst; +} GPH; + +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(v * sizeof(NODE *)); + g->vis = malloc(sizeof(int) * v); + + for (i = 0; ialst[i] = NULL; + g->vis[i] = 0; + } + + return g; +} + +void DFS(GPH *g,int v_nr) +{ + NODE *adj_list = g->alst[v_nr]; + NODE *aux = adj_list; + + g->vis[v_nr] = 1; + + while (aux != NULL) + { + int con_ver = aux->data; + + if (g->vis[con_ver] == 0) + DFS(g, con_ver); + + aux = aux->next; + } +} + +void insert_edges(GPH *g,int edg_nr) +{ + int src, dest, i; + printf("adauga %d munchii (de la 0 la %d)\n",edg_nr,g->v - 1); + + for (i=0; iv; i++) + { + g->vis[i] = 0; + } +} + +int can_reach(GPH *g, int src, int dest) +{ + wipe(g); + DFS(g, src); + + return g->vis[dest]; //verificam daca dest este vizitat +} + +int main() +{ + + int nrv; + int edg_nr; + int src, dest; + int i; + + printf("cate noduri are graful? "); + scanf("%d", &nrv); + + printf("cate muchii are graful? "); + scanf("%d", &edg_nr); + + GPH *g = create_g(nrv); + + insert_edges(g, edg_nr); + + printf("Introduceti restaurantul de la care plecati: "); + scanf("%d",&src); + + printf("Introduceti restaurantul la care ajungeti: "); + scanf("%d",&dest); + + if(can_reach(g, src, dest)) + { + printf("Exista drum de la restaurantul %d la rest %d\n", src, dest); + } + else + { + printf("Nu exista drum de la restaurantul %d la restaurantul %d\n", src, dest); + } + + return 0; +}