From 187e54c474238733e7034a7c34872d855ea85e83 Mon Sep 17 00:00:00 2001 From: andreirares5 Date: Sat, 12 Apr 2025 22:38:46 +0300 Subject: [PATCH] reparare cod --- ex1.c | 187 +++++++++++++++++++++++++++++++++++++++++++++ ex2.c | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++ example1.c | 148 ------------------------------------ example2.c | 176 ------------------------------------------ 4 files changed, 406 insertions(+), 324 deletions(-) create mode 100644 ex1.c create mode 100644 ex2.c delete mode 100644 example1.c delete mode 100644 example2.c diff --git a/ex1.c b/ex1.c new file mode 100644 index 0000000..11ae119 --- /dev/null +++ b/ex1.c @@ -0,0 +1,187 @@ +/*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 vertices; + int *visited; + struct Node **adjlist; +}GPH; + +typedef struct stack +{ + int top; + int stack_cap; + int *array; +}STK; + +NODE *create_node(int value) +{ + NODE *new = (NODE*)malloc(sizeof(NODE)); + new->data = value; + new->next = NULL; + return new; +} + +void add_edge(GPH *graph,int source,int destination) +{ + NODE *new = create_node(destination); + + new->next = graph->adjlist[source]; + graph->adjlist[source] = new; + + new = create_node(source); + + new->next = graph->adjlist[destination]; + graph->adjlist[destination] = new; +} + +GPH *create_graph(int number_of_vertices) +{ + int i; + + GPH *graph = (GPH*)malloc(sizeof(GPH)); + graph->vertices = number_of_vertices; + + graph->adjlist = (NODE**)malloc(sizeof(NODE *)* number_of_vertices); + graph->visited = (int*)malloc(sizeof(int) * number_of_vertices); + + for (i = 0 ; iadjlist[i] = NULL; + graph->visited[i] = 0; + } + return graph; +} + +STK *create_stack(int stack_cap) +{ + STK *stack = (STK*)malloc(sizeof(STK)); + stack->array = (int*)malloc(stack_cap * sizeof(int)); + stack->top = -1; + stack->stack_cap = stack_cap; + + return stack; +} + +void push(int value,STK *stack) +{ + stack->top = stack->top+1; + stack->array[stack->top] = value; +} + +void DFS(GPH *graph,STK *stack,int vertex_index) +{ + NODE *adjlist = graph->adjlist[vertex_index]; + NODE *aux = adjlist; + graph->visited[vertex_index] = 1; + + printf("%d ",vertex_index); + push(vertex_index , stack); + + while (aux != NULL) + { + int connected_vertex = aux->data; + if (graph->visited[connected_vertex] == 0) + DFS(graph , stack , connected_vertex); + + aux=aux->next; + } +} + +void insert_edges(GPH *graph , int number_of_edges , int number_of_vertices) +{ + int source,destination,i; + + printf("Adauga %d munchii (de la 1 la %d)\n",number_of_edges,number_of_vertices); + for (i = 0 ; ivisited[i] = 0; + } +} + +int next_DFS(GPH* graph, STK* stack, int start, int target) +{ + stack->top = -1; + wipe(graph, graph->vertices); + + DFS(graph, stack, start); + + for (int i = 0; i <= stack->top; i++) + { + if (stack->array[i] == target) + return 1; + } + return 0; +} + +int has_direct_edge(GPH *graph, int source, int destination) +{ + NODE *current = graph->adjlist[source]; + while (current != NULL) + { + if (current->data == destination) + { + return 1; + } + current = current->next; + } + return 0; +} +int main() +{ + + int number_of_vertices, number_of_edges, source, destination; + + printf("Cate noduri are graful? "); + scanf("%d", &number_of_vertices); + + printf("Cate muchii are graful? "); + scanf("%d", &number_of_edges); + + GPH *graph = create_graph(number_of_vertices); + + STK *stack1 = create_stack(2 * number_of_vertices); + + insert_edges(graph, number_of_edges, number_of_vertices); + + + printf("Introdu doua restaurante pentru a verifica daca exista drum direct intre ele:\n"); + scanf("%d%d", &source, &destination); + + + if (has_direct_edge(graph, source - 1, destination - 1)) + { + printf("Exista drum direct intre restaurantul %d si restaurantul %d\n", source, destination); + } + else if (next_DFS(graph, stack1, source - 1, destination - 1)) + { + printf("Nu exista drum direct intre restaurantul %d si restaurantul %d\n ,dar exista un drum ce se compune", source, destination); + } + else + { + printf("Nu exista drum direct intre restaurantul %d si restaurantul %d\n", source, destination); + } + + return 0; +} diff --git a/ex2.c b/ex2.c new file mode 100644 index 0000000..e060eab --- /dev/null +++ b/ex2.c @@ -0,0 +1,219 @@ +#include +#include + +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 = (NODE*)malloc(sizeof(NODE)); + new_node->data = v; + new_node->next = NULL; + return new_node; +} + +GPH *create_graph(int vertices) +{ + int i; + + GPH *graph = (GPH*)malloc(sizeof(GPH)); + graph->vertices = vertices; + graph->adjacency_lists = (NODE**)malloc(vertices * sizeof(NODE *)); + graph->visited = malloc(sizeof(int) * vertices); + + for (int i = 0; i < vertices; i++) + { + 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); + + 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; +} + +void insert_edges(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) +{ + 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) +{ + if(*queue==NULL) + return -1; + + int data = (*queue)->data; + NODE *temp = *queue; + *queue = (*queue)->next; + + return data; +} + +void print_graph(GPH *graph) +{ + int i; + for (i = 0; i < graph->vertices; i += 1) + { + 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; + } +} + + +void reset_visited(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; + } +} + +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); + + 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 main() +{ + + int nr_of_vertices, nr_of_edges, src, dest, i; + + int starting_vertex, *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); + insert_edges(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); + + reset_visited(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; +} \ No newline at end of file diff --git a/example1.c b/example1.c deleted file mode 100644 index 2bfeeed..0000000 --- a/example1.c +++ /dev/null @@ -1,148 +0,0 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 diff --git a/example2.c b/example2.c deleted file mode 100644 index 0e67b95..0000000 --- a/example2.c +++ /dev/null @@ -1,176 +0,0 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - -#include - -#include -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 *)); - - - - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) - { - 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); - - 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) -{ - 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; -} - -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"); - } -} - -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; -} -} - -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); - -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 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?"); - scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); - BFS(graph, starting_vertex); -return - 0; -}