35
LEDA A Library of Efficient Data-Types and Algorithms http://www.algorithmic-solutions.com/ enleda.htm Presentation by Amitai Armon

LEDA A Library of Efficient Data-Types and Algorithms Presentation by Amitai Armon

Embed Size (px)

Citation preview

Page 1: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

LEDAA Library of Efficient Data-Types

and Algorithms

http://www.algorithmic-solutions.com/enleda.htm

Presentation by Amitai Armon

Page 2: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Example: Planarity Testing

#include <LEDA/graph_alg.h>using namespace leda;int main(int argc, char * argv[]){ graph G; string filename(argv[1]); G.read(filename); cout << PLANAR(G) << endl;}

Page 3: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Topics

• What is LEDA?

• What does it include?

• LEDA data structures examples

• LEDA graphs

• Compiling with LEDA

• Where to find more info

Page 4: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

What is LEDA

• A C++ library of data-types and algorithms• Includes dozens of classes, developed over more

than 15 years (Naher & Mehlhorn)• Works cross-platform • Extensively tested• Efficient• Extensively documented (manual, guide, book)• Installed in more than 3000 sites.

Page 5: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A note about efficiency

• LEDA code is likely to be more efficient than the first implementation of most users

• It is also more efficient than STL in many cases• …but it is not magical:

– The right DS should be used (e.g. dynamic graph vs. static graph, array vs. dictionary)

– Robustness damages efficiency, so if efficiency is an issue, check if LEDA creates bottlenecks and should be replaced by platform/application optimized code.

Page 6: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

LEDA Contents

• Basic data structures: lists, queues, stacks, arrays, sets, etc.

• Advanced data structures: e.g. dictionary, partition, priority queues (with Fibonacci heaps), dynamic trees, and more.

• Graphs: Data-types, generators, iterators, and algorithms - including BFS, DFS, MST, Max-Flow, Min-Cost-Flow, Min-Cut, Matchings, Planarity-testing, Triangulation, Five-Colors,…

Page 7: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

LEDA Contents – continued

• Linear algebra & number theory: matrices, modular arithmetic, integers of arbitrary length, numeric functions…

• Lossless compression coders (Huffman, LZ…)• Geometric types & algorithms. (e.g. circle,

sphere, triangulation, convex hull…)• Graphics: windows, menus, graph-window• Simple data types and support functions: strings,

tuples, random-variables, I/O, etc.

Page 8: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

LEDA Extensions

12 packages, including:

• Curve reconstruction Algorithms

• K-cut (approximation)

• Minimum Mean cycle

• D-dimensional Geometry

• Dynamic Graph algorithms• And more

Page 9: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A DS Example: Dynamic Trees

#include < LEDA/dynamic_trees.h > dynamic_trees Dvertex D.make(void* x=nil)void D.link(vertex v, vertex w, double x, void* e_inf=nil)void D.update(vertex v, double x)vertex D.mincost(vertex v)double D.cut(vertex v)vertex D.lca(vertex v, vertex w)void* D.vertex_inf(vertex v), void* D.edge_inf(vertex(v)• O(log2n) expected amortized time per operation

Page 10: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A DS Example: Dynamic Trees

#include < LEDA/dynamic_trees.h > dynamic_trees Dvertex D.make(void* x=nil)void D.link(vertex v, vertex w, double x, void* e_inf=nil)void D.update(vertex v, double x)vertex D.mincost(vertex v)double D.cut(vertex v)vertex D.lca(vertex v, vertex w)void* D.vertex_inf(vertex v), void* D.edge_inf(vertex(v)• O(log2n) expected amortized time per operation

Page 11: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A DS Example: Dynamic Trees

#include < LEDA/dynamic_trees.h > dynamic_trees Dvertex D.make(void* x=nil)void D.link(vertex v, vertex w, double x, void* e_inf=nil)void D.update(vertex v, double x)vertex D.mincost(vertex v)double D.cut(vertex v)vertex D.lca(vertex v, vertex w)void* D.vertex_inf(vertex v), void* D.edge_inf(vertex(v)• O(log2n) expected amortized time per operation

