Algorithms. Algorithms ALGORITHM DESIGN

Similar documents
Final exam. During finals period. 7:30-10:30 PM on Friday, January 15. McCosh 50. Rules.

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

5. DIVIDE AND CONQUER I

Chapter 8. NP and Computational Intractability. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance.

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

! Greed. O(n log n) interval scheduling. ! Divide-and-conquer. O(n log n) FFT. ! Dynamic programming. O(n 2 ) edit distance.

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Introduction to Algorithms Third Edition

Chapter 6. Dynamic Programming. Modified from slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Divide and Conquer 1

Problem set 2. Problem 1. Problem 2. Problem 3. CS261, Winter Instructor: Ashish Goel.

Virtual University of Pakistan

Design and Analysis of Algorithms. Comp 271. Mordecai Golin. Department of Computer Science, HKUST

Algorithms Interview Questions

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

5105 BHARATHIDASAN ENGINEERING COLLEGE

Design and Analysis of Algorithms

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Review for Midterm Exam

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Reductions. Linear Time Reductions. Desiderata. Reduction. Desiderata. Classify problems according to their computational requirements.

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

CS 440 Theory of Algorithms /

Algorithmic Paradigms

( ) 1 B. 1. Suppose f x

MSc(IT) Program. MSc(IT) Program Educational Objectives (PEO):

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

COMP 251 Winter 2017 Online quizzes with answers

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

Algorithm classification

Algorithm Design Techniques. Hwansoo Han

Review of course COMP-251B winter 2010

Combinatorial Search. permutations backtracking counting subsets paths in a graph. Overview

The Algorithm Design Manual

8/19/13. Computational problems. Introduction to Algorithm

1. To reduce the probability of having any collisions to < 0.5 when hashing n keys, the table should have at least this number of elements.

Lecture 3, Review of Algorithms. What is Algorithm?

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

8.1 Polynomial-Time Reductions

8.1 Polynomial-Time Reductions

Subject Index. Journal of Discrete Algorithms 5 (2007)

Algorithms COMBINATORIAL SEARCH. permutations backtracking counting subsets paths in a graph

4.1.2 Merge Sort Sorting Lower Bound Counting Sort Sorting in Practice Solving Problems by Sorting...

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Material covered. Areas/Topics covered. Logistics. What to focus on. Areas/Topics covered 5/14/2015. COS 226 Final Exam Review Spring 2015

Introduction to Computer Science

n 2 ( ) ( ) + n is in Θ n logn

L.J. Institute of Engineering & Technology Semester: VIII (2016)

Contents. CS 124 Final Exam Practice Problem 5/6/17. 1 Format and Logistics 2

Ch5. Divide-and-Conquer

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

3/7/2018. CS 580: Algorithm Design and Analysis. 8.1 Polynomial-Time Reductions. Chapter 8. NP and Computational Intractability

CS 580: Algorithm Design and Analysis

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

CS483 Design and Analysis of Algorithms

CSci 231 Final Review

CAD Algorithms. Categorizing Algorithms

TOTAL CREDIT UNITS L T P/ S SW/F W. Course Title: Analysis & Design of Algorithm. Course Level: UG Course Code: CSE303 Credit Units: 5

CSC 373: Algorithm Design and Analysis Lecture 8

Pascal s triangle, 167 path counting, 168 recursion trees, 166 recursive decomposition, 169 sequence comparison, tangled dependencies, 166

Welcome to the course Algorithm Design

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

Algorithmic problem-solving: Lecture 2. Algorithmic problem-solving: Tractable vs Intractable problems. Based on Part V of the course textbook.

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Data Structures and Algorithm Analysis in C++

Algorithms: Lecture 7. Chalmers University of Technology

Chapter 9 Graph Algorithms

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

Algorithms and Data Structures

END-TERM EXAMINATION

COURSE: DATA STRUCTURES USING C & C++ CODE: 05BMCAR17161 CREDITS: 05

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

Practice Problems for the Final

CSE 373: Data Structures and Algorithms

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

Index. stack-based, 400 A* algorithm, 325

Design and Analysis of Algorithms - - Assessment

Data Structures and Algorithms

