Boost Graph Library-BGL学习笔记1

首先需要说明的是BGL是一个增强STL map的通用类库,这个库是一个仅有header文件的C++库,不需要分开编译。我发现很多文章上来就讲怎么用,其实安装和导入可能会让一些人卡一会儿,做法也比较简单,就是在搜索路径中加入我们下载解压的文件路径就可以了。比如我的文件位置在下图:

因为我是用VS2017编译运行的,我只要右键项目->>属性->>C++->>常规->>附加包含目录处双击填入上面蓝色链接路径即可:

完成后效果如上图。

接下来我们跑一下官方文档的toyexample

//=======================================================================
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <boost/config.hpp>
#include <iostream> // for std::cout
#include <utility> // for std::pair
#include <algorithm> // for std::for_each
#include <boost/utility.hpp> // for boost::tie
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

using namespace boost;

template < class Graph > struct exercise_vertex
{
	exercise_vertex(Graph& g_, const char name_[]) : g(g_), name(name_) {}
	typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
	void operator()(const Vertex& v) const
	{
		using namespace boost;
		typename property_map< Graph, vertex_index_t >::type vertex_id
			= get(vertex_index, g);
		std::cout << "vertex: " << name[get(vertex_id, v)] << std::endl;

		// Write out the outgoing edges
		std::cout << "\tout-edges: ";
		typename graph_traits< Graph >::out_edge_iterator out_i, out_end;
		typename graph_traits< Graph >::edge_descriptor e;
		for (boost::tie(out_i, out_end) = out_edges(v, g); out_i != out_end;
			++out_i)
		{
			e = *out_i;
			Vertex src = source(e, g), targ = target(e, g);
			std::cout << "(" << name[get(vertex_id, src)] << ","
				<< name[get(vertex_id, targ)] << ") ";
		}
		std::cout << std::endl;

		// Write out the incoming edges
		std::cout << "\tin-edges: ";
		typename graph_traits< Graph >::in_edge_iterator in_i, in_end;
		for (boost::tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i)
		{
			e = *in_i;
			Vertex src = source(e, g), targ = target(e, g);
			std::cout << "(" << name[get(vertex_id, src)] << ","
				<< name[get(vertex_id, targ)] << ") ";
		}
		std::cout << std::endl;

		// Write out all adjacent vertices
		std::cout << "\tadjacent vertices: ";
		typename graph_traits< Graph >::adjacency_iterator ai, ai_end;
		for (boost::tie(ai, ai_end) = adjacent_vertices(v, g); ai != ai_end;
			++ai)
			std::cout << name[get(vertex_id, *ai)] << " ";
		std::cout << std::endl;
	}
	Graph& g;
	const char* name;
};

int main(int, char*[])
{
	// create a typedef for the Graph type
	typedef adjacency_list< vecS, vecS, bidirectionalS, no_property,
		property< edge_weight_t, float > >
		Graph;

	// Make convenient labels for the vertices
	enum
	{
		A,
		B,
		C,
		D,
		E,
		N
	};
	const int num_vertices = N;
	const char name[] = "ABCDE";

	// writing out the edges in the graph
	typedef std::pair< int, int > Edge;
	Edge edge_array[] = {
		Edge(A, B),
		Edge(A, D),
		Edge(C, A),
		Edge(D, C),
		Edge(C, E),
		Edge(B, D),
		Edge(D, E),
	};
	const int num_edges = sizeof(edge_array) / sizeof(edge_array[0]);

	// average transmission delay (in milliseconds) for each connection
	float transmission_delay[] = { 1.2, 4.5, 2.6, 0.4, 5.2, 1.8, 3.3, 9.1 };

	// declare a graph object, adding the edges and edge properties
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
	// VC++ can't handle the iterator constructor
	Graph g(num_vertices);
	property_map< Graph, edge_weight_t >::type weightmap = get(edge_weight, g);
	for (std::size_t j = 0; j < num_edges; ++j)
	{
		graph_traits< Graph >::edge_descriptor e;
		bool inserted;
		boost::tie(e, inserted)
			= add_edge(edge_array[j].first, edge_array[j].second, g);
		weightmap[e] = transmission_delay[j];
	}
#else
	Graph g(
		edge_array, edge_array + num_edges, transmission_delay, num_vertices);
#endif

	boost::property_map< Graph, vertex_index_t >::type vertex_id
		= get(vertex_index, g);
	boost::property_map< Graph, edge_weight_t >::type trans_delay
		= get(edge_weight, g);

	std::cout << "vertices(g) = ";
	typedef graph_traits< Graph >::vertex_iterator vertex_iter;
	std::pair< vertex_iter, vertex_iter > vp;
	for (vp = vertices(g); vp.first != vp.second; ++vp.first)
		std::cout << name[get(vertex_id, *vp.first)] << " ";
	std::cout << std::endl;

	std::cout << "edges(g) = ";
	graph_traits< Graph >::edge_iterator ei, ei_end;
	for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
		std::cout << "(" << name[get(vertex_id, source(*ei, g))] << ","
		<< name[get(vertex_id, target(*ei, g))] << ") ";
	std::cout << std::endl;

	std::for_each(vertices(g).first, vertices(g).second,
		exercise_vertex< Graph >(g, name));

	std::map< std::string, std::string > graph_attr, vertex_attr, edge_attr;
	graph_attr["size"] = "3,3";
	graph_attr["rankdir"] = "LR";
	graph_attr["ratio"] = "fill";
	vertex_attr["shape"] = "circle";

	boost::write_graphviz(std::cout, g, make_label_writer(name),
		make_label_writer(trans_delay),
		make_graph_attributes_writer(graph_attr, vertex_attr, edge_attr));

	return 0;
}

 运行结果如下:


4月26日更新

最近又遇到了这个问题,但是上面的方法又不行了,真是。。。。

我在VS2022上进行了相同的设置,但这次出现了如下的报错

 尚未解决,仍在摸索~~

几个重要链接资源:

(1条消息) Boost Graph Library 快速入门_seamanj的博客-CSDN博客_boost graphhttps://blog.csdn.net/seamanj/article/details/50636845

Table of Contents: Boost Graph Library - 1.78.0https://www.boost.org/doc/libs/1_78_0/libs/graph/doc/table_of_contents.html(1条消息) Boost Graph_seamanj的博客-CSDN博客https://blog.csdn.net/seamanj/article/details/50636876

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梓潼-多米

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值