Page 12: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A DS Example: Lists

• #include < LEDA/list.h > list <E> L – creates a list of items with information of type E.E.g.: list<int>, list<string>, list<edge>, etc.• Operations: push(E item), append, pop, reverse, size, sort,

unique, max, head, tail, succ, pred, print, forall(x, L), etc.

• List access returns a list_item object (‘pointer to element’).Its information can be found only in the list itself.For example:list_item lowest=L.min(); cout << L[lowest];

Page 13: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

A DS Example - contd.• For a proof of non-planarity we supply an empty

list of edges:list<edge> edge_list;if (PLANAR(G, edge_list)==0)

forall (x,edge_list) G.print_edge(x);

• List is implemented by a doubly linked list (use slist<E> for a singly-linked list).

• Templates and the items concept are used in most of LEDA’s DSs, including stack<E>, queue<E>, array<E>, array2<E>, set<E>, and others.

Page 14: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

raphs in LEDA

Page 15: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

The graph/ugraph Classes

• Represent directed/undirected graphs• Allow lots of graph operations:

Adding/removing nodes/edges, node/edge iteration and sorting, computing and iterating faces, and more.

• Persistent – can be read/written from/into a file• Has lots of implemented algorithms

Page 16: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Creating a new graph

• First option: start with an empty graph and update it

graph G; // Initializes an empty directed graph

ugraph G; // Initializes an empty undirected graph

node G.new_node(); // New node is returned

edge G.new_edge(node v, node w)

=> A lot of work!

Page 17: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Creating a new graph - easier

Use the generators for various graph types:• void random_graph(graph& G, int n, int m)• void random_graph(graph& G, int n, double p)• void random_simple_graph(graph& G, int n, int m)• void complete_graph(graph& G, int n)• random_bigraph – for a bipartite graph• random_planar_graph• And more

Page 18: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Creating a new graph – from file

• int G.read(string filename) (returns 0 if OK)

void G.read(istream& I = cin)

void G.write(ostream& O = cout)

void G.write(string filename)• File format is simple: textual description, with each

edge/node in a different line• Can also read another standard format, GML, with

read_gml, etc.

Page 19: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Nodes/Edges Iteration

• forall_nodes(v, G) (The nodes of G are successively assigned to v)• forall_edges(e, G) • forall_adj_nodes(v, w) • forall_adj_edges(e, w) • forall_out_edges(e, w) • forall_in_edges(e, w) • etc…

Page 20: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Node and Edge Data Structures

• Node/edge arrays:

The index to the array is a node/edge

Construction: node_array<T> A(graph G)

Access/assignment: T& A[node v]

• Similarly we have node/edge lists, sets, maps, priority queues. They are optimized for nodes/edges.

Page 21: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Introducing Weights• The class GRAPH<vtype, etype> includes information of the

specified types for each vertex and node.E.g.: GRAPH<int, int> G;

• Some useful functionality:node G.new_node(vtype x)edge G.new_edge(node v, node w, etype x)void G.assign(edge e, etype x)etype G.inf(edge e)edge_array<etype>& G.edge_data()

• A parameterized GRAPH may be used wherever a graph may be used.

• It can be read/written from/to a file with all the information.

Page 22: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Graph Algorithms

• Include: BFS, DFS, MST, Dijkstra, Bellman-Ford, Max-Flow, Min-Cost-Flow, Min-Cut, Matchings, Planarity-testing, Triangulation, Five-Colors,…

• Can be included all at once: <LEDA/graph_alg.h>• Examples:

– void DIJKSTRA_T(graph G, node s, edge_array<NT> cost, node_array<NT>& dist, node_array<edge>& pred)

