SDSU CS 660: Combinatorial Algorithms
Leda Examples

[To Lecture Notes Index]
San Diego State University -- This page last updated October 21, 1995
----------

Contents of Leda Examples Lecture

  1. Starting out
    1. On Saturn
  2. Leda Data Types
  3. Simple C++ IO
    1. Leda Integers
    2. Dictionaries

Starting out


On Saturn

Compiling on Saturn with g++

g++ -O -I/usr/local/src/public_domain/LEDA-3.1.2/incl -c hello.c
g++ -O -o hello hello.o /usr/local/src/public_domain/LEDA-3.1.2/libL.a -lm
Sample Makefile
CFLAGS = -O

LIB     = /usr/local/src/public_domain/LEDA-3.1.2/libL.a -lm

INCL    = /usr/local/src/public_domain/LEDA-3.1.2/incl

CC      = g++

.c.o:
	$(CC) $(CFLAGS) -I$(INCL) -c $*.c

.o:
	$(CC) $(CFLAGS) -o $* $*.o $(LIB)

.c:
	$(CC) $(CFLAGS) -I$(INCL) -c $*.c
	$(CC) $(CFLAGS) -o $* $*.o $(LIB)

clean:
	rm *.o

More examples are Found in:

/usr/local/src/public_domain/LEDA-3.1.2/prog

Leda Data Types


String
Random Sources
Vector
Matrix
Large Integers
Rational Numbers
Real Numbers
One Dimensional Arrays
Two Dimensional Arrays
Stacks
Queues
Linear Lists
Singly Linked Lists
Dictionaries
Sorted Sequences
Priority Queues
Graphs
Points
Rational Points
Lines
Polygons
Circles

String Example

#include <LEDA/basic.h>
#include <LEDA/stream.h>

main() {
	string Part1 = "Hello ";
	string Part2 = "World\n";
	string Message = Part1 + Part2;
	cout << Message;

}
Output

Hello World


#include <LEDA/basic.h>
#include <LEDA/stream.h>

main() {
string Part1 = read_string("Input a string: " );
string Part2 = read_string("Input a second string: " );
string Message = Part1 + Part2;
cout << Message;

}
Output
Input a string: Is that
Input a second string: you
Is thatyou

Simple C++ IO

#include <iostream.h>

int main() 
{
	int accessCode;
	float transactionAmount;
	char userName[50];
	
	cout << "Please enter:  
			your name, access code and amount\n";

	cin >> userName>> accessCode>> transactionAmount;
	
	cout 	<< "Your input: ";
	cout 	<< userName 
			<< accessCode  
			<< transactionAmount
			<< endl ;
	return 0;
}

cin
Removes all white space
Accepts multiple input values in order - left to right
Returns false when reaches end-of-file condition

endl
add "\n" and flush output buffer

More IO

Predefined Streams
cin
standard input
cout
standard output (buffered)
cerr
standard error
clog
standard error, but buffered


Leda Streams
#include <LEDA/basic.h>
#include <LEDA/stream.h>

main()
{
	string FancyFormated = string( "[%2d] ", 8 );

	file_istream InputFile( read_string("Read file: " ) );

	string BadText = read_string( "Replace string: " );
	string CorrectText = read_string( "by      string : " );
	string Remove = read_char  ( "Delete  char   :" );

	int LineNumber=0;

	while (InputFile ) { 
		string LineOfText;

		LineOfText.read_line( InputFile );
		LineOfText = LineOfText.replace_all( BadText , CorrectText );
		LineOfText = LineOfText.del_all( Remove );

		cout << string( "[%2d] ", LineNumber++ ) 
			<< LineOfText;
		newline;
	}
	newline;

	return 0;
}

Leda Integers

#include <LEDA/basic.h>
#include <LEDA/integer.h>

integer factorial(integer n)   {
	integer SubProduct = 1;

	for ( integer index = 1 ;  index <= n;  index++ )  {
		SubProduct = SubProduct * index;
	}

	return SubProduct;
}

main() {
	integer n = read_int( "compute factorial of:  " );

	float StartTime = used_time();
	integer Result  = factorial( n );
	float TimeUsed = used_time() - StartTime;

	cout << string( "%6.3f sec",TimeUsed  ) << endl;

	cout  << "factorial of " << n << " is: " <<  Result  << endl;
	return 0;
}

Output

compute factorial of: 100
0.000 sec
factorial of 100 is:
933262154439441526816992388562667004907159682643816214685929638952175999
932299150894146397615651828625369792082722375825118521091686400000000000
0000000000000

Leda Arrays and Random Numbers
#include <LEDA/array.h>
#include <LEDA/stream.h>

main () {
	int NormalIntArray[ 10 ];
	array< int > 	IntArray( 1, 10 );           //Index from 1 to 10
	array< string >	StringArray( -2, 25 );

	StringArray[ 3 ] = "Hi Mom";

	random_source	Numbers( 1, 13 ) ;

	for ( int Index = 1 ; Index <= 10 ; Index++ )
		IntArray[ Index ] = Numbers();

	IntArray.print( " Orginal Array " );
	newline;

	IntArray.sort();

	IntArray.print( " Sorted Array " );		newline;
};
Output
saturn 15-> a.out
 Orginal Array  5   8   10   11   5   4   4   12   1   9
 Sorted Array  1   4   4   5   5   8   9   10   11   12

saturn 16-> a.out
 Orginal Array  12   6   7   3   8   11   2   2   7   8
 Sorted Array  2   2   3   6   7   7   8   8   11   12

More Arrays
#include <LEDA/array.h>
#include <LEDA/stream.h>

main () {
	int	Size = read_int( "Size of your array: " );
	array< int > 	IntArray( 1, Size ); 

	for ( int Index = 1 ; Index <= Size ; Index++ )
		IntArray[ Index ] = Index;

	IntArray.print( "Orginal Array " );
	newline;

	if (IntArray.binary_search(3) == 0 )
		cout << "Found 3 " << endl;

	IntArray.permute();
	IntArray.print( "Mixed up Array " );	
	newline;

	IntArray.read( "Please type " + string( Size ) + " integers ");
	IntArray.print( "New elements Array " );	
	newline;
};
Output

Size of your array: 5
Orginal Array 1 2 3 4 5
Mixed up Array 5 2 3 4 1
Please type integers 2 8 3 9 1
New elements Array 2 8 3 9 1
Dictionary Arrays
#include <LEDA/d_array.h>

main() {
	d_array < string,  int >  StringIndexedArray( 0 ) ;

	StringIndexedArray[ "Sam" ] = 5;
	StringIndexedArray[ "Peter" ] = 3;
	StringIndexedArray[ "My Good Man" ] = 13;

	d_array < string, string > StringStringArray ( 2 ) ;

	StringStringArray[ "Mary" ] = "English";
	StringStringArray[ "Chen" ] = " Chineese ";
	StringStringArray[ "Xaichen" ] = " Romainian ";

	string Index;
	forall_defined(Index, StringIndexedArray ) 
		cout << Index << "\t" 
			<< StringIndexedArray [ Index ]  << endl;

	newline;
	forall_defined( Index, StringStringArray ) 
		cout << Index << "\t" 
			<< StringStringArray [ Index ]  << endl;

}
Output
My Good Man     13
Peter   3
Sam     5

Chen     Chineese
Mary    English
Xaichen  Romainian
More Dictionary Arrays
#include <LEDA/d_array.h>

main() {
	d_array< string, int > StringCount( 0 );
	string InputString;

	cout << "Input strings seperated by white space."
		<< " End with control D" << endl;

	while ( cin >> InputString ) 
		StringCount[ InputString ] ++;

	cout << "String followed by (count)" << endl;

	string Index;
	forall_defined( Index, StringCount ) 
		cout << Index << "\t" 
			<< "(" 
			<< StringCount[ Index ] 
			<< ")" << endl;
}
Output
Input strings seperated by white space. End with control D
a b a a b aa bb a b cat dog b cat mat
String followed by (count)
a (4)
aa (1)
b (4)
bb (1)
cat (2)
dog (1)
mat (1)

Dictionaries

#include <LEDA/dictionary.h>

