Implement Papadimitriou's algorithm

This commit is contained in:
Felix Martin 2021-02-28 21:02:37 -05:00
parent 78b8cf95b6
commit 1258ce2ceb
11 changed files with 3100116 additions and 4 deletions

View File

@ -1,3 +1,7 @@
# algos
My solutions for the Coursera specialization algorithms in Rust.
This repository contains my solutions to the Coursera Algorithms specialization
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
knowledge about algorithms.

5
data/c4a4_sat.txt Normal file
View File

@ -0,0 +1,5 @@
4
1 2
-1 3
3 4
-2 -4

100001
data/c4a4_sat1.txt Normal file

File diff suppressed because it is too large Load Diff

200001
data/c4a4_sat2.txt Normal file

File diff suppressed because it is too large Load Diff

400001
data/c4a4_sat3.txt Normal file

File diff suppressed because it is too large Load Diff

600001
data/c4a4_sat4.txt Normal file

File diff suppressed because it is too large Load Diff

800001
data/c4a4_sat5.txt Normal file

File diff suppressed because it is too large Load Diff

1000001
data/c4a4_sat6.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ mod knapsack;
mod merge_sort;
mod min_cut;
mod mwis;
mod papadimitriou;
mod prims;
mod quick_sort;
mod ssc;
@ -25,6 +26,7 @@ use crate::knapsack::knapsack;
use crate::merge_sort::merge_sort_inversions;
use crate::min_cut::min_cut;
use crate::mwis::mwis;
use crate::papadimitriou::sat2;
use crate::prims::prims;
use crate::quick_sort::quick_sort;
use crate::ssc::ssc;
@ -172,7 +174,12 @@ fn c4a3() {
#[allow(dead_code)]
fn c4a4() {
println!("we are done soon!");
for i in 1..7 {
let f = format!("data/c4a4_sat{}.txt", i);
let s = util::read_sat2(&f).unwrap();
let r = sat2(s);
println!("{} = {}", f, r);
}
}
fn main() {
@ -188,6 +195,6 @@ fn main() {
// c3a3();
// c3a4();
// c4a1();
c4a3();
// c4a4();
// c4a3();
c4a4();
}

57
src/papadimitriou.rs Normal file
View File

@ -0,0 +1,57 @@
use rand::Rng;
#[derive(Debug)]
pub struct Sat2 {
pub n_clauses: usize,
pub clauses: Vec<Clause>,
}
#[derive(Debug)]
pub struct Clause {
pub v1: usize,
pub n1: bool,
pub v2: usize,
pub n2: bool,
}
fn get_random_assignment(n_clauses: usize) -> Vec<bool> {
let mut rng = rand::thread_rng();
let mut assignment = vec![false; n_clauses];
for i in 0..n_clauses {
assignment[i] = rng.gen_bool(0.5);
}
assignment
}
pub fn sat2(s: Sat2) -> bool {
let n_clauses: f64 = s.n_clauses as f64;
let outer_repeats: usize = n_clauses.log(2.0).ceil() as usize;
let inner_repeats: usize = 2 * s.n_clauses * s.n_clauses;
let mut rng = rand::thread_rng();
for _ in 0..outer_repeats {
let mut a = get_random_assignment(s.n_clauses);
for _ in 0..inner_repeats {
let mut valid = true;
for c in &s.clauses {
if a[c.v1] == c.n1 || a[c.v2] == c.n2 {
// println!("{:?} matches", c);
} else {
// println!("{:?} doesn't match", c);
let first = rng.gen_bool(0.5);
if first {
a[c.v1] = !a[c.v1];
} else {
a[c.v2] = !a[c.v2];
}
valid = false;
break;
}
}
if valid {
return true;
}
}
}
false
}

View File

@ -6,6 +6,7 @@ use crate::k_clustering_big;
use crate::knapsack;
use crate::min_cut;
use crate::mwis;
use crate::papadimitriou;
use crate::prims;
use crate::ssc;
use crate::tsp;
@ -382,3 +383,35 @@ pub fn read_tsp_graph(path: &str) -> Result<tsp::Graph, io::Error> {
Ok(g)
}
pub fn read_sat2(path: &str) -> Result<papadimitriou::Sat2, io::Error> {
let file = File::open(path)?;
let mut lines = BufReader::new(file).lines();
let line = lines.next().unwrap()?;
let n_clauses = line.parse().unwrap();
let mut s = papadimitriou::Sat2 {
n_clauses: n_clauses,
clauses: Vec::new(),
};
for line in lines {
let line = line?;
let mut it = line.split_whitespace();
let var1: i32 = it.next().unwrap().parse().unwrap();
let neg1 = var1 < 0;
let var2: i32 = it.next().unwrap().parse().unwrap();
let neg2 = var2 < 0;
let var1: usize = var1.abs() as usize;
let var2: usize = var2.abs() as usize;
let c = papadimitriou::Clause {
v1: var1 - 1,
n1: !neg1,
v2: var2 - 1,
n2: !neg2,
};
s.clauses.push(c);
}
Ok(s)
}