CS302: Fundamental Algorithms (and Data Structures) Fall 2005 Instructor: Dr. Lynne Parker http://www.cs.utk.edu/~parker/Courses/CS302-fall05
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 Our Office Hours: Mon. Mon. Tues. Tues. Wed. Wed. Thurs. Thurs. Fri. Fri. • 1 – 3 PM: • 10 – 11 AM: • 2:30 – 5:30 PM: • 10 –11 AM: • 9:05 – 12:05 AM: Bailey office hrs. Phillips office hrs. Lab 1 (Bailey) Phillips office hrs. Lab 2 (Phillips) • 2:10 – 3:25: Class • 2:10 – 3:25: Class • 3:30 – 4:30 PM • 3:30 – 4:30 Parker office hrs. Parker office hrs. http://www.cs.utk.edu/~parker/Courses/CS302-fall05
Today’s Topics � Administrivia (syllabus, labs, … ) � Overview of Data Structures and Algorithms � “Bookstore” example � Abstraction � Interface � Implementation � Reminder of C stuff � Homework!!
The “bookstore” example � How do you find the book you’re looking for? You need to use some kind of algorithm and…
The “bookstore” example � How do you find the book you’re looking for? You need to use some kind of algorithm and… � … of course, we want the best algorithm possible, but…
The “bookstore” example � How do you find the book you’re looking for? You need to use some kind of algorithm and… � … of course, we want the best algorithm possible, but… � … what “best” means will depend on how the books are organized (stored)
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 ).
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…
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 (log 2 n ) � 1,000,000 books, 10 seconds/ book = ???
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 (log 2 n ) � 1,000,000 books, 10 seconds/ book = 200s < 4 min!!! [ 2 20 = 1,048,576; so, log 2 (1,000,000) ≈ 20 ]
BinaryBooks, Inc. � This bookstore keeps its books organized 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
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
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
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)
Interface vs. Implementation � So, what you really care about is the interface of 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)
Interface vs. Implementation BookSet void add(Book b) void remove(Book b) bool has(Book b) SortedArrayBookSet BinaryTreeBookSet Attributes Attributes Operations Operations HashBookSet Attributes Operations
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
That’s all, folks… � Your homework: � Lab 1 assigned today!! 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
Recommend
More recommend