【问题标题】:Lemon Graph Library C++ - addNode using loopLemon Graph Library C++ - 使用循环的 addNode
【发布时间】:2012-10-24 15:07:12
【问题描述】:

是否有可能在 C++ 中使用循环创建柠檬图?

我的问题:

  1. 一个数据库表(我们称之为 t_nodes),列:node
  2. 带有图形信息的数据库表(我们称之为 t_edges):node1 |节点2 | edgeScore
  3. 超过 10000 个条目

我想要的结果:

  1. 有向图:例如N1->N2; N2 -> N3; N3 -> N1

我的问题

  1. 是否可以在t_nodes 表中为每个条目使用循环向图表添加节点

    • 到目前为止,我只是找到了手动添加每个节点的实现(参见下面的示例)
    • 真的没有机会使用循环将节点添加到柠檬图中吗?
  2. 如何为t_edges 中提到的所有关系使用循环?

感谢您的宝贵时间,非常感谢您提供任何帮助!


在周末有一些空闲时间并花一些时间骑自行车后,我找到了解决方案:)

我的解决方案:

似乎,柠檬没有提供支持图表内边缘的附加信息的可能性。因此,我刚刚创建了一个额外的向量来存储这些信息。但是,出于某些目的,使用 hashmap 访问节点可能更明智。

看看开发的示例脚本(非常简单;))


柠檬C++-代码示例(参考: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];
  }

}

【问题讨论】:

    标签: c++ graph lemon-graph-library


    【解决方案1】:

    当然,您可以循环执行 AddNode 和 AddArc。或者在递归函数中。或者以您想要的任何其他方式。

    你试过了吗?有什么错误吗?

    【讨论】:

    • 感谢您回答 rici! ...但是我该怎么做呢? ...对不起,也许我只需要一点实现提示。你将如何编写循环,尤其是在循环中添加一个节点?
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多