Download presentation
Presentation is loading. Please wait.
1
29-Jan-00 CPSC 212: Data Structures and Algorithms Instructor: Harry Plantinga
2
29-Jan-00 Computer Science 212 Data Structures and Algorithms The Heart of Computer Science Data structures Study of important algorithms Algorithm analysis Algorithm design techniques Plus GUIs and a GUI toolkit Intelligent systems Course Information: http://cs.calvin.edu/CS/212/ http://cs.calvin.edu/CS/212/
3
29-Jan-00 Why study DS & Algorithms? Some problems are difficult to solve and good solutions are known Some “solutions” don’t always work Some simple algorithms don’t scale well Data structures and algorithms make good tools for addressing new problems Why not just get a faster computer? Esthetic beauty!
4
29-Jan-00 Asymptotic Analysis What does it mean to say that an algorithm has runtime O(n log n)? n: Problem size Big-O: upper bound over all inputs of size n “Ignore constant factor” (why?) Most algorithms we study will have runtimes of O(1), O(log n), O(n), O(n log n), O(n 2 ), O(n 3 ), O(2 n ) g(n) is said to be O(f(n)) if there exist constants c 0 and n 0 such that g(n) n 0 “as n grows large”
5
29-Jan-00 Connectivity Building railroads across America Dutch Bingo IC connectivity Telephone network Given an edge from A to B, print it out unless previous edges already connect A and B n edges are added
6
29-Jan-00 Example 3-44-98-02-35-6 2-9[2-3-4-9]5-97-34-8 5-6[5-6] 0-2[0-8-4-3-2]6-1
7
29-Jan-00 Abstract Data Types What is an abstract data type? A collection of data A set of operations on that data What abstract data type do we need? Data: Operations: Sets of elements Find(x) – what set is x in? Union(A,B) – merge sets A and B
8
29-Jan-00 Algorithm How to solve the railroads problem? How to implement the Union-Find ADT? For road (x,y) if find(x) != find(y) union(find(x),find(y)) print “x – y”
9
29-Jan-00 Quick Find For each city, keep a pointer to the “capital” of the set Find(x): return the set name for x Runtime: Union(A,B): change all sets named A (=Find(x)) in the array to B (=Find(y)) Runtime: Can we do faster unions? O(1) O(n) 10^8 items, 10^9 unions, 100 mips: 10sec 10^8 items, 10^9 unions, 31 yrs
10
29-Jan-00 Quick Union To do a Unite(X,Y) (where X and Y are sets), just point X to Y Union runtime: Find runtime: Total time for adding n roads? Can we do better? O(1) O(n)
11
29-Jan-00 Weighted Union Keep track of the number of cities in each set Insert the smaller set into the larger Runtime for Unite(A,B): Runtime for Find(x): What’s the worst-case tree height? O(1) O(?)
12
29-Jan-00 Weighted Union Worst case What sequence of inputs gives the tallest trees? How tall are the trees? What is the runtime for find? What is the runtime for n unions? <= log n (prove by induction) O(log n) O(n log n) Better still: average O(1) per union!
13
29-Jan-00 Path Compression When you do a find, make every city on the path to the “capital” point to the capital Runtime? O(n log* n)
14
29-Jan-00 What difference does it make? Small problem: use the simplest algorithm Large problem: use the best algorithm! N M qf qu wqu pc 1000 6206 14 25 6 5 10000 83857 1216 4577 91 73 100000 1545119 1071 1106 A better algorithm is much more advantageous than a faster computer.
15
29-Jan-00 Lessons Start with simple algorithm Need a good algorithm for big problems Higher level of abstraction (tree) is helpful Strive for worst-case performance guarantees Average-case is better than nothing Identify fundamental abstraction Show me your code—you’ll have to explain your DS Show me your DS—I don’t need to see your code
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.