Download presentation
Presentation is loading. Please wait.
1
Dongmin Kim, Bolat Azamat STEM Lab
Boost 설치 및 실습 Dongmin Kim, Bolat Azamat STEM Lab
2
What is Boost? C++ source Libraries (not Library)
Because Boost libraries consist of a set of 80 libraries for C++. The libraries are available for free and can be used for both free and proprietary software projects. Libraries List : Current Release : version
3
Installation for Windows
download ‘current release’ 7z or zip file
4
Installation Extract zip file to appropriate path.
ex) D:\dev\boost_1_68_0 It contains only header Some of libraries are Header-only, but others are not You need build boost, if you want to use not Header-only library.
5
Build and Setting For Code::Blocks For Visual Studio 2017 page 6 - 9
6
Build on gcc, codeblocks, mingw…
Invoke cmd on your boost folder Check g++ version and path : g++ --version version > 7.x.x Run bootstrap : .\bootstrap.bat gcc Build : .\b2 toolset=gcc
7
Setting Code::Blocks 1. Create project
Reference :
8
Setting Code::Blocks 2. click Build options... 3. Search directories
9
Setting Code::Blocks 4. Add your boost folder 5. Add libraries folder
10
Build on msvc(Microsoft visual c++)
1. Invoke Native Tools
11
Build on msvc 2. Enter boost folder 3. run bootstrap
If you get ‘vswhere’ error → update VS
12
Build on msvc 3. Build : run b2.exe append --toolset option
visual studio 버전 숫자 - VS2008 : 9.0 - VS2010 : 10.0 - VS2012 : 11.0 - VS2013 : 12.0 VS2015 : 14.0 ex) VS 2013 .\b2 --toolset=msvc-12.0
13
Setting Visual Studio 1. Create Project 2. Project properties
14
Setting Visual Studio Edit “Include Directory(포함 디렉터리)” and “Library Directory(라이브러리 디렉터리)”
15
Header-Only Library You don’t need to build Boost when you use only header- only library. And, library “BGL” which we study today is a header-only library. So, you can skip previous slides and follow just this instructions : JUST DOWNLOAD and INCLUDE [Code::Blocks] page 9. number 4 [VS] page 14, add only include directory
16
Boost Graph Library(BGL)
The Boost Graph Library is a c++ library for graph theoretic data structures and algorithms.
17
Graph class adjacency_list adjacency_matrix edge_list
Each list describes the set of neighbors of a vertex in the graph adjacency_matrix it store edges in a |V| x |V| matrix (where |V| is the number of vertices) edge_list
18
Using adjacency_list Adjacency List of a Undirected Graph
Adjacency List of a Directed Graph
19
Using adjacency_list adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> Graph
20
Using adjacency_list adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> OutEdgeList : controls what kind of container is used to represent the out edge lists for each vertex VertexList : controls what kind of container is used to represent vertex list (outer two-dimensional container) ex) vector<vector<…> > adj_list OutEdgeList VertexList OutEdgeList VertexList
21
Choosing the EdgeList and VertexList
Selector vecS selects std::vector listS selects std::list slistS selects std::slist (Singly linked list) setS selects std::set The choice affects the time complexity and the space complexity add removal
22
Directed adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> Directed (default : directedS) directedS bidirectionalS undirectedS
23
Properties adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> Properties (default : no_property) Edge, Vertex, Graph with integer, float, string, etc… Define type here EdgeList (default : listS) selector for list of every edges.
24
Practice Source code BFS Dijkstra GraphViz (Visualization)
BFS Dijkstra GraphViz (Visualization)
25
Breadth First Search (1) 110 (2) 120 (0) 102 (3) 456 (0) (5) 130
(4) 140 (6) 321
26
Breadth First Search (1) 110 (2) 120 (0) 102 (3) 456 (0) – (1 – 3) (5)
130 (4) 140 (6) 321
27
Breadth First Search (1) 110 (2) 120 (0) 102 (3) 456
(0) – (1 – 3) – (2 – 4 – 6) (5) 130 (4) 140 (6) 321
28
Breadth First Search (1) 110 (2) 120 (0) 102 (3) 456
(0) – (1 – 3) – (2 – 4 – 6) – (5) (5) 130 (4) 140 (6) 321
29
Breadth First Search (bfs.cpp)
typedef property<vertex_distance_t, double> VertexProperty;
30
Breadth First Search (bfs.cpp)
typedef property<vertex_distance_t, double> VertexProperty; Original typename Alternative typename
31
Breadth First Search (bfs.cpp)
typedef property<vertex_distance_t, double> VertexProperty; Tag (simply identifies or gives a unique name to the property) struct vertex_index_t { }; struct vertex_index1_t { }; struct vertex_index2_t { }; struct edge_index_t { }; struct graph_name_t { }; struct vertex_name_t { }; struct edge_name_t { }; struct edge_weight_t { }; struct edge_weight2_t { }; struct edge_capacity_t { }; … Also, we can make our own property tag
32
BFS Let’s see main first
property_map<Graph, vertex_distance_t>::type scores = get(vertex_distance_t(), g); put(scores, 0, 102); put(scores, 1, 110); Get all property which tag is “vertex_distance_t” from Graph “g”
33
BFS scores Key (node) Value (Property) 1 2 3 4 5 6
1 2 3 4 5 6 property_map<Graph, vertex_distance_t>::type scores = get(vertex_distance_t(), g); put(scores, 0, 102); put(scores, 1, 110); and make property_map
34
BFS scores Key (node) Value (Property) 102 1 2 3 4 5 6
102 1 2 3 4 5 6 property_map<Graph, vertex_distance_t>::type scores = get(vertex_distance_t(), g); put(scores, 0, 102); put(scores, 1, 110); put value “102” to node “0”
35
BFS Now, let’s see upper part scores Key (node) Value (Property) 102 1
102 1 110 2 3 4 5 6 property_map<Graph, vertex_distance_t>::type scores = get(vertex_distance_t(), g); put(scores, 0, 102); put(scores, 1, 110); put value “110” to node “1” Now, let’s see upper part
36
BFS double total = 0.0; The variable which will contain total value
class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; }; The variable which will contain total value
37
BFS Invoke “discover_vertex” during searching In this default class,
function “discover_vertex” do nothing double total = 0.0; class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; };
38
BFS We will implement function “discover_vertex” in our custom class
double total = 0.0; class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; };
39
BFS double total = 0.0; class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; }; store scores in class
40
BFS double total = 0.0; class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; }; get u’s value in _scores (This “get” is different from “get” in main)
41
BFS double total = 0.0; class custom_bfs_visitor : public boost::default_bfs_visitor { public: template < typename Vertex, typename Graph > void discover_vertex(Vertex u, const Graph & g) const total += get(_scores, u); cout << u << " : " << get(_scores, u) << endl; } property_map<Graph, vertex_distance_t>::type _scores; }; print Vertex and value
42
BFS Go back to main Create our custom visitor, and store scores
custom_bfs_visitor vis; vis._scores = scores; breadth_first_search(g, 0, visitor(vis));
43
BFS custom_bfs_visitor vis; Run BFS by using our custom class
vis._scores = scores; breadth_first_search(g, 0, visitor(vis)); Run BFS by using our custom class
44
BFS This is named parameter custom_bfs_visitor vis;
vis._scores = scores; breadth_first_search(g, 0, visitor(vis));
45
Named Parameter In Python, we can do like this
def func(name, score): print(name, score) return func(name="Kim", score=50) func(score=50, name="Kim")
46
Named Parameter Boost implements functions which act like python named parameter breadth_first_search(g, 0, visitor(vis)); It means visitor=vis breadth_first_search(g, 0, visitor(vis).color_map(cmap)); It means (g, 0, visitor=vis, color_map=cmap)
47
Dijkstra Algorithm Result
(1) node distance 1 2 3 4 5 6 node parent 1 2 3 4 5 6 5 (2) 10 (0) 3 (3) 8 6 (5) 7 5 (4) (6) Distance from source(0) Parent of node
48
Dijkstra Algorithm Result
(1) node distance 1 9 2 3 4 5 6 node parent 1 3 2 4 5 6 5 (2) 10 (0) 3 (3) 8 6 (5) 7 5 (4) (6) Distance from source(0) Parent of node
49
Dijkstra Algorithm Result
(1) node distance 1 9 2 14 3 4 5 6 node parent 1 3 2 4 5 6 5 (2) 10 (0) 3 (3) 8 6 (5) 7 5 (4) (6) Distance from source(0) Parent of node
50
Dijkstra Algorithm Result
(1) node distance 1 9 2 14 3 6 4 11 5 22 13 node parent 1 3 2 4 5 6 5 (2) 10 (0) 3 (3) 8 6 (5) 7 5 (4) (6) Distance from source(0) Parent of node
51
Dijkstra Algorithm (dijkstra.cpp)
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor;
52
Dijkstra Algorithm (dijkstra.cpp)
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor; Just like the iterators of STL, graph have associated types: vertex_descriptor, edge_descriptor, edte_iterator, vertex_iterator, etc… graph_traits<Graph> has these members
53
Dijkstra Algorithm (dijkstra.cpp)
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor; When you refer to a vertex in a graph, you should use vertex_descriptor. vertex_descriptor will change depend on the underlying data structure for vertices. it will be an index when you are using a vector (i.e. boost::vecS) a pointer to the list item when you are using a list (i.e. boost:listS) That way if you later decide to use a different data structure, you won't have to change your code.
54
Dijkstra Algorithm int N = num_vertices(g);
vector<vertex_descriptor> parents(N); // To store parents vector<int> distances(N); // To store distances value (int) 1 2 3 4 5 6 value (vertex) 1 2 3 4 5 6 distances parents
55
Dijkstra Algorithm meaning
dijkstra_shortest_paths(g, 0, predecessor_map(&parents[0]).distance_map(&distances[0])); meaning dijkstra_shortest_paths(g, 0, predecessor_map=&parents[0], distance_map=&distances[0]); address of parents address of distances
56
Dijkstra Algorithm meaning
dijkstra_shortest_paths(g, 0, predecessor_map(&parents[0]).distance_map(&distances[0])); meaning dijkstra_shortest_paths(g, 0, predecessor_map=&parents[0], distance_map=&distances[0]); predecessor map records parents of each node. (= it records the edges in the minimum spanning tree)
57
Graph Visualization Install Graphviz
extract graphviz-2.38.zip Boost create DOT language file to store graph, and graphviz program visualize that. Reference :
58
GraphViz (graph_viz.cpp)
boost::dynamic_properties dp; dp.property("node_id", get(vertex_index, g)); dp.property("label", get(edge_weight_t(), g)); ofstream dot("graph.dot"); write_graphviz_dp(dot, g, dp);
59
GraphViz (graph_viz.cpp)
boost::dynamic_properties dp; dp.property("node_id", get(vertex_index, g)); dp.property("label", get(edge_weight_t(), g)); ofstream dot("graph.dot"); write_graphviz_dp(dot, g, dp); return property_map of weight (slide 29, 30)
60
GraphViz move directory to graphviz
[Graphviz_dir]\bin > dot –T pdf [result_dot_file] -O
62
Extra Examples
63
Example) add Edges with weight
#include <iostream> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty; /* adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> */ typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty> Graph; int main(int, char*[]) { // Create a graph object Graph g(3); EdgeWeightProperty e1 = 5; add_edge(0, 1, e1, g); EdgeWeightProperty e2 = 3; add_edge(1, 2, e2, g); boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g); typedef boost::graph_traits<Graph>::edge_iterator edge_iter; std::pair<edge_iter, edge_iter> edgePair; for (edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) std::cout << EdgeWeightMap[*edgePair.first] << " "; } return 0;
64
Example) Articulation points
A vertex in an undirected connected graph is an articulation point iff removing it (and edges through it) disconnects the graph. B, A, G : Articulation point
65
Example) Articulation points
#include <boost/config.hpp> #include <vector> #include <list> #include <boost/graph/biconnected_components.hpp> #include <boost/graph/adjacency_list.hpp> #include <iterator> #include <iostream> #include <fstream> using namespace boost; int main(){ typedef adjacency_list < vecS, vecS, undirectedS > graph_t; typedef graph_traits < graph_t >::vertex_descriptor vertex_t; graph_t g(9); add_edge(0, 5, g); add_edge(0, 1, g); add_edge(0, 6, g); add_edge(1, 2, g); add_edge(1, 3, g); add_edge(1, 4, g); add_edge(2, 3, g); add_edge(4, 5, g); add_edge(6, 8, g); add_edge(6, 7, g); add_edge(7, 8, g); typedef boost::property_map<graph_t, boost::vertex_index_t>::type IndexMap; IndexMap index = get(boost::vertex_index, g); std::vector<vertex_t> art_points; articulation_points(g, std::back_inserter(art_points)); std::cout << "Found " << art_points.size() << " articulation points.\n"; std::ofstream fout; fout.open("art.dot"); fout << "graph A {\n" << "node[shape=\"circle\"]\n"; for (std::size_t i = 0; i < art_points.size(); ++i) { fout << index[art_points[i]] << " [ style=\"filled\", fillcolor=\"red\" ];" << std::endl; } typedef boost::graph_traits<graph_t>::edge_iterator edge_iter; std::pair<edge_iter, edge_iter> edgePair; for (edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) { fout << index[source(*edgePair.first, g)]<< "--"; fout << index[target(*edgePair.first, g)] << std::endl; fout << "}"; fout.close(); return 0;
66
Example) Articulation points
visualization in Graphviz
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.