summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs37
1 files changed, 22 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs
index 3aa9320..c8865fa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,9 +1,10 @@
-use std::io::{self, BufRead};
use std::fmt;
+use std::io::{self, BufRead};
+use std::time::Instant;
struct Graph {
size: usize,
- matrix: Vec<Vec<u8>>
+ matrix: Vec<Vec<u8>>,
}
impl Clone for Graph {
@@ -14,7 +15,10 @@ impl Clone for Graph {
matrix[row][col] = self.matrix[row][col];
}
}
- return Graph { size: self.size, matrix };
+ return Graph {
+ size: self.size,
+ matrix,
+ };
}
}
@@ -59,7 +63,11 @@ impl Graph {
}
fn degree(&self, vertex: usize) -> usize {
- return self.matrix[vertex as usize].iter().sum::<u8>() as usize;
+ let mut sum: usize = 0;
+ for i in &self.matrix[vertex] {
+ sum += *i as usize;
+ }
+ return sum;
}
fn get_closure(&self) -> Graph {
@@ -67,7 +75,9 @@ impl Graph {
for _ in 0..(closure.size * closure.size) {
for row in 0..closure.size {
for col in 0..closure.size {
- if row == col { continue }
+ if row == col {
+ continue;
+ }
let sum = closure.degree(row) + closure.degree(col);
if sum >= closure.size {
closure.matrix[row][col] = 1;
@@ -93,24 +103,21 @@ impl Graph {
fn main() {
let stdin = io::stdin();
+ let mut t = 0;
let mut count = 0;
for line in stdin.lock().lines() {
let line = line.unwrap();
+
+ let start = Instant::now();
let g = Graph::from_g6(&line);
let closure = g.get_closure();
-
- println!("Vector of size {}", g.size);
- println!("{}", g);
- println!("Its closure:");
- println!("{}", closure);
if closure.is_complete() {
- println!("Graph {} is Hamiltonian", &line);
count += 1;
}
- else {
- println!("Graph {} is not Hamiltonian", &line);
- }
- println!("");
+ let elapsed = start.elapsed();
+ t += elapsed.as_nanos();
}
+
+ println!("Time elapsed: {}s", t as f64 / 1e9);
println!("Count of Hamiltonian graphs: {}", count);
}