Course navigation & on this pageLecture 01 · CS-702
Thinking in algorithms
Understand the relationship between a problem, an algorithm, a data structure and a program.
Beginner~25 min guideSource-based learning
What you’ll understand
Define an algorithm and a data structure.
Separate correctness from efficiency.
Explain why representation and growth rate matter.
Before you begin: Basic programming concepts
Expanded study guide
Algorithms and data structures
An algorithm is a precise method for solving a problem. A data structure organizes information so the required operations can be performed. The introduction connects these two ideas: how you store information affects how you process it.
A program implements an algorithm in a language such as Java. An algorithm can be described using words, diagrams or pseudocode before a program exists.
Problem: specify the permitted inputs and required outputs.
Algorithm: specify finite, unambiguous steps that produce the output.
Data structure: specify how the information is represented and accessed.
Program: implement and test the procedure in a concrete environment.
Why study algorithms?
The source lecture illustrates the reach of algorithms across internet search, routing, biology, computer systems and scientific modelling. It also emphasizes intellectual development and becoming a more capable programmer.
The practical question is not merely whether a program runs. It is whether it remains usable as inputs grow. An efficient method can make a previously impractical computation possible.
Predict whether a problem fits available time and memory.
Choose between implementations using evidence.
Understand correctness conditions before optimizing.
Recognize reusable problem-solving patterns.
One problem, different methods
Consider finding an integer in a collection. Linear search checks entries one by one. Binary search can repeatedly halve the remaining region when the array is sorted. Both solve a search problem, but their preconditions and costs differ.
For an unsorted array used for only one query, sorting first may cost more than scanning once. For many queries on stable data, preprocessing can be worthwhile. Always include the cost of preparing the input.
Correctness comes before speed
An invariant states something that remains true throughout an algorithm. For binary search, if the key exists, it is inside the current search interval. Each step must preserve that statement while making progress toward termination.
Testing checks selected inputs. A correctness argument explains why every permitted input is handled. Useful tests include empty input, a single item, duplicates, boundary values and a missing search key.
Reading the supplied material in context
The original introduction is labelled CS-503, Winter 2020, and credits Kevin Wayne. This platform organizes the supplied files for CS-702; the original course identifiers and attribution remain unchanged in the source. Historical textbook prices and course-specific requirements in those slides are source context, not current requirements for this platform.
The original README calls both algorithms and programs hardware/software independent. An abstract algorithm can be analyzed independently of a machine, but a concrete program depends on its language and runtime. This clarification does not change the archived README.
Key takeaways
Choose the representation and the algorithm together. State preconditions, establish correctness, and then compare time and space. A fast implementation of the wrong procedure does not solve the problem.
Original implementations
Read the complete original code and its documentation. Core algorithm pages add a walkthrough, complexity discussion and a worked example.
Supplementary practice. Try each question before opening the answer.
How is an algorithm different from a Java program?
The algorithm describes the abstract steps. The Java program implements them using particular language features, types and runtime behavior.
Why is binary search not automatically better for an unsorted array?
It requires sorted input. Sorting has a cost; a single linear scan may be cheaper for one query.
Original course material
Complete lecture source
Every source page is preserved below. Open a page to read its text and inspect the original diagram, formula or example. Expanded explanations above are supplementary.
Page 01 · CS-503, WINTER 2020Original page 1 · Open the image for full detail.
CS-503, WINTER 2020
ALGORITHMS
AND
DATA STRUCTURES
KEVIN WAYNE
http://www.princeton.edu/~cos226
Page 02 · CS-503 course overviewOriginal page 2 · Open the image for full detail.
CS-503 course overview
What is CS-503?
survey course.・Intermediate-level
and problem solving, with applications.・Programming
method for solving a problem.・Algorithm:
structure: method to store information.・Data
topic data structures and algorithms
data types stack, queue, bag, union-find, priority queue
sorting quicksort, mergesort, heapsort, radix sorts
searching BST, red-black BST, hash table
graphs BFS, DFS, Prim, Kruskal, Dijkstra
strings KMP, regular expressions, tries, data compression
advanced B-tree, k-d tree, suffix array, maxflow
2
Page 03 · Why study algorithms?Original page 3 · Open the image for full detail.
Why study algorithms?
Their impact is broad and far-reaching.
Internet. Web search, packet routing, distributed file sharing, ...
Biology. Human genome project, protein folding, …
Computers. Circuit layout, file system, compilers, …
Computer graphics. Movies, video games, virtual reality, …
Security. Cell phones, e-commerce, voting machines, …
Multimedia. MP3, JPG, DivX, HDTV, face recognition, …
Social networks. Recommendations, news feeds, advertisements, …
Physics. N-body simulation, particle collision simulation, …
⋮
3
Page 04 · Why study algorithms?Original page 4 · Open the image for full detail.
Why study algorithms?
Their impact is broad and far-reaching.
4
Page 05 · Why study algorithms?Original page 5 · Open the image for full detail.
Why study algorithms?
Old roots, new opportunities.
of algorithms dates at least to Euclid.・Study
by Church and Turing in 1930s.・Formalized
important algorithms were discovered ・Some
by undergraduates in a course like this!
BCE 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s
300
5
Page 06 · Why study algorithms?Original page 6 · Open the image for full detail.
Why study algorithms?
For intellectual stimulation.
F R O M T H E
E D I T O R S
THE JOY OF ALGORITHMS
Francis Sullivan, Associate Editor-in-Chief
HE THEME OF THIS FIRST-OF-THE-CENTURY ISSUE OF COMPUTING IN “ For me, great algorithms are the poetry of computation. Just SCIENCE & ENGINEERING IS ALGORITHMS. IN FACT, WE WERE BOLD T
ENOUGH—AND PERHAPS FOOLISH ENOUGH—TO CALL THE 10 EXAMPLES WE’VE SE-
LECTED “THE TOP 10 ALGORITHMS OF THE CENTURY.”
Computational algorithms are probably as old as civilization. mysterious. But once unlocked, they cast a brilliant new light
Sumerian cuneiform, one of the most ancient written records, on some aspect of computing. A colleague recently claimed
consists partly of algorithm descriptions for reckoning in base that he’d done only 15 minutes of productive work in his
60. And I suppose we could claim that the Druid algorithm for whole life. He wasn’t joking, because he was referring to the
estimating the start of summer is embodied in Stonehenge. 15 minutes during which he’d sketched out a fundamental op-
timization algorithm. He regarded the previous years of really hard hardware!)
thought and investigation as a sunk cost that might or might like verse, they can be terse, allusive, dense, and even mysterious. (That’sLike so many other things that technology affects, algo-
rithms have advanced in startling and unexpected ways in the not have paid off.
20th century—at least it looks that way to us now. The algo- Researchers have cracked many hard problems since 1 Jan-
rithms we chose for this issue have been essential for progress uary 1900, but we are passing some even harder ones on to the
in communications, health care, manufacturing, economics, next century. In spite of a lot of good work, the question of
weather prediction, defense, and fundamental science. Con- how to extract information from extremely large masses of
versely, progress in these areas has stimulated the search for data is still almost untouched. There are still very big chal-
ever-better algorithms. I recall one late-night bull session on lenges coming from more “traditional” tasks, too. For exam-
the Maryland Shore when someone asked, “Who first ate a ple, we need efficient methods to tell when the result of a large
crab? After all, they don’t look very appetizing.’’ After the usual floating-point calculation is likely to be correct. Think of the
speculations about the observed behavior of sea gulls, someone way that check sums function. The added computational cost But once unlocked, they cast a brilliant new light on some gave what must be the right answer—namely, “A very hungry is very small, but the added confidence in the answer is large.
person first ate a crab.” Is there an analog for things such as huge, multidisciplinary
The flip side to “necessity is the mother of invention’’ is “in- optimizations? At an even deeper level is the issue of reason-
vention creates its own necessity.’’ Our need for powerful ma- able methods for solving specific cases of “impossible’’ prob-
chines always exceeds their availability. Each significant com- lems. Instances of NP-complete problems crop up in at-
putation brings insights that suggest the next, usually much tempting to answer many practical questions. Are there
larger, computation to be done. New algorithms are an attempt efficient ways to attack them?
to bridge the gap between the demand for cycles and the avail- I suspect that in the 21st century, things will be ripe for an-
able supply of them. We’ve become accustomed to gaining the other revolution in our understanding of the foundations of
Moore’s Law factor of two every 18 months. In effect, Moore’s computational theory. Questions already arising from quan-
Law changes the constant in front of the estimate of running tum computing and problems associated with the generation
of random numbers seem to require that we somehow tie to- as a function of problem size. Important new algorithms
gether theories of computing, logic, and the nature of the aspect of computing. ” — Francis Sullivan timedo not come along every 1.5 years, but when they do, they can
change the exponent of the complexity! physical world.
For me, great algorithms are the poetry of computation. The new century is not going to be very restful for us, but it
Just like verse, they can be terse, allusive, dense, and even is not going to be dull either!
2 C O MPUTING IN SCIENCE & ENGINEERING
“ An algorithm must be seen to be believed. ” — Donald Knuth
6
Page 07 · Why study algorithms?Original page 7 · Open the image for full detail.
Why study algorithms?
To become a proficient programmer.
“ I will, in fact, claim that the difference between a bad programmer
and a good one is whether he considers his code or his data structures
more important. Bad programmers worry about the code. Good
programmers worry about data structures and their relationships. ”
— Linus Torvalds (creator of Linux)
“ Algorithms + Data Structures = Programs. ” — Niklaus Wirth
7
Page 08 · Why study algorithms?Original page 8 · Open the image for full detail.
Why study algorithms?
They may unlock the secrets of life and of the universe.
“ Computer models mirroring real life have become crucial for most
advances made in chemistry today…. Today the computer is just as
important a tool for chemists as the test tube. ”
— Royal Swedish Academy of Sciences
(Nobel Prize in Chemistry 2013)
Martin Karplus, Michael Levitt, and Arieh Warshel
8
Page 09 · Why study algorithms?Original page 9 · Open the image for full detail.
Why study algorithms?
To solve problems that could not otherwise be addressed.
http://www.youtube.com/watch?v=ua7YlN4eL_w
9
Page 10 · Why study algorithms?Original page 10 · Open the image for full detail.
Why study algorithms?
For fun and profit.
10
Page 11 · Why study algorithms?Original page 11 · Open the image for full detail.
Why study algorithms?
impact is broad and far-reaching.・Their
roots, new opportunities.・Old
intellectual stimulation.・For
become a proficient programmer.・To
may unlock the secrets of life and of the universe.・They
solve problems that could not otherwise be addressed.・To
else is doing it.・Everybody
fun and profit.・For
Why study anything else?
11
Page 12 · Resources (textbook)Original page 12 · Open the image for full detail.
Resources (textbook)
Required reading. Algorithms 4th edition by R. Sedgewick and K. Wayne, Addison-
Wesley Professional, 2011, ISBN 0-321-57351-X.
AlgorithmsF O U R T H E D I T I O N
1st edition (1982) 2nd edition (1988) 3rd edition (1997)
R O B E R T S E D G E W I C K K E V I N W A Y N E
4th edition (2011)
3rd book scanned
by Google books
Available in hardcover and Kindle.
Amazon ($60/$35 to buy), Chegg ($25 to rent), ...・Online:
Labyrinth Books (122 Nassau St).・Brick-and-mortar:
reserve: Engineering library.・On
12