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

Commit

Permalink
Merge pull request #21 from storopoli/storopoli/zig
Browse files Browse the repository at this point in the history
feat: zig examples
  • Loading branch information
storopoli authored Oct 6, 2024
2 parents a1fba77 + 7a00a96 commit af421d3
Show file tree
Hide file tree
Showing 48 changed files with 2,109 additions and 103 deletions.
7 changes: 3 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "Graphs and Algorithmic Complexity",
"image": "mcr.microsoft.com/devcontainers/base:alpine",

"postCreateCommand": "sudo apk update && sudo apk add just typst gdb",

"postCreateCommand": "sudo apk update && sudo apk add just typst gdb zig",
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools",
"vadimcn.vscode-lldb",
"ziglang.vscode-zig",
"myriad-dreamin.tinymist"
]
}
}
}
}
18 changes: 18 additions & 0 deletions .github/workflows/c-code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: c-code
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
clang-format:
name: Check if C code compiles and runs
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup just
uses: extractions/setup-just@v2
- name: Compile and run C code
run: just c-run
20 changes: 20 additions & 0 deletions .github/workflows/zig-code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: zig-code
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
clang-format:
name: Check if zig code compiles and runs
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup zig
uses: mlugg/setup-zig@v1
- name: Setup just
uses: extractions/setup-just@v2
- name: Compile and run zig code
run: just z-run
18 changes: 18 additions & 0 deletions .github/workflows/zig-fmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: zig-fmt
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
clang-format:
name: Check if zig code is formatted
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup zig
uses: mlugg/setup-zig@v1
- name: Check zig code format
run: zig fmt --check ./code/zig/*.zig
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@

# C++
*.out
*.exe
*.exe

# Output
output/
10 changes: 9 additions & 1 deletion README-pt.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Teoria dos Grafos e Complexidade Computacional

[![CC0](https://img.shields.io/badge/License-CC0-lightgrey.svg)](https://creativecommons.org/publicdomain/zero/1.0/)
[![C code](https://img.shields.io/badge/code-C-blue)](./code/c)
[![Zig code](https://img.shields.io/badge/code-Zig-orange)](./code/zig)
[![Rodar no Repl.it](https://repl.it/badge/github/storopoli/grafos-complexidade)](https://repl.it/github/storopoli/grafos-complexidade)
[![Abrir no Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/storopoli/grafos-complexidade)

Expand Down Expand Up @@ -30,9 +32,15 @@
1. Divisão e Conquista; e
1. Algoritmos Gulosos.

## Exemplos de Código
A principal linguagem de programação usada para os exemplos é C.
Também há exemplos em Zig.
Confira-os nos diretórios `code/c/` e `code/zig/`, respectivamente.

## Dependências

- Compilador C/C++; e
- Compilador C/C++;
- (Opcional) compilador [Zig](https://ziglang.org); e
- [`typst`](https://typst.app) para os slides.

Os slides são gerados usando [Typst](https://typst.app) com GitHub Actions
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Graph Theory and Computational Complexity

[![CC0](https://img.shields.io/badge/License-CC0-lightgrey.svg)](https://creativecommons.org/publicdomain/zero/1.0/)
[![C code](https://img.shields.io/badge/code-C-blue)](./code/c)
[![Zig code](https://img.shields.io/badge/code-Zig-orange)](./code/zig)
[![Run on Repl.it](https://repl.it/badge/github/storopoli/grafos-complexidade)](https://repl.it/github/storopoli/grafos-complexidade)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/storopoli/grafos-complexidade)

Expand Down Expand Up @@ -30,9 +32,16 @@
1. Divide and Conquer; and
1. Greedy Algorithms.

## Code Examples

The primary programming language used for examples is C.
Also there are [Zig](https://ziglang.org) examples.
Check them at `code/c/` and `code/zig/` directories, respectively.

## Dependencies

- C/C++ Compiler; and
- C/C++ Compiler;
- (Optional) [Zig](https://ziglang.org) compiler; and
- [`typst`](https://typst.app) for the slides.

The slides are generated using [Typst](https://typst.app) with GitHub Actions
Expand Down
32 changes: 0 additions & 32 deletions code/01-graphs-adjacency-list.c

This file was deleted.

57 changes: 0 additions & 57 deletions code/01-graphs-adjacency-matrix.c

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
139 changes: 139 additions & 0 deletions code/zig/01-graphs_eulerian_path.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const std = @import("std");
const print = std.debug.print;

/// Number of vertices in the graph
const N = 5;
/// Maximum length of the path
const MAX_PATH_LENGTH = 20;

/// Find Eulerian Path in a graph
///
/// # Parameters
///
/// - `graph`: Adjacency matrix of the graph
fn findPath(graph: *[N][N]i32) void {
var numofadj: [N]i32 = [_]i32{0} ** N;
var stack: [N]i32 = [_]i32{0} ** N;
var path: [MAX_PATH_LENGTH]i32 = [_]i32{0} ** MAX_PATH_LENGTH;
var top: usize = 0;
var path_length: usize = 0;

// Find out number of edges each vertex has
for (0..N) |i| {
for (0..N) |j| {
numofadj[i] += graph.*[i][j];
}
}

// Find out how many vertices have an odd number of edges
var startpoint: usize = 0;
var numofodd: i32 = 0;
var idx_i: usize = N;
while (idx_i > 0) : (idx_i -= 1) {
const idx = idx_i - 1;
if (@mod(numofadj[idx], 2) == 1) {
numofodd += 1;
startpoint = idx;
}
}

// If the number of vertices with odd number of edges is greater than two, return "No Solution".
if (numofodd > 2) {
print("No Solution\n", .{});
return;
}

// Initialize empty stack and path; take the starting current as discussed
var cur: usize = startpoint;

// Loop will run until there is an element in the stack or current vertex has some neighbor.
while (top > 0 or numofadj[cur] > 0) {
// If current node has no neighbors, add it to path and pop stack; set new current to the popped element
if (numofadj[cur] == 0) {
path[path_length] = @intCast(cur);
path_length += 1;
if (top > 0) {
top -= 1;
cur = @intCast(stack[top]);
} else {
break;
}
}
// If the current vertex has at least one neighbor, add the current vertex to stack,
// remove the edge between them, and set the current to its neighbor.
else {
for (0..N) |j| {
if (graph.*[cur][j] == 1) {
stack[top] = @intCast(cur);
top += 1;
graph.*[cur][j] = 0;
graph.*[j][cur] = 0;
numofadj[cur] -= 1;
numofadj[j] -= 1;
cur = j;
break;
}
}
}
}

// Add the last vertex to the path
path[path_length] = @intCast(cur);
path_length += 1;

// Print the path
var idx: usize = path_length;
while (idx > 0) : (idx -= 1) {
print("{d} -> ", .{path[idx - 1]});
}
print("\n", .{});
}

/// Main function
pub fn main() !void {
// Test case 1:
// 0 --- 1
// | | \
// | 2--3
// 4
var graph1: [N][N]i32 = .{
.{ 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 },
};

print("Test Case 1:\n", .{});
findPath(&graph1);

// Test case 2:
// 0 -- 1 -- 2
// /| / \ | \
// 3--4 5
var graph2: [N][N]i32 = .{
.{ 0, 1, 0, 1, 1 },
.{ 1, 0, 1, 0, 1 },
.{ 0, 1, 0, 1, 1 },
.{ 1, 0, 1, 0, 0 },
.{ 1, 1, 1, 0, 0 },
};

print("Test Case 2:\n", .{});
findPath(&graph2);

// Test case 3:
// 0 --- 1
// /|\ |\
// 2 4---5 3
var graph3: [N][N]i32 = .{
.{ 0, 1, 1, 0, 1 },
.{ 1, 0, 0, 1, 1 },
.{ 1, 0, 0, 1, 0 },
.{ 0, 1, 1, 0, 1 },
.{ 1, 1, 0, 1, 0 },
};

print("Test Case 3:\n", .{});
findPath(&graph3);
}
Loading

0 comments on commit af421d3

Please sign in to comment.