summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 3aa93201f99b65926c3ce483ad4ea97f0c44d576 (plain)
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
107
108
109
110
111
112
113
114
115
116
use std::io::{self, BufRead};
use std::fmt;

struct Graph {
    size: usize,
    matrix: Vec<Vec<u8>>
}

impl Clone for Graph {
    fn clone(&self) -> Self {
        let mut matrix = vec![vec![0; self.size]; self.size];
        for row in 0..self.size {
            for col in 0..self.size {
                matrix[row][col] = self.matrix[row][col];
            }
        }
        return Graph { size: self.size, matrix };
    }
}

impl fmt::Display for Graph {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        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(());
    }
}

impl Graph {
    fn from_g6(line: &String) -> Graph {
        let mut chars: Vec<u8> = Vec::new();
        for character in line.chars() {
            chars.push((character as i32 - 63) as u8);
        }
        // TODO: spec allows multi-byte vector size
        let size = chars[0] as usize;
        let bytes = &chars[1..];

        let mut matrix: Vec<Vec<u8>> = vec![vec![0; size]; size];
        let mut i = 0;
        for col in 1..size {
            for row in 0..col {
                let bit: u8 = bytes[i / 6] >> (5 - i % 6) & 1;
                matrix[row][col] = bit;
                matrix[col][row] = bit;
                i += 1;
            }
        }
        return Graph { size, matrix };
    }

    fn degree(&self, vertex: usize) -> usize {
        return self.matrix[vertex as usize].iter().sum::<u8>() as usize;
    }

    fn get_closure(&self) -> Graph {
        let mut closure = self.clone();
        for _ in 0..(closure.size * closure.size) {
            for row in 0..closure.size {
                for col in 0..closure.size {
                    if row == col { continue }
                    let sum = closure.degree(row) + closure.degree(col);
                    if sum >= closure.size {
                        closure.matrix[row][col] = 1;
                    }
                }
            }
        }
        return closure;
    }

    fn is_complete(&self) -> bool {
        for row in 0..self.size {
            for col in 0..self.size {
                if row != col && self.matrix[row][col] != 1 {
                    return false;
                }
            }
        }
        return true;
    }
}

fn main() {
    let stdin = io::stdin();

    let mut count = 0;
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        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!("");
    }
    println!("Count of Hamiltonian graphs: {}", count);
}