Boost graph library Summary 1

Source: Internet
Author: User

Boost
Graph library uses the concept of generics to completely compile various algorithms in the graph. For example, wide search, deep search, and shortest path

1. Create a graph

1.1
Graph Representation

An image can be represented by an adjacent matrix and an adjacent table. In BGL, adjacency_list and adjacency_matrix are used for representation. adjacency_list uses an adjacent table to store an image, adjacency_matrix uses the adjacent matrix for storage, which is suitable for sparse graph and dense graph. When using an adjacent table, you can use the following definitions:

 

1 typedef adjacency_list<listS, vecS, directedS> file_dep_graph;

 

 

Lists stores the outbound edges of each vertex in STD: List, and VECs stores each vertex in STD: vector.

 

Directeds indicates that the graph is a directed graph. If the file (file. dat) contains the following format input:

5

1 2

0 3

3 4

2 4

4 3

 

 

The first line indicates the number of vertices. The following lines indicate the start vertex and end vertex of each edge. You can use the standard input/output stream and adjacency_list to represent a graph. The Code is as follows:

1 STD: ifstream file_in ("file. dat ");
2 typedef graph_traits <file_dep_graph>: vertices_size_type size_type;
3 size_type n_vertices;
4 file_in> n_vertices;
5 STD: istream_iterator <STD: pair <size_type, size_type> input_befin (file_in), input_end;
6 file_dep_graph g (input_begin, input_end, n_vertices );
7 // operator> needs to be reloaded
8 namespace STD {
9 template <typename T>
10 STD: istream & operator> (STD: istream & in, STD: pair <t, t> & P ){
11 In> P. First> P. Second;
12 Return in;
13}
14}

In the code, adjacency_list is used to convert the input in the file into a graph, indicating that the input is in the memory. That is, G. With the representation of the graph in memory, you can write code and write related algorithms for the graph.

1.2 iterator shown in the figure

In general, the iterator in the figure is mainly an adjacent iterator. For example, if you want to perform iterative access to the adjacent vertex of a vertex (that is, the tail vertex of an edge), write the following code:

1 graph_traits<file_dep_graph>::adjacency_iterator vi, vi_end;
2 for(tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
3 std::cout<<*vi<<std::endl;

 

 

The code above indicates that all adjacent vertices of u are printed iteratively.

 

To traverse all vertices, write the following code:

1 graph_traits<file_dep_graph>::vertex_iterator vi, vi_end;
2 for(tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
3 std::cout<<*vi<<std::endl;

 

 

Vertices returns the iterator for the vertex. For other iterators, see the BGL user manual.


 

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.