CSCE 350: Chin-Tser Huang. University of South Carolina

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

INSTITUTE OF AERONAUTICAL ENGINEERING

CMSC Theory of Algorithms Second Midterm

CSE 373: Data Structures and Algorithms

Lecture 1. Introduction

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

Algorithms. Algorithms 6.5 REDUCTIONS. introduction designing algorithms establishing lower bounds classifying problems intractability

About the Author. Dependency Chart. Chapter 1: Logic and Sets 1. Chapter 2: Relations and Functions, Boolean Algebra, and Circuit Design

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

4. Give the diagram representation of Notion of algorithm. [M 13]

Transcription:

Algorithms ROBERT SEDGEWICK KEVIN WAYNE ALGORITHM DESIGN Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability Last updated on 12/15/17 2:58 PM

Algorithm design Algorithm design patterns (and antipatterns). Analysis of algorithms. Greedy. Divide-and-conquer. Dynamic programming. Network flow. Randomized algorithms. Intractability. Want more? See COS 343, COS 423, COS 445, COS 451,. 2

Interview questions 3

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Egg drop Goal. Find T using fewest number of tosses. n. breaks.. T... does not break. 3 2 1 5

Egg drop Goal. Find T using fewest number of tosses. Variant 1. 1 egg. Variant 2. eggs. Variant 3. eggs and ~ 2 lg T tosses. Variant 4. 2 eggs and c n 1/2 tosses. Variant 5. 2 eggs and c T 1/2 tosses. breaks does not break n... T.... 3 2 1 6

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Greedy algorithms Make locally optimal choices at each step. Familiar examples. Huffman coding. Prim s algorithm. Kruskal s algorithm. Dijkstra s algorithm. More classic examples. U.S. coin changing. Activity scheduling. Gale Shapley stable marriage.... Caveat. Greedy algorithm rarely leads to globally optimal solution. (but is often used anyway, especially for intractable problems) 13

Document search Given a document that is a sequence of n words, and a query that is a sequence of m words, find the smallest range in the document that includes the m query words (in the same order). Ex. Query = textbook programming computer This book is intended to survey the most important computer algorithms in use today, and to teach fundamental techniques to the growing number of people in need of knowing them. It is intended for use as a textbook for a second course in computer science, after students have acquired basic programming skills and familiarity with computer systems. The book also may be useful for self-study or as a reference for people engaged in the development of computer systems or applications programs, since it contains implementations of useful algorithms and detailed information on performance characteristics and clients. 14

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Divide and conquer Break up problem into two or more independent subproblems. Solve each subproblem recursively. Combine solutions to subproblems to form solution to original problem. Familiar examples. Mergesort. Quicksort. More classic examples. Closest pair. Convolution and FFT. Matrix multiplication. Integer multiplication. needs to take COS 226? Prototypical usage. Turn brute-force n 2 algorithm into n log n algorithm. 31

Personalized recommendations Music site tries to match your song preferences with others. Your ranking of songs: 0, 1,, n 1. My ranking of songs: a 0, a 1,, a n 1. Music site consults database to find people with similar tastes. Kendall-tau distance. Number of inversions between two rankings. Inversion. Songs i and j are inverted if i < j, but a i > a j. A B C D E F G H you 0 1 2 3 4 5 6 7 me 0 2 3 1 4 5 7 6 3 inversions: 2-1, 3-1, 7-6 32

Counting inversions Problem. Given a permutation of length n, count the number of inversions. 0 2 3 1 4 5 7 6 3 inversions: 2-1, 3-1, 7-6 Brute-force n 2 algorithm. For each i < j, check if a i > a j. A bit better. Run insertion sort; return number of exchanges. Goal. n log n time (or better). 33

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Dynamic programming Break up problem into a series of overlapping subproblems. Build up solutions to larger and larger subproblems. (caching solutions to subproblems in a table for later reuse) Familiar examples. Shortest paths in DAGs. Seam carving. Bellman Ford. More classic examples. Unix diff. Viterbi algorithm for hidden Markov models. Smith Waterman for DNA sequence alignment. CKY algorithm for parsing context-free grammars.... THE THEORY OF DYNAMIC PROGRAMMING RICHARD BELLMAN 38

