Difference between revisions of "Boost/BGL/Directed/Weighted"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==Edge.cpp== <source lang="cpp"> #include <iostream> #include <boost/graph/graph_traits.hpp> #include <boost/graph/directed_graph.hpp> typedef boost::property<boost::edge_weight…')
 
Line 26: Line 26:
 
   boost::add_edge(v1,v2,weight1,g);
 
   boost::add_edge(v1,v2,weight1,g);
  
   // Retrieve the weight of an edge
+
   // Retrieve the weight of an edge (method 1, works)
 
+
  {
 
   typedef boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap;
 
   typedef boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap;
 
   EdgeWeightMap edgeWeightMap = get(boost::edge_weight, g);
 
   EdgeWeightMap edgeWeightMap = get(boost::edge_weight, g);
Line 34: Line 34:
 
   Graph::edge_descriptor edge = edgePair.first;
 
   Graph::edge_descriptor edge = edgePair.first;
  
   std::cout << std::cout << "Edge (" << v0 << ", " << v1 << ") has weight " << edgeWeightMap[edge] << std::endl;
+
   std::cout << "Edge (" << v0 << ", " << v1 << ") has weight " << edgeWeightMap[edge] << std::endl;
 +
  }
 +
 
 +
  // Retrieve the weight of an edge (method 2, doesn't work)
 +
  {
 +
  std::pair<Graph::edge_descriptor, bool> edgePair = boost::edge(v0, v1, g);
 +
  Graph::edge_descriptor edge = edgePair.first;
 +
  std::cout << "Edge (" << v0 << ", " << v1 << ") has weight " << g[edge]->edge_weight << std::endl;
 +
  }
 
    
 
    
 
   return 0;
 
   return 0;

Revision as of 17:06, 11 June 2011

Edge.cpp

#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/directed_graph.hpp>
 
typedef boost::property<boost::edge_weight_t, float> EdgeWeightProperty;
 
typedef boost::directed_graph<boost::no_property, EdgeWeightProperty> Graph;
 
int main(int,char*[])
{
  // Create a graph object
  Graph g;
 
  // Add vertices to the graph
  Graph::vertex_descriptor v0 = g.add_vertex();
  Graph::vertex_descriptor v1 = g.add_vertex();
  Graph::vertex_descriptor v2 = g.add_vertex();
 
  // Create weighted edges
  EdgeWeightProperty weight0 = 5;
  boost::add_edge(v0,v1,weight0,g);
 
  EdgeWeightProperty weight1 = 6;
  boost::add_edge(v1,v2,weight1,g);
 
  // Retrieve the weight of an edge (method 1, works)
  {
  typedef boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap;
  EdgeWeightMap edgeWeightMap = get(boost::edge_weight, g);
 
  std::pair<Graph::edge_descriptor, bool> edgePair = boost::edge(v0, v1, g);
  Graph::edge_descriptor edge = edgePair.first;
 
  std::cout << "Edge (" << v0 << ", " << v1 << ") has weight " << edgeWeightMap[edge] << std::endl;
  }
 
  // Retrieve the weight of an edge (method 2, doesn't work)
  {
  std::pair<Graph::edge_descriptor, bool> edgePair = boost::edge(v0, v1, g);
  Graph::edge_descriptor edge = edgePair.first;
  std::cout << "Edge (" << v0 << ", " << v1 << ") has weight " << g[edge]->edge_weight << std::endl;
  }
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(Weighted)
 
set(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.38 COMPONENTS required)
 
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
ADD_EXECUTABLE(Weighted Weighted.cpp)