<div dir="ltr">Hello everyone,<div><br></div><div>I am still new to Lemon, and not very confident with its concepts. I would like to know the "proper" way to construct a ListGraph or SmartGraph from a list of edges.</div><div><br></div><div>For simplicity, support the edges are given as an std::vector of std::pair<int,int>. Then it is very easy to build a StaticDigraph.</div><div><br></div><div>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,0,128)">vector</span><<span style="color:rgb(128,0,128)">pair</span><<span style="color:rgb(128,128,0)">int</span>,<span style="color:rgb(128,128,0)">int</span>>><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">edges</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>{{<span style="color:rgb(0,0,128)">0</span>,<span style="color:rgb(0,0,128)">1</span>},{<span style="color:rgb(0,0,128)">1</span>,<span style="color:rgb(0,0,128)">0</span>},{<span style="color:rgb(0,0,128)">1</span>,<span style="color:rgb(0,0,128)">2</span>}};<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">already</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">sorted</span></pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,0,128)">StaticDigraph</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">digraph</span>;</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(9,46,100)">digraph</span>.<span style="color:rgb(0,103,124)">build</span>(<span style="color:rgb(0,0,128)">3</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">edges</span>.<span style="color:rgb(0,103,124)">begin</span>(),<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">edges</span>.<span style="color:rgb(0,103,124)">end</span>());</pre><pre style="margin:0px;text-indent:0px"><br></pre>If I want a ListGraph, I believe I could do this:<br></div><div><br></div><div>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,0,128)">ListGraph</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">lg</span>;</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,0,128)">IdMap</span><<span style="color:rgb(128,0,128)">ListGraph</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">ListGraph</span>::<span style="color:rgb(128,0,128)">Node</span>><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">nodeMap</span>(<span style="color:rgb(9,46,100)">lg</span>);</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,128,0)">auto</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">inverseMap</span><span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">nodeMap</span>.<span style="color:rgb(0,103,124)">inverse</span>();</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,128,0)">for</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(128,128,0)">int</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">i</span>=<span style="color:rgb(0,0,128)">0</span>;<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">i</span><span style="color:rgb(192,192,192)"> </span><<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">3</span>;<span style="color:rgb(192,192,192)"> </span>++<span style="color:rgb(9,46,100)">i</span>)</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">lg</span>.<span style="color:rgb(0,103,124)">addNode</span>();</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,128,0)">for</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(128,128,0)">const</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">auto</span><span style="color:rgb(192,192,192)"> </span>&<span style="color:rgb(9,46,100)">edge</span><span style="color:rgb(192,192,192)"> </span>:<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">edges</span>)</pre> <pre style="margin:0px;text-indent:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">lg</span>.<span style="color:rgb(0,103,124)">addEdge</span>(<span style="color:rgb(9,46,100)">inverseMap</span>[<span style="color:rgb(0,103,124)">edge</span>.<span style="color:rgb(0,103,124)">first</span>],<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">inverseMap</span>[<span style="color:rgb(0,103,124)">edge</span>.<span style="color:rgb(0,103,124)">second</span>]);</pre> <pre style="margin:0px;text-indent:0px"><br></pre>Is this the best way to do it? Is there a simpler or faster way? Do I really need to call .addNode() as many times as the number of nodes? Is there a better way to look up nodes by ID? Should I be using RangeIdMap instead, or will that get messed up during the process of adding nodes and edges? With IdMap, will the node ID always be n-1 for the nth node added? What about RangeIdMap?</div><div><br></div><div>Thanks for any advice in advance.</div><div><br></div><div>Szabolcs</div><div><br></div></div>