All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
k_cut_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014 Piotr Smulewicz
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
18 
19 #include <boost/graph/adjacency_list.hpp>
20 
21 #include <iostream>
22 
23 int main() {
24  // sample data
25  std::vector<std::pair<int,int>> edges_p {{1,2},{1,5},{2,3},{2,5},{2,6},
26  {3,4},{3,7},{4,7},{4,0},{5,6},{6,7},{7,0}};
27  std::vector<int> costs{2,3,3,2,2,4,2,2,2,3,1,3};
28  boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
29  boost::no_property,
30  boost::property<boost::edge_index_t, std::size_t>
31  > graph(8);
32  for (std::size_t i = 0; i < edges_p.size(); ++i) {
33  add_edge(edges_p[i].first, edges_p[i].second, i, graph);
34  }
35  const int parts = 3;
36 
37  auto edge_id = get(boost::edge_index, graph);
38  auto weight = make_iterator_property_map(costs.begin(), edge_id);
39 
40  // solve
41  int cost_cut;
42  std::vector<std::pair<int,int>> vertices_parts;
43  cost_cut = paal::greedy::k_cut(graph, parts, back_inserter(vertices_parts),
44  boost::weight_map(weight));
45 
46  // alternative form
47  // cost_cut = paal::greedy::k_cut(graph, parts, back_inserter(vertices_parts));
48  // this works if the graph has and internal edge weight property map
49 
50  // print result
51  std::cout << "cost cut:" << cost_cut << std::endl;
52  std::vector<int> vertices_to_parts;
53  for (auto i: vertices_parts) {
54  std::cout << i.first << "(" << i.second << "), ";
55  }
56  std::cout << std::endl;
57 }
int main()
[K Cut Example]
auto k_cut(const InGraph &graph, unsigned int number_of_parts, OutputIterator result, VertexIndexMap index_map, EdgeWeightMap weight_map) -> typename boost::property_traits< EdgeWeightMap >::value_type
this is solve k_cut problem and return cut_cost example:
Definition: k_cut.hpp:54