summaryrefslogtreecommitdiff
path: root/graph-checker/src/graph.rs
diff options
context:
space:
mode:
authorAndrew Guschin <guschin.drew@gmail.com>2023-04-10 16:31:57 +0400
committerAndrew Guschin <guschin.drew@gmail.com>2023-04-10 16:31:57 +0400
commit30b7dc646f262ec6d97ac701859d0cf9008816c9 (patch)
tree6550cf940989b7d49f9f7f231b3fcb9cf00cf9c5 /graph-checker/src/graph.rs
parent134dd321468448c67e8d24cbbcee3030bcfb3e39 (diff)
Added is_free_of, is_2_connected and thorem 3.1
Diffstat (limited to 'graph-checker/src/graph.rs')
-rw-r--r--graph-checker/src/graph.rs88
1 files changed, 87 insertions, 1 deletions
diff --git a/graph-checker/src/graph.rs b/graph-checker/src/graph.rs
index 7ba6cb4..25d7f72 100644
--- a/graph-checker/src/graph.rs
+++ b/graph-checker/src/graph.rs
@@ -1,13 +1,14 @@
use std::collections::HashSet;
use std::fmt;
+// TODO: manual Debug impl
#[derive(Clone, PartialEq, Eq)]
pub struct Graph {
pub size: usize,
pub matrix: Vec<Vec<i32>>,
}
-#[derive(Clone, PartialEq, Eq)]
+#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Cutset {
pub cardinality: usize,
pub vertices: Vec<i32>,
@@ -31,6 +32,25 @@ impl fmt::Display for Graph {
}
}
+// TODO: manual Debug impl
+impl fmt::Debug for Graph {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "\n")?;
+ for row in 0..self.size {
+ for col in 0..self.size {
+ write!(f, "{}", self.matrix[row][col])?;
+ if col < self.size - 1 {
+ write!(f, " ")?;
+ }
+ }
+ if row < self.size - 1 {
+ write!(f, "\n")?;
+ }
+ }
+ return Ok(());
+ }
+}
+
fn iterate(n: usize) -> Vec<Vec<i32>> {
let mut components = Vec::new();
@@ -216,6 +236,33 @@ impl Graph {
self.count_components_partial(&vec![1; self.size])
}
+ // TODO: replace with is_n_connected
+ pub fn is_2_connected(&self) -> bool {
+ let mut vertices = vec![1; self.size];
+ for i in 0..self.size {
+ for j in 0..self.size {
+ if i == j {
+ continue;
+ }
+ vertices[i] = 0;
+ vertices[j] = 0;
+ let c = self.count_components_partial(&vertices);
+ vertices[i] = 1;
+ vertices[j] = 1;
+
+ if c != 1 {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ pub fn is_n_connected(&self, n: usize) -> bool {
+ // TODO: implement
+ todo!();
+ }
+
pub fn get_closure(&self) -> Graph {
self.get_closure_traced(false)
}
@@ -329,4 +376,43 @@ impl Graph {
}
return false;
}
+
+ pub fn is_free_of(&self, graphs: &Vec<Graph>) -> bool {
+ fn trim_cutset(cutset: &Cutset) -> Graph {
+ let mut mat = vec![vec![0; cutset.cardinality]; cutset.cardinality];
+
+ let mut i = 0;
+ for vi in 0..cutset.graph.size {
+ if cutset.vertices[vi] == 0 {
+ continue;
+ }
+ let mut j = 0;
+ for vj in 0..cutset.graph.size {
+ if cutset.vertices[vj] == 0 {
+ continue;
+ }
+ mat[i][j] = cutset.graph.matrix[vi][vj];
+ j += 1;
+ }
+ i += 1;
+ }
+ return Graph {
+ size: cutset.cardinality,
+ matrix: mat,
+ };
+ }
+
+ for cutset in self.cutsets() {
+ let subg = trim_cutset(&cutset);
+ for g in graphs {
+ if cutset.cardinality != g.size {
+ continue;
+ }
+ if subg.is_isomorphic(g) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
}