Finish specialization

This commit is contained in:
2021-02-28 23:27:04 -05:00
parent 1258ce2ceb
commit c6fa92aba2
3 changed files with 9 additions and 4 deletions

View File

@@ -5,3 +5,5 @@ by Tim Roughgarden from the Stanford University. I implemented the assignments
in Rust to learn more about the language. I took the class to refresh my in Rust to learn more about the language. I took the class to refresh my
knowledge about algorithms. knowledge about algorithms.
[Certificate](https://coursera.org/share/8af17a8221800221c68a8d7bfb69fd88)

View File

@@ -170,6 +170,7 @@ fn c4a3() {
let g = util::read_tsp_graph("data/c4a3_tsp.txt").unwrap(); let g = util::read_tsp_graph("data/c4a3_tsp.txt").unwrap();
let r = tsp_heuristic(g); let r = tsp_heuristic(g);
println!("r = {}", r); println!("r = {}", r);
// r = 1203406
} }
#[allow(dead_code)] #[allow(dead_code)]
@@ -180,6 +181,7 @@ fn c4a4() {
let r = sat2(s); let r = sat2(s);
println!("{} = {}", f, r); println!("{} = {}", f, r);
} }
// r = 101100
} }
fn main() { fn main() {

View File

@@ -25,13 +25,14 @@ fn get_random_assignment(n_clauses: usize) -> Vec<bool> {
pub fn sat2(s: Sat2) -> bool { pub fn sat2(s: Sat2) -> bool {
let n_clauses: f64 = s.n_clauses as f64; let n_clauses: f64 = s.n_clauses as f64;
let outer_repeats: usize = n_clauses.log(2.0).ceil() as usize; let outer_repeats: usize = n_clauses.log(2.0).ceil() as usize;
let inner_repeats: usize = 2 * s.n_clauses * s.n_clauses; let inner_repeats: usize = 2 * s.n_clauses;
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
for _ in 0..outer_repeats { for _ in 0..outer_repeats {
let mut a = get_random_assignment(s.n_clauses); let mut a = get_random_assignment(s.n_clauses);
for _ in 0..inner_repeats { let mut i = 0;
while i < inner_repeats {
let mut valid = true; let mut valid = true;
for c in &s.clauses { for c in &s.clauses {
if a[c.v1] == c.n1 || a[c.v2] == c.n2 { if a[c.v1] == c.n1 || a[c.v2] == c.n2 {
@@ -45,7 +46,7 @@ pub fn sat2(s: Sat2) -> bool {
a[c.v2] = !a[c.v2]; a[c.v2] = !a[c.v2];
} }
valid = false; valid = false;
break; i += 1;
} }
} }
if valid { if valid {