Louis-Nol Pouchet
pouchet@cse.ohio-state.edu
September 2010
888.11
Generalities on Algorithms:
Writing Algorithms
Algorithms
OSU
Generalities on Algorithms:
Writing Algorithms
Algorithms are...
OSU
...
Writing Algorithms
Generalities on Algorithms:
A Few Rules
OSU
Writing Algorithms
Generalities on Algorithms:
A Syntax Proposal
I
OSU
break
continue
Generalities on Algorithms:
Writing Algorithms
An example
Algorithm
algorithm gcd
input: integer a, b
output: greatest common divisor of a and b
if a = 0 then
return b
while b 6= 0 do
if a > b then
a = ab
else
b = ba
return a
OSU
Generalities on Algorithms:
Writing Algorithms
Another example
Algorithm
BuildSearchSpace: Compute T
Input:
pdg: polyhedral dependence graph
Output:
T : the bounded space of candidate multidimensional schedules
d 1
while pdg 6= 0/ do
Td createPolytope([1, 1],[1, 1])
for each dependence DR,S pdg do
WDR,S buildWeakLegalSchedules(DR,S )
Td Td WDR,S
end for
for each dependence DR,S pdg do
SDR,S buildStrongLegalSchedules(DR,S )
if
Td SDR,S 6= 0/ then
Td Td SDR,S
pdg pdg - DR,S
end if
end for
end do
OSU
Writing Algorithms
Generalities on Algorithms:
Recursive Algorithms
OSU
Writing Algorithms
Data Structures:
Vectors
OSU
Generic container with random access capability via the index of the
element
I Example: A[i], A[i][j][function(i,j)]
Writing Algorithms
Data Structures:
Stack: LIFO
I
I
I
Queue: FIFO
I
I
I
OSU
10
Writing Algorithms
Data Structures:
Graphs
OSU
11
Writing Algorithms
Data Structures:
Trees
OSU
binary tree
search tree
...
12
Writing Algorithms
Data Structures:
I
I
4
OSU
Depends on how you think, how easy it is to exhibit invariants, what is the
decomposition in sub-problems, ...
13
Data Structures:
Writing Algorithms
Reference
OSU
14
Writing Algorithms
Exercises:
Exercise 1
Input:
I
Ouput:
I
OSU
15
Writing Algorithms
Exercises:
Exercise 2
Input:
I
Ouput:
I
OSU
16
Writing Algorithms
Exercises:
Exercise 3
Input:
I
The left subtree of a node contains only nodes with keys less than the
nodes key.
The right subtree of a node contains only nodes with keys greater than the
nodes key.
Both the left and right subtrees must also be binary search trees.
Output:
I
A
Exercise: write an algorithm which implements the above description
OSU
17