main()
{
	dictionary< string, int >  StudentGrades;

	StudentGrades.insert( "Sam", 1 );
	StudentGrades.insert( "Chen", 4 );
	StudentGrades.insert( "Roger", 1 );
	StudentGrades.insert( "Chen", 3 );
	
	cout << StudentGrades.access( "Chen" ) << endl;

	StudentGrades.del( "Roger" );

	dic_item  StudentRecord;
	forall_items( StudentRecord, StudentGrades ) 
		cout << StudentGrades.key( StudentRecord ) 
			<< " : " 
			<< StudentGrades.inf( StudentRecord )  
			<< "\n";

	cout << StudentGrades.access( "Sue" ); //Core dump don't do this

	StudentRecord = StudentGrades.lookup( "Sue" );  // Do this
	if ( StudentRecord  !=nil ) 
		cout << StudentGrades.access( "Sue" );
	
	return 0;
}

Counting Strings
#include <LEDA/dictionary.h>

main()
{
	dictionary< string, int > CountTable;

	dic_item  Count;
	string InputString;

	cout << "Type strings, seperated by white space. End with eof" 
		<< endl;

	while ( cin >> InputString )  {
		Count = CountTable.lookup( InputString );

		if ( Count==nil ) 
			CountTable.insert( InputString, 1 );
		else 
			CountTable.change_inf( Count, CountTable.inf( Count ) + 1);
	 }

	forall_items( Count, CountTable) 
		cout <<	CountTable.key( Count ) << " : " 
			<< CountTable.inf( Count )  << "\n";

	return 0;
}

Dictionaries with Implementation
#include <LEDA/_dictionary.h>
#include <LEDA/impl/bb_tree.h>

main()
{
	_dictionary< string, int, bb_tree > CountTable;

	dic_item  Count;
	string InputString;

	cout << "Type strings, seperated by white space. End with eof" 
		<< endl;

	while ( cin >> InputString )  {
		Count = CountTable.lookup( InputString );

		if ( Count==nil ) 
			CountTable.insert( InputString, 1 );
		else 
			CountTable.change_inf( Count, CountTable.inf( Count ) + 1 );
	 }

	forall_items( Count, CountTable) 
		cout <<	CountTable.key( Count ) << " : " 
			<< CountTable.inf( Count )  << "\n";

	return 0;
}

Structures Available
Tree Like
ab_tree.h	a-b tree
avl_tree.h	AVL tree
bb_tree.h	BB[alpha]
bin_tree.h	binary search tree
pers_tree.h	persistant tree
rb_tree.h	red-black tree
rs_tree.h	random search tree
skiplist.h	skip lists

For Geometric Algorithms
delaunay_tree.h	delaunay tree
iv_tree.h	interval tree
ps_tree.h	priority search tree
range_tree.h	range tree
seg_tree.h	segment tree

Priority Queues
bin_heap.h	binary heap
eb_tree.h	Emde-Boas tree
f_heap.h	Fibonnacci heap
k_heap.h	k-nary heap
m_heap.h	monotonic heap
p_heap.h	pairing heap

Timing Test The Wrong Way
#include <LEDA/basic.h>

main()
{
	int Size = read_int( "Size: " );
	float crap;
	float aFloat = 12.345;
	int inner, outer;

	float StartTime = used_time();
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = 0.0;
	float LoopTime = used_time( StartTime );

	float StartCompute = used_time( LoopTime );	// Bad News
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = aFloat * outer / inner ;
	float LoopAndComputeTime = used_time( StartCompute );

	cout << StartTime << "\t" << LoopTime  << endl;
	cout << StartCompute << "\t" << LoopAndComputeTime 
		<< endl;
}

Timing Test - OK
#include <LEDA/basic.h>

main()
{
	int Size = read_int( "Size: ");
	float crap;
	float aFloat = 12.345;
	int inner, outer;

	float StartTime = used_time();
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = 0.0;

	float LoopTime = used_time( StartTime );

	for ( outer = 0; outer < Size; outer ++) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = aFloat * outer / inner ;

	float LoopAndComputeTime = used_time( StartTime );

	cout << LoopTime  << endl;
	cout << LoopAndComputeTime << endl;
}

Timing Test - OK, More Direct
#include <LEDA/basic.h>

main()
{
	int Size = read_int( "Size: " );
	float crap;
	float aFloat = 12.345;
	int inner, outer;

	float StartTime = used_time();
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = 0.0;
	float LoopTime = used_time( ) - StartTime;

	float StartCompute = used_time( );	
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = aFloat * outer / inner;
	float LoopAndComputeTime = used_time(  ) - StartCompute;

	cout << LoopTime  << endl;
	cout << LoopAndComputeTime << endl;
}
Output

Size: 10000
7.5
7.53333 // ??

