Difference between revisions of "Boost/BGL/EdgeProperties"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==CreateGraph.cpp== <source lang="cpp"> #include <iostream> // for std::cout #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> /…')
 
Line 1: Line 1:
 
==CreateGraph.cpp==
 
==CreateGraph.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
#include <iostream>                 // for std::cout
+
#include <iostream>
 
#include <boost/graph/graph_traits.hpp>
 
#include <boost/graph/graph_traits.hpp>
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/adjacency_list.hpp>
  
// define the type of the edge weight property (we want the weights to be doubles)
 
 
typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
 
typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
  
// Define the type of the graph
+
/*
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, EdgeWeightProperty> Graph; //simple
+
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*[])
 
int main(int,char*[])
 
{
 
{
   // Declare a graph object
+
   // Create a graph object
   Graph g(2); //a graph with 2 vertices
+
   Graph g(3);
  
   // Add an edge between node 0 and node 1 with weight 1.2
+
   EdgeWeightProperty e1 = 5;
   EdgeWeightProperty e = 1.2;
+
  add_edge(0, 1, e1, g);
   add_edge(0, 1, e, g);
+
 
 +
   EdgeWeightProperty e2 = 3;
 +
   add_edge(1, 2, e2, g);
  
  //get the 0th edge weight
 
 
   boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), 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;
 
   typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
 
   std::pair<edge_iter, edge_iter> edgePair;
 
   std::pair<edge_iter, edge_iter> edgePair;
 
   for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
 
   for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
 
   {
 
   {
       std::cout << "(" << EdgeWeightMap[source(*edgePair.first, g)]
+
       std::cout << EdgeWeightMap[*edgePair.first] << " ";
              << "," << EdgeWeightMap[target(*edgePair.first, g)] << ") weight: " << EdgeWeightMap[*edgePair.first];
+
 
   }
 
   }
  
return 0;
+
  return 0;
 
}
 
}
  

Revision as of 10:56, 27 January 2011

CreateGraph.cpp

#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;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(Properties)
 
set(Boost_USE_MULTITHREADED ON) # which is the default
FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required)
 
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
ADD_EXECUTABLE(Properties Properties.cpp)
target_link_libraries(Properties)