Beispiel #1
0
void dinic(Net net, output * out_p, int flags){
	uint an_flow = 0, x = 0, r = 0, t = 1, i = 0;
	bool end = false;
	output out = *out_p;
	path p = NULL;
	nodes_list nodes = NULL;

	nodes = net_get_nodes(net);
	out_set_net(out, net);

	for(i = 0; !end ; i++) {

		an_flow = 0;

		nodes_aux_reset(nodes);
		net_aux_new(net);

		end = is_t_in_an(nodes);
		
		if (!end) {
			
			while(true) {

				x = advance(net); /* ADVANCE */

				if (x == t) { /* AUGMENT */
					r = residual_capacity(net); 
					an_flow += r;
					p = path_new();
					augment(net, p, r);
					out_add_path(out, p);
				} 
				else break;
			}

			if((flags & VERBOSE)){
				out_print_paths(out, i+1);
			}
		}

		out_path_destroy(out);
	}
	*out_p = out;
}
Beispiel #2
0
int main()
{
  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
      boost::no_property,
      boost::property<boost::edge_index_t, std::size_t> > GraphType;

  GraphType graph;

  typedef boost::graph_traits<GraphType>::vertex_descriptor VertexDescriptor;
  typedef boost::graph_traits<GraphType>::edge_descriptor EdgeDescriptor;
  typedef boost::graph_traits<GraphType>::vertices_size_type VertexIndex;
  typedef boost::graph_traits<GraphType>::edges_size_type EdgeIndex;

  unsigned int numberOfVertices = 2*2 + 2; // a 2x2 grid
  std::vector<int> groups(numberOfVertices);

  std::vector<EdgeDescriptor> reverse_edges;

  std::vector<float> capacity;
  
  graph_builder<GraphType> builder(graph,capacity,reverse_edges);

  // Add edges between grid vertices
  builder.create_edge(0,1,1.f);

  builder.create_edge(1,2,1.f);

  builder.create_edge(2,3,1.f);

  builder.create_edge(3,0,1.f);

  int sourceId = 4;
  int sinkId = 5;
  // Add edges between all vertices and the source, as well as between all vertices and the sink
  for(size_t i = 0; i < 4; ++i)
  {
      builder.create_edge(i,sourceId,2.f);

      builder.create_edge(i,sinkId,2.f);
  }

  std::vector<float> residual_capacity(num_edges(graph), 0);

  VertexDescriptor sourceVertex = vertex(sourceId,graph);
  VertexDescriptor sinkVertex = vertex(sinkId,graph);

  // There should be 2*2 + 2 = 6 nodes
  std::cout << "Number of vertices " << num_vertices(graph) << std::endl;

  // There should be 4 + 4 + 4 = 12 edges
  std::cout << "Number of edges " << num_edges(graph) << std::endl;
  

  boost::boykov_kolmogorov_max_flow(graph,
      boost::make_iterator_property_map(&capacity[0], get(boost::edge_index, graph)),
      boost::make_iterator_property_map(&residual_capacity[0], get(boost::edge_index, graph)),
      boost::make_iterator_property_map(&reverse_edges[0], get(boost::edge_index, graph)),
      boost::make_iterator_property_map(&groups[0], get(boost::vertex_index, graph)),
      get(boost::vertex_index, graph),
      sourceVertex,
      sinkVertex);

  // Display the segmentation
  for(size_t index=0; index < groups.size(); ++index)
  {
       std::cout << "Vertex " << index << " is in group " << groups[index] << std::endl;
  }

  return EXIT_SUCCESS;
}