Dip  0.92.4
TSP_Boost.h
Go to the documentation of this file.
1 //===========================================================================//
2 // This file is part of the Decomp Solver Framework. //
3 // //
4 // Decomp is distributed under the Common Public License as part of the //
5 // COIN-OR repository (http://www.coin-or.org). //
6 // //
7 // Authors: Matthew Galati, SAS Institute Inc. (matthew.galati@sas.com) //
8 // Ted Ralphs, Lehigh University (ted@lehigh.edu) //
9 // Jiadong Wang, Lehigh University (jiw408@lehigh.edu) //
10 // //
11 // Copyright (C) 2002-2019, Lehigh University, Matthew Galati, and Ted Ralphs//
12 // All Rights Reserved. //
13 //===========================================================================//
14 
15 #ifndef TSP_BOOST_INCLUDED
16 #define TSP_BOOST_INCLUDED
17 
18 //---
19 //--- Interface class to Boost/Graph (an open source graph library).
20 //---
21 
22 //THINK design:
23 //design wise there should be a class that just wraps boost algos
24 //then one that carries boost data specific to your application
25 
26 // --------------------------------------------------------------------- //
27 #include <boost/graph/adjacency_list.hpp>
28 #include <boost/graph/graph_utility.hpp>
29 #include <boost/graph/connected_components.hpp>
30 #include <boost/graph/kruskal_min_spanning_tree.hpp>
31 using namespace boost;
32 
33 typedef property<edge_weight_t, double,
34  property<edge_index_t, int> > edge_prop;
35 typedef adjacency_list<vecS, vecS, undirectedS,
36  no_property, edge_prop> Graph;
37 
38 // --------------------------------------------------------------------- //
39 class TSP_Boost {
40 public:
43 
44 public:
46  m_sg (),
47  m_cgV() {}
49 
50 public:
52  void buildSubGraph(const int len,
53  const double * val,
54  const double tol = 1.0e-6){
55 
56  property_map<Graph, edge_weight_t>::type e_weight
57  = get(edge_weight, m_sg);
58  property_map<Graph, edge_index_t>::type e_index
59  = get(edge_index, m_sg);
60  graph_traits<Graph>::edge_descriptor ed; bool inserted;
61  for (int c = 0; c < len; ++c) {
62  if(val[c] <= tol)
63  continue;
64  pair<int,int> uv = UtilBothEndsU(c);
65  tie(ed, inserted) = add_edge(uv.first, uv.second, m_sg);
66  e_weight[ed] = val[c];
67  e_index[ed] = c;
68  }
69  }
70 
71  void buildCompleteGraphMinusVert(const int vert,
72  const int nVerts){
73 
74  property_map<Graph, edge_index_t>::type e_index
75  = get(edge_index, m_cgV);
76  graph_traits<Graph>::edge_descriptor ed; bool inserted;
77  int u, v;
78  int index = 0;
79  for(u = 1; u < nVerts; u++){
80  for(v = 0; v < u; v++){
81  if(u != vert && v != vert){
82  tie(ed, inserted) = add_edge(u, v, m_cgV);
83  e_index[ed] = index;
84  }
85  index++;
86  }
87  }
88  }
89 
90  inline void clearSubGraph(){
91  m_sg.clear();
92  }
93 
94  inline int findConnectedComponents(vector<int> & component){
95  return connected_components(m_sg, &component[0]);
96  }
97 
98  inline int getDegree(const int nodeIndex){
99  return static_cast<int>(degree(nodeIndex, m_sg));
100  }
101 
102 };
103 
104 #endif
Graph m_cgV
Definition: TSP_Boost.h:42
int getDegree(const int nodeIndex)
Definition: TSP_Boost.h:98
~TSP_Boost()
Definition: TSP_Boost.h:48
int findConnectedComponents(vector< int > &component)
Definition: TSP_Boost.h:94
Graph m_sg
Definition: TSP_Boost.h:41
adjacency_list< vecS, vecS, undirectedS, no_property, edge_prop > Graph
Definition: TSP_Boost.h:36
pair< int, int > UtilBothEndsU(const int index)
TSP_Boost()
Definition: TSP_Boost.h:45
void clearSubGraph()
Definition: TSP_Boost.h:90
void buildSubGraph(const int len, const double *val, const double tol=1.0e-6)
Definition: TSP_Boost.h:52
property< edge_weight_t, double, property< edge_index_t, int > > edge_prop
Definition: TSP_Boost.h:34
void buildCompleteGraphMinusVert(const int vert, const int nVerts)
Definition: TSP_Boost.h:71