Last active
August 29, 2015 14:17
-
-
Save cvvergara/1e31d1ad14db171b26d4 to your computer and use it in GitHub Desktop.
Stores in an array pgr edge structure and that serves as the input of the graphs (graphBoostTest.expected)
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; | |
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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment