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
/
Copy path02-tree_detection.zig
106 lines (97 loc) · 2.63 KB
/
02-tree_detection.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const std = @import("std");
const print = @import("std").debug.print;
/// Number of vertices in the graph
const N = 5;
/// Search for a cycle in a graph
///
/// # Parameters
///
/// - `graph`: adjacency matrix of the graph
/// - `v`: current vertex
/// - `visited`: array of visited vertices
/// - `parent`: parent of the current vertex
///
/// # Returns
///
/// `true` if a cycle is found, `false` otherwise
fn searchCycle(graph: *[N][N]i32, v: usize, visited: *[N]bool, parent: isize) bool {
visited[v] = true;
for (0..N) |i| {
if (graph.*[v][i] != 0) {
if (!visited[i]) {
if (searchCycle(graph, i, visited, @intCast(v))) return true;
} else if (@as(isize, @intCast(i)) != parent) {
// Cycle detected
return true;
}
}
}
return false;
}
/// Check if all vertices have been visited
///
/// # Parameters
///
/// - `visited`: array of visited vertices
///
/// # Returns
///
/// `true` if all vertices have been visited, `false` otherwise
fn allVisited(visited: [N]bool) bool {
var i: usize = 0;
// Find first unvisited vertex
while (i < N and visited[i]) : (i += 1) {}
// Return true if no unvisited vertices found, false otherwise
return i == N;
}
/// Check if a graph is a tree
///
/// # Parameters
///
/// - `graph`: adjacency matrix of the graph
///
/// # Returns
///
/// `true` if the graph is a tree, `false` otherwise
fn isTree(graph: *[N][N]i32) bool {
var visited: [N]bool = [_]bool{false} ** N;
// Perform depth-first search from vertex 0 to check if all vertices
// are reachable (connected)
const has_cycle = searchCycle(graph, 0, &visited, -1);
if (has_cycle) return false;
// Check if all vertices have been visited
if (!allVisited(visited)) return false;
// Return true if no cycle is found and all vertices are connected
return true;
}
/// Main function
pub fn main() !void {
// Tree example
// (0)--(1)--(2)
// |
// |
// |
// (3)-------(4)
var graph1: [N][N]i32 = .{
.{ 0, 1, 0, 1, 0 },
.{ 1, 0, 1, 0, 0 },
.{ 0, 1, 0, 0, 0 },
.{ 1, 0, 0, 0, 1 },
.{ 0, 0, 0, 1, 0 },
};
print("Test Case 1: Is Tree? {}\n", .{isTree(&graph1)});
// Non-tree example (has at least one cycle)
// (0)--(1)--(2)
// | / \ |
// | / \ |
// | / \ |
// (3)-------(4)
var graph2: [N][N]i32 = .{
.{ 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("Test Case 1: Is Tree? {}\n", .{isTree(&graph2)});
}