Skip to content

Instantly share code, notes, and snippets.

@thirdwing
Last active December 4, 2015 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thirdwing/6777b8b190b4abff297b to your computer and use it in GitHub Desktop.
Save thirdwing/6777b8b190b4abff297b to your computer and use it in GitHub Desktop.
// boost graph serialization example
// g++ boost_graph_serialize.cpp -lboost_serialization -o test
#include <iostream>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adj_list_serialize.hpp>
using namespace boost;
typedef boost::adjacency_list<listS, vecS, undirectedS> mygraph;
int main() {
mygraph::vertex_iterator vertexIt, vertexEnd;
mygraph::adjacency_iterator neighbourIt, neighbourEnd;
mygraph g1;
add_edge (0, 1, g1);
add_edge (0, 3, g1);
add_edge (1, 2, g1);
add_edge (2, 3, g1);
std::cout << "graph 1" << std::endl;
tie(vertexIt, vertexEnd) = vertices(g1);
for (; vertexIt != vertexEnd; ++vertexIt) {
std::cout << *vertexIt << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt, g1);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << *neighbourIt << " ";
std::cout << std::endl;
}
mygraph g2;
add_edge (0, 1, g2);
add_edge (0, 3, g2);
std::cout << "graph 2" << std::endl;
tie(vertexIt, vertexEnd) = vertices(g2);
for (; vertexIt != vertexEnd; ++vertexIt) {
std::cout << *vertexIt << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt, g2);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << *neighbourIt << " ";
std::cout << std::endl;
}
std::string filename = "demofile.txt";
std::cout << "Saving to " << filename << std::endl;
std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << g1;
oa << g2;
ofs.close();
std::ifstream ifs(filename.c_str());
boost::archive::text_iarchive ia(ifs);
std::cout << "Reading from " << filename << std::endl;
mygraph g3, g4;
ia >> g3;
std::cout << "Graph 1" << std::endl;
tie(vertexIt, vertexEnd) = vertices(g3);
for (; vertexIt != vertexEnd; ++vertexIt) {
std::cout << *vertexIt << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt, g2);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << *neighbourIt << " ";
std::cout << std::endl;
}
std::cout << "Graph 2" << std::endl;
ia >> g4;
ifs.close();
tie(vertexIt, vertexEnd) = vertices(g4);
for (; vertexIt != vertexEnd; ++vertexIt) {
std::cout << *vertexIt << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt, g4);
for (; neighbourIt != neighbourEnd; ++neighbourIt)
std::cout << *neighbourIt << " ";
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment