0

I am trying to implement a directed graph in order to compute the shortest path by means of Dijkstra's algorithm. In order to do this I look at the example provided by the boost website and try to apply what is done in the example. The graph that I try to implement has more than 15k nodes and 50k of edges. The code of the example provided on the website looks similar to:

typedef adjacency_list < listS, vecS, directedS, no_property, property <edge_weight_t, int > > graph_t;
typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;
const int num_nodes = 5;

enum nodes { A, B, C, D, E };
char name[] = "ABCDE";

Edge edge_array[] = { Edge(A, C), Edge(B, B), Edge(B, D), Edge(B, E),
Edge(C, B), Edge(C, D), Edge(D, E), Edge(E, A), Edge(E, B)
};
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1 };

int num_arcs = sizeof(edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);
property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight,   g);
std::vector<vertex_descriptor> p(num_vertices(g));
std::vector<int> d(num_vertices(g));
vertex_descriptor s = vertex(A, g);

dijkstra_shortest_paths(g, s, predecessor_map(boost::make_iterator_property_map(p.begin(), get(boost::vertex_index, g))).distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, g))));
std::cout << "distances and parents:" << std::endl;
graph_traits < graph_t >::vertex_iterator vi, vend;
for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";
std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::
  endl;
}

Using this to implement my own graph results in:

typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, int > > MyGraph;
typedef graph_traits < MyGraph >::vertex_descriptor vertex_descriptor;
typedef std::pair<int, int> Edge;

// edge_array is filled. Assume that it works correct.

int num_arcs = sizeof(edge_array) / sizeof(Edge);//<-- Cause of the error
MyGraph Graph(edge_array, edge_array + num_arcs, weights, num_nodes);//<-- Exception is raised. 
property_map<MyGraph, edge_weight_t>::type weightmap = get(edge_weight,   Graph);

std::vector<vertex_descriptor> p(num_vertices(Graph));
std::vector<int> d(num_vertices(Graph));
vertex_descriptor s = vertex(A, Graph);

There where the arrow was commented is the cause of the error which occurs at the line thereafter. There is no error outputed other than Unhandled exception at 0x012BA9A9 in celfpp.exe: 0xC0000005: Access violation reading location 0x00434000.

In case of using two edge_arrays, one used from the example and the other of my own. In case of exectuing the code:

int num_arcs = sizeof(example_edge_array) / sizeof(Edge);
graph_t g(edge_array, edge_array + num_arcs, weights, num_nodes);

The code does not encounter an exception. To me it is not clear why and I assume that this is not the way to solve my problem. However, it does provides a suggestion of what causes the problem.

Is there anyone whom might have a suggestion how I could solve this?

EDIT (1):

I have found that in the following remains m_value remains undefined:

template <class Tag, class T, class  
Base = no_property>
struct property {
typedef Base next_type;
typedef Tag tag_type;
typedef T value_type;
property(const T& v = T()) : m_value(v) { }
property(const T& v, const Base& b) : m_value(v), m_base(b) { }
// copy constructor and assignment operator will be generated by compiler

T m_value;
Base m_base;
};  
Jordi Ozir
  • 109
  • 4
  • Step through with your debugger to identify the source of the problem. `0xC0000005` usually indicates there's a `nullptr` dereferenced. I'd suspect some out of bounds access in one of these lines: _`std::cout << "distance(" << name[*vi] << ") = " << d[*vi] << ", ";`_. – πάντα ῥεῖ Nov 04 '15 at 13:30
  • I will have a look at it. However, at the line of code after defining num_arcs an exception is raised and the program is not ran further and thus the code of line that you mention is never executed. – Jordi Ozir Nov 04 '15 at 13:34
  • 2
    @Clifford I've looked at it. You need to make it a SSCCE. We can't make heads or tails of that patchwork puzzle – sehe Nov 04 '15 at 19:29

0 Answers0