0% found this document useful (0 votes)
144 views

Notes On Algorithms by Sedgewick - Ch1 Fundamentals

1. This chapter introduces fundamental concepts in algorithms including data structures, abstraction, recursion, and sorting. 2. It discusses different data structures like arrays, stacks, queues, and linked lists that are useful for organizing data. 3. The chapter also covers object-oriented concepts like classes, objects, and generics that allow defining new data types and collections.

Uploaded by

syl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Notes On Algorithms by Sedgewick - Ch1 Fundamentals

1. This chapter introduces fundamental concepts in algorithms including data structures, abstraction, recursion, and sorting. 2. It discusses different data structures like arrays, stacks, queues, and linked lists that are useful for organizing data. 3. The chapter also covers object-oriented concepts like classes, objects, and generics that allow defining new data types and collections.

Uploaded by

syl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ch 1 - Fundamentals Sedgewick, Algorithms

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

Fundamental Data Types for ​collections ​of objects:  


bag  ● no removal of items 
● order is unimportant 
● iterable with ​foreach 
 
Example: standard deviation 
 

queue (FIFO)  ● iterable with ​foreach ​in the 


order that they were put in 
● preserves relative order 
 
Example: create an array for data 
  of an unknown size 

stack (LIFO)  ● iterable with ​foreach ​in the 


reverse order that they were 
put in (most recent first)  
● reverses relative order 
 
  Example: order of operations 
 
Generics: ​also known as ​parameterized types,​ allow us to use these ADTs for any kind of data.  
 
when we write ​Queue<t>​ , ​t ​is a ​type parameter​, a placeholder for a type determined by the client. 
the client can then write  
Stack<String> myData = new Stack<String>(); 
 
Iterable collections: ​as shown in the APIs, some classes are ​iterable​ (can be processed item-by-item) 
 
if a collection is iterable, the following code is possible:  
for (Transaction x : collection) { StdOut.println(x); } 
 
To support this capability, our collection needs to:  
- implement an ​iterator()​ method that returns an ​Iterator​ object 
 
For example, we might add this code to our class:  
public Iterator<Item> iterator() { return new MyArrayIterator(); } 
 
- the Iterator class must include two methods:  
- hasNext() which returns a boolean (true if there is a next item 
- next() which returns an item 
 
We can put a nested class such as this one within our class:  
private class MyArrayIterator implements Iterator<Item> { 
private int i = N; 
public boolean hasNext() { return i > 0; } 
public Item next() { return a[--i]; } 
public void remove() { } } 
Ch 1 - Fundamentals Sedgewick, Algorithms

We now have two ways to represent collections of objects:  


● arrays​ (sequential allocation), including resizing arrays 
○ PRO: the index provides immediate, constant-time access to any item 
○ CON: we must know the size of the entire array on initialization 
 
● linked lists ​(linked allocation) 
○ PRO: uses space proportional to its size (no extra space needed)  
○ CON: we must know the reference in order to access a specific item 
 
A linked list is a ​recursive ​data structure: it may be either (1) empty (null) or (2) a reference to a node. That 
node then contains a generic item and a reference to a linked list.  

 
   
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.  
 

You might also like