1 - Algorithms, Proofs, Debugging, and Big-O Notation Flashcards
What is an abstraction? Explain how a function is a form of abstraction.
From the POV of the person calling the function, they only need to know the name of the action specified.
For example, for a function, you do not need to know the internal details.
What is an algorithm?
A description of how a specific problem can be solved, written at a level of detail that can be followed by the reader. (e.g. Newton’s method of a series of guesses to find the square root of a number).
What is the relationship between an algorithm and a function?
A function is a set of instructions written in a particular programming language. Although the function embodies the algorithm, the details of the algorithm are the same no matter the language used.
Give an example of an input condition that cannot be specified using only a type.
Types are function type signatures. For example, a minimum function written in integers will make sure that only integers are inputted.
However, a range restriction is not a type. For example, the square root program only works for positive numbers. Therefore, programs must check the range of the input at run-time.
What are two different techniques used to specify the input conditions for an algorithm?
- Argument types (will make sure that the arguments passed are of the correct type)
- Run-time checking (e.g. range restriction)
What are some ways used to describe the outcome, or result, of executing an algorithm?
- The result type
2. Documentation (comments)
In what way does the precision of instructions needed to convey an algorithm to another human being differ from that needed to convey an algorithm to a computer?
To explain algorithms in pseudo-code, we need only to emphasize the important properties of the algorithm, and downplay incidental details that do nothing to assist in the understanding of the procedure.
In considering the execution time of algorithms, what are two general types of questions one can ask?
First, will the algorithm terminate for all legal input values?
Second, is there a more accurate characterization of the amount of time it will execute?
What are some situations in which termination of an algorithm would not be immediately obvious?
For example, in Euclid’s GCD algorithm, there is no definite limit on iterations, but either n or m becomes smaller on each iteration. That means m+n satisfies the three conditions!
while (m !=n) { if (n > m) n = n - m; else m = m - n; } return n;
What are the three properties a value must possess in order to be used to prove termination of an algorithm?
- The property or value can be placed into a correspondence with integer values
- The property is nonnegative
- The property or value decreases steadily as the algorithm executes.
What is a recursive algorithm? What are the two sections of a recursive algorithm?
A recursive algorithm calls a version of itself.
There must be a base case, which does not have a recursive call, and the recursive/inductive case, which does have this recursive call.
What does it mean to say you are debugging a function?
Systematic testing of the program to uncover errors.
What are some useful hints to help you debug a function or program?
- Test small sections at a time
- Once found, find the simplest input that produces the same error
- Simulate execution of the test input
- Use print statements to view the state of the computation in the middle
- Don’t assume that handling one thing right means it’s always right.
What is an assertion?
A comment that explains what you know to be true when execution reaches a specific point in the program.
For example, if you have a series of if statements, then you could say that if it goes down one branch, that /* we know a == b / and else / we know a != b */.
What is an invariant?
An assertion inside a loop, since it must describe a condition that does not vary during the course of executing the loop.
For example, assertion 2 is an invariant, because before the assignment, s is only the partial sum of whatever loops executed before:
double s = 0.0; /* 1. s is the sum of an empty array */ for (int i = 0; i
Once you have identified assertions and invariants, how do you create a proof of correctness?
A proof of correctness is an informal argument that explains why you believe the code is correct.
You can use a series of small arguments that moves from one invariant to the next, and know how the value of the variables change as the execution progresses.