Notes On Algorithms by Sedgewick - Ch1 Fundamentals
Notes On Algorithms by Sedgewick - Ch1 Fundamentals
FUNDAMENTALS
We learn how to write and understand algorithms, and how to choose the best one. We get comfortable with
Java, as well as data abstractions, data structures, and other essential tools.
Algorithms: methods for solving problems that are suitable for computer implementation
Example: Euclid’s algorithm, implemented in Java:
public static int gcd(int p, int q) // compute gcd of p and q
{
if (q==0) return p; // if q is 0, p is the answer
int r = p % q; // r is remainder when q divides p
return gcd(q, r); // repeat with q and p as inputs
}
Data structures: schemes for organizing data that make it amenable to efficient algorithmic processing
Abstract data types (ADTs): including the bag, the queue, and the stack.
Every Java program in this book is either a data-type definition or a library of static methods.
Static method: a function. When called (with or without arguments), it executes statements, then gives a
return value. It may also have other side effects.
Recursion: methods may call themselves. A recursive program should:
1. have a base case.
(Include a conditional statement as the first statement with a return)
2. address smaller subproblems that converge to the base case
(Ensure that things continuously decrease, or increase, or whatever, so that the base case occurs.)
3. address smaller subproblems that do not overlap
(Ensure that the portions of the array, or other subproblems, are disjoint)
Example: Recursive implementation of binary search
Ch 1 - Fundamentals Sedgewick, Algorithms
Unit Testing: in a library of static methods, include a main() method that tests the methods in the
library. This can be refined to be a “development client” or a “test client” to test the code.
Data abstraction: also called object-oriented programming is the next level--more complex than the
functional programming described above (with libraries of static methods).
As mentioned above, static methods implement functions. They are called like so:
Classname.staticmethod();
Instance methods, by contrast, implement data-type operations. They are called like so:
objectname.instancemethod();
An object has:
- a state = a value, from the data type
- an identity = a place in memory
- a behavior = the effect of data type operations
In object-oriented programming, the client code interacts with objects to:
● create objects (establish identity) using the new command:
○ allocate memory space to the object (establishing its unique identity)
○ invoking the constructor to initialize the object’s value (state)
○ return a reference to the object
● manipulate data-type values (control an object’s behavior, and possibly change its state) by invoking
an instance method that operates on that object’s instance variables
● manipulate objects by assigning them to variables, passing them to functions, and returning them to
methods. This works as with primitive-types, except that variables refer to references to values, and
not the values themselves.*
* A reference allows access to an object, like a pointer. When you assign a variable to an object, you point it
at the object. It is possible to connect two variables to the same object--this is called aliasing.
How we approach programming problems by creating abstract data types (ADTs):
1. Specify an API. This separates clients from implementations, enables clear and correct client code,
and lists the operations we plan to implement.
2. Implement a java class that meets the API specifications.
3. Develop test clients, to validate our design decisions.
We strive to articulate good APIs, but the perfect API (one that clearly articulates behavior for all possible
inputs, including side effects) is impossible due to the specification problem (the problem of determining
whether two programs perform the same computation is undecidable.) Possible API problems:
- too hard to implement
- too narrow (missing methods the client needs)
- too wide (including methods the client doesn’t need)
- too general (no useful abstractions)
- too specific (abstractions too detailed or diffuse)
- too dependent on a particular representation (doesn’t free client code from the details)
Ch 1 - Fundamentals Sedgewick, Algorithms
Ch 1 - Fundamentals Sedgewick, Algorithms
SORTING
We learn algorithms for sorting data (specifically, arrays), and compare which ones are best for which
applications. These algorithms for the basis for many later algorithms.
SEARCHING
We learn algorithms for finding an item in a large collection. Like sorting, there are many different alternatives
with different efficiencies depending on the underlying data structure.
GRAPHS
Graphs are abstract data structures that consist of items and connections (sometimes with weights or
orientations). This chapter deals with algorithms for processing graphs.
STRINGS
Strings, or sequences of characters, are very important in modern computing. This chapter deals with algorithms
for processing strings--particularly, how to implement searching, sorting, and other algorithms in ways that are
more efficient for strings.
CONTEXT
This chapter relates the topics in this book to broader subjects, from scientific computing to the theory of
computation.