This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from storopoli/storopoli/slides/graphs
feat: Grafos, Caminhos e Ciclos
- Loading branch information
Showing
16 changed files
with
1,321 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.