Skip to content

Commit

Permalink
Removed log crate and adapted tests to new rustc hash version
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoulLuque committed Jun 26, 2024
1 parent caadfef commit f97268a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 88 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
[package]
name = "treewidth_heuristic_clique_graph"
name = "treewidth_heuristic_using_clique_graph"
version = "0.1.0"
edition = "2021"
rust-version = "1.76"
authors = ["Raoul Luque <raoulsluque@gmail.com>"]
description = "A heuristic for calculating an upper bound on the treewidth of graphs using clique-graphs"
description = "A heuristic for calculating an upper bound on the treewidth of graphs using clique graphs"
repository = "https://github.com/RaoulLuque/treewidth-heuristic-clique-graph"


[dependencies]
petgraph = "0.6.4"
itertools = "0.13"
rand = "0.8.5"
log = "0.4"
rustc-hash = { git = "https://github.com/rust-lang/rustc-hash"}
20 changes: 9 additions & 11 deletions src/check_tree_decomposition.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{
collections::{HashMap, HashSet},
hash::BuildHasher,
};

use itertools::Itertools;
use log::error;
use petgraph::{
prelude::*,
visit::{IntoNodeReferences, NodeRef},
};
use std::{
collections::{HashMap, HashSet},
hash::BuildHasher,
};

/// Given a tree decomposition checks if it is a valid tree decomposition. Returns true if the decomposition
/// is valid, returns false otherwise.
Expand All @@ -31,7 +29,7 @@ pub fn check_tree_decomposition<N, E, O, S: BuildHasher + Default>(
.node_weights()
.find(|s| s.contains(&vertex))
{
error!("Tree decomposition doesn't contain vertex: {:?}", vertex);
println!("Tree decomposition doesn't contain vertex: {:?}", vertex);
return false;
}
}
Expand All @@ -51,7 +49,7 @@ pub fn check_tree_decomposition<N, E, O, S: BuildHasher + Default>(
}

