Lemon Graph Library C++ - addNode using loop

1.6k views Asked by At

Is there a possibility to create a lemon graph using a loop in C++?

My Problem:

  1. A database table (let's call it t_nodes) with column: node
  2. A database table (let's call it t_edges) with graph information: node1 | node2 | edgeScore
  3. More than 10000 entries

My desired outcome:

  1. A directed graph: e.g. N1 -> N2; N2 -> N3; N3 -> N1

My Question

  1. Is it possible to use a loop for each entry within the t_nodes table to add a node to a graph

    • So far, I just found implementations where they add each node manually (see example below)
    • Is there really no opportunity to use a loop to add nodes to a lemon graph?
  2. How can I use a loop for all the relations mentioned in t_edges?

Thanks for your time and any help is greatly appreciated!


After having some free time over the weekend and spending some time on my bike I found the solution :)

My Solution:

It seems, that lemon did not offer the possibility to support additional information to the edges within the graph. Therefore I just created a additional vector for storing this information. However, for some purposes it may be more wise to use a hashmap for accessing the nodes.

Have a look at the developed example script (very trivial ;) )


Lemon C++-Code example (reference: http://lemon.cs.elte.hu/pub/tutorial/a00022.html):

 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2010
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */


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

using namespace lemon;
using namespace std;

int main()
{
  ListDigraph g;


  ListDigraph::Node u = g.addNode();
  ListDigraph::Node v = g.addNode();
  ListDigraph::Arc  a = g.addArc(u, v);

  cout << "Hello World! This is LEMON library here." << endl;
  cout << "We have a directed graph with " << countNodes(g) << " nodes "
       << "and " << countArcs(g) << " arc." << endl;

  return 0;


  // Further development
  ListDigraph graph;

  vector <string> name;
  name.push_back("A");
  name.push_back("B");
  name.push_back("C");

  for (unsigned int n=0; n<name.size(); n++) {
      ListDigraph::Node node = graph.addNode();
      lemon_node_vector[n].id = n;
      lemon_node_vector[n].name = name[n];
  }

}
1

There are 1 answers

1
rici On

Of course, you can execute AddNode and AddArc in a loop. Or in a recursive function. Or in any other way you want to.

Did you try it? Was there some error?