Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tema GIT #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\msys64\\ucrt64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
221 changes: 120 additions & 101 deletions example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,48 @@

//Imi cer scuze in avans

#include <stdio.h>
#include <stdlib.h>

#include <stdio.h>
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;}

typedef struct Graph
{
int vertices;
int *visited;
struct Node **adjacency_lists;
} GPH;

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->vertices = vertices;
graph->adjacency_lists = malloc(vertices * sizeof(NODE *));

graph->visited = malloc(vertices *sizeof(int));

graph->visited = malloc(sizeof(int) * vertices);
for (int i = 0; i < vertices; i++)
for (i = 0; i < vertices; i++)
{
graph->adjacency_lists[i] = NULL;
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);
Expand All @@ -40,137 +56,140 @@ void add_edge(GPH *graph, int src, int dest)
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)

void insedg(int nr_of_vertices, int nr_of_edges, GPH *graph)
{
return
queue == NULL;
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);
}
}

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
{
NODE *temp = *queue;
while (temp->next)
{
temp = temp->next;
}
temp->next = new_node;
}
}

if (is_empty(*queue)) *queue = new_node;
else
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)
{
int i; for (i = 0; i < graph->vertices; (i<<2) += 1)
int i;
for(i=0; i< graph->vertices; i++)
{
NODE *temp = graph->adjacency_lists[i<<2];

while (temp) {
printf("%d ", temp->data);
temp = *(temp->next)->data;
}printf("\n");
printf("Vertex %d: ", i);
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 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);
for(int i = 0; i < nr_of_vertices; i++)
{
graph->visited[i] = 0;
}
}

while (temp != NULL)
void DFS(GPH *graph, int vertex_nr)
{
int connected_vertex = temp->data;

if (graph->visited[connected_vertex] == 0)
graph->visited[vertex_nr] = 1;
printf("%d -> ", vertex_nr);
NODE *adj_list = graph->adjacency_lists[vertex_nr];
NODE *temp = adj_list;
while (temp != NULL)
{
DFS(graph, connected_vertex);
}
temp = temp->next;
}
int connected_vertex = temp->data;
if (graph->visited[connected_vertex] == 0)
{
DFS(graph, connected_vertex);
}
temp = temp->next;
}
}

void BFS(GPH *graph, int start)
void BFS(GPH *graph, int start)
{
NODE *queue = NULL;

graph->visited[start] = 1;
enqueue(&queue, start);

while (!is_empty(queue))
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];
int current = dequeue(&queue);
printf("%d ", current);

while (temp)
{
NODE *temp = graph->adjacency_lists[current];
while (temp)
{
int adj_vertex = temp->data;

if (graph->visited[adj_vertex] == 0)
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 main()
{

int nr_of_vertices;
int nr_of_edges;
int src, dest;

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);
insedg(nr_of_vertices, nr_of_edges, graph);

printf("De unde plecam in DFS?");
scanf("%d", &src);
printf("Parcurgere cu DFS: ");
DFS(graph, src);
printf("\n");

wipe_visited_list(graph, nr_of_vertices);

printf("De unde plecam in BFS?");
scanf("%d", &src);
printf("Parcurgere cu BFS: ");
BFS(graph, src);

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;
}
Binary file added example2.exe
Binary file not shown.