if !edge_is_contained {
error!("Tree decomposition doesn't contain edge: {:?}", edge_as_set);
println!("Tree decomposition doesn't contain edge: {:?}", edge_as_set);
return false;
}
}
Expand Down Expand Up @@ -106,7 +104,7 @@ pub fn check_tree_decomposition<N, E, O, S: BuildHasher + Default>(
.collect();

// DEBUG
error!("Between the vertex: {:?} \n
println!("Between the vertex: {:?} \n
and vertex: {:?} \n
the bags intersect with: {:?} \n
however vertex {:?} along their path doesn't contain the following vertices: {:?} \n \n
Expand All @@ -119,12 +117,12 @@ pub fn check_tree_decomposition<N, E, O, S: BuildHasher + Default>(
{
// DEBUG
for node_index in vertices_missing_along_path {
error!("The intersecting vertex {:?} is contained in the following vertices in the clique graph: {:?}", node_index, clique_graph_map.get(&node_index).unwrap())
println!("The intersecting vertex {:?} is contained in the following vertices in the clique graph: {:?}", node_index, clique_graph_map.get(&node_index).unwrap())
}

// DEBUG
for node_index in path {
error!(
println!(
"{:?} with level: {} and predecessor {:?}
and bag {:?}",
node_index,
Expand Down
36 changes: 21 additions & 15 deletions src/compute_treewidth_upper_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,15 @@ pub fn compute_treewidth_upper_bound<
};

if check_tree_decomposition_bool {
assert!(check_tree_decomposition(
&graph,
&clique_graph_tree_after_filling_up,
&predecessor_map,
&clique_graph_map
));
assert!(
check_tree_decomposition(
&graph,
&clique_graph_tree_after_filling_up,
&predecessor_map,
&clique_graph_map
),
"Tree decomposition is invalid. See previous print statements for reason."
);
}
let treewidth = find_width_of_tree_decomposition(&clique_graph_tree_after_filling_up);

Expand Down Expand Up @@ -309,11 +312,15 @@ mod tests {
|| computation_method
== TreewidthComputationMethod::MSTAndUseTreeStructure))
{
assert_eq!(
computed_treewidth, test_graph.treewidth,
"Test graph number {} failed with computation method {:?}",
i, computation_method
);
if i == 1 && computation_method == TreewidthComputationMethod::FillWhilstMST {
assert_eq!(computed_treewidth, 4);
} else {
assert_eq!(
computed_treewidth, test_graph.treewidth,
"Test graph number {} failed with computation method {:?}",
i, computation_method
);
}
}
}
}
Expand All @@ -333,7 +340,7 @@ mod tests {
&test_graph.graph,
negative_intersection,
computation_method,
false,
true,
None,
);
if !(i == 1
Expand All @@ -352,8 +359,7 @@ mod tests {
}

#[test]
#[should_panic]
fn negative_intersection_weight_heuristic_fails_on_first_test_graph() {
fn negative_intersection_weight_heuristic_does_not_fail_on_first_test_graph() {
let i = 1;
let computation_method = TreewidthComputationMethod::MSTAndUseTreeStructure;

Expand All @@ -367,7 +373,7 @@ mod tests {
&test_graph.graph,
negative_intersection,
computation_method,
false,
true,
None,
);
assert_eq!(
Expand Down
45 changes: 0 additions & 45 deletions src/fill_bags_along_paths.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use itertools::Itertools;
use log::{debug, info};
use petgraph::{graph::NodeIndex, Graph};
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -88,25 +87,13 @@ pub fn fill_bags_along_paths_using_structure<E: Default + Debug, S: Default + Bu
graph: &mut Graph<HashSet<NodeIndex, S>, E, petgraph::prelude::Undirected>,
clique_graph_map: &HashMap<NodeIndex, HashSet<NodeIndex, S>, S>,
) -> HashMap<NodeIndex, (NodeIndex, usize), S> {
info!("Building tree structure");

let mut tree_predecessor_map: HashMap<NodeIndex, (NodeIndex, usize), S> = Default::default();
let root = graph
.node_indices()
.max_by_key(|v| graph.neighbors(*v).collect::<Vec<_>>().len())
.expect("Graph shouldn't be empty");
setup_predecessors(&graph, &mut tree_predecessor_map, root);

// DEBUG
debug!(
"Clique Tree Graph currently looks like this: {:?} \n",
graph
);
debug!(
"Predecessor map looks like this: {:?}",
tree_predecessor_map
);

for vertex_in_initial_graph in clique_graph_map.keys() {
fill_bags_until_common_predecessor(
graph,
Expand All @@ -118,12 +105,6 @@ pub fn fill_bags_along_paths_using_structure<E: Default + Debug, S: Default + Bu
)
}

// DEBUG
debug!(
"Clique Tree Graph looks like this after filling up: {:?} \n",
graph
);

tree_predecessor_map
}

Expand Down Expand Up @@ -190,26 +171,14 @@ pub fn fill_bags_until_common_predecessor<E, S: BuildHasher>(
}
}

// DEBUG
debug!("Currently filling in {:?}", vertex_in_initial_graph);

// Loop that looks at ancestor of vertex with highest level index in tree. Inserts the ancestors
// in the predecessors, not inserting duplicates. If only one ancestor is left, the common ancestor is found.
while predecessors.len() > 1 {
// DEBUG
debug!("Predecessors: {:?}", predecessors);
// Current vertex should be the one with the highest level index in the tree
let current_vertex_in_clique_graph = predecessors
.pop_last()
.expect("Collection shouldn't be empty by loop invariant");
//DEBUG
debug!("Current vertex: {:?}", current_vertex_in_clique_graph);

//DEBUG
debug!(
"Filling in {:?} into {:?}",
vertex_in_initial_graph, current_vertex_in_clique_graph
);
// Insert the vertex from the original graph in the bag of the current vertex in the clique graph
// that is on the path to the common ancestor
clique_graph
Expand All @@ -224,25 +193,11 @@ pub fn fill_bags_until_common_predecessor<E, S: BuildHasher>(
node_index: *predecessor_clique_graph_vertex,
level_index: *index,
};
// DEBUG
debug!(
"Current vertex is: {:?}, predecessor is: {:?}",
current_vertex_in_clique_graph, predecessor
);
predecessors.insert(predecessor);
debug!(
"After inserting predecessor the predecessors look like this: {:?} \n \n",
predecessors
);
}
}
// This is reached once the common ancestor is found and the only element left in the collection
if let Some(common_predecessor) = predecessors.first() {
// DEBUG
debug!(
"Filling in vertex from initial graph: {:?} into common ancestor: {:?}",
vertex_in_initial_graph, common_predecessor
);
clique_graph
.node_weight_mut(common_predecessor.node_index)
.expect("Bag for the vertex should exist")
Expand Down
2 changes: 0 additions & 2 deletions src/find_path_in_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::warn;
use petgraph::visit::IntoNeighborsDirected;
use std::collections::{HashSet, VecDeque};
use std::fmt::Debug;
Expand Down Expand Up @@ -39,6 +38,5 @@ where
}
}
}
warn!("Found no path in tree that should be connected");
None
}
3 changes: 0 additions & 3 deletions src/generate_partial_k_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::info;
use petgraph::{graph::NodeIndex, visit::IntoNodeIdentifiers, Graph, Undirected};
use rand::prelude::SliceRandom;
use rand::{seq::IteratorRandom, Rng};
Expand Down Expand Up @@ -26,8 +25,6 @@ pub fn generate_partial_k_tree_with_guaranteed_treewidth(
if let Some(graph) = generate_partial_k_tree(k, n, p, rng) {
if maximum_minimum_degree_plus(&graph) == k {
return Some(graph);
} else {
info!("Random partial-k-tree graph was just discarded");
}
} else {
return None;
Expand Down

0 comments on commit f97268a

Please sign in to comment.