Boost Graph Library A common gallery for C + + developers

Source: Internet
Author: User
Keywords Developer C++ library General Gallery
Tags aliyun application application software applications class code computing create

The axiom expression of computation is usually quite controversial. However, graph theory, one of the most important theoretical pillars of modern computing, is not one of these axioms. Countless engineering areas (from designing routers and networks to designing chips that make up the core of mobile devices) are applications of graph theory.

As c++++ application software http://www.aliyun.com/zixun/aggregation/7155.html "> Developers, we usually need to directly transform the actual engineering problem into an equivalent graph theory problem." If you have a reliable, C + +-based generic library that can help us implement this transformation, this gallery is obviously very popular: the Boost Graph Library (BGL) will fill this void.

In this article, you will first create a forward-free graph, and then create a forward graph according to the regular traversal routines. You can then apply some classic algorithms, all of which do not need to add a lot of code. This is the magic of BGL.

Download and install

BGL can be downloaded free of charge from the Boost website. BGL is a library with only header files, so you will need to include related header files in your source code later when you use the library in your application code. But BGL needs this serialization library for linking. The following is a typical command-line format:

root# g++ test_bgl.cpp i/usr/boost/boost_1_44/-l/usr/boost/boost_1_44/lib

If you want to experiment with the code in this article, you need to install the Boost 1.44 version.

adjacency table (adjacency lists)

Any diagram implementation of the header file has an adjacency table (adjacency list) or adjacency matrix. Listing 1 shows how the adjacency table is declared in the Boost header file ADJACENCY_LIST.HPP.

Listing 1. Declare an adjacency table in Boost

Template <class outedgelists = vecs,//a Sequence or an Associativecontainer class vertexlists = VECs,//a Sequence or a Randomaccesscontainer class directeds = Directeds, class Vertexproperty = No_property, class Edgeproperty = No_property, Class GraphProperty = No_property, Class edgelists = Lists> class Adjacency_list {};

For simplicity, we will focus on the first three template parameters.

The outedgelist template parameter determines the type of container that will be used to store the Edge list (edge-list) information. Looking back at the basic knowledge of graph theory, we can know that for a direction graph, only those vertices with the edges have a corresponding null adjacency table. The default value is set to VECs, which corresponds to std::vector. The vertexlist template parameter determines the type of container used to represent the list of vertices in the graph, and the default value is also set to Std::vector. The directeds template parameter determines whether the graph is either a direction graph or a directeds according to whether the supplied value is a undirecteds or a map.

Create a diagram in BGL

While declaring the adjacency table, the code in Listing 2 creates a simple BGL graph in the box, where the edge information is stored in the std::list and the vertex information is stored in the std::vector.

Listing 2. Create a non-graph

#include <boost/graph/adjacency_list.hpp> using namespace boost; typedef boost::adjacency_list<lists, VECs, undirecteds> mygraph; int main () {mygraph g; Add_edge (0, 1, g); Add_edge (0, 3, g); Add_edge (1, 2, g); Add_edge (2, 3, g);

In Listing 2, figure G is created without providing any vertex or edge information in the constructor. At run time, you use Help functions such as Add_edge and Add_vertex to create edges and vertices. Add_edge function, as the name suggests: Add an edge between two vertices of a graph. After the code in Listing 2 finishes, figure G should have 4 vertices, labeled 0, 1, 2, and 3 respectively, Vertex 1 joins vertex 0 and Vertex 2, and so on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.