2012-10-24 44 views
2

是否有可能使用C++中的循環創建檸檬圖?檸檬圖庫C++ - 使用循環的addNode

我的問題:

  1. 數據庫表(姑且稱之爲t_nodes)與列:節點
  2. 數據庫表(姑且稱之爲t_edges)與圖形信息:節點1 | node2 | edgeScore
  3. 超過10000條目

我期望的結果:

  1. 一個有向圖:例如N1→N2; N2→N3; N3 - > N1

我的問題

  1. 是否有可能使用循環每個條目t_nodes內的節點添加到圖

    • 到目前爲止,我剛剛發現他們手動添加每個節點的實現(請參閱下面的示例)
    • 真的沒有機會使用循環將節點添加到檸檬圖嗎?
  2. 如何在t_edges中提及的所有關係使用循環?

感謝您的時間和任何幫助,非常感謝!


有周末空閒的時間和花費一些時間對我的自行車後,我找到了解決辦法:)

我的解決方案:

看來,檸檬沒有提供支持圖中邊緣附加信息的可能性。因此我只是創建了一個額外的矢量來存儲這些信息。但是,出於某些目的,使用散列映射來訪問節點可能更明智。

看一看發達示例腳本(很瑣碎;))


檸檬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]; 
    } 

} 

回答

0

當然,你可以執行ADDNODE和一個循環中的AddArc。或者在遞歸函數中。或以任何其他方式,你想。

你試過了嗎?有沒有一些錯誤?

+0

謝謝你回答rici! ...但我該怎麼做? ...對不起,也許我只需要一點實施提示。 你將如何編寫循環,特別是在循環中添加一個節點? –