Docsity

Data Structures and Algorithms Lecture Notes - CSC517 Spring 2005, Assignments of Computer Science

Toggle sidebar
bg1
Lectures 1-2
CSC517: Data Structures and Algorithms - Spring 2005
Dr. Christian A. Duncan
1 Announcements
•Read Chapter 1 by next Tuesday.
Section 1.6 is only necessary for CSC401 students.
•Homework 1: Due Thursday, January 27
R-1.6, R-1.9, R-1.14, C-1.10, C-1.14, C-1.17, C-1.25
Note: For C-1.25, by expending a taste tester, we mean using a taste tester. Basically, the king must
use O(log n) taste testers and you cannot assume that the poison works in precisely one month. One
creative (but incorrect) solution was to have one taste tester take each poison but separated by a few
minutes or so. Then when they died you could calculate the bad bottle by when they took each bottle.
•Program 1 (CSC401): Due Thursday, Feburary 3
P-1.3
2 Outline of Lecture 1
In class we covered the following topics:
•Syllabus: see course web page
•Expectations:
You are expected to be comfortable with the following:
–CSC120: Basic programming: loops, conditionals, methods, recursion (which language is irrele-
vant)
–CSC220: Linked lists, stacks, queues, maybe heaps, binary trees, and sorting algorithms.
–MTH309: Summations, logs, counting, combinations, simple probability.
Appendix A in the book gives a small review of mathematical facts.
•What is an algorithm?
A set of instructions for performing a task. This differs from the language used to describe the
algorithm. We’ll use pseudo-code: a fake language consisting of a combination of natural (English) and
programming (Java/C) languages.
•Why analyze algorithms?
When solving a problem, one presents an algorithm. However, we also need to guarantee that the algo-
rithm works properly (prove correctness) and almost as important has a certain performance guarantee
(run-time analysis).
•Run-time analysis (performance):
Typically, as the input to a program increases the time it takes to solve it also increases. The question
is by how much? For example, sorting ten numbers should be much quicker than sorting a million
numbers but how much faster.
•Experimental testing:
One way to measure performance is to implement the algorithm and then run it on several inputs of
varying sizes. The performance could then be graphed and compared to see an expected guess.
Very weak approach. But why? E.g. can’t test every input, hard to compare across machines or
systems, actually have to program the algorithm.
1
pf3
pf4

Partial preview of the text

Download Data Structures and Algorithms Lecture Notes - CSC517 Spring 2005 and more Assignments Computer Science in PDF only on Docsity!

Lectures 1-

CSC517: Data Structures and Algorithms - Spring 2005 Dr. Christian A. Duncan [email protected]

1 Announcements

  • Read Chapter 1 by next Tuesday. Section 1.6 is only necessary for CSC401 students.
  • Homework 1: Due Thursday, January 27 R-1.6, R-1.9, R-1.14, C-1.10, C-1.14, C-1.17, C-1. Note: For C-1.25, by expending a taste tester, we mean using a taste tester. Basically, the king must use O(log n) taste testers and you cannot assume that the poison works in precisely one month. One creative (but incorrect) solution was to have one taste tester take each poison but separated by a few minutes or so. Then when they died you could calculate the bad bottle by when they took each bottle.
  • Program 1 (CSC401): Due Thursday, Feburary 3 P-1.

2 Outline of Lecture 1

In class we covered the following topics:

  • Syllabus: see course web page
  • Expectations: You are expected to be comfortable with the following: - CSC120: Basic programming: loops, conditionals, methods, recursion (which language is irrele- vant) - CSC220: Linked lists, stacks, queues, maybe heaps, binary trees, and sorting algorithms. - MTH309: Summations, logs, counting, combinations, simple probability. Appendix A in the book gives a small review of mathematical facts.
  • What is an algorithm? A set of instructions for performing a task. This differs from the language used to describe the algorithm. We’ll use pseudo-code: a fake language consisting of a combination of natural (English) and programming (Java/C) languages.
  • Why analyze algorithms? When solving a problem, one presents an algorithm. However, we also need to guarantee that the algo- rithm works properly (prove correctness) and almost as important has a certain performance guarantee (run-time analysis).
  • Run-time analysis (performance): Typically, as the input to a program increases the time it takes to solve it also increases. The question is by how much? For example, sorting ten numbers should be much quicker than sorting a million numbers but how much faster.
  • Experimental testing: One way to measure performance is to implement the algorithm and then run it on several inputs of varying sizes. The performance could then be graphed and compared to see an expected guess. Very weak approach. But why? E.g. can’t test every input, hard to compare across machines or systems, actually have to program the algorithm.
  • Analytic approach: Do a mathematical analysis by counting the number of steps needed. Don’t need to implement, so no concern about what machine, and our analysis usually takes into consideration every single possible input. Analsysis stated as a function rather than in say microseconds. Why?
  • Requirements for analyzing: Language (pseudo-code), computational model, metric for time, and a way to state the time.
  • RAM: Random Access Machine In this model we assume certain operations take constant (say one) time. These operations include variable assignment, method invocation, basic math operations, comparisons, array access. What does constant time mean? Why can’t we just say it takes 5 nanoseconds? Not perfectly realistic: For example, there is an unlimited amount of memory so we aren’t concerned with accessing the one gazillionth number - still constant time, and numbers can be as large or as accurate as necessary, e.g. one gazillion or one gazillionth. Operations on these are hundred percent accurate and take constant time. This is just to make our analysis simple.

3 A Pseudocode Example

Look over pseudocode for the following algorithm Sort(A, n) used to sort an array A of n numbers (via SelectionSort):

Algorithm Sort(A,n): Input: Array A of n>=1 integers Output: The Array A in sorted (increasing) order Begin for i := 1 to n-1 do for j := i+1 to n do if (A[i] > A[j]) then swap(A[i], A[j]) return A End

Why is this algorithm correct? How long will it take? We shall count it step by step. Why is such a method of counting going to be very ineffective and difficult for harder algorithms?

4 Recurrence Relations

The above code and analysis was for an iterative approach. Another means of coding is via recursion. Recursive algorithms often are easier to analyze (once we learn some new techniques). Here is a (somewhat) recursive solution to the above sorting algorithm:

Algorithm Sort(A,n): Input: Array A of n>=1 integers (index starts at 1) Output: The Array A in sorted (increasing) order Begin if (n <= 1) return A for j := n-1 downto 1 do if (A[n] < A[j]) then swap(A[n], A[j]) Sort(A,n-1) return A End

6 Math Review

Section 1.3 and Appendix A give basic math material that should be familiar or at least easily understood by everyone in class. We shall look over each theorem quickly to make sure of this.

  • Geometric Summations:

∑n i=0 a

i (^) = 1 −an+ 1 −a.

  • Summation:

∑n i=1 i^ =^

n(n+1)

  • Logarithm and Exponent facts.
  • Floor and ceiling notation (though often ignored in notation).
  • Proof techniques like counterexample, contrapositive, contradiction, induction, loop invariant. Loop invariant may be a little new but easy to see. In an interative loop, we prove something is correct by proving that if the invariants are true before iteration of a loop they are true after the iteration (and before the next iteration). Coupled with proving it is true before the loop begins at all, this the implies that once the loop ends, the final invariant is true and hopefully what we are trying to prove. Badly worded - but trying to be brief.
  • Probability