House coloring problem Goal. Paint a row of n houses red, green, or blue so that No two adjacent houses have the same color. Minimize total cost, where cost(i, color) is cost to paint i given color. A B C D E F 7 6 7 8 9 20 3 8 9 22 12 8 16 10 4 2 5 7 cost to paint house i the given color 39

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Network flow Classic problems on edge-weighted graphs. Familiar examples. Shortest paths. Bipartite matching. Maxflow and mincut. Minimum spanning tree. Other classic examples. Minimum-cost arborescence. Non-bipartite matching. Assignment problem. Minimum-cost flow.... Applications. Many many problems can be modeled using network flow. 42

Shortest path with orange and black edges Goal. Given a digraph, where each edge has a positive weight and is orange or black, find shortest path from s to t that uses at most k orange edges. G s 8 1 1 10 2 9 t 4 3 2 7 3 k = 0: s 1 t (17) k = 1: s 3 t (13) k = 2: s 2 3 t (11) k = 3: s 2 1 3 t (10) 43

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

Randomized algorithms Algorithm that uses random coin flips to guide its behavior. Familiar examples. Quicksort. Quickselect. More classic examples. Rabin Karp substring search. Miller Rabin primality testing. Polynomial identity testing. Volume of convex body. Universal hashing. Global min cut. 47

Nuts and bolts Problem. A disorganized carpenter has a mixed pile of n nuts and n bolts. 独The goal is to find the corresponding pairs of nuts and bolts. 独Each nut fits exactly one bolt and each bolt fits exactly one nut. 独By fitting a nut and a bolt together, the carpenter can see which one is bigger (but cannot directly compare either two nuts or two bolts). Brute-force n 2 solution. Compare each bolt to each nut. Challenge. Design an n log n algorithm. 48

ALGORITHM DESIGN Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu analysis of algorithms greedy divide-and-conquer dynamic programming network flow randomized algorithms intractability

NP-completeness Fundamental barrier to designing efficient algorithms. Familiar examples. 3-SATISFIABILITY. INTEGER-LINEAR-PROGRAMMING. TRAVELING-SALESPERSON-PROBLEM. More classic examples. PLANAR-MAP-3-COLOR. REGISTER-ALLOCATION. PROTEIN-FOLDING. HAMILTON-PATH. KNAPSACK. 3D-ISING.... 52

Exhaustive search Exhaustive search. Iterate over all elements of a search space. Applicability. Huge range of problems (including intractable ones). Caveat. Search space is typically exponential in size of input. Backtracking. Systematic method to iterate over elements of a search space, pruning useless ones to save time. 53

Sudoku Goal. Fill 9-by-9 grid so that every row, column, and box contains each of the digits 1 through 9. 7 8 3 2 1 5 4 2 6 3 8 1 9 9 6 4 7 5 54

Sudoku Goal. Fill 9-by-9 grid so that every row, column, and box contains each of the digits 1 through 9. 7 2 8 9 4 6 3 1 5 9 3 4 2 5 1 6 7 8 5 1 6 7 3 8 2 4 9 1 4 7 5 9 3 8 2 6 3 6 9 4 8 2 1 5 7 8 5 2 1 6 7 4 9 3 2 9 3 6 1 5 7 8 4 4 8 1 3 7 9 5 6 2 6 7 5 8 2 4 9 3 1 Sudoku is a denial of service attack on human intellect. Ben Laurie (founding director of Apache Software Foundation) 55

Sudoku is (probably) intractable Remark. Natural generalization of Sudoku is NP-complete. 2-by-2 Sudoku 25-by-25 Sudoku http://xkcd.com/74 56

Hamilton path Goal. Does there exist a path that visits every vertex exactly once? visit every edge exactly once Remark. Linear-time algorithm for EULER-PATH; HAMILTON-PATH is NP-complete. 60

Credits Faculty lead preceptors and graduate student AIs. Undergraduate graders and lab TAs. Apply to be one next semester! Ed tech. Most developed by undergrads! 62

A final thought Algorithms and data structures are love. Algorithms and data structures are life. anonymous COS 226 student 63