cs302 fundamental algorithms and data structures
play

CS302: Fundamental Algorithms (and Data Structures) Fall 2005 - PowerPoint PPT Presentation

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,


  1. CS302: Fundamental Algorithms (and Data Structures) Fall 2005 Instructor: Dr. Lynne Parker http://www.cs.utk.edu/~parker/Courses/CS302-fall05

  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 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

  3. Today’s Topics � Administrivia (syllabus, labs, … ) � Overview of Data Structures and Algorithms � “Bookstore” example � Abstraction � Interface � Implementation � Reminder of C stuff � Homework!!

  4. The “bookstore” example � How do you find the book you’re looking for? You need to use some kind of algorithm and…

  5. 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…

  6. 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)

  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 ).

  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…

  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 (log 2 n ) � 1,000,000 books, 10 seconds/ book = ???

  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 (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 ]

  11. 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

  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

  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

  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)

  15. 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)

  16. 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

  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

  18. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend