Algorithms and Data Structures Princeton University Spring 2008Kevin Wayne2What is COS 226?•Intermediate-level survey course.•Programming and problem solving with applications.•Algorithm: method for solving a problem.•Data structure: method to store information.topicdata structures and algorithmsdata typesstack, queue, union-find, priority queuesortingquicksort, mergesort, heapsort, radix sortssearchinghash table, BST, red-black treegraphsBFS, DFS, Prim, Kruskal, DijkstrastringsKMP, Regular expressions, TST, Huffman, LZWgeometryGraham scan, k-d tree, Voronoi diagramCOS 226 course overview3Their 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. CD player, DVD, MP3, JPG, DivX, HDTV, ...Transportation. Airline crew scheduling, map routing, ...Physics. N-body simulation, particle collision simulation, ...…Why study algorithms?Old roots, new opportunities.•Study of algorithms dates at least to Euclid•Some important algorithms werediscovered by undergraduates!4300 BC1920s1940s1950s1960s1970s1980s1990s2000sWhy study algorithms?5To solve problems that could not otherwise be addressed.Ex. Network connectivity. [stay tuned]Why study algorithms?6For intellectual stimulation.Why study algorithms?“ For me, great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. ” — Francis Sullivan“ An algorithm must be seen to be believed. ” — D. E. KnuthThey may unlock the secrets of life and of the universe.Computational models are replacing mathematical models in scientific enquiry720th century science(formula based)€ E = mc2€ F = ma€ F = G m1m2r2 € −h22m∇2+ V (r) Ψ(r) = E Ψ(r)Why study algorithms?“ Algorithms: a common language for nature, human, and computer. ” — Avi Wigderson21st century science(algorithm based)for (double t = 0.0; true; t = t + dt) for (int i = 0; i < N; i++) { bodies[i].resetForce(); for (int j = 0; j < N; j++) if (i != j) bodies[i].addForce(bodies[j]); }For fun and profit.8Why study algorithms?•Their impact is broad and far-reaching.•Old roots, new opportunities.•To solve problems that could not otherwise be addressed.•For intellectual stimulation.•They may unlock the secrets of life and of the universe.•For fun and profit.9Why study algorithms?Why study anything else?10Lectures. Introduce new material, answer questions.Precepts. Answer questions, solve problems, discuss programming assignment.The usual suspects#WhenWhereWhoOffice hoursL01M W 11-12:20Frist 302Kevin WayneM 2-3, T 11-12P01Th 12:30Friend 110Moses CharikarT 2:30-4:30P01ATh 12:30Friend 111Szymon RusinkiewiczF 1-3P02Th 1:30Friend 007Szymon RusinkiewiczF 1-3P02ATh 1:30Friend 110Moses CharikarT 2:30-4:30P03Th 3:30Friend 109Nadia HeningerM 3-5first precept meets Thursday118 programming assignments. 45%•Available via course website.•Due 11:55pm, starting Tuesday 2/12.•Electronic submission.Exams.•Closed-book with cheatsheet.•Midterm. 20%•Final. 25%Staff discretion. 10%•Participation in lecture and precepts.•Everyone needs to meet me at office hours!FinalMidtermProgramsStaffCoursework and gradingCourse content.•Course info.•Lecture slides.•Programming assignments.Course administration.•Check grades.•Submit assignments.Booksites. •Brief summary of content.•Download code from lecture.12Resources (web)http://www.princeton.edu/~cos226https://moodle.cs.princeton.edu/course/view.php?id=31http://www.cs.princeton.edu/IntroProgramminghttp://www.cs.princeton.edu/algs4Algorithms in Java, 3rd edition•Parts 1-4. [sorting, searching]•Part 5. [graph algorithms]Introduction to Programming in Java•Basic programming model.•Elementary AofA and data structures.Algorithms in Pascal(!)/C/C++, 2nd edition•Strings.•Geometric algorithms.Algorithms in Java 4th edition [in preparation]13Resources (books)14QuestionsNot registered? You can't submit assignments until register in SCORE.Change precept? Make the precept change in SCORE yourself;see Donna O'Leary (CS 410) for serious conflicts.Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008‣dynamic connectivity‣quick find‣quick union‣improvements‣applicationsUnion-Find AlgorithmsSteps to developing a usable algorithm.•Model the problem.•Find an algorithm to solve it.•Fast enough? Fits in memory?•If not, figure out why.•Find a way to address the problem.•Iterate until satisfied.The scientific method.Mathematical analysis.2Subtext of today’s lecture (and this course)3‣dynamic connectivity‣quick find‣quick union‣improvements‣applicationsGiven a set of objects•Union: connect two objects.•Find: is there a path connecting the two objects?4Dynamic connectivity6 51487320union(3, 4)union(8, 0)union(2, 3)union(5, 6) find(0, 2) no find(2, 4) yesunion(5, 1)union(7, 3)union(1, 6) find(0, 2) yes find(2, 4) yesunion(4, 8)5Network connectivity: larger examplepqDynamic connectivity applications involve manipulating objects of all types.•Variable name aliases.•Pixels in a digital photo.•Computers in a network.•Web pages on the Internet.•Transistors in a computer chip.•Metallic sites in a composite system.When programming, convenient to name objects 0 to N-1.•Use integers as array index.•Suppress details not relevant to union-find.•Could use symbol table to translate from object names to integers.6Modeling the objectsTransitivity. If p is connected to q and q is connected to r,then p is connected to r.Connected components. Maximal subset of objects that are mutually connected.7Modeling the connections6 51487320{ 1 5 6 } { 2 3 4 7 } { 0 8 }connected componentsFind query. Check if two objects are in the same subset.Union command. Replace subsets containing two objects with their union.8Implementing the operations6 51487320{ 1 5 6 } { 2 3 4 7 } { 0 8 }6 517320{ 1 5 6 } { 0 2 3 4 7 8 }48union(4, 8)9Goal. Design efficient data structure for union-find.•Find queries
View Full Document