3 #ifndef DEGREE_BRANCHING_HPP
4 #define DEGREE_BRANCHING_HPP
8 #include "utils/graph_typedefs.hpp"
9 #include "utils/scaffolding_typedefs.hpp"
11 #include "solv/trees.hpp"
12 #include "solv/branching.hpp"
21 void translate_branch(Branch<Graph>& branch,
const VTranslateMap<Graph,Graph>& translate){
22 for(
auto e = branch.edges.begin(); e != branch.edges.end();){
24 e = branch.edges.emplace(e, std::make_pair(translate.at(e->first), translate.at(e->second)));
26 e = branch.edges.erase(++e);
32 void get_branches_for_vertex(
const Graph& g,
const Vertex<Graph>& v, std::list<Branch<Graph> >& branches){
34 for(AdjIterRange<Graph> r = adjacent_vertices(v, g); r.first != r.second; ++r.first)
35 branches.push_back(Branch<Graph>(TakeBranch, std::make_pair(v, *r.first)));
40 void apply_branch(Instance<Graph>& I,
const Branch<Graph>& b, DynProgSolution<>*
const S = NULL){
44 I.delete_edges(b.edges, S);
48 for(
auto e : b.edges) {
49 DEBUG4(std::cout <<
"taking edge "<<VertexPairAndGraph<Graph>(e, *I.g)<<std::endl);
66 I.clear_vertex_except(e.first, e.second, S);
67 I.clear_vertex_except(e.second, e.first, S);
76 Instance<Graph>* copy_and_apply_branch(
const Instance<Graph>& I, Branch<Graph>& b, DynProgSolution<>*
const S = NULL){
78 VTranslateMap<Graph,Graph> translate;
79 Instance<Graph>* H =
new Instance<Graph>(I, &translate,
false);
81 translate_branch(b, translate);
83 apply_branch(*H, b, S);
85 H->infos.update_all();
92 void deg_solve_recursively(Instance<Graph>& I, DynProgSolution<>*& S,
size_t depth = 0){
94 Instance<Graph> *J = I.split_off_component();
97 DEBUG3(std::cout <<
"splitting into components of size "<<num_vertices(*I.g)<<
" and "<<num_vertices(*J->g)<<
" (at "<<J<<
")"<<std::endl);
98 DynProgSolution<>* solJ =
new DynProgSolution<>(*J);
99 deg_solve_recursively(I, S, depth + 1);
100 deg_solve_recursively(*J, solJ, depth + 1);
103 DEBUG3(std::cout <<
"depth "<<depth<<
": rejoining solutions for components"<<std::endl);
105 if(!S->no_solutions() && !solJ->no_solutions()){
107 S->combine_disjoint_union(*solJ);
118 DEBUG3(std::cout <<
"depth "<< depth<<
": solving instance with " << num_vertices(*I.g) <<
" vertices:" << std::endl);
119 DEBUG5(write_adj_list_graph(std::cout, *I.g); std::cout << std::endl);
120 if(I.max_deg_two()) {
121 DEBUG3(std::cout <<
"it's already max-deg 2, getting all sub-solutions"<<std::endl);
122 get_all_solutions(I, S);
126 DEBUG3(std::cout <<
"it's a tree, getting all sub-solutions"<<std::endl);
133 const VertexAndDegree<Graph> vd = get_max_degree(*I.g);
134 const Vertex<Graph>& v = vd.first;
135 DEBUG4(std::cout <<
"branching on vertex: "<< VertexAndGraph<Graph>(v, *I.g)<<std::endl);
137 std::list<Branch<Graph> > branches;
138 get_branches_for_vertex(*I.g, v, branches);
141 DynProgSolution<> max_sol(I);
142 for(Branch<Graph> b : branches){
143 DEBUG3(std::cout <<
"depth "<<depth<<
":"<<std::endl<<
" branching: "<< BranchAndGraph<Graph>(b, *I.g)<<std::endl);
145 DynProgSolution<>* solH =
new DynProgSolution<>(I);
146 Instance<Graph>* H = copy_and_apply_branch(I, b, solH);
148 deg_solve_recursively(*H, solH, depth + 1);
149 DEBUG4(std::cout <<
"depth "<<depth<<
": returned with solution"<<std::endl);
152 if(!solH->no_solutions()) max_sol.combine_min_of_two(*solH);
155 if(max_sol.no_solutions()) S->invalidate();
else S->destructive_append(max_sol);
156 DEBUG3(std::cout <<
"returning from depth "<<depth<<
" with ";
if(S->no_solutions()) std::cout <<
"no solutions"<<std::endl;
else std::cout <<
"solutions: "<<*S<<std::endl;);
Definition: read_adj_list.hpp:22