Title: Programming with LEDA
1- Programming with LEDA
- Combinatorial and Graph Algorithms
- Lecture by Leong Hon Wai
- (Adapted from Notes by David Ong Tat Wee)
2Introduction to LEDA
- Info about LEDA
- Why LEDA
- Overview of LEDA
- LEDA Environmental Settings
- Compiling A LEDA Program
- LEDA Issues
3Overview of LEDA
- LEDA Library of Efficient Data types and
Algorithms - St. Naher and Kurt Mehlhorn (more than 10 years
history) - Max-Planck-Institut für Informatik, Germany
- LEDA Contains
- Basic Data Structures (stuff in STL)
- Advanced Data Structures
- Graph Algorithms (Efficiency)
- Written by Knowledgable People
- concise and abstract specification of data types
- efficient implementation for performance
- Rapid Prototyping and Fast Implementation
- LEDA Url
- http//www.mpi-sb.mpg.de/LEDA/leda.html
4Caveats and Information
- Caveat I am not a LEDA Expert
- Lecture Notes adapted from
- David Ong Tat Wees presentation (Thanks!)
- References
- Web-site http//www.mpi-sb.mpg.de/LEDA/
- The LEDA User Manual (Version R3.4.1)
- The LEDA Book (written by Kurt Mehlhorn and St.
Naher) - http//www.mpi-sb.mpg.de/mehlhorn/LEDAbook.html
- Other Sites that Uses LEDA
- Many -- just follow links in LEDA Web-site
5First LEDA Program
- include ltLEDA/d_array.hgt
- main()
-
- d_arrayltstring,intgt N(0)
- string s
- while (cin gtgt s) Ns
- forall_defined(s,N)
- cout ltlt s ltlt " " ltlt Ns ltlt endl
-
- Input
- This LEDA program does a concordance table. This
is simple! - Output
- This 2 does 1 table 1
- LEDA 1 a 1 is 1
- program 1 concordance 1 simple 1
6Explanation of First LEDA Program
- include ltLEDA/d_array.hgt
- main()
-
- d_arrayltstring,intgt N(0)
- string s
- while (cin gtgt s) Ns
- forall_defined(s,N)
- cout ltlt s ltlt " " ltlt Ns ltlt endl
-
- Uses LEDA dictionary array Class d_array
- Builds up a concordance table with it
- ltword_1, occurrence_1gt,,ltword_n, occurrence_ngt
- Uses iterator construct
- forall_defined
7LEDA -- Compiling Environment Settings
- LEDA C Header Files
- /home/proj/leda/4.1/include (Required during
compilation.) - LEDA Runtime Library Files
- /home/proj/leda/4.1/lib xor /home/proj/leda/4.1/l
ib/CC - Environment Settings
- Add in .profile
- LD_LIBRARY_PATHLD_LIBRARY_PATH/home/proj/leda/
4.1/lib - Path required during both compilation (see
makefile) and runtime. - Due to name conflicts of lib files, cannot
include both lib paths. - Compiling a LEDA Program (say leda-test.cpp)
- g -lL -lm leda-test.cpp ...-lW -lP -lG
- If required and not done
- -I/home/proj/leda/4.1/include
-L/home/proj/leda/4.1/lib
8A Sample LEDA Graph Program
- Sample Graph-MST program
- Generate a random graph, print it
- Find its MST and print it out
edge_arrayltintgt E(G) listltedgegt L //
compute MST with Krusal's algo L
(MIN_SPANNING_TREE(G, E)) cout ltlt "MST" ltlt
endl forall(e, L)
G.print_edge(e) cout ltlt endl
main() GRAPHltint, intgt G edge e
random_graph(G, 6, 15) forall_edges(e, G)
G.assign(e, (rand() 10) 1)
G.make_undirected() cout ltlt "Graph" ltlt
endl G.print()
9Sample Output of Graph-MST Program
- Output of Graph-MST Program
- Note Format of edge (u,v) with weight w is
u(w)v - Graph
- 0(0) 0(9)12(6)02(8)03
(7)0 4(10)05(6)0 - 1(0) 1(9)51(4)30(9)1
- 2(0) 2(6)02(2)52(8)02
(1)5 4(5)2 - 3(0) 3(10)53(3)43(7)0
1(4)3 4(8)35(1)3 - 4(0) 4(10)04(8)34(5)2
3(3)4 - 5(0) 5(1)35(6)01(9)52
(2)5 2(1)53(10)5 - MST
- 4(8)3
- 3(7)0
- 4(5)2
- 5(1)3
- 1(9)5
10LEDA Issues
- Specifications
- Parameterized (Generic) Data Type
- Linearly Ordered Type
- Iteration Macros
- Algorithms
11LEDA Specification (Stack as example)
- Specification of a Stack Data Type
12Specification (d_array)
- Definition
- statement of the definition of d_array (see next
slide) - Creation
- d_arrayltI,Egt A
- d_arrayltI,Egt A(E x) creates an injective
function a from I to the set of unused variable
of type E, sets xdef to x and dom(A) to the
empty set, and initializes A with a - Operation
- E AI i returns the variable A(i)
- bool A.defined(I i) returns true if i in
dom(A) - void A.undefined(I i) removes i from dom(A)
make i undefined - Iteration
- forall_defined(i, A) the elements from dom(A)
are successfully assigned to i - forall(x, A) for all i in dom(A) the entries
Ai are sucessfully assigned to x - Implementation
- impl. by randomized search tree
- Access in O(log dom(A)) O(dom(A)) space
13Definition Creation (d_array)
- Definition
- An instance A of d_arrayltI, Egt (dictionary
array) is an injective mapping - from the linearly ordered type I (index type of
A) - to the set of variables of data type E (element
type of A) - Use A(i) to denote the variable with index i
- dom(A) -- denote the set of used indices
- empty at time of creation modified by array
accesses - A(i) has default value xdef for all i not-in
dom(A) - Creation -- (also Constructors)
- d_arrayltI,Egt A creates an injective function
a from I to the set of unused variable of type
E, sets xdef to the default value of type E (if
E has no default value then xdef
stays undefined) and dom(A) to the empty set,
and initializes A with a - d_arrayltI,Egt A(E x) // gives mapping A I
--gt E creates an injective function a from I
to the set of unused variable of type E, sets
xdef to x and dom(A) to the empty set, and
initializes A with a
14Parameterized Data Type
- Also all LEDA classes are parameterized data
types - Example d_arrayltsometype, sometypegt
- Any built-in or LEDA type can be used. eg int,
char, pointer, node, graph, - For user-defined class (T) to be used, must
define - TT() //constructor
- TT(const T) //copy constructor
- T Toperator(const T) //assignment op
- (Only if required)
- istream operatorgtgt(istream, T)
- ostream operatorltlt(ostream, const T)
- int compare(const T, const T)
- int Hash(const T)
15Operations
- Each operation description has 2 parts
- (a) Interface of the operation
- list_item L.insert (E x, list_item it, int
dirafter) - (See Section 4.7) arguments are element x of type
E, a list_item it, and an optional relative
position argument dir - (b) Effect of the operation
- A new item with content x is inserted after (if
dirafter) or before (if dirbefore) item it into
L. The new item is returned. - Precondition item it must be in L.
- Eg Operations on object A of type d_array
- E AI i returns the variable A(i)
- bool A.defined (I i) returns true if i in
dom(A) - void A.undefined(I i) removes i from dom(A)
make i undefined
16Implementation Parameters
- Default impl. for all advanced data structures
- provided by LEDA
- usually fast for most general cases.
- But, LEDA also allows users to change impl.
- Dictionary, priority queue, d_array, sortseq
- Done by adding one more data structure
parameter - Why?
- User has specific and special needs
- Use wants to do research with these data
structures - Example
- d_array vs _d_array
17Implementation Parameter -- example
d_array implemented with skiplist include
ltLEDA/_d_array.hgt include ltLEDA/impl/skiplist.hgt
main() _d_arrayltstring,int,skiplistgt N(0)
string s while (cin gtgt s) Ns
forall_defined(s,N) cout ltlt s ltlt " "
ltlt Ns ltlt endl
- LEDA Default d_array
- include ltLEDA/d_array.hgt
- main()
-
- d_arrayltstring,intgt N(0)
- string s
- while (cin gtgt s) Ns
- forall_defined(s,N)
- cout ltlt s ltlt " "
- ltlt Ns ltlt endl
18Arguments, Parameter Passing
- Trailing arguments may be optional
- if missing, default value given in specification
will be used - by-value argument passing
- type x in arg list
- S.push (E x) // adds x as new top element //
to S x is unchanged - by-reference argument passing
- used to change value of arg, for large
argument objects - type x in arg list
- L1.conc(slistltEgt L2) // appends list L2
to // list L1 and makes L2 the empty list.
19Linearly Ordered Type
- Linear order required for some types (eg. sorted
sequences, sets) - When type T with no default ordering, user must
define - int compare(const T, const T)
class A public A(int i) mID i
A() int id() return mID private
int mID
Want to declare setltAgt ASet Must provide
function int compare(A x, A y) return
compare(x.id(), y.id())
20Items in Advanced Data Types
- Most advanced ADT (eg dictionary, priority
queue, graphs) - a collection of items -- which are container
used to hold - user-defined objects of interest
- plus other things such as pointers and so on.
- Example
- listltedgegt MyEdgeList // defines a list of
edges called MyEdgeList - However, the items of the list are of type
list_item (and not edge) - Of course, the key information in list_item is an
edge - Similarity with records in Pascal or structs in
C/C
21Working with Items...
- Careful when updating data types with items
- list_item L.insert (E x, list_item here, int
dir after) - This operation will insert element x into the
list L after the list_item here - Example of a call will be
- newitem MyEdgeList (shortedge, current, after)
- will create a new list_item called newitem with
the new element shortedge, and insert it after
the list_item current. - The newly created list_item will be returned as
newitem.
22Algorithms
- Provides a number of standard algorithms
- Graph algorithms
- TOPSORT, DFS, BFS
- DIJKSTRA, BELLMAN_FORD
- MIN_COST_MAX_FLOW
- MAX_WEIGHT_BIPARTITE_MATCHING
- Plane algorithms
- DELAUNAY_TRIANG
- CONVEX_HULL
- VORONOI
23Algorithms
- Provides a number of standard algorithms
- Graph algorithms
- TOPSORT, DFS, BFS
- DIJKSTRA, BELLMAN_FORD
- MIN_COST_MAX_FLOW
- MAX_WEIGHT_BIPARTITE_MATCHING
- Plane algorithms
- DELAUNAY_TRIANG
- CONVEX_HULL
- VORONOI
24DIJKSTRA
Dijkstras Algorithm include ltLEDA/graph.hgt inc
lude ltLEDA/node_pq.hgt void DIJKSTRA (graph G,
node s, edge_arrayltintgt cost,
node_arrayltintgt dist,
node_arrayltintgt pred ) node_pqltintgt PQ(G)
int c node u, v edge e
forall_nodes(v,G) predv 0
distv infinity PQ.insert(v, distv)
dists 0 PQ.decrease_inf(s, 0)
while ( ! PQ.empty()) u PQ.del_min()
forall_adj_edges(e,u) v
G.target(e) c distu coste
if (c lt distv) distvc
predvu PQ.decrease_inf(v,c)
// if // forall)adj_edges //
while