diff --git a/example1.c b/example1.c index 2bfeeed..89a13e6 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,225 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 vertex; + struct Node *next; +} Node; + +typedef struct Graph +{ + int numVertices; + int *visited; + Node **adjLists; +} Graph; + +typedef struct Queue +{ + int *items; + int front; + int rear; +} Queue; + +Node *create_node(int vertex) +{ + Node *newNode = malloc(sizeof(Node)); + newNode->vertex = vertex; + newNode->next = NULL; + return newNode; +} + +Graph *create_graph(int vertices) +{ + int i; + Graph *graph = malloc(sizeof(Graph)); + graph->numVertices = vertices; + graph->adjLists = malloc(vertices * sizeof(Node *)); + graph->visited = malloc(vertices * sizeof(int)); + + for (i = 0; i < vertices; i++) + { + graph->adjLists[i] = NULL; + graph->visited[i] = 0; + } + + return graph; +} + +void add_edge(Graph *graph, int src, int dest) +{ + Node *newNode = create_node(dest); + + newNode->next = graph->adjLists[src]; + graph->adjLists[src] = newNode; + + newNode = create_node(src); + newNode->next = graph->adjLists[dest]; + graph->adjLists[dest] = newNode; +} + +void print_graph(Graph *graph) +{ + int i; + for (i = 0; i < graph->numVertices; i++) + { + Node *temp = graph->adjLists[i]; + printf("Nodul %d: ", i); + while (temp) + { + printf("%d -> ", temp->vertex); + temp = temp->next; + } + printf("NULL\n"); + } +} + +void reset_visited(Graph *graph) +{ + int i; + for (i = 0; i < graph->numVertices; i++) + { + graph->visited[i] = 0; + } +} + +Queue *create_queue(int size) +{ + Queue *queue = malloc(sizeof(Queue)); + + queue->items = malloc(size * sizeof(int)); + queue->front = -1; + queue->rear = -1; + + return queue; +} + +int is_queue_empty(Queue *queue) +{ + return queue->front == -1; +} + +void enqueue(Queue *queue, int value) +{ + if (queue->rear == -1) + { + queue->front = queue->rear = 0; + } + else + { + queue->rear++; + } + queue->items[queue->rear] = value; +} + +int dequeue(Queue *queue) +{ + int value = queue->items[queue->front]; + + if (queue->front == queue->rear) + { + queue->front = queue->rear = -1; + } + else + { + queue->front++; + } + return value; +} + +void DFS(Graph *graph, int vertex) +{ + graph->visited[vertex] = 1; + + printf("%d ", vertex); + + Node *adjList = graph->adjLists[vertex]; + + while (adjList) + { + int nextVertex = adjList->vertex; + + if (!graph->visited[nextVertex]) + { + DFS(graph, nextVertex); + } + + adjList = adjList->next; + } +} + +void BFS(Graph *graph, int startVertex) +{ + Queue *queue = create_queue(graph->numVertices); + + graph->visited[startVertex] = 1; + enqueue(queue, startVertex); + + while (!is_queue_empty(queue)) + { + int currentVertex = dequeue(queue); + + printf("%d ", currentVertex); + + Node *temp = graph->adjLists[currentVertex]; + + while (temp) + { + int adjVertex = temp->vertex; + + if (!graph->visited[adjVertex]) + { + graph->visited[adjVertex] = 1; + enqueue(queue, adjVertex); + } + + temp = temp->next; + } + } + + free(queue->items); + free(queue); +} + +int main() +{ + int numVertices, numEdges, i, startVertex;; + + printf("Numarul de noduri in graf: "); + scanf("%d", &numVertices); + + Graph *graph = create_graph(numVertices); + + printf("Numarul de muchii: "); + scanf("%d", &numEdges); + + printf("Introduceti cele %d muchii (ex: 0 1):\n", numEdges); + for (i = 0; i < numEdges; i++) + { + int src, dest; + + scanf("%d %d", &src, &dest); + add_edge(graph, src, dest); + } + + printf("\nReprezentarea grafului:\n"); + print_graph(graph); + + printf("\nStart DFS de la nodul: "); + scanf("%d", &startVertex); + + printf("Parcurgere DFS: "); + DFS(graph, startVertex); + + reset_visited(graph); + + printf("\n\nStart BFS de la nodul: "); + scanf("%d", &startVertex); + + printf("Parcurgere BFS: "); + BFS(graph, startVertex); + + printf("\n"); + + return 0; +} diff --git a/example2.c b/example2.c index 0e67b95..b5d9ecf 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,126 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +#include #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 *)); + int vertex; + struct Node *next; +} 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) +typedef struct Graph { - NODE *new_node = create_node(dest); - - new_node->next = graph->adjacency_lists[src]; - graph->adjacency_lists[src] = new_node; - - new_node = create_node(src); + int numVertices; + Node **adjLists; +} Graph; - 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) +Node *create_node(int vertex) { - return - queue == NULL; -} + Node *newNode = malloc(sizeof(Node)); + newNode->vertex = vertex; + newNode->next = NULL; + return newNode; +} +Graph *create_graph(int numVertices) +{ + int i; + Graph *graph = malloc(sizeof(Graph)); + graph->numVertices = numVertices; + graph->adjLists = malloc(numVertices * sizeof(Node *)); + for (i = 0; i < numVertices; i++) + { + graph->adjLists[i] = NULL; + } -void enqueue(NODE ***queue, int data) -{ - NODE *new_node = create_node(data); + return graph; +} - if (is_empty(*queue)) *queue = new_node; -else +void add_edge(Graph *graph, int src, int dest) { - NODE *temp = *queue; - while (temp->next) - {temp = temp->next;}temp->next = new_node;}} + Node *newNode = create_node(dest); -int dequeue(NODE -**queue) -{ int data = (*queue)->data;NODE *temp = *queue;*queue = (*queue)->next;return data; + newNode->next = graph->adjLists[src]; + graph->adjLists[src] = newNode; + newNode = create_node(src); + newNode->next = graph->adjLists[dest]; + graph->adjLists[dest] = newNode; } -void print_graph(GPH *graph) +void print_graph(Graph *graph) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) - { - NODE *temp = graph->adjacency_lists[i<<2]; + int i; - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); - } -} + for (i = 0; i < graph->numVertices; i++) + { + Node *temp = graph->adjLists[i]; -void print_queue(NODE *queue) -{ -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} + printf("Nod %d: ", i); + while (temp) + { + printf("%d -> ", temp->vertex); -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; + temp = temp->next; + } -graph->visited[vertex_nr] = 1; -printf("%d->", vertex_nr); + printf("NULL\n"); + } +} -while (temp != NULL) +int has_direct_path(Graph *graph, int src, int dest) { - int connected_vertex = temp->data; + Node *temp = graph->adjLists[src]; - if (graph->visited[connected_vertex] == 0) + while (temp) { - DFS(graph, connected_vertex); -} -temp = temp->next; -} + if (temp->vertex == dest) + { + return 1; + } + temp = temp->next; + } + return 0; } -void BFS(GPH *graph, int start) +int main() { -NODE *queue = NULL; + int numVertices, numEdges, i, src, dest, restaurantA, restaurantB; -graph->visited[start] = 1; -enqueue(&queue, start); + printf("Numarul de restaurante (noduri): "); + scanf("%d", &numVertices); - while (!is_empty(queue)) - { -int current = dequeue(&queue); -printf("%d ", current); + printf("Numarul de legaturi directe (muchii): "); + scanf("%d", &numEdges); -NODE *temp = graph->adjacency_lists[current]; + Graph *graph = create_graph(numVertices); - while (temp) - { - int adj_vertex = temp->data; + printf("Introduceti cele %d muchii (ex: 0 1 pentru muchie intre 0 si 1):\n", numEdges); + for (i = 0; i < numEdges; i++) + { + scanf("%d %d", &src, &dest); - if (graph->visited[adj_vertex] == 0) - { - graph->visited[adj_vertex] = 1; - enqueue(&*queue, adj_vertex); - } - temp = temp->next; -} + add_edge(graph, src, dest); } -} -int main() -{ + printf("Verificare drum direct intre restaurantele:\n"); + printf("Restaurant A: "); + scanf("%d", &restaurantA); + printf("Restaurant B: "); + scanf("%d", &restaurantB); + + if (has_direct_path(graph, restaurantA, restaurantB)) + { + printf("Exista drum direct intre %d si %d.\n", restaurantA, restaurantB); + } + else + { + printf("NU exista drum direct intre %d si %d.\n", restaurantA, restaurantB); + } - 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; + return 0; }