fork download
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <iostream>
  3.  
  4. int main() {
  5. // Define a simple undirected graph using an adjacency list
  6. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
  7.  
  8. // Create a graph instance
  9. Graph g;
  10.  
  11. // Add some vertices and edges to the graph
  12. boost::add_edge(0, 1, g);
  13. boost::add_edge(0, 2, g);
  14. boost::add_edge(1, 2, g);
  15. boost::add_edge(1, 3, g);
  16.  
  17. // Loop over all edges in the graph
  18. typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
  19. std::pair<edge_iter, edge_iter> edgePair;
  20. for (edgePair = boost::edges(g); edgePair.first != edgePair.second; ++edgePair.first) {
  21. // Get the current edge
  22. boost::graph_traits<Graph>::edge_descriptor e = *edgePair.first;
  23.  
  24. // Get the source and target vertices of the current edge
  25. boost::graph_traits<Graph>::vertex_descriptor u = boost::source(e, g);
  26. boost::graph_traits<Graph>::vertex_descriptor v = boost::target(e, g);
  27.  
  28. // Print the edge
  29. std::cout << "Edge: " << u << " -> " << v << std::endl;
  30. }
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Edge: 0 -> 1
Edge: 0 -> 2
Edge: 1 -> 2
Edge: 1 -> 3