diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a6d8aad --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\MinGW\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/example1.c b/example1.c index 2bfeeed..9ed8598 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 +#include +#include + +typedef struct ListaRestaurante +{ + int restaurant_id; /// pentru simplitate, folosim variabile de tip int pentru a numi restaurantele/locatiile + struct ListaRestaurante *next; +}RESTAURANT; + +typedef struct LantRestaurante +{ + int nr_noduri; + int *vizitate; + RESTAURANT **lista; +}RESTAURANT_GRAF; + +typedef struct stiva +{ + int top; + int capacitate_max; + int *elemente_stiva; +} STIVA; + +RESTAURANT *creeazaNod(int numar_restaurant) +{ + RESTAURANT *nod = malloc(sizeof(RESTAURANT)); + + nod->restaurant_id = numar_restaurant; + nod->next = NULL; + + return nod; +} + +void adaugaLaturi(RESTAURANT_GRAF *graf,int plecare,int destinatie) +{ + RESTAURANT *nod = creeazaNod(destinatie); + + nod->next = graf->lista[plecare - 1]; + graf->lista[plecare - 1] = nod; + + nod = creeazaNod(plecare); + + nod->next = graf->lista[destinatie - 1]; + graf->lista[destinatie - 1] = nod; +} + +RESTAURANT_GRAF *creeazaGraf(int nr_noduri) +{ + int contor; + RESTAURANT_GRAF *graf_rest = malloc(sizeof(RESTAURANT_GRAF)); + + graf_rest->nr_noduri = nr_noduri; + graf_rest->lista = malloc(sizeof(RESTAURANT *)*nr_noduri); + graf_rest->vizitate = malloc(sizeof(int) * nr_noduri); + + for (contor = 0; contor < nr_noduri; contor++) + { + graf_rest->lista[contor] = NULL; + graf_rest->vizitate[contor] = 0; + } + + return graf_rest; +} + +STIVA *creeazaStiva(int capacitate_stiva) +{ + STIVA *stiva = malloc(sizeof(STIVA)); + stiva->elemente_stiva = malloc(capacitate_stiva*sizeof(int)); + stiva->top = -1; + stiva->capacitate_max = capacitate_stiva; + + return stiva; +} + +void adaugaElement(int restaurant_id,STIVA *stiva_rest) +{ + stiva_rest->top = stiva_rest->top + 1; + stiva_rest->elemente_stiva[stiva_rest->top] = restaurant_id; +} + +int parcurgeNoduri(RESTAURANT_GRAF *graf_rest,STIVA *stiva_rest,int id_inceput,int *drum,int nod_sfarsit) +{ + RESTAURANT *noduri_vecine = graf_rest->lista[id_inceput]; + RESTAURANT *lista_auxiliara = noduri_vecine; + + graf_rest->vizitate[id_inceput] = 1; + adaugaElement(id_inceput + 1, stiva_rest); + drum[stiva_rest->top] = id_inceput + 1; + + if (id_inceput == nod_sfarsit) + { + return 1; + } + + while (lista_auxiliara != NULL) + { + int nod_vecin = lista_auxiliara->restaurant_id; + if (graf_rest->vizitate[nod_vecin - 1] == 0) + { + if (parcurgeNoduri(graf_rest, stiva_rest, nod_vecin - 1, drum, nod_sfarsit)) + { + return 1; + } + } + lista_auxiliara = lista_auxiliara->next; + } + + graf_rest->vizitate[id_inceput] = 0; + stiva_rest->top--; + + return 0; +} + +void adaugaMuchii(RESTAURANT_GRAF *graf_rest,int numar_muchii,int nr_noduri) +{ + int plecare, destinatie, contor; + printf("Adauga nodul de plecare si nodul de sosire pentru fiecare dintre cele %d munchii (notate de la 1 la %d):\n", numar_muchii, nr_noduri); + for (contor = 0; contor < numar_muchii ; contor++) + { + scanf("%d%d", &plecare, &destinatie); + adaugaLaturi(graf_rest, plecare, destinatie); + } +} + +void stergeRestauranteVizitate(RESTAURANT_GRAF *graf_rest, int nr_noduri) +{ + for (int contor=0; contor < nr_noduri; contor++) + { + graf_rest->vizitate[contor] = 0; + } +} + +void existaDrum(RESTAURANT_GRAF *graf_rest, int nr_noduri, STIVA *stiva, int nod_start, int nod_final) +{ + int *drum = malloc(nr_noduri * sizeof(int)); + + if (parcurgeNoduri(graf_rest, stiva, nod_start - 1, drum, nod_final - 1)) + { + printf("Drumul parcurs: "); + for (int i = 0; i <= stiva->top; i++) + { + printf("%d ", drum[i]); + } + printf("\n"); + } + else + { + printf("Nu exista drum intre restaurantele %d si %d.\n", nod_start, nod_final); + } + + free(drum); +} + + +int main() +{ + int nr_noduri; + int numar_muchii; + int plecare, destinatie; + + printf("Cate noduri are graficul? "); + scanf("%d", &nr_noduri); + + printf("Cate muchii are graficul? "); + scanf("%d", &numar_muchii); + + RESTAURANT_GRAF *graf_rest = creeazaGraf(nr_noduri); + + STIVA *stiva = creeazaStiva(2 * nr_noduri); + + adaugaMuchii(graf_rest, numar_muchii, nr_noduri); + + printf("Introduceti nodul de plecare si nodul de sosire al drumului de cautat(notate de la 1 la %d): ", nr_noduri); + scanf("%d%d", &plecare, &destinatie); + + existaDrum(graf_rest, nr_noduri, stiva, plecare, destinatie); + return 0; +} diff --git a/example2.c b/example2.c index 0e67b95..eac982f 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,216 @@ /*parcurgerge graf cu DFS/BFS*/ - -//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; -/// 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 Graphx +{ + int vertices; + int *visited; + NODE **adjacency; +} GRAPH; + +NODE *create_node(int value) { - int i; - GPH *graph = malloc(sizeof(GPH)); - graph->vertices = vertices;graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + NODE *new_node = malloc(sizeof(NODE)); + new_node->data = value; + new_node->next = NULL; + return new_node; +} +GRAPH *create_graph(int vertices) +{ + GRAPH *graph = malloc(sizeof(GRAPH)); + graph->vertices = vertices + 1; + graph->adjacency = malloc((vertices + 1) * sizeof(NODE *)); + graph->visited = malloc(sizeof(int) * (vertices + 1)); - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) + for (int i = 0; i < graph->vertices; i++) { - graph->adjacency_lists[i] = NULL; + graph->adjacency[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(GRAPH *graph, int source, int destination) +{ + NODE *new_node = create_node(destination); - new_node = create_node(src); + new_node->next = graph->adjacency[source]; + graph->adjacency[source] = new_node; - new_node->next = graph->adjacency_lists[dest]; - graph->adjacency_lists[dest] = new_node; + new_node = create_node(source); + new_node->next = graph->adjacency[destination]; + graph->adjacency[destination] = 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) +void *insert_edges(int nr_of_vertices, int nr_of_edges, GRAPH *graph) { - return - queue == NULL; + int source, destination; + 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", &source, &destination); + add_edge(graph, source, destination); + } } +int is_empty(NODE *queue) +{ + 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 + 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; - 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) +void print_graph(GRAPH *graph) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) + for (int i = 1; i < graph->vertices; i++) { - NODE *temp = graph->adjacency_lists[i<<2]; - - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); + printf("Nodul %d",i); + NODE *temp = graph->adjacency[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;}} - + printf("The queue: "); + while (queue != NULL) + { + printf("%d ", queue->data); + queue = queue->next; + } +} -void wipe_visited_list(GPH *graph, int nr_of_vertices) +void wipe_visited(GRAPH *graph) { -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); + for (int i = 0; i < graph->vertices; i++) + { + graph->visited[i] = 0; + } +} -while (temp != NULL) +void DFS(GRAPH *graph, int vertex_nr) { - int connected_vertex = temp->data; + NODE *adj_list = graph->adjacency[vertex_nr]; + NODE *temp = adj_list; + + 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) + { + printf("->"); + DFS(graph, connected_vertex); + } + temp = temp->next; + } } -void BFS(GPH *graph, int start) +void BFS(GRAPH *graph, int start) { -NODE *queue = NULL; + NODE *queue = NULL; -graph->visited[start] = 1; -enqueue(&queue, start); + graph->visited[start] = 1; + enqueue(&queue, start); + int first=1; while (!is_empty(queue)) { -int current = dequeue(&queue); -printf("%d ", current); - -NODE *temp = graph->adjacency_lists[current]; - - while (temp) - { + int current = dequeue(&queue); + if (!first) + { + printf("->"); + } + printf("%d", current); + first=0; + NODE *temp = graph->adjacency[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; + } } } 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 starting_vertex; + + printf("Cate noduri are graful?" ); + scanf("%d", &nr_of_vertices); + + printf("Cate muchii are graful? "); + scanf("%d", &nr_of_edges); + + GRAPH *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); + + wipe_visited(graph); + 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; +} \ No newline at end of file