31.01.2014 Views

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4. Implementation<br />

Constant Overhead.<br />

5. Example<br />

We count the number of ’red nodes’ in a parameterized graph G.<br />

int count_red(graph G, node_array COL) {<br />

node_array_da Color(COL);<br />

int counter=0;<br />

NodeIt it(G);<br />

while (it.valid()) {<br />

if (get(Color,it)==red) counter++;<br />

it++; }<br />

return counter;<br />

}<br />

Suppose we want to make this ’algorithm’ flexible in the representation of colors. <strong>The</strong>n<br />

we could write this version:<br />

template<br />

int count_red_t(graph G, DA Color) {<br />

int counter=0;<br />

NodeIt it(G);<br />

while (it.valid()) {<br />

if (get(Color,it)==red) counter++;<br />

it++; }<br />

return counter;<br />

}<br />

With the templatized version it is easily to customize it to match the interface of the<br />

version:<br />

int count_red(graph G, node_array COL) {<br />

node_array_da Color(COL);<br />

return count_red_t(G,Color); }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!