#include <LEDA/basic.h>
#include <LEDA/integer.h>

main()
{
	int Size = read_int( "Size: " );
	integer crap = 0;
	int inner, outer;

	float StartTime = used_time();
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = 0;
	float LoopTime = used_time( ) - StartTime;

	float StartCompute = used_time( );	
	for ( outer = 0; outer < Size; outer ++ ) 
		for ( inner = 0; inner < Size; inner ++ )
			crap = crap * outer + inner;
	float LoopAndComputeTime = used_time(  ) - StartCompute;

	cout << LoopTime  << endl;
	cout << LoopAndComputeTime << endl;
}
Size	LoopTime	LoopAndComputeTime 
10	0	0
20	0	0.0166667
40	0	0.266667
80	0.0166667	4.53333


Timing Tree Access

#include <LEDA/basic.h>
#include <LEDA/list.h>
#include <LEDA/_dictionary.h>
#include <LEDA/impl/bb_tree.h>

#define KeyType int
#define DataType int


float AverageAccessTime( dictionary<KeyType, DataType>& table,
						 list< KeyType >& accessList) {
	KeyType key;
	KeyType ForLoopTimeOnly;

	float StartTime = used_time();
	forall( key, accessList) { 
		ForLoopTimeOnly = key;
	};
	float LoopTime = used_time( StartTime );

	forall( key, accessList) { 
		table.access( key ); 
	};

	float LoopAndAccessTime = used_time( StartTime );

	return LoopAndAccessTime - LoopTime ;
}


Timing Tree Access Part 2

float AverageAccessTime( dictionary<KeyType, DataType>& table ) {
	list< KeyType > tableKeys;
	dic_item	tableRecord;

	forall_items( tableRecord, table )
		tableKeys.push( table.key( tableRecord ) );
	return AverageAccessTime( table, tableKeys );
}


Timing Tree Access Main and Results

main()
{
	_dictionary< KeyType, DataType, bb_tree > WeightBalancedTree;
	_dictionary< KeyType, DataType, bin_tree > BinaryTree;

	int Size = 0;
	while (cin ) {

		Size = read_int( "Tree Size ");
		for ( int k = 0; k < Size; k++ ) {
			WeightBalancedTree.insert( k , k );
			BinaryTree.insert( k , k );
		};

		cout <<Size << "\t"
			<< AverageAccessTime( BinaryTree )<< "\t"
			<<AverageAccessTime(WeightBalancedTree)
			<< endl;;
	};
}


Tree Size	Binarytree	WeightBalancedTree
1000	0.3	0.0166667
2000	1.66667	0.0333333
4000	8.58333	0.0333328
8000	37.7667	0.100006
16000	154.567	0.199982

Where are the Trees?
#include <LEDA/impl/bin_tree.h>
#include <LEDA/impl/bb_tree.h>

main()
{
	bin_tree* BinaryTreePtr = new bin_tree;
	bin_tree BinaryTree;
	bin_tree* WeightBalancedTreePtr = new bb_tree;
	bb_tree WeightBalancedTree;

	int key;
	int Size = 5;

	for ( key=0; key < Size; key++ ) {
		BinaryTreePtr ->insert( (void*)key ,0 );
		WeightBalancedTreePtr ->insert( (void*)key ,0 );
		BinaryTree.insert( (void*)key ,0 );
		WeightBalancedTree.insert( (void*)key ,0 );
	}

	// Following is not useful
	BinaryTreePtr ->print();
	BinaryTree.print();
	WeightBalancedTreePtr ->print();
	WeightBalancedTree.print();

	return 0;
}
Hash Tables - Counting Strings
#include <LEDA/dictionary.h>
#include <LEDA/impl/ch_hash.h>

inline int Hash(const string& s)  { 
	return ( s.length() > 0 ) ? s[0] : 0; 
}

main()
{
	_dictionary<string, int, hash> HashTable;

	dic_item it;
	string s;

	while ( cin >> s ){ 
		it = HashTable.lookup( s ) ;
		if ( it==nil ) 
			HashTable.insert( s, 1 );
		else 
			HashTable.change_inf( it, HashTable.inf( it ) + 1 );
	}

	forall_items( it, HashTable )
		cout <<  HashTable.key( it ) 
			<< " : " 
			<< HashTable.inf( it ) 
			<< "\n";

	return 0;
}

----------