diff options
| author | Andrew Guschin <guschin@altlinux.org> | 2024-04-01 00:47:17 +0400 |
|---|---|---|
| committer | Andrew Guschin <guschin@altlinux.org> | 2024-04-01 00:52:51 +0400 |
| commit | 950c3cfe266453a2a723c0d490baf1cd330a9c1e (patch) | |
| tree | 5216eaad2730c631678a1eac8924bfd6a48dedd9 /graph-checker/src/graph.rs | |
| parent | f7aa97e10a2fbddb76e1893b7deb193ad56e7192 (diff) | |
feat: replace stdin with geng-generator
Diffstat (limited to 'graph-checker/src/graph.rs')
| -rw-r--r-- | graph-checker/src/graph.rs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/graph-checker/src/graph.rs b/graph-checker/src/graph.rs index dd8a6f4..67dbb59 100644 --- a/graph-checker/src/graph.rs +++ b/graph-checker/src/graph.rs @@ -4,13 +4,13 @@ use std::fmt; #[derive(Clone, PartialEq, Eq)] pub struct Graph { pub size: usize, - pub matrix: Vec<Vec<i32>>, + pub matrix: Vec<Vec<u32>>, } #[derive(Clone, PartialEq, Eq, Debug)] pub struct Cutset { pub cardinality: usize, - pub vertices: Vec<i32>, + pub vertices: Vec<u32>, pub graph: Graph, } @@ -73,10 +73,10 @@ fn trim_cutset(cutset: &Cutset) -> Graph { }; } -fn iterate(n: usize) -> Vec<Vec<i32>> { +fn iterate(n: usize) -> Vec<Vec<u32>> { let mut components = Vec::new(); - let mut v: Vec<i32> = vec![0; n]; + let mut v: Vec<u32> = vec![0; n]; loop { let mut sum: usize = 0; for i in &v { @@ -104,6 +104,19 @@ fn iterate(n: usize) -> Vec<Vec<i32>> { } impl Graph { + pub fn to_g6(&self) -> String { + let mut g = vec![0; self.size]; + for i in 0..self.size { + let mut row: u32 = 0; + for j in 0..self.size { + row |= self.matrix[i][j] << (31 - j); + } + g[i] = row; + } + + crate::geng::get_g6(g, self.size) + } + pub fn from_g6(line: &String) -> Graph { let mut chars: Vec<u8> = Vec::new(); for character in line.chars() { @@ -112,11 +125,11 @@ impl Graph { let size = chars[0] as usize; let bytes = &chars[1..]; - let mut matrix: Vec<Vec<i32>> = vec![vec![0; size]; size]; + let mut matrix: Vec<Vec<u32>> = vec![vec![0; size]; size]; let mut i = 0; for col in 1..size { for row in 0..col { - let bit: i32 = (bytes[i / 6] >> (5 - i % 6) & 1) as i32; + let bit: u32 = (bytes[i / 6] >> (5 - i % 6) & 1) as u32; matrix[row][col] = bit; matrix[col][row] = bit; i += 1; @@ -195,7 +208,7 @@ impl Graph { } } } - let cardinality = vertices.iter().sum::<i32>() as usize; + let cardinality = vertices.iter().sum::<u32>() as usize; cs.push(Cutset { cardinality, vertices: vertices.clone(), @@ -231,7 +244,7 @@ impl Graph { } } - fn count_components_partial(&self, included_vertices: &Vec<i32>) -> usize { + fn count_components_partial(&self, included_vertices: &Vec<u32>) -> usize { let mut visited = vec![0; self.size]; for i in 0..included_vertices.len() { if included_vertices[i] == 0 { |