Finish course 3 week 3 assignment
This commit is contained in:
13
src/main.rs
13
src/main.rs
@@ -4,6 +4,7 @@ mod huffman;
|
||||
mod jobs;
|
||||
mod k_clustering;
|
||||
mod k_clustering_big;
|
||||
mod mwis;
|
||||
mod merge_sort;
|
||||
mod min_cut;
|
||||
mod prims;
|
||||
@@ -23,6 +24,7 @@ use crate::prims::prims;
|
||||
use crate::quick_sort::quick_sort;
|
||||
use crate::ssc::ssc;
|
||||
use crate::two_sum::find_two_sums;
|
||||
use crate::mwis::mwis;
|
||||
use std::cmp::min;
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -120,7 +122,15 @@ fn c3a2() {
|
||||
fn c3a3() {
|
||||
let h = util::read_huffman_alphabet("data/c3a3_huffman.txt").unwrap();
|
||||
let r = huffman::build_huffman_tree(&h);
|
||||
println!("r1 = {} r2 = {}", r.1, r.0);
|
||||
|
||||
let s = util::read_max_weight_set("data/c3a3_mwis.txt").unwrap();
|
||||
let r3 = mwis(&s);
|
||||
println!("r1 = {} r2 = {} r3 = {:?}", r.1, r.0, r3);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn c3a4() {
|
||||
println!("continue here");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -134,4 +144,5 @@ fn main() {
|
||||
// c3a1();
|
||||
// c3a2();
|
||||
c3a3();
|
||||
c3a4();
|
||||
}
|
||||
|
||||
60
src/mwis.rs
Normal file
60
src/mwis.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IndependentSet {
|
||||
pub length: usize,
|
||||
pub weights: Vec<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Solution {
|
||||
weight: u64,
|
||||
nodes: Vec<usize>,
|
||||
}
|
||||
|
||||
pub fn mwis(s: &IndependentSet) -> Vec<usize> {
|
||||
let mut solutions: Vec<Solution> = Vec::new();
|
||||
|
||||
|
||||
solutions.push(Solution {
|
||||
weight: s.weights[0],
|
||||
nodes: vec![0],
|
||||
});
|
||||
|
||||
solutions.push(Solution {
|
||||
weight: s.weights[1],
|
||||
nodes: vec![1],
|
||||
});
|
||||
|
||||
for i in 2..s.weights.len() {
|
||||
let s1 = Solution {
|
||||
weight: solutions[i - 1].weight,
|
||||
nodes: solutions[i - 1].nodes.to_vec(),
|
||||
};
|
||||
|
||||
let mut s2_vec = solutions[i - 2].nodes.to_vec();
|
||||
s2_vec.push(i);
|
||||
let s2 = Solution {
|
||||
weight: solutions[i - 2].weight + s.weights[i],
|
||||
nodes: s2_vec,
|
||||
};
|
||||
|
||||
if s1.weight > s2.weight {
|
||||
solutions.push(s1);
|
||||
} else {
|
||||
solutions.push(s2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let solution = solutions.pop().unwrap();
|
||||
let mut result = Vec::new();
|
||||
let vertices = vec![0, 1, 2, 3, 16, 116, 516, 996];
|
||||
for v in &vertices {
|
||||
if solution.nodes.contains(v) {
|
||||
result.push(1);
|
||||
} else {
|
||||
result.push(0);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
21
src/util.rs
21
src/util.rs
@@ -5,6 +5,7 @@ use crate::k_clustering_big;
|
||||
use crate::min_cut;
|
||||
use crate::prims;
|
||||
use crate::ssc;
|
||||
use crate::mwis;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::{BufRead, BufReader, Error, ErrorKind};
|
||||
@@ -260,3 +261,23 @@ pub fn read_huffman_alphabet(path: &str) -> Result<huffman::HuffmanAlphabet, io:
|
||||
assert!(length == h.frequencies.len());
|
||||
Ok(h)
|
||||
}
|
||||
|
||||
pub fn read_max_weight_set(path: &str) -> Result<mwis::IndependentSet, io::Error> {
|
||||
let file = File::open(path)?;
|
||||
let mut lines = BufReader::new(file).lines();
|
||||
let line = lines.next().unwrap().unwrap();
|
||||
let length = line.parse().unwrap();
|
||||
|
||||
let mut s = mwis::IndependentSet {
|
||||
length: length,
|
||||
weights: Vec::new(),
|
||||
};
|
||||
|
||||
for line in lines {
|
||||
let line = line?;
|
||||
let weight = line.parse().unwrap();
|
||||
s.weights.push(weight);
|
||||
}
|
||||
assert!(length == s.weights.len());
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user