of 35
8/14/2019 Led A
1/35
8/14/2019 Led A
2/35
Example: Planarity Testing
#include
using namespace leda;
int main(int argc, char * argv[]){
graph G;
string filename(argv[1]);
G.read(filename);
cout
8/14/2019 Led A
3/35
Topics
What is LEDA?
What does it include?
LEDA data structures examples
LEDA graphs
Compiling with LEDA Where to find more info
8/14/2019 Led A
4/35
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.
8/14/2019 Led A
5/35
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.
8/14/2019 Led A
6/35
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,
8/14/2019 Led A
7/35
8/14/2019 Led A
8/35
LEDA Extensions
12 packages, including:
Curve reconstruction Algorithms
K-cut (approximation)
Minimum Mean cycle
D-dimensional Geometry
Dynamic Graph algorithms
And more
8/14/2019 Led A
9/35
A DS Example: Dynamic Trees
#include < LEDA/dynamic_trees.h >
dynamic_trees D
vertex 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
8/14/2019 Led A
10/35
A DS Example: Dynamic Trees
#include < LEDA/dynamic_trees.h >
dynamic_trees D
vertex 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
8/14/2019 Led A
11/35
A DS Example: Dynamic Trees
#include < LEDA/dynamic_trees.h >
dynamic_trees D
vertex 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
8/14/2019 Led A
12/35
A DS Example: Lists
#include < LEDA/list.h >
list L creates a list of items with information of type E.
E.g.: list, list, list, 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
8/14/2019 Led A
13/35
A DS Example - contd.
For a proof of non-planarity we supply an empty list ofedges:list 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 for asingly-linked list).
Templates and the items concept are used in most of LEDAsDSs, including stack, queue, array, array2,set, and others.
8/14/2019 Led A
14/35
raphs in LEDA
8/14/2019 Led A
15/35
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
8/14/2019 Led A
16/35
Creating a new graph
First option: start with an empty graph and update it
graph G; // Initializes an empty directed graphugraph 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!
8/14/2019 Led A
17/35
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
8/14/2019 Led A
18/35
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.
8/14/2019 Led A
19/35
Nodes/Edges Iteration
forall_nodes(v, G)
(The nodes ofG 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
8/14/2019 Led A
20/35
Node and Edge Data Structures
Node/edge arrays:
The index to the array is a node/edge
Construction: node_array 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.
8/14/2019 Led A
21/35
Introducing Weights
The class GRAPH includes information of the specifiedtypes for each vertex and node.
E.g.: GRAPH 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& 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.
8/14/2019 Led A
22/35
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: Examples:
void DIJKSTRA_T(graph G, node s, edge_array cost,node_array& dist, node_array& pred)
list MIN_CUT(graph G, edge_array weight)
list BFS(graph G, node s, node_array& dist,node_array& pred)
list DFS_NUM(graph G, node_array& dfsnum,node_array& compnum)
8/14/2019 Led A
23/35
Graph Window
A powerful interactive graphic interface for graphoperations and editing.
Can perform anything that can be done through aprogram, 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.
8/14/2019 Led A
24/35
Graph Window - Screenshots
8/14/2019 Led A
25/35
Semi-Dynamic Graphs
The graph class allows dynamic graph changes, but inmost applications the graph doesnt change afterconstruction.
Semi-dynamic graphs are an alternativeimplementation of graphs, in which upper bounds onn 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
8/14/2019 Led A
26/35
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 withnodes/edges (compared to node/edge arrays).
But: this is an experimental class with limitedfunctionality compared to semi-dynamic graphs orordinary graphs.
8/14/2019 Led A
27/35
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
8/14/2019 Led A
28/35
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
8/14/2019 Led A
29/35
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 dont have to add leda::
8/14/2019 Led A
30/35
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 .loginfile.
8/14/2019 Led A
31/35
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/
8/14/2019 Led A
32/35
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.
8/14/2019 Led A
33/35
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.
8/14/2019 Led A
34/35
Conclusions
Weve seen an overview of LEDA
Its a useful and easy-to-use tool
Lets use it
8/14/2019 Led A
35/35
LEDA
Questions?