SLIDE 1
CS302: Fundamental Algorithms (and Data Structures)
Fall 2005 Instructor: Dr. Lynne Parker
http://www.cs.utk.edu/~parker/Courses/CS302-fall05
SLIDE 2 CS302: Fundamental Algorithms (and Data Structures)
- Dr. Lynne Parker, Associate Professor, Dept. of
Computer Science – parker@cs.utk.edu; Claxton 220 TA: Michael Bailey – mbailey@cs.utk.edu; Claxton 125d TA: Charles Phillips – cphillip@cs.utk.edu; Claxton 110g
Mon. Mon. Tues. Tues. Wed. Wed. Thurs. Thurs. Fri. Fri.
Bailey office hrs.
Phillips office hrs.
- 2:10 – 3:25: Class
- 3:30 – 4:30 PM
Parker office hrs.
Lab 1 (Bailey)
Phillips office hrs.
- 2:10 – 3:25: Class
- 3:30 – 4:30
Parker office hrs.
Lab 2 (Phillips)
Our Office Hours:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05
SLIDE 3 Today’s Topics
- Administrivia (syllabus, labs, …
)
- Overview of Data Structures and Algorithms
- “Bookstore” example
- Abstraction
- Interface
- Implementation
- Reminder of C stuff
- Homework!!
SLIDE 4
The “bookstore” example
How do you find the book you’re looking for? You need to use some kind of algorithm and…
SLIDE 5 The “bookstore” example
How do you find the book you’re looking for? You need to use some kind of algorithm and… …
- f course, we want the best
algorithm possible, but…
SLIDE 6 The “bookstore” example
How do you find the book you’re looking for? You need to use some kind of algorithm and… …
- f course, we want the best
algorithm possible, but… … what “best” means will depend on how the books are
SLIDE 7 MessyBookPile, Inc.
This bookstore keeps its books all it one big pile, with no organization of any kind Your algorithm is then:
- For each book:
- If it is not the book I want, continue searching
If the bookstore has 1,000,000 books, and if it takes 10 seconds for you to check each book, it make take you as long as 4 months (!) to find the book you want Not very good, is it? NOTE: Search time is directly proportional to the number of books: O(n).
SLIDE 8 LongBookShelf, Inc.
This competitor bookstore keeps its books all in one very long array sorted by ISBN One possible algorithm is then (linear search):
- For each book
- If it is not the book I want, continue searching
(Obviously, this is the same algorithm used with MessyBookPile, Inc.) Again… search time is directly proportional to the number of books: O(n). Hmmm… we didn’t make use of the fact that the books are sorted…
SLIDE 9 LongBookShelf, Inc.
Another possible algorithm is then (binary search):
- Look at the middle book
- If it is the book I want, stop.
- If the book I want comes before the middle book, search the
first half of the books using this same algorithm
- If the book I want comes after the middle book, search the
second half of the books using this same algorithm
This now makes use of the fact that the books are
- sorted. And, it’s recursive!
Now … search time is proportional to the logarithm of the number of books: O(log2 n) 1,000,000 books, 10 seconds/ book = ???
SLIDE 10 LongBookShelf, Inc.
Another possible algorithm is then (binary search):
- Look at the middle book
- If it is the book I want, stop.
- If the book I want comes before the middle book, search the
first half of the books using this same algorithm
- If the book I want comes after the middle book, search the
second half of the books using this same algorithm
This now makes use of the fact that the books are
- sorted. And, it’s recursive!
Now … search time is proportional to the logarithm of the number of books: O(log2 n) 1,000,000 books, 10 seconds/ book = 200s < 4 min!!! [ 220 = 1,048,576; so, log2(1,000,000) ≈ 20 ]
SLIDE 11 BinaryBooks, Inc.
This bookstore keeps its books
- rganized in a binary search tree
It’s suing LongBookShelf, Inc., because they stole the idea of a binary search from BinaryBooks, Inc. The algorithm is binary search:
- Look at a node of the tree
- If it contains the book I want, stop
- if the book I want comes before the book in this node,
search this node’s left child, using the same algorithm
- If the book I want comes after the book in this node,
search this node’s right child, using this same algorithm
SLIDE 12 CategoriesRUs, Inc.
This other competitor bookstore keeps its books all organized by sections The search algorithm is now:
- For each section
- If it is the section of the book I want
- Search each book in that section using one of the previous
algorithms
This is a little faster than the previous algorithms
SLIDE 13
Bottom line…
The choice and efficiency of an algorithm depends on how the data is organized Likewise, how you’re going to store your data depends on what you want to do with it. If you don’t care about searching, you might not care if the algorithm is linear search or binary search. In addition, a data structure depends on the properties of the data it stores Pile of books: equality Sorted books: less than
SLIDE 14
Interface vs. Implementation
Consider an int variable int is a data type It could be that the computer you’re using stores int in 1’s complement or 2’s complement format When you use it, you don’t care how it’s really implemented inside the computer All you care about is that the following operations are defined for that particular data type:
+ , -, * , / , < , > , ≤, ≥, = = , != , + + , --
A data type like that is called an Abstract Data Type (or ADT)
SLIDE 15 Interface vs. Implementation
So, what you really care about is the interface
- f an ADT, not its implementation
The interface of an ADT is the set of operations defined for it So, in the bookstore example, you could create a general interface for your bookstore, and then have as many individual implementations as you wanted This sounds like Object-Oriented Programming (or OOP)
SLIDE 16
Interface vs. Implementation
BookSet void add(Book b) void remove(Book b) bool has(Book b) SortedArrayBookSet Attributes Operations BinaryTreeBookSet Attributes Operations HashBookSet Attributes Operations
SLIDE 17 Now … Refresher on C Stuff
3 handouts for today:
- 1. Fundamentals
- 2. General C Stuff
- 3. Coding Clearly
- See “Schedule/ Readings/ Notes” link for these
handouts: http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html
SLIDE 18 That’s all, folks…
Your homework:
Due Friday, Sept. 9.
- See “Lab Assignments” link:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Labs.html
- Read 3 handouts for today, and work examples
contained in them:
- See “Schedule/ Readings/ Notes” link for today:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html
Before class next time:
- Read 4 handouts for next class:
- See “Schedule/ Readings/ Notes” link for today:
http://www.cs.utk.edu/~parker/Courses/CS302-fall05/Schedule.html