Created
March 30, 2015 04:13
-
-
Save cvvergara/c1049f3ea79a3d5c0583 to your computer and use it in GitHub Desktop.
Based on pgr_edge_t the graph is loaded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/config.hpp> | |
#include <iostream> | |
#include <stdlib.h> | |
#include <fstream> | |
#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; | |
typedef struct { | |
int64_t id; | |
} pgr_vertex_t; | |
typedef struct { | |
int64_t id; | |
int64_t source; | |
int64_t target; | |
float8 cost; | |
} boost_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 <class G, class E> | |
static void | |
graph_add_edge(G &graph, const pgr_edge_t &edge, bool undirectedOnDirected) | |
{ | |
bool inserted; | |
E e; | |
if (edge.cost >= 0) { | |
tie(e, inserted) = add_edge(edge.source, edge.target, graph); | |
graph[e].cost = edge.cost; | |
graph[e].id = edge.id; | |
} | |
if (edge.reverse_cost >= 0) { | |
tie(e, inserted) = add_edge(edge.target, edge.source, graph); | |
graph[e].cost = edge.reverse_cost; | |
graph[e].id = edge.id; | |
} | |
if (undirectedOnDirected) { | |
if (edge.cost >= 0) { | |
tie(e, inserted) = add_edge(edge.target, edge.source, graph); | |
graph[e].cost = edge.cost; | |
graph[e].id = edge.id; | |
} | |
if (edge.reverse_cost >= 0) { | |
tie(e, inserted) = add_edge(edge.source, edge.target, graph); | |
graph[e].cost = edge.reverse_cost; | |
graph[e].id = edge.id; | |
} | |
} | |
} | |
template < typename DirectedGraph > void | |
simulation_undirected_in_directed_graph(pgr_edge_t *data_edges, int count) | |
{ | |
const int V = 1; | |
DirectedGraph digraph(V); | |
typename graph_traits < DirectedGraph >::vertex_descriptor vd; | |
typedef typename graph_traits < DirectedGraph >::edge_descriptor ed; | |
typename graph_traits < DirectedGraph >::out_edge_iterator out, out_end; | |
typename graph_traits < DirectedGraph >::edge_iterator ei; | |
typename graph_traits < DirectedGraph >::vertex_iterator vi; | |
bool inserted; | |
std::cout << "UNDIRECTED ON BOOST::DIRECTED GRAPH DEMO\n"; | |
for (int i=0; i < count; ++i) { | |
graph_add_edge< DirectedGraph, ed>(digraph, data_edges[i], true); | |
} | |
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 << "=" << digraph[*out].cost; | |
} | |
std::cout << std::endl; | |
} | |
std::cout << std::endl; | |
} | |
template < typename DirectedGraph > void | |
directed_graph_demo(pgr_edge_t *data_edges, int count) | |
{ | |
const int V = 1; | |
DirectedGraph digraph(V); | |
typename graph_traits < DirectedGraph >::vertex_descriptor vd; | |
typedef typename graph_traits < DirectedGraph >::edge_descriptor edge_descriptor; | |
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; | |
bool inserted; | |
std::cout << "DIRECTED GRAPH DEMO\n"; | |
for (int i=0; i < count; ++i) { | |
graph_add_edge< DirectedGraph, edge_descriptor>(digraph, data_edges[i], false); | |
} | |
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 << "=" << digraph[*out].cost; | |
} | |
std::cout << std::endl; | |
} | |
std::cout << std::endl; | |
} | |
template < typename UndirectedGraph > void | |
undirected_graph_demo(pgr_edge_t *data_edges, int count) | |
{ | |
const int V = 3; | |
UndirectedGraph undigraph(V); | |
typename graph_traits < UndirectedGraph >::vertex_descriptor vd; | |
typedef typename graph_traits < UndirectedGraph >::edge_descriptor ed; | |
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; | |
bool inserted; | |
std::deque< typename graph_traits < UndirectedGraph >::vertex_descriptor > vert; | |
std::cout << "UNDIRECTED GRAPH DEMO\n"; | |
for (int i=0; i < count; ++i) { | |
graph_add_edge< UndirectedGraph, ed>(undigraph, data_edges[i],false); | |
} | |
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 << "=" << undigraph[*out].cost; | |
} | |
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 adjacency_list < listS, vecS, undirectedS, | |
pgr_vertex_t, boost_edge_t > UndirectedGraph; | |
typedef adjacency_list < listS, vecS, directedS, | |
pgr_vertex_t, boost_edge_t > DirectedGraph; | |
undirected_graph_demo < UndirectedGraph > (edges, count); | |
directed_graph_demo < DirectedGraph > (edges, count); | |
simulation_undirected_in_directed_graph < DirectedGraph > (edges, count); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment