Skip to content

Instantly share code, notes, and snippets.

@Zia-
Last active August 29, 2015 14:27
Show Gist options
  • Save Zia-/4da689ab9284386ffd4d to your computer and use it in GitHub Desktop.
Save Zia-/4da689ab9284386ffd4d to your computer and use it in GitHub Desktop.
Trying to replicate the test done by Vicky to see how to use an external file as graph data provider (https://gist.github.com/cvvergara/1e31d1ad14db171b26d4#file-graphboosttest1-cpp-L5)
#include <boost/config.hpp>
#include <iostream>
#include <stdlib.h>
#include <fstream>
//To include the lib file from a local dir: http://stackoverflow.com/questions/15935207/adding-header-files-to-eclipse-build-path-for-c
#include "postgres.h"
#include <boost/graph/adjacency_list.hpp>
/*
To be able to distinguish the edges (source,target) from the (target,source)
"cost" column weights are set to 1 for (source,target)
"reverse_cost" column weights are set to 2 for (target,source)
*/
typedef struct {
int64_t id;
int64_t source;
int64_t target;
float8 cost;
float8 reverse_cost;
} pgr_edge_t;
void import_from_file(const std::string &input_file_name, pgr_edge_t *edges, unsigned int *count) {
const char* file_name = input_file_name.c_str();
std::ifstream ifs(file_name);
if (!ifs) {
std::cerr << "The file " << file_name << " can not be opened!" << std::endl;
exit(1);
}
ifs >> (*count);
long edge_id, start_id, end_id;
double edge_weight, reverse_weight;
int i = 0;
while (i < (*count) && ifs >> edge_id) {
if (edge_id == -1) break;
edges[i].id = edge_id;
ifs >> edges[i].source;
ifs >> edges[i].target;
ifs >> edges[i].cost;
ifs >> edges[i].reverse_cost;
i++;
}
ifs.close();
}
using namespace boost;
template < typename DirectedGraph > void
simulation_undirected_in_directed_graph(pgr_edge_t *edges, int count)
{
const int V = count;
DirectedGraph digraph(V);
typename graph_traits < DirectedGraph >::vertex_descriptor vd;
typedef typename DirectedGraph::edge_property_type Weight;
typename property_map < DirectedGraph, edge_weight_t >::type
weight = get(edge_weight, digraph);
typename graph_traits < DirectedGraph >::out_edge_iterator out, out_end;
typename graph_traits < DirectedGraph >::edge_iterator ei;
typename graph_traits < DirectedGraph >::vertex_iterator vi;
std::deque< typename graph_traits < DirectedGraph >::vertex_descriptor > vert;
std::cout << "UNDIRECTED ON BOOST::DIRECTED GRAPH DEMO\n";
for (int i=0; i < count; ++i) {
if (edges[i].cost >= 0) {
add_edge(edges[i].source, edges[i].target, Weight(edges[i].cost), digraph);
add_edge(edges[i].target, edges[i].source, Weight(edges[i].cost), digraph);
}
if (edges[i].reverse_cost >= 0) {
add_edge(edges[i].target, edges[i].source, Weight(edges[i].reverse_cost), digraph);
add_edge(edges[i].source, edges[i].target, Weight(edges[i].reverse_cost), digraph);
}
}
for (vi = vertices(digraph).first; vi!=vertices(digraph).second; ++vi) {
std::cout << "out_edges(" << *vi << "):";
for (boost::tie(out, out_end) = out_edges(*vi, digraph); out != out_end; ++out) {
std::cout << ' ' << *out << "=" << get(weight, *out);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template < typename DirectedGraph > void
directed_graph_demo(pgr_edge_t *edges, int count)
{
const int V = count;
DirectedGraph digraph(V);
typename graph_traits < DirectedGraph >::vertex_descriptor vd;
typedef typename DirectedGraph::edge_property_type Weight;
typename property_map < DirectedGraph, edge_weight_t >::type
weight = get(edge_weight, digraph);
typename graph_traits < DirectedGraph >::out_edge_iterator out, out_end;
typename graph_traits < DirectedGraph >::edge_iterator e, ei;
typename graph_traits < DirectedGraph >::vertex_iterator vi;
std::deque< typename graph_traits < DirectedGraph >::vertex_descriptor > vert;
std::cout << "DIRECTED GRAPH DEMO\n";
for (int i=0; i < count; ++i) {
if (edges[i].cost >= 0)
add_edge(edges[i].source, edges[i].target, Weight(edges[i].cost), digraph);
if (edges[i].reverse_cost >= 0)
add_edge(edges[i].target, edges[i].source, Weight(edges[i].reverse_cost), digraph);
}
for (vi = vertices(digraph).first; vi!=vertices(digraph).second; ++vi) {
std::cout << "out_edges(" << *vi << "):";
for (boost::tie(out, out_end) = out_edges(*vi, digraph); out != out_end; ++out) {
std::cout << ' ' << *out << "=" << get(weight, *out);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template < typename UndirectedGraph > void
undirected_graph_demo(pgr_edge_t *edges, int count)
{
const int V = 3;
UndirectedGraph undigraph(V);
typename graph_traits < UndirectedGraph >::vertex_descriptor vd;
typedef typename UndirectedGraph::edge_property_type Weight;
typename property_map < UndirectedGraph, edge_weight_t >::type
weight = get(edge_weight, undigraph);
typename graph_traits < UndirectedGraph >::out_edge_iterator out, out_end;
typename graph_traits < UndirectedGraph >::edge_iterator e, ei;
typename graph_traits < UndirectedGraph >::vertex_iterator vi;
std::deque< typename graph_traits < UndirectedGraph >::vertex_descriptor > vert;
std::cout << "UNDIRECTED GRAPH DEMO\n";
for (int i=0; i < count; ++i) {
if (edges[i].cost >= 0)
add_edge(edges[i].source, edges[i].target, Weight(edges[i].cost), undigraph);
if (edges[i].reverse_cost >= 0)
add_edge(edges[i].target, edges[i].source, Weight(edges[i].reverse_cost), undigraph);
}
for (vi = vertices(undigraph).first; vi!=vertices(undigraph).second; ++vi) {
std::cout << "out_edges(" << *vi << "):";
for (boost::tie(out, out_end) = out_edges(*vi,undigraph); out != out_end; ++out) {
std::cout << ' ' << *out << "=" << get(weight, *out);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int
main()
{
pgr_edge_t edges[100];
unsigned int count = 0;
std::string fileName("./sampledata.data");
import_from_file(fileName, edges, &count);
typedef property < edge_weight_t, double >Weight;
typedef adjacency_list < listS, vecS, undirectedS,
no_property, Weight > UndirectedGraph;
typedef adjacency_list < listS, vecS, directedS,
no_property, Weight > DirectedGraph;
undirected_graph_demo < UndirectedGraph > (edges, count);
directed_graph_demo < DirectedGraph > (edges, count);
simulation_undirected_in_directed_graph < DirectedGraph > (edges, count);
return 0;
}
18
1 1 2 1 2
2 2 3 -1 2
3 3 4 -1 2
4 2 5 1 2
5 3 6 1 -1
6 7 8 1 2
7 8 5 1 2
8 5 6 1 2
9 6 9 1 2
10 5 10 1 2
11 6 11 1 -1
12 10 11 1 -1
13 11 12 1 -1
14 10 13 1 2
15 9 12 1 2
16 4 9 1 2
17 14 15 1 2
18 16 17 1 2
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment