boost 最大流求解示例

#include <iostream>


#include <boost/config.hpp>


#include <boost/graph/boykov_kolmogorov_max_flow.hpp>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <boost/graph/edmonds_karp_max_flow.hpp>


#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/graph/graph_utility.hpp>


/*
,
boost::property <boost::vertex_color_t, boost::default_color_type,
boost::property <boost::vertex_distance_t, long,
boost::property <boost::vertex_predecessor_t, Traits::edge_descriptor > > >
*/
typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> Traits;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS,
	boost::property < boost::vertex_name_t, std::pair<int,int>,
	boost::property < boost::vertex_color_t, boost::default_color_type,
	boost::property < boost::vertex_distance_t, long,
	boost::property < boost::vertex_predecessor_t, Traits::edge_descriptor >  > > >,
	boost::property < boost::edge_capacity_t, double,
	boost::property < boost::edge_residual_capacity_t, double,
	boost::property < boost::edge_reverse_t, Traits::edge_descriptor> > >
> Graph;


void AddEdge(	Traits::vertex_descriptor &v1,
				Traits::vertex_descriptor &v2,
				boost::property_map < Graph, boost::edge_reverse_t >::type &rev,
				const double capacity, 
				Graph &g)
{
	Traits::edge_descriptor e1 = boost::add_edge(v1, v2, g).first;
	Traits::edge_descriptor e2 = boost::add_edge(v2, v1, g).first;
	boost::put(boost::edge_capacity, g, e1, capacity);
	boost::put(boost::edge_capacity, g, e2, capacity);


	rev[e1] = e2;
	rev[e2] = e1;
	
}


int main(int argc, char* argv[])
{
	Graph g;
	boost::property_map < Graph, boost::edge_reverse_t >::type rev = get(boost::edge_reverse, g);
	std::pair<int, int> point1(0, 0);
	std::pair<int, int> point2(1, 0);
	std::pair<int, int> point3(1, 1);
	std::pair<int, int> point4(0, 1);


	Graph::vertex_descriptor u = boost::add_vertex(point1, g);
	Graph::vertex_descriptor v = boost::add_vertex(point2, g);
	Graph::vertex_descriptor w = boost::add_vertex(point3, g);
	Graph::vertex_descriptor x = boost::add_vertex(point4, g);


	//boost::add_edge(u, v, g);
	AddEdge(u, v, rev, 10.1, g);
	AddEdge(u, w, rev, 10.2, g);
	AddEdge(v, x, rev, 8.1, g);
	AddEdge(w, x, rev, 6.2, g);


	//double flow = boost::boykov_kolmogorov_max_flow(g, u, x);
	//double flow = boost::push_relabel_max_flow(g, u, x);
	double flow = boost::edmonds_karp_max_flow(g, u, x);
	boost::property_map < Graph, boost::vertex_color_t >::type
		vertex_color = get(boost::vertex_color, g);
	boost::graph_traits < Graph >::vertex_iterator u_iter, u_end;
	for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
	{
		std::cout << "ver: " << *u_iter << " " << vertex_color[*u_iter] << std::endl;
	}


	return 0;
}

开头先以代码给大家,使用boost库进行最大流的求解。

`differentiate` 是 Boost 库中自动微分算法的一个函数,它用于计算指定函数在指定点处的值和导数,返回一个 `boost::math::differentiation::detail::dual` 类型的对象。 以下是一个使用 Boost 库计算函数 f(x) = x^2 在 x = 2 处的值和导数的示例代码: ```cpp #include <iostream> #include <boost/math/differentiation/autodiff.hpp> using namespace boost::math::differentiation; ad::dual f(ad::dual x) { return x * x; } int main() { double x = 2.0; auto result = differentiate(f, ad::make_dual(x)); std::cout << "f(x) = " << result.value() << std::endl; std::cout << "f'(x) = " << result.derivative() << std::endl; return 0; } ``` 在上面的代码中,我们使用了 Boost 库的自动微分算法计算 f(x) 在 x = 2 处的值和导数。我们首先定义了函数 f(x),然后在主函数中定义了 x,并使用自动微分算法计算 f(x) 在 x = 2 处的值和导数。最后输出 f(x) 和 f'(x) 的值。 `differentiate` 函数的第一个参数是要求值和导数的函数,第二个参数是一个 `ad::dual` 类型的变量,用于指定求值和求导的点。`ad::dual` 类型是一个包含值和导数信息的类,可以对其进行基本运算和函数调用。在上面的代码中,我们使用 `ad::make_dual` 函数创建了一个 `ad::dual` 类型的变量。 Boost 库的自动微分算法可以计算高阶导数,非常适合进行复杂函数的求导计算,同时也可以用于求解优化、最小二乘等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值