– list<node> MIN_CUT(graph G, edge_array<int> weight)– list<node> BFS(graph G, node s, node_array<int>& dist,

node_array<edge>& pred)– list<edge> DFS_NUM(graph G, node_array<int>& dfsnum,

node_array<int>& compnum)

Page 23: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Graph Window

• A powerful interactive graphic interface for graph operations and editing.

• Can perform anything that can be done through a program, and has many customization options.

• Basic operations:– GraphWin gw(graph& G, const char* win_label="")– gw.display()– Gw.edit()

• More details in the LEDA website.

Page 24: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Graph Window - Screenshots

Page 25: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Semi-Dynamic Graphs

• The graph class allows dynamic graph changes, but in most applications the graph doesn’t change after construction.

• Semi-dynamic graphs are an alternative implementation of graphs, in which upper bounds on n and m may be supplied, in order to get better performance: graph G;

void G.init(int n, int m);• Requires compilation with -DGRAPH_REP=2

Page 26: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Static Graphs

• May not change at all after their construction.• Significantly more efficient. We use:

void G.start_construction(int n, int m)void G.finish_construction()

• Slots: more efficient ways for associating data with nodes/edges (compared to node/edge arrays).

• But: this is an experimental class with limited functionality compared to semi-dynamic graphs or ordinary graphs.

Page 27: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA

• We have LEDA version 4.4 for the following platforms:– Linux: Redhat 7.0 with g++ 2.96

– Linux: Redhat 8.0 with g++ 3.2

Both are installed in the TAU CS network

- Windows with Visual C++ 6.0

- Windows with Visual C++ 7.0 (.Net)

Both can be installed from CD on a PC

Page 28: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA: Libraries

The LEDA library is actually divided into 7 libraries:

• Most of LEDA is in libL

• Graphs and related data type are in libG

• The alternative semi-dynamic implementation is in libG2

• libP- Geometry in the plane

• libW – Graphics such as window and GraphWin

• libD3 – 3D geometry

• libGeoW – Visualizing sets of geometric objects

Page 29: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA: Includes

• Naturally, all the classes we use should be included (explicitly or by other includes).

• LEDA types are in namespace leda (prefix leda::), in order to prevent ambiguity.

• If ambiguity is not an issue – write:

using namespace leda;

and then you don’t have to add leda::

Page 30: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA: CS Linux

• Write the following line in your xterm:setenv LEDAROOT /usr/local/lib/LEDA-4.4-complete-i386-linux-redhat-7.0-g++-2.96/

Or, with Redhat 8.0:setenv LEDAROOT /usr/local/lib/LEDA-4.4-complete-i386-linux-redhat-8.0-g++-3.2/

- It is recommended to append the above line to your .login file.

Page 31: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA: CS Linux

Makefile example:

Is_planar : Is_planar.o

/usr/bin/g++ -Wall -O2 -L$(LEDAROOT) -o Is_planar Is_planar.o -lL -lG

Is_planar.o: Is_planar.c

/usr/bin/g++ -I$(LEDAROOT)/incl -O2 -c -o Is_planar.o Is_planar.c

• Graphwin requires –lW -lP –lG –lL –lX11 –L/usr/X11R6/lib/

Page 32: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Compiling with LEDA: Windows

• Install the library for the appropriate compiler version from the CD (after signing a non-distribution form).

• Open the supplied sample workspace.• Adjust the include and lib dirs in VS.• Add your LEDA directory to the PATH

environment variable. • Elaborate instructions are in the LEDA website.

Page 33: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Documentation

• The LEDA website includes a user manual, describing all the LEDA classes, and a user guide, with examples and tips.http://www.algorithmic-solutions.com/enleda.htm

• The LEDA book, by Mehlhorn and Naher, can be found at the library, and is also available on-line at the LEDA website.

Page 34: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

Conclusions

• We’ve seen an overview of LEDA• It’s a useful and easy-to-use tool• Let’s use it…

Page 35: LEDA A Library of Efficient Data-Types and Algorithms  Presentation by Amitai Armon

LEDA

Questions?