Difference between revisions of "Boost/BGL/CopyAGraph"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(Created page with '==CopyAGraph.cpp== <source lang="cpp"> // http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/read_graphviz.html #include <iostream> #include <string> #include <fstream> #includ…')
 
Line 1: Line 1:
 +
This example simply demonstrates that the copy operator preserves all of the graph properties.
 +
 
==CopyAGraph.cpp==
 
==CopyAGraph.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
Line 5: Line 7:
 
#include <string>
 
#include <string>
 
#include <fstream>
 
#include <fstream>
 
+
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/graphviz.hpp>
 
#include <boost/graph/graphviz.hpp>
 
+
 
// We need a bool to store visibility of each edge
 
// We need a bool to store visibility of each edge
 
struct EdgeVisibility
 
struct EdgeVisibility
Line 14: Line 16:
 
   bool visible;
 
   bool visible;
 
};
 
};
 
+
 
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeVisibility> Graph;
 
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeVisibility> Graph;
 
+
 
Graph CreateGraph();
 
Graph CreateGraph();
void Test(const Graph g);
+
void Test(const Graph& g);
 
+
 
int main(int argc, char*argv[])
 
int main(int argc, char*argv[])
 
{
 
{
Line 26: Line 28:
 
   return 0;
 
   return 0;
 
}
 
}
 
+
void Test(const Graph inputGraph)
+
void Test(const Graph& g)
 
{
 
{
   Graph g = inputGraph;
+
  // Copy the graph
   // Iterate over the edges, setting the 10th one to invisible
+
   Graph copiedGraph = g;
 +
 +
   // Set the 10th edge of the copied graph to invisible
 
   unsigned int counter = 0;
 
   unsigned int counter = 0;
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
+
   std::pair<Graph::edge_iterator, Graph::edge_iterator> copiedGraphOriginalIteratorRange = boost::edges(copiedGraph);
   for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
+
   for(Graph::edge_iterator copiedGraphOriginalEdgeIterator = copiedGraphOriginalIteratorRange.first; copiedGraphOriginalEdgeIterator != copiedGraphOriginalIteratorRange.second; ++copiedGraphOriginalEdgeIterator)
 
     {
 
     {
 
     if(counter == 10)
 
     if(counter == 10)
 
       {
 
       {
       g[*edgeIterator].visible = false;
+
       copiedGraph[*copiedGraphOriginalEdgeIterator].visible = false;
 
       }
 
       }
 
     counter++;
 
     counter++;
 
     }
 
     }
 
      
 
      
   // Output the visibility of the edges. This one works properly:
+
   // Display both graphs edge visibilities so that we are convinced the two graphs are actually separate objects.
{
+
 
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g); // Note this is the new graph
+
  // Display the input graph's edge visibility
   for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
+
  std::cout << "Input graph edge visiblity: " << std::endl;
 +
   std::pair<Graph::edge_iterator, Graph::edge_iterator> gEdgeIteratorRange = boost::edges(g); // Note this is the new graph
 +
   for(Graph::edge_iterator gEdgeIterator = gEdgeIteratorRange.first; gEdgeIterator != gEdgeIteratorRange.second; ++gEdgeIterator)
 
     {
 
     {
     std::cout << g[*edgeIterator].visible << " ";
+
     std::cout << g[*gEdgeIterator].visible << " ";
 
     }
 
     }
 
   std::cout << std::endl;
 
   std::cout << std::endl;
}
+
 
+
 
// Output the visibility of the edges. This one always says all of the edges are visible
+
  // Display the copied graph's edge visibility
{
+
  std::cout << "Copied graph edge visiblity: " << std::endl;
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(inputGraph); // Note this is the original graph
+
   std::pair<Graph::edge_iterator, Graph::edge_iterator> copiedGraphEdgeIteratorRange = boost::edges(copiedGraph); // Note this is the original graph
   for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
+
   for(Graph::edge_iterator copiedGraphEdgeIterator = copiedGraphEdgeIteratorRange.first; copiedGraphEdgeIterator != copiedGraphEdgeIteratorRange.second; ++copiedGraphEdgeIterator)
 
     {
 
     {
     std::cout << g[*edgeIterator].visible << " ";
+
     std::cout << copiedGraph[*copiedGraphEdgeIterator].visible << " ";
 
     }
 
     }
  std::cout << std::endl;
+
    std::cout << std::endl;
 
}
 
}
 
+
}
+
 
+
 
Graph CreateGraph()
 
Graph CreateGraph()
 
