Skip to content

Commit

Permalink
Changed edge weights in clique graphs to vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoulLuque committed May 11, 2024
1 parent 1a8cf84 commit b309273
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 50 deletions.
6 changes: 3 additions & 3 deletions k_tree_benchmarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ fn main() {
// FullPath -type f -name "*.dot" | xargs dot -Tpdf -O
fn create_dot_files<S>(
graph: &Graph<i32, i32, petgraph::prelude::Undirected>,
clique_graph: &Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected>,
clique_graph: &Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected>,
clique_graph_tree_after_filling_up: &Graph<
HashSet<NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
>,
clique_graph_tree_before_filling_up: &Option<
Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected>,
Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected>,
>,
i: usize,
name: &str,
Expand Down
2 changes: 1 addition & 1 deletion treewidth_heuristic/src/check_tree_decomposition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn check_tree_decomposition<N, E, S: BuildHasher + Default>(
starting_graph: &Graph<N, E, Undirected>,
tree_decomposition_graph: &Graph<
std::collections::HashSet<petgraph::prelude::NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
>,
predecessor_map: &Option<HashMap<NodeIndex, (NodeIndex, usize), S>>,
Expand Down
38 changes: 20 additions & 18 deletions treewidth_heuristic/src/clique_graph_edge_weight_heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,57 @@ use std::{collections::HashSet, hash::BuildHasher};

use petgraph::graph::NodeIndex;

pub fn neutral_heuristic<S>(_: &HashSet<NodeIndex, S>, _: &HashSet<NodeIndex, S>) -> i32 {
0
pub fn neutral_heuristic<S>(_: &HashSet<NodeIndex, S>, _: &HashSet<NodeIndex, S>) -> Vec<i32> {
vec![0]
}

// Classic
pub fn negative_intersection_heuristic<S: BuildHasher + Default>(
first_vertex: &HashSet<NodeIndex, S>,
second_vertex: &HashSet<NodeIndex, S>,
) -> i32 {
-(first_vertex
.intersection(second_vertex)
.collect::<HashSet<_, S>>()
.len() as i32)
) -> Vec<i32> {
vec![
-(first_vertex
.intersection(second_vertex)
.collect::<HashSet<_, S>>()
.len() as i32),
]
}

pub fn positive_intersection_heuristic<S: BuildHasher + Default>(
first_vertex: &HashSet<NodeIndex, S>,
second_vertex: &HashSet<NodeIndex, S>,
) -> i32 {
first_vertex
) -> Vec<i32> {
vec![first_vertex
.intersection(second_vertex)
.collect::<HashSet<_, S>>()
.len() as i32
.len() as i32]
}

pub fn disjoint_union_heuristic<S: BuildHasher>(
first_vertex: &HashSet<NodeIndex, S>,
second_vertex: &HashSet<NodeIndex, S>,
) -> i32 {
(first_vertex.len() + second_vertex.len()) as i32
) -> Vec<i32> {
vec![(first_vertex.len() + second_vertex.len()) as i32]
}

pub fn union_heuristic<S: BuildHasher + Default>(
first_vertex: &HashSet<NodeIndex, S>,
second_vertex: &HashSet<NodeIndex, S>,
) -> i32 {
first_vertex
) -> Vec<i32> {
vec![first_vertex
.union(second_vertex)
.collect::<HashSet<_, S>>()
.len() as i32
.len() as i32]
}

// Classic alt?
pub fn least_difference_heuristic<S: BuildHasher + Default>(
first_vertex: &HashSet<NodeIndex, S>,
second_vertex: &HashSet<NodeIndex, S>,
) -> i32 {
first_vertex
) -> Vec<i32> {
vec![first_vertex
.symmetric_difference(second_vertex)
.collect::<HashSet<_, S>>()
.len() as i32
.len() as i32]
}
18 changes: 9 additions & 9 deletions treewidth_heuristic/src/compute_treewidth_upper_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ pub enum TreewidthComputationMethod {
/// and path that is faulty.
pub fn compute_treewidth_upper_bound<N: Clone, E: Clone, S: Default + BuildHasher + Clone>(
graph: &Graph<N, E, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
treewidth_computation_method: TreewidthComputationMethod,
check_tree_decomposition_bool: bool,
) -> (
Graph<HashSet<NodeIndex, S>, i32, Undirected>,
Graph<HashSet<NodeIndex, S>, i32, Undirected>,
Option<Graph<HashSet<NodeIndex, S>, i32, Undirected>>,
Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
Option<Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>>,
Option<HashMap<NodeIndex, (NodeIndex, usize), S>>,
Option<HashMap<NodeIndex, HashSet<NodeIndex, S>, S>>,
usize,
Expand All @@ -54,7 +54,7 @@ pub fn compute_treewidth_upper_bound<N: Clone, E: Clone, S: Default + BuildHashe

let mut clique_graph_tree: Graph<
std::collections::HashSet<petgraph::prelude::NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
> = petgraph::data::FromElements::from_elements(petgraph::algo::min_spanning_tree(
&clique_graph,
Expand All @@ -79,7 +79,7 @@ pub fn compute_treewidth_upper_bound<N: Clone, E: Clone, S: Default + BuildHashe

let mut clique_graph_tree: Graph<
std::collections::HashSet<petgraph::prelude::NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
> = petgraph::data::FromElements::from_elements(petgraph::algo::min_spanning_tree(
&clique_graph,
Expand All @@ -89,7 +89,7 @@ pub fn compute_treewidth_upper_bound<N: Clone, E: Clone, S: Default + BuildHashe
// DEBUG
let clique_graph_tree_copy: Graph<
std::collections::HashSet<petgraph::prelude::NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
> = petgraph::data::FromElements::from_elements(petgraph::algo::min_spanning_tree(
&clique_graph,
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn compute_treewidth_upper_bound<N: Clone, E: Clone, S: Default + BuildHashe

let clique_graph_tree: Graph<
std::collections::HashSet<petgraph::prelude::NodeIndex, S>,
i32,
Vec<i32>,
petgraph::prelude::Undirected,
> = fill_bags_while_generating_mst::<N, E, S>(
&clique_graph,
Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn compute_treewidth_upper_bound_not_connected<
S: Default + BuildHasher + Clone,
>(
graph: &Graph<N, E, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
treewidth_computation_method: TreewidthComputationMethod,
check_tree_decomposition_bool: bool,
) -> usize {
Expand Down
12 changes: 6 additions & 6 deletions treewidth_heuristic/src/construct_clique_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use petgraph::Graph;
/// and edges that connect two vertices if the intersection of the corresponding cliques is not empty.
pub fn construct_clique_graph<InnerCollection, OuterIterator, S: Default + BuildHasher>(
cliques: OuterIterator,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
) -> Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected>
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
) -> Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected>
where
OuterIterator: IntoIterator<Item = InnerCollection>,
InnerCollection: IntoIterator<Item = NodeIndex>,
{
let mut result_graph: Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected> =
let mut result_graph: Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected> =
Graph::new_undirected();
for clique in cliques {
let vertex_index = result_graph.add_node(HashSet::from_iter(clique.into_iter()));
Expand Down Expand Up @@ -54,17 +54,17 @@ where
/// that contain the vertex from the original graph.
pub fn construct_clique_graph_with_bags<InnerCollection, OuterIterator, S: Default + BuildHasher>(
cliques: OuterIterator,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
) -> (
Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected>,
Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected>,
HashMap<NodeIndex, HashSet<NodeIndex, S>, S>,
)
where
OuterIterator: IntoIterator<Item = InnerCollection>,
InnerCollection: IntoIterator<Item = NodeIndex>,
InnerCollection: Clone,
{
let mut result_graph: Graph<HashSet<NodeIndex, S>, i32, petgraph::prelude::Undirected> =
let mut result_graph: Graph<HashSet<NodeIndex, S>, Vec<i32>, petgraph::prelude::Undirected> =
Graph::new_undirected();
let mut result_map: HashMap<NodeIndex, HashSet<NodeIndex, S>, S> = Default::default();

Expand Down
7 changes: 2 additions & 5 deletions treewidth_heuristic/src/fill_bags_along_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PartialOrd for Predecessor {

/// Given a tree graph with bags (HashSets) as Vertices, checks all 2-combinations of bags for non-empty-intersection
/// and inserts the intersecting nodes in all bags that are along the (unique) path of the two bags in the tree.
pub fn fill_bags_along_paths<E: Copy + Measure + Default, S: BuildHasher>(
pub fn fill_bags_along_paths<E, S: BuildHasher>(
graph: &mut Graph<HashSet<NodeIndex, S>, E, petgraph::prelude::Undirected>,
) {
// Finding out which paths between bags have to be checked
Expand Down Expand Up @@ -84,10 +84,7 @@ pub fn fill_bags_along_paths<E: Copy + Measure + Default, S: BuildHasher>(

/// Given a tree graph with bags (HashSets) as Vertices, checks all 2-combinations of bags for non-empty-intersection
/// and inserts the intersecting nodes in all bags that are along the (unique) path of the two bags in the tree.
pub fn fill_bags_along_paths_using_structure<
E: Copy + Default + Debug,
S: Default + BuildHasher,
>(
pub fn fill_bags_along_paths_using_structure<E: Default + Debug, S: Default + BuildHasher>(
graph: &mut Graph<HashSet<NodeIndex, S>, E, petgraph::prelude::Undirected>,
clique_graph_map: &HashMap<NodeIndex, HashSet<NodeIndex, S>, S>,
) -> HashMap<NodeIndex, (NodeIndex, usize), S> {
Expand Down
16 changes: 8 additions & 8 deletions treewidth_heuristic/src/fill_bags_while_generating_mst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::{
use petgraph::{graph::NodeIndex, Graph, Undirected};

pub fn fill_bags_while_generating_mst<N, E, S: Default + BuildHasher + Clone>(
clique_graph: &Graph<HashSet<NodeIndex, S>, i32, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
clique_graph: &Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
clique_graph_map: HashMap<NodeIndex, HashSet<NodeIndex, S>, S>,
) -> Graph<HashSet<NodeIndex, S>, i32, Undirected> {
let mut result_graph: Graph<HashSet<NodeIndex, S>, i32, Undirected> = Graph::new_undirected();
) -> Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected> {
let mut result_graph: Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected> = Graph::new_undirected();
// Maps the vertex indices from the clique graph to the corresponding vertex indices in the result graph
let mut node_index_map: HashMap<NodeIndex, NodeIndex, S> = Default::default();
let mut vertex_iter = clique_graph.node_indices();
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn fill_bags_while_generating_mst<N, E, S: Default + BuildHasher + Clone>(
fn fill_bags<S: BuildHasher>(
start_vertex: NodeIndex,
end_vertex: NodeIndex,
graph: &mut Graph<HashSet<NodeIndex, S>, i32, Undirected>,
graph: &mut Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
vertex_to_be_insert_from_starting_graph: NodeIndex,
) {
let mut path: Vec<_> = petgraph::algo::simple_paths::all_simple_paths::<Vec<NodeIndex>, _>(
Expand Down Expand Up @@ -146,9 +146,9 @@ fn fill_bags<S: BuildHasher>(
/// Returns a tuple with a node index from the result graph in the first and node index from the clique graph
/// in the second entry
fn find_cheapest_vertex<S>(
clique_graph: &Graph<HashSet<NodeIndex, S>, i32, Undirected>,
result_graph: &Graph<HashSet<NodeIndex, S>, i32, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> i32,
clique_graph: &Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
result_graph: &Graph<HashSet<NodeIndex, S>, Vec<i32>, Undirected>,
edge_weight_heuristic: fn(&HashSet<NodeIndex, S>, &HashSet<NodeIndex, S>) -> Vec<i32>,
currently_interesting_vertices: &HashSet<(NodeIndex, NodeIndex), S>,
) -> (NodeIndex, NodeIndex) {
*currently_interesting_vertices
Expand Down

0 comments on commit b309273

Please sign in to comment.