Title: LEDA
1LEDA
- A Library of Efficient Data-Types and Algorithms
http//www.algorithmic-solutions.com/enleda.htm
Presentation by Amitai Armon
2Example Planarity Testing
- include ltLEDA/graph_alg.hgt
- using namespace leda
- int main(int argc, char argv)
-
- graph G
- string filename(argv1)
- G.read(filename)
- cout ltlt PLANAR(G) ltlt endl
3Topics
- What is LEDA?
- What does it include?
- LEDA data structures examples
- LEDA graphs
- Compiling with LEDA
- Where to find more info
4What 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.
5A 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.
6LEDA 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,
7LEDA 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.
8LEDA Extensions
- 12 packages, including
- Curve reconstruction Algorithms
- K-cut (approximation)
- Minimum Mean cycle
- D-dimensional Geometry
- Dynamic Graph algorithms
- And more
9A DS Example Dynamic Trees
- include lt LEDA/dynamic_trees.h gt
- dynamic_trees D
- vertex D.make(void xnil)
- void D.link(vertex v, vertex w, double x, void
e_infnil) - 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
10A DS Example Dynamic Trees
- include lt LEDA/dynamic_trees.h gt
- dynamic_trees D
- vertex D.make(void xnil)
- void D.link(vertex v, vertex w, double x, void
e_infnil) - 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
11A DS Example Dynamic Trees
- include lt LEDA/dynamic_trees.h gt
- dynamic_trees D
- vertex D.make(void xnil)
- void D.link(vertex v, vertex w, double x, void
e_infnil) - 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
12A DS Example Lists
- include lt LEDA/list.h gt
- list ltEgt L creates a list of items with
information of type E. - E.g. listltintgt, listltstringgt, listltedgegt, 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 lowestL.min()
- cout ltlt Llowest
13A DS Example - contd.
- For a proof of non-planarity we supply an empty
list of edges - listltedgegt 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
slistltEgt for a singly-linked list). - Templates and the items concept are used in most
of LEDAs DSs, including stackltEgt, queueltEgt,
arrayltEgt, array2ltEgt, setltEgt, and others.
14raphs in LEDA
15The 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
16Creating 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)
- gt A lot of work!
17Creating 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
18Creating 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.
19Nodes/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
20Node and Edge Data Structures
- Node/edge arrays
- The index to the array is a node/edge
- Construction node_arrayltTgt A(graph G)
- Access/assignment T Anode v
- Similarly we have node/edge lists, sets, maps,
priority queues. They are optimized for
nodes/edges.
21Introducing Weights
- The class GRAPHltvtype, etypegt includes
information of the specified types for each
vertex and node. - E.g. GRAPHltint, intgt 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_arrayltetypegt 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.
22Graph 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 ltLEDA/graph_alg.hgt
- Examples
- void DIJKSTRA_T(graph G, node s, edge_arrayltNTgt
cost, node_arrayltNTgt dist, node_arrayltedgegt
pred) - listltnodegt MIN_CUT(graph G, edge_arrayltintgt
weight) - listltnodegt BFS(graph G, node s, node_arrayltintgt
dist, node_arrayltedgegt pred) - listltedgegt DFS_NUM(graph G, node_arrayltintgt
dfsnum, node_arrayltintgt compnum)
23Graph 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.
24Graph Window - Screenshots
25Semi-Dynamic Graphs
- The graph class allows dynamic graph changes, but
in most applications the graph doesnt 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_REP2
26Static 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.
27Compiling 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
28Compiling 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
29Compiling 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 dont have to add leda
30Compiling with LEDA CS Linux
- Write the following line in your xterm
- setenv LEDAROOT /usr/local/lib/LEDA-4.4-complete-i
386-linux-redhat-7.0-g-2.96/ - Or, with Redhat 8.0
- setenv LEDAROOT /usr/local/lib/LEDA-4.4-complete-i
386-linux-redhat-8.0-g-3.2/ - - It is recommended to append the above line to
your .login file.
31Compiling 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/
32Compiling 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.
33Documentation
- 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.
34Conclusions
- Weve seen an overview of LEDA
- Its a useful and easy-to-use tool
- Lets use it
35LEDA