{
 
{
 
   Graph g(19);
 
   Graph g(19);
 
+
 
   boost::add_edge(0,1,g);
 
   boost::add_edge(0,1,g);
 
   boost::add_edge(1,2,g);
 
   boost::add_edge(1,2,g);
Line 86: Line 90:
 
   boost::add_edge(16,17,g);
 
   boost::add_edge(16,17,g);
 
   boost::add_edge(17,18,g);
 
   boost::add_edge(17,18,g);
 
+
 
   // Set all edges to visible
 
   // Set all edges to visible
 
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
 
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
Line 93: Line 97:
 
     g[*edgeIterator].visible = true;
 
     g[*edgeIterator].visible = true;
 
     }
 
     }
 +
   
 
   return g;
 
   return g;
 
}
 
}

Revision as of 18:43, 15 November 2011

This example simply demonstrates that the copy operator preserves all of the graph properties.

CopyAGraph.cpp

// http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/read_graphviz.html
#include <iostream>
#include <string>
#include <fstream>
 
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
 
// We need a bool to store visibility of each edge
struct EdgeVisibility
{
  bool visible;
};
 
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeVisibility> Graph;
 
Graph CreateGraph();
void Test(const Graph& g);
 
int main(int argc, char*argv[])
{
  Graph g = CreateGraph();
  Test(g);
  return 0;
}
 
void Test(const Graph& g)
{
  // Copy the graph
  Graph copiedGraph = g;
 
  // Set the 10th edge of the copied graph to invisible
  unsigned int counter = 0;
  std::pair<Graph::edge_iterator, Graph::edge_iterator> copiedGraphOriginalIteratorRange = boost::edges(copiedGraph);
  for(Graph::edge_iterator copiedGraphOriginalEdgeIterator = copiedGraphOriginalIteratorRange.first; copiedGraphOriginalEdgeIterator != copiedGraphOriginalIteratorRange.second; ++copiedGraphOriginalEdgeIterator)
    {
    if(counter == 10)
      {
      copiedGraph[*copiedGraphOriginalEdgeIterator].visible = false;
      }
    counter++;
    }
 
  // Display both graphs edge visibilities so that we are convinced the two graphs are actually separate objects.
 
  // Display the input graph's edge visibility
  std::cout << "Input graph edge visiblity: " << std::endl;
  std::pair<Graph::edge_iterator, Graph::edge_iterator> gEdgeIteratorRange = boost::edges(g); // Note this is the new graph
  for(Graph::edge_iterator gEdgeIterator = gEdgeIteratorRange.first; gEdgeIterator != gEdgeIteratorRange.second; ++gEdgeIterator)
    {
    std::cout << g[*gEdgeIterator].visible << " ";
    }
  std::cout << std::endl;
 
 
  // Display the copied graph's edge visibility
  std::cout << "Copied graph edge visiblity: " << std::endl;
  std::pair<Graph::edge_iterator, Graph::edge_iterator> copiedGraphEdgeIteratorRange = boost::edges(copiedGraph); // Note this is the original graph
  for(Graph::edge_iterator copiedGraphEdgeIterator = copiedGraphEdgeIteratorRange.first; copiedGraphEdgeIterator != copiedGraphEdgeIteratorRange.second; ++copiedGraphEdgeIterator)
    {
    std::cout << copiedGraph[*copiedGraphEdgeIterator].visible << " ";
    }
    std::cout << std::endl;
}
 
Graph CreateGraph()
{
  Graph g(19);
 
  boost::add_edge(0,1,g);
  boost::add_edge(1,2,g);
  boost::add_edge(2,3,g);
  boost::add_edge(3,4,g);
  boost::add_edge(4,5,g);
  boost::add_edge(5,6,g);
  boost::add_edge(6,7,g);
  boost::add_edge(7,8,g);
  boost::add_edge(7,9,g);
  boost::add_edge(9,10,g);
  boost::add_edge(7,11,g);
  boost::add_edge(11,12,g);
  boost::add_edge(12,13,g);
  boost::add_edge(13,14,g);
  boost::add_edge(13,15,g);
  boost::add_edge(15,16,g);
  boost::add_edge(16,17,g);
  boost::add_edge(17,18,g);
 
  // Set all edges to visible
  std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
  for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
    {
    g[*edgeIterator].visible = true;
    }
 
  return g;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(CopyAGraph)
 
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(CopyAGraph CopyAGraph.cpp)
target_link_libraries(CopyAGraph boost_graph)