Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
feat: Grafos, Caminhos e Ciclos
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Aug 1, 2024
1 parent 44a8020 commit be2c4c6
Show file tree
Hide file tree
Showing 16 changed files with 1,321 additions and 42 deletions.
21 changes: 7 additions & 14 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: sudo apt-get install clang-format
- name: Check formatting C code
run: |
find . -type f -name "*.c" -exec clang-format -n -Werror --ferror-limit=1 {} \;
- name: Check formatting C++ code
run: |
find . -type f -name "*.c" -exec clang-format -n -Werror --ferror-limit=1 {} \;
- name: Check formatting C header code
run: |
find . -type f -name "*.c" -exec clang-format -n -Werror --ferror-limit=1 {} \;
- name: Check formatting C++ header code
run: |
find . -type f -name "*.c" -exec clang-format -n -Werror --ferror-limit=1 {} \;
- name: Check clang-format
uses: DoozyX/[email protected]
with:
source: '.'
extensions: 'c,h'
clangFormatVersion: 17
style: Google
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
1. Análise de Algoritmos;
1. Algoritmos de Busca e Ordenação;
1. Recursividade;
1. Algortimos Gulosos; e
1. Algoritmos Gulosos; e
1. Problemas P, NP-Completo, e NP-Difícil.

## Dependências
Expand Down
23 changes: 23 additions & 0 deletions code/01-graphs-adjacency-list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

/** Function to create an adjacency list */
void print_adj_list(int arr[5][5]) {
// Initialize to all zeros
int adj_list[5][5] = {{0}};

// Traverse the given matrix
// FIXME: add the necessary code here
}

/** Main Function */
int main() {
// Given matrix
int arr[5][5] = {
{1, 0, 1, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 1},
{0, 0, 0, 0, 1}, {1, 0, 0, 1, 0},
};

print_adj_list(arr);

return 0;
}
45 changes: 45 additions & 0 deletions code/01-graphs-adjacency-matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>

/** N vertices and M edges */
int N, M;

/** Function to create an adjacency matrix */
void create_adj_matrix(int adj[][N + 1], int arr[][2]) {
// Initialize all value to zero
// FIXME: add the necessary code here

// Traverse the array of edges and update the values
// FIXME: add the necessary code here
}

/** Function to print an adjacency matrix */
void print_adj_matrix(int adj[][N + 1]) {
// Traverse the adj[][]
for (int i = 1; i < N + 1; i++) {
for (int j = 1; j < N + 1; j++) {
// Print the value at adj[i][j]
printf("%d ", adj[i][j]);
}
printf("\n");
}
}

/** Main function */
int main() {
// Number of vertices
N = 5;

// Given edges
int arr[][2] = {{1, 2}, {2, 3}, {4, 5}, {1, 5}};

// Number of edges
M = sizeof(arr) / sizeof(arr[0]);

int adj[N + 1][N + 1];

create_adj_matrix(adj, arr);

print_adj_matrix(adj);

return 0;
}
80 changes: 80 additions & 0 deletions code/02-graphs-eulerian-path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>

/** Number of vertices in the graph */
#define N 5
/** Maximum length of the path */
#define MAX_PATH_LENGTH 20

/** Find Eulerian Path in a graph
* @param graph Adjacency matrix of the graph
*/
void find_path(int graph[N][N]) {
int numofadj[N];
int stack[N];
int path[MAX_PATH_LENGTH];
int top = 0;
int path_length = 0;

// Find out number of edges each vertex has
// FIXME: Add the code here

// Find out how many vertex has odd number edges
// FIXME: Add the code here

// If number of vertex with odd number of edges is greater than two return "No
// Solution".
// FIXME: Add the code here

// If there is a path find the path
// Initialize empty stack and path take the starting current as discussed
// FIXME: Add the code here

// Loop will run until there is element in the stack or current edge has some
// neighbour.
// FIXME: Add the code here

// If the current vertex has at least one neighbour add the current vertex
// to stack, remove the edge between them and set the current to its
// neighbour.
// FIXME: Add the code here

// Add the last vertex to the path
// FIXME: Add the code here

// Print the path
// FIXME: Add the code here
}

int main() {
// Test case 1
int graph1[N][N] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 1, 0},
{0, 1, 1, 0, 0},
{1, 0, 0, 0, 0}};

printf("Test Case 1:\n");
find_path(graph1);

// Test case 2
int graph2[N][N] = {{0, 1, 0, 1, 1},
{1, 0, 1, 0, 1},
{0, 1, 0, 1, 1},
{1, 1, 1, 0, 0},
{1, 0, 1, 0, 0}};

printf("Test Case 2:\n");
find_path(graph2);

// Test case 3
int graph3[N][N] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 1},
{0, 1, 0, 1, 0},
{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0}};

printf("Test Case 3:\n");
find_path(graph3);

return 0;
}
61 changes: 61 additions & 0 deletions code/02-graphs-hamiltonian-cycle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>

/** Number of vertices in the graph */
#define N 5

/** A utility function to print solution */
void print_solution(int path[]) {
printf("Solution Exists: ");

for (int i = 0; i < N; i++) printf("%d ", path[i]);

// Let us print the first vertex again to show the complete cycle
printf("%d ", path[0]);
printf("\n");
printf("\n");
}

/** This function solves the Hamiltonian cycle problem using Backtracking.
* It returns false if there is no Hamiltonian Cycle possible,
* otherwise return true and prints the path.
*/
int hamiltonian_cycle(int graph[N][N]) {
// FIXME: Implement the function
// print_solution(path);
return 1;
}

/** Main function */
int main() {
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4) */
int graph1[N][N] = {
{0, 1, 0, 1, 0}, {1, 0, 1, 1, 1}, {0, 1, 0, 0, 1},
{1, 1, 0, 0, 1}, {0, 1, 1, 1, 0},
};

// Print the solution
printf("Test Case 1:\n");
hamiltonian_cycle(graph1);

/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4) */
int graph2[N][N] = {
{0, 1, 0, 1, 0}, {1, 0, 1, 1, 1}, {0, 1, 0, 0, 1},
{1, 1, 0, 0, 0}, {0, 1, 1, 0, 0},
};

// Print the solution
printf("Test Case 2:\n");
hamiltonian_cycle(graph2);

return 0;
}
38 changes: 38 additions & 0 deletions images/four_color_graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/graph_isomorphism.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit be2c4c6

Please sign in to comment.