diff --git a/example1.c b/example1.c index 2bfeeed..2ebdfc7 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,139 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 + +typedef struct Node { + int data; + struct Node *next; +} NODE; + +typedef struct g { + int v; + int *vis; + 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(sizeof(NODE *) * v); + g->vis = malloc(sizeof(int) * v); + + 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) { + if (s->t < s->scap - 1) { + s->t++; + s->arr[s->t] = pshd; + } +} + +void DFS(GPH *g, STK *s, int v_nr) { + NODE *aux = g->alst[v_nr]; + 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 muchii (de la 0 la %d)\n", edg_nr, nrv - 1); + 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; + } +} + +void canbe(GPH *g, int nrv, STK *s1, STK *s2) { + int found = 0; + for (int i = 0; i <= s1->t && !found; i++) { + for (int j = 0; j <= s2->t && !found; j++) { + if (s1->arr[i] == s2->arr[j]) { + found = 1; + } + } + } + printf("\nDrum %s\n", found ? "existent" : "inexistent"); +} + +int main() { + int nrv; + int edg_nr; + int vortex_1; + int vortex_2; + + printf("Cate noduri are graful? "); + 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("Verifica drum intre doua noduri (ex: 1 3): "); + scanf("%d%d", &vortex_1, &vortex_2); + + wipe(g, nrv); + DFS(g, s1, vortex_1); + + wipe(g, nrv); + DFS(g, s2, vortex_2); + + canbe(g, nrv, s1, s2); + + return 0; +} diff --git a/example2.c b/example2.c index 0e67b95..2c2373f 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,147 @@ -/*parcurgerge graf cu DFS/BFS*/ +#include +#include -//Imi cer scuze in avans +typedef struct Node { + int data; + struct Node* next; +} Node; + +typedef struct Graph { + int vertices; + int* visited; + Node** adjacency_lists; +} Graph; + +Node* create_node(int value) { + Node* new_node = (Node*)malloc(sizeof(Node)); + new_node->data = value; + new_node->next = NULL; + return new_node; +} -#include +Graph* create_graph(int vertices) { + Graph* graph = (Graph*)malloc(sizeof(Graph)); + graph->vertices = vertices; + graph->adjacency_lists = (Node**)malloc(vertices * sizeof(Node*)); + graph->visited = (int*)malloc(vertices * sizeof(int)); -#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; + 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); +void add_edge(Graph* 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 insert_edges(int vertices, int edges, Graph* graph) { + int src, dest; + printf("Adauga %d muchii (noduri indexate de la 0 la %d):\n", edges, vertices - 1); + for (int i = 0; i < edges; i++) { + scanf("%d %d", &src, &dest); + add_edge(graph, src, dest); + } } -void print_graph(GPH *graph) -{ - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) - { - NODE *temp = graph->adjacency_lists[i<<2]; +int is_empty(Node* queue) { + return queue == NULL; +} - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); +void enqueue(Node** queue, int data) { + Node* new_node = create_node(data); + if (*queue == NULL) { + *queue = new_node; + } else { + Node* temp = *queue; + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = new_node; } } -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; -} +int dequeue(Node** queue) { + int data = (*queue)->data; + Node* temp = *queue; + *queue = (*queue)->next; + free(temp); + return data; } -void BFS(GPH *graph, int start) -{ -NODE *queue = NULL; +void reset_visited(Graph* graph) { + for (int i = 0; i < graph->vertices; i++) { + graph->visited[i] = 0; + } +} -graph->visited[start] = 1; -enqueue(&queue, start); +void DFS(Graph* graph, int vertex) { + Node* temp = graph->adjacency_lists[vertex]; + graph->visited[vertex] = 1; + printf("%d -> ", vertex); + + while (temp != NULL) { + int connected_vertex = temp->data; + if (graph->visited[connected_vertex] == 0) { + DFS(graph, connected_vertex); + } + temp = temp->next; + } +} - while (!is_empty(queue)) - { -int current = dequeue(&queue); -printf("%d ", current); +void BFS(Graph* graph, int start_vertex) { + Node* queue = NULL; + graph->visited[start_vertex] = 1; + enqueue(&queue, start_vertex); -NODE *temp = graph->adjacency_lists[current]; + while (!is_empty(queue)) { + int current_vertex = dequeue(&queue); + printf("%d -> ", current_vertex); - while (temp) - { + Node* temp = graph->adjacency_lists[current_vertex]; + 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; + } } } -int main() -{ +int main() { + int vertices, edges, start_vertex; - int nr_of_vertices; - int nr_of_edges; - int src, dest; + printf("Cate noduri are graful? "); + scanf("%d", &vertices); + printf("Cate muchii are graful? "); + scanf("%d", &edges); + Graph* graph = create_graph(vertices); + insert_edges(vertices, 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?"); - scanf("%d", &starting_vertex); -printf("parcurgere cu BFS:"); - BFS(graph, starting_vertex); -return - 0; -} + printf("De unde plecam in DFS? "); + scanf("%d", &start_vertex); + printf("Parcurgere cu DFS: "); + DFS(graph, start_vertex); + printf("\n"); + + reset_visited(graph); + + printf("De unde plecam in BFS? "); + scanf("%d", &start_vertex); + printf("Parcurgere cu BFS: "); + BFS(graph, start_vertex); + printf("\n"); + + return 0; +} \ No newline at end of file