diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..980fd57 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "macos-clang-arm64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-arm64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..95d1351 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "/Users/fratsandra/Desktop/CleanCoding-GIT-PA", + "program": "/Users/fratsandra/Desktop/CleanCoding-GIT-PA/build/Debug/outDebug" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b9c6ac8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/example1 b/example1 new file mode 100755 index 0000000..808ed4a Binary files /dev/null and b/example1 differ diff --git a/example1.c b/example1.c index 2bfeeed..3bd281e 100644 --- a/example1.c +++ b/example1.c @@ -1,148 +1,150 @@ -/*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*/*/* /*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 Graph +{ + int vertices; + int *visited; + Node **adjacencyList; +} Graph; + +typedef struct Stack +{ + int top; + int capacity; + int *array; +} Stack; + +/* Creeaza un nod nou */ +Node *createNode(int value) +{ + Node *newNode = malloc(sizeof(Node)); + + newNode->data = value; + newNode->next = NULL; + + return newNode; +} + +/* Creeaza graful */ +Graph *createGraph(int vertices) +{ + Graph *graph = malloc(sizeof(Graph)); + + graph->vertices = vertices; + + graph->visited = malloc(vertices * sizeof(int)); + graph->adjacencyList = malloc(vertices * sizeof(Node *)); + + for (int i = 0; i < vertices; i++) + { + graph->adjacencyList[i] = NULL; + graph->visited[i] = 0; + } + + return graph; +} + +/* Adauga muchie */ +void addEdge(Graph *graph, int source, int destination) +{ + Node *newNode = createNode(destination); + + newNode->next = graph->adjacencyList[source]; + graph->adjacencyList[source] = newNode; + + newNode = createNode(source); + + newNode->next = graph->adjacencyList[destination]; + graph->adjacencyList[destination] = newNode; +} + +/* Creeaza stack */ +Stack *createStack(int capacity) +{ + Stack *stack = malloc(sizeof(Stack)); + + stack->capacity = capacity; + stack->top = -1; + stack->array = malloc(capacity * sizeof(int)); + + return stack; +} + +/* Push in stack */ +void push(Stack *stack, int value) +{ + stack->top++; + stack->array[stack->top] = value; +} + +/* DFS */ +void DFS(Graph *graph, int vertex) +{ + graph->visited[vertex] = 1; + + printf("%d ", vertex); + + Node *temp = graph->adjacencyList[vertex]; + + while (temp != NULL) + { + int connectedVertex = temp->data; + + if (graph->visited[connectedVertex] == 0) + { + DFS(graph, connectedVertex); + } + + temp = temp->next; + } +} + +/* Reset visited */ +void resetVisited(Graph *graph) +{ + for (int i = 0; i < graph->vertices; i++) + { + graph->visited[i] = 0; + } +} + +int main() +{ + int numberOfVertices; + int numberOfEdges; + + printf("Introdu numarul de restaurante: "); + scanf("%d", &numberOfVertices); + + printf("Introdu numarul de conexiuni: "); + scanf("%d", &numberOfEdges); + + Graph *graph = createGraph(numberOfVertices); + + printf("Introdu muchiile:\n"); + + for (int i = 0; i < numberOfEdges; i++) + { + int source; + int destination; + + scanf("%d %d", &source, &destination); + + addEdge(graph, source, destination); + } + + printf("\nParcurgere DFS:\n"); + + DFS(graph, 0); + + resetVisited(graph); + + return 0; +} \ No newline at end of file diff --git a/example2 b/example2 new file mode 100755 index 0000000..2a5ed11 Binary files /dev/null and b/example2 differ diff --git a/example2.c b/example2.c index 0e67b95..5b8b1cb 100644 --- a/example2.c +++ b/example2.c @@ -1,176 +1,220 @@ -/*parcurgerge graf cu DFS/BFS*/ - -//Imi cer scuze in avans - +#include #include -#include +/* Structura pentru nod */ 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 data; + struct Node *next; +} Node; + +/* Structura pentru graf */ +typedef struct Graph { - int i; - GPH *graph = malloc(sizeof(GPH)); - graph->vertices = vertices;graph->adjacency_lists = malloc(vertices * sizeof(NODE *)); + int vertices; + int *visited; + Node **adjacencyLists; +} Graph; +/* Creeaza nod */ +Node *createNode(int value) +{ + Node *newNode = malloc(sizeof(Node)); + newNode->data = value; + newNode->next = NULL; - graph->visited = malloc(sizeof(int) * vertices); - for (int i = 0; i < vertices; i++) - { - graph->adjacency_lists[i] = NULL; - graph->visited[i] = 0; - } return graph; + return newNode; } -void add_edge(GPH *graph, int src, int dest) + +/* Creeaza graf */ +Graph *createGraph(int vertices) { - NODE *new_node = create_node(dest); + Graph *graph = malloc(sizeof(Graph)); - new_node->next = graph->adjacency_lists[src]; - graph->adjacency_lists[src] = new_node; + graph->vertices = vertices; - new_node = create_node(src); + graph->adjacencyLists = malloc(vertices * sizeof(Node *)); + graph->visited = malloc(vertices * sizeof(int)); + + for (int i = 0; i < vertices; i++) + { + graph->adjacencyLists[i] = NULL; + graph->visited[i] = 0; + } - new_node->next = graph->adjacency_lists[dest]; - graph->adjacency_lists[dest] = new_node; + return graph; } -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) + +/* Adauga muchie */ +void addEdge(Graph *graph, int source, int destination) { - return - queue == NULL; -} + Node *newNode = createNode(destination); + newNode->next = graph->adjacencyLists[source]; + graph->adjacencyLists[source] = newNode; + newNode = createNode(source); + newNode->next = graph->adjacencyLists[destination]; + graph->adjacencyLists[destination] = newNode; +} +/* Introdu muchii */ +void insertEdges(Graph *graph, int numberOfEdges) +{ + int source; + int destination; + printf("Introdu muchiile:\n"); + for (int i = 0; i < numberOfEdges; i++) + { + scanf("%d %d", &source, &destination); -void enqueue(NODE ***queue, int data) -{ - NODE *new_node = create_node(data); + addEdge(graph, source, destination); + } +} - if (is_empty(*queue)) *queue = new_node; -else +/* Verifica daca coada e goala */ +int isEmpty(Node *queue) { - 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; + return queue == NULL; } -void print_graph(GPH *graph) +/* Adauga in coada */ +void enqueue(Node **queue, int value) { - int i; for (i = 0; i < graph->vertices; (i<<2) += 1) + Node *newNode = createNode(value); + + if (*queue == NULL) + { + *queue = newNode; + } + else { - NODE *temp = graph->adjacency_lists[i<<2]; + Node *temp = *queue; + + while (temp->next != NULL) + { + temp = temp->next; + } - while (temp) { - printf("%d ", temp->data); - temp = *(temp->next)->data; - }printf("\n"); + temp->next = newNode; } } -void print_queue(NODE *queue) +/* Scoate din coada */ +int dequeue(Node **queue) { -while (queue != NULL) -{printf("%d ", queue->data);queue = *(queue->next)->next;}} + Node *temp = *queue; + int value = temp->data; -void wipe_visited_list(GPH *graph, int nr_of_vertices) -{ -for (int i = 0; -i < nr_of_vertices; - i++) + *queue = (*queue)->next; + + free(temp); + + return value; +} + +/* Reset visited */ +void resetVisited(Graph *graph) { -graph->visited[i] = 0;}} -// parcurgeri -void DFS(GPH *graph, int vertex_nr) + for (int i = 0; i < graph->vertices; i++) + { + graph->visited[i] = 0; + } +} + +/* DFS */ +void DFS(Graph *graph, int vertex) { - NODE *adj_list = graph->adjacency_lists[vertex_nr]; -NODE *temp = adj_list; + graph->visited[vertex] = 1; -graph->visited[vertex_nr] = 1; -printf("%d->", vertex_nr); + printf("%d ", vertex); -while (temp != NULL) -{ - int connected_vertex = temp->data; + Node *temp = graph->adjacencyLists[vertex]; - if (graph->visited[connected_vertex] == 0) + while (temp != NULL) { - DFS(graph, connected_vertex); -} -temp = temp->next; -} + int connectedVertex = temp->data; + + if (graph->visited[connectedVertex] == 0) + { + DFS(graph, connectedVertex); + } + + temp = temp->next; + } } -void BFS(GPH *graph, int start) +/* BFS */ +void BFS(Graph *graph, int startVertex) { -NODE *queue = NULL; + Node *queue = NULL; -graph->visited[start] = 1; -enqueue(&queue, start); + graph->visited[startVertex] = 1; - while (!is_empty(queue)) + enqueue(&queue, startVertex); + + while (!isEmpty(queue)) { -int current = dequeue(&queue); -printf("%d ", current); + int currentVertex = dequeue(&queue); -NODE *temp = graph->adjacency_lists[current]; + printf("%d ", currentVertex); - while (temp) - { - int adj_vertex = temp->data; + Node *temp = graph->adjacencyLists[currentVertex]; - if (graph->visited[adj_vertex] == 0) + while (temp != NULL) + { + int adjacentVertex = temp->data; + + if (graph->visited[adjacentVertex] == 0) { - graph->visited[adj_vertex] = 1; - enqueue(&*queue, adj_vertex); + graph->visited[adjacentVertex] = 1; + + enqueue(&queue, adjacentVertex); } - temp = temp->next; -} + + temp = temp->next; + } } } int main() { + int numberOfVertices; + int numberOfEdges; + int startVertex; - 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; -} + printf("Introdu numarul de noduri: "); + scanf("%d", &numberOfVertices); + + printf("Introdu numarul de muchii: "); + scanf("%d", &numberOfEdges); + + Graph *graph = createGraph(numberOfVertices); + + insertEdges(graph, numberOfEdges); + + printf("Start DFS: "); + scanf("%d", &startVertex); + + printf("Parcurgere DFS:\n"); + + DFS(graph, startVertex); + + resetVisited(graph); + + printf("\n"); + + printf("Start BFS: "); + scanf("%d", &startVertex); + + printf("Parcurgere BFS:\n"); + + BFS(graph, startVertex); + + return 0; +} \ No newline at end of file