Download Algorithms and Data Structures

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Algorithms and Data Structures
◦ An algorithm is a step-by-step procedure (a “recipe”) for performing
a task.
◦ A data structure is a systematic way of organising data and making
it accessible in certain ways
This thread of the course is concerned with the design and analysis of
“good” algorithms and data structures.
Algorithms and Data Structures in CS1
Data Structures
Arrays, linked lists, stacks, trees
Algorithm design principles
Recursive algorithms, dynamic programming
Sorting Algorithms
Insertion sort, selection sort, bucket sort
Textbooks
[GT] Michael T. Goodrich and Roberto Tamassia, Algorithm Design –
Foundations, Analysis, and Internet Examples. Wiley, 2002.
[CLRS] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein, Introduction to Algorithms. McGraw-Hill, 2002.
[K] Jeffrey H. Kingston, Algorithms and Data Structures. 2nd Edition,
Addison-Wesley, 1997.
[AU] Alfred V. Aho and Jeffrey D. Ullman, Foundations of Computer
Science. C-edition, Computer Science Press, 1995.
[W] Mark A. Weiss, Data Structures and Problem Solving using
JAVA. 2nd Edition, Addison-Wesley, 2002.
Criteria for evaluating algorithms
◦ Correctness


 runtime


space (= amount of memory used)
◦ Efficiency w.r.t.

network traffic



number of accesses to secondary storage
◦ Simplicity
Measuring the Runtime
The runtime of a program depends on a number of factors such as:
(1) The time complexity of the algorithm.
(2) The input of the program.
(3) The quality of the implementation and the quality of the code generated by the compiler.
(4) The machine used to execute the program.
Example 1: Sequential Search
Algorithm seqSearch(A, k)
Input:
An integer array A, an integer k
Output: The smallest index i with A[i] = k, if such an i exists,
or −1 otherwise.
i ← 0 to A.length − 1 do
2.
if A[i] = k then
return i
3.
4. return −1
1. for
JAVA code for sequential search
public static int seqSearch(int[] A,int k) {
for(int i = 0; i < A.length; i++)
if ( A[i] == k )
return i;
return -1;
}
Worst Case Time Complexity
Assign a size to each possible input.
Definition
The (worst-case) time-complexity of an algorithm A is the function
TA : N → N where TA(n) is the maximum number of computation
steps performed by A on an input of size n.
Average Time Complexity
Worst-case time complexity seems overly pessimistic!
Definition (?)
The average time-complexity of an algorithm A is the function
AVT A : N → N where AVT A(n) is the average number of computation steps performed by A on an input of size n.
Problems with average time
◦ What precisely does average mean? It depends on the application how the average input looks.
◦ Average time analysis is mathematically very difficult and often
infeasible.
A reasonable approach
Worst-Case Analysis
+
Experiments
Example 2: Binary Search
Algorithm binarySearch(A, k, i1, i2)
Input:
An integer array A sorted increasingly, integers i1, i2 and k
Output: An index i, i1 ≤ i ≤ i2 with A[i] = k, if such an i exists,
or −1 otherwise.
1. if i2 < i1 then
2.
return −1
3. else
4.
5.
6.
7.
8.
9.
10.
2
j ← b i1+i
2 c
if k = A[j] then
return j
else if k < A[j] then
return binarySearch(A, k, i1, j − 1)
else
return binarySearch(A, k, j + 1, i2)
JAVA code for binary search
public static int binarySearch(int[] A,int k,int i1,int i2) {
if ( i1 > i2 )
return -1;
int j = i1+i2/2;
if ( A[j] == k )
return j;
else if ( A[j] < k )
return binarySearch(A,k,i1,j-1);
else
return binarySearch(A,k,j+1,i2);
}
A code fragment for measuring the runtime
Random rand = new Random(System.currentTimeMillis());
int[] A = new int[size];
for( int j =0; j < size; j++)
A[j] = rand.nextInt(size);
int k = rand.nextInt(size);
long start = System.currentTimeMillis();
seqSearch(A,k);
long end = System.currentTimeMillis();
int t = (int) (end - start);
The actual runtime
input size |
seqSearch
| binarySearch
|
wc
|
avc
|
wc
|
avc
------------+---------+---------+---------+-------100 |
4 ms | <= 1 ms | <= 1 ms | <= 1 ms
1000 | <= 1 ms | <= 1 ms | <= 1 ms | <= 1 ms
10000 | <= 1 ms | <= 1 ms | <= 1 ms | <= 1 ms
100000 |
3 ms | 1.5 ms | <= 1 ms | <= 1 ms
200000 |
5 ms | 3.2 ms | <= 1 ms | <= 1 ms
300000 |
7 ms | 4.6 ms | <= 1 ms | <= 1 ms
400000 |
11 ms | 5.9 ms | <= 1 ms | <= 1 ms
500000 |
12 ms | 7.5 ms | <= 1 ms | <= 1 ms
600000 |
14 ms | 8.6 ms | <= 1 ms | <= 1 ms
700000 |
17 ms | 9.5 ms | <= 1 ms | <= 1 ms
800000 |
20 ms | 11.9 ms | <= 1 ms | <= 1 ms
900000 |
22 ms | 13.5 ms | <= 1 ms | <= 1 ms
1000000 |
25 ms | 15.6 ms | <= 1 ms | <= 1 ms