[Lemon-user] newbie question: ArcLookUp segfaults?!

Bjoern bjoern.l at fu-berlin.de
Fri May 28 09:42:51 CEST 2010


Hi,

i possibly like  to use lemon for network graphs, but i'm wondering about
the following, i tried to write a simple program:

#########################################################
#include <iostream>
#include <string>
#include <lemon/list_graph.h>
#include <map>

using namespace std;
using namespace lemon;

class TopGraph {

public:
   lemon::ListDigraph m_graph;

   lemon::ListDigraph::NodeMap<std::string> nameMap;
   lemon::ListDigraph::ArcMap<double> doubleMap;

   std::map<std::string, lemon::ListDigraph::Node> index;

   TopGraph() :  nameMap(m_graph), doubleMap(m_graph)  {
   }

   // adds node if it not already exists
   bool enter(std::string v){

       if(index.find(v) == index.end()){

           index[v] = m_graph.addNode();
           nameMap[index[v]] = v;
           return true;
       }
       return false;
   }

   // sets or updates an arc
   void set(std::string v1, std::string v2, double lq){
       lemon::DynArcLookUp<lemon::ListDigraph> arclookup(m_graph);
       //lemon::ArcLookUp<lemon::ListDigraph> arclookup(m_graph); // <-
segfaults
             enter(v1); // ensure nodes are there
       enter(v2);
             lemon::ListDigraph::Arc arc = lemon::INVALID;

       cout << "setLQ: " << v1 << " " << v2 << " " <<
m_graph.id(index[v1]) << " " << m_graph.id(index[v2]) << "\n";
       arc = arclookup(index[v1], index[v2]);

       if(arc == lemon::INVALID){
           arc = m_graph.addArc(index[v1], index[v2]);
       }

       doubleMap[arc] = lq;
   }

   void print(){
       cout << "*****\n";
         for (ListDigraph::ArcIt ait(m_graph); ait != INVALID; ++ait){
             cout << nameMap[m_graph.source(ait)] << " -> " <<
nameMap[m_graph.target(ait)] << " lq=" << doubleMap[ait] <<
"\n";
         }
   }
};

TopGraph topgraph;

int
main (int argc, char *argv[])
{
   topgraph.set("a", "b", 0.5);
   topgraph.print();
   topgraph.set("a", "c", 0.7);
   topgraph.print();
   topgraph.set("a", "b", 0.6);
   topgraph.print();
}
#########################################################

What i'm wondering about is that when i use ArcLookUp instead of
DynArcLookUp the program segfaults.
I read the documentation, but it just says i should call refresh if the
graph changed (but it does not from
the creation of ArcLookUp til it's usage), and even when i use refresh it
segfaults.

DynArcLookUp may be what i'm looking for, however getting a segfault with
ArcLookUp i'm wondering if i do something fundamentally wrong?!

Second question, is there a more straightforwarded way to access nodes via
a map, so to say the reverse
of what a lemon-map is doing, than keeping the nodes in a std::map like i
do it above?

Thanks in advance, Björn









More information about the Lemon-user mailing list