Fix bug and add reverse search for minimum

This commit is contained in:
hal8174 2024-11-14 21:52:45 +01:00
parent ec0592cb05
commit 53f8c2034c
2 changed files with 24 additions and 1 deletions

View file

@ -155,8 +155,11 @@ enum Method {
#[derive(Debug, Parser)]
struct Args {
filename: PathBuf,
#[clap(default_value = "reduction2")]
method: Method,
k: Option<u16>,
#[clap(short, long)]
reverse: bool,
}
fn main() {
@ -169,6 +172,20 @@ fn main() {
let r = vertex_cover(&graph, k, args.method);
eprintln!("{}: {:?}", k, start.elapsed());
r
} else if args.reverse {
let mut k = graph.num_vertices();
let mut last_solution = None;
loop {
let start = std::time::Instant::now();
let r = vertex_cover(&graph, k, args.method);
eprintln!("{}: {:?}", k, start.elapsed());
if let Some(r) = r {
k = r.len() as u16 - 1;
last_solution = Some(r);
} else {
break last_solution;
};
}
} else {
let mut k = 0;
loop {

View file

@ -237,6 +237,8 @@ pub fn reduction2(graph: &mut Graph, k: u16) -> Option<Vec<u16>> {
let _ = graph.reinsert_vertices(1);
r
} else if graph.is_empty() {
Some(Vec::new())
} else if k == 0 {
if graph.is_empty() {
Some(Vec::new())
@ -343,6 +345,10 @@ pub fn reduction2(graph: &mut Graph, k: u16) -> Option<Vec<u16>> {
}
}
} else if let Some(vertex) = graph.get_vertex_with_degree_greater(3) {
if k < 1 {
return None;
}
graph.remove_vertex(vertex);
match reduction2(graph, k - 1) {
@ -374,6 +380,6 @@ pub fn reduction2(graph: &mut Graph, k: u16) -> Option<Vec<u16>> {
}
}
} else {
unreachable!()
unreachable!("{k}")
}
}