diff --git a/src/jobs.rs b/src/jobs.rs index d560125..ded07e5 100644 --- a/src/jobs.rs +++ b/src/jobs.rs @@ -1,17 +1,48 @@ +use std::cmp::Ordering; + #[derive(Debug)] pub struct Job { - pub weight: u32, - pub length: u32, + pub weight: i64, + pub length: i64, } pub type Jobs = Vec; +fn weighted_completion_time(jobs: &Jobs) -> i64 { + let mut time = 0; + let mut weighted_completion_time = 0; -fn weighted_completion_time(jobs: &Jobs) -> usize { + for job in jobs { + let completion_time = time + job.length; + weighted_completion_time += completion_time * job.weight; + time += job.length; + } - jobs.len() + weighted_completion_time } -pub fn jobs1(jobs: &Jobs) -> usize { +fn compare_1(a: &Job, b: &Job) -> Ordering { + let da = a.weight - a.length; + let db = b.weight - b.length; + if da == db { + b.weight.cmp(&a.weight) + } else { + db.cmp(&da) + } +} + +fn compare_2(a: &Job, b: &Job) -> Ordering { + let da: f64 = a.weight as f64 / a.length as f64; + let db: f64 = b.weight as f64 / b.length as f64; + db.partial_cmp(&da).unwrap() +} + +pub fn jobs_1(jobs: &mut Jobs) -> i64 { + jobs.sort_by(compare_1); weighted_completion_time(&jobs) -} \ No newline at end of file +} + +pub fn jobs_2(jobs: &mut Jobs) -> i64 { + jobs.sort_by(compare_2); + weighted_completion_time(&jobs) +} diff --git a/src/main.rs b/src/main.rs index 2ca00ac..9072156 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use crate::min_cut::min_cut; use crate::quick_sort::quick_sort; use crate::ssc::ssc; use crate::two_sum::find_two_sums; -use crate::jobs::jobs1; +use crate::jobs::{jobs_1, jobs_2}; use crate::prims::prims; @@ -91,12 +91,15 @@ fn c2a4() { #[allow(dead_code)] fn c3a1() { - let jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap(); - let r1 = jobs1(&jobs); - let r2 = -1; - let r3 = prims(); + let mut jobs = util::read_jobs("data/c3a1_jobs.txt").unwrap(); + let r1 = jobs_1(&mut jobs); + let r2 = jobs_2(&mut jobs); + + let g = util::read_weighted_graph("data/c3a1_edges.txt").unwrap(); + let r3 = prims(&g); println!("r1 = {}; r2 = {}; r3 = {} ", r1, r2, r3); + // r1 = 69119377652; r2 != 67311454237; } fn main() { diff --git a/src/prims.rs b/src/prims.rs index bfe7e60..7d9f9ec 100644 --- a/src/prims.rs +++ b/src/prims.rs @@ -1,4 +1,14 @@ +#[derive(Debug, Clone)] -pub fn prims() -> usize { +pub struct Edge { + pub node_a: u32, + pub node_b: u32, + pub weight: u32, +} + +pub type WeightedGraph = Vec; + +pub fn prims(graph: &WeightedGraph) -> usize { + println!("{:?}", graph); 42 } \ No newline at end of file diff --git a/src/util.rs b/src/util.rs index 6524e61..152d5af 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,6 +3,7 @@ use std::io; use std::io::{BufRead, BufReader, Error, ErrorKind}; use std::vec::Vec; use crate::jobs::{Job, Jobs}; +use crate::prims::{WeightedGraph, Edge}; #[derive(Debug, Clone)] pub struct Graph { @@ -149,8 +150,8 @@ pub fn read_jobs(path: &str) -> Result { for line in lines { let line = line?; let mut nodes = line.split_whitespace(); - let weight: u32 = nodes.next().unwrap().parse().unwrap(); - let length: u32 = nodes.next().unwrap().parse().unwrap(); + let weight = nodes.next().unwrap().parse().unwrap(); + let length = nodes.next().unwrap().parse().unwrap(); let job = Job { weight: weight, length: length, @@ -160,3 +161,30 @@ pub fn read_jobs(path: &str) -> Result { Ok(jobs) } + +pub fn read_weighted_graph(path: &str) -> Result { + let g = Vec::new(); + let file = File::open(path)?; + let br = BufReader::new(file); + let mut lines = br.lines(); + let line = lines.next().unwrap().unwrap(); + let mut nodes = line.split_whitespace(); + let n_nodes: usize = nodes.next().unwrap().parse().unwrap(); + let n_edges: usize = nodes.next().unwrap().parse().unwrap(); + + println!("{:?} {:?}", n_nodes, n_edges); + // lines.next(); + // for line in lines { + // let line = line?; + // let mut nodes = line.split_whitespace(); + // let weight = nodes.next().unwrap().parse().unwrap(); + // let length = nodes.next().unwrap().parse().unwrap(); + // let job = Job { + // weight: weight, + // length: length, + // }; + // jobs.push(job); + // } + + Ok(g) +}