Design and Analysis of Algorithms (I)

Similar documents
Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (IX) Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Introduction to Algorithms Third Edition

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

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

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

Algorithms for Data Science

Homework Assignment #3 Graph

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Randomized Graph Algorithms

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

CS521 \ Notes for the Final Exam

Lecture 1. Introduction

CS Final - Review material

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

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

Advanced Algorithms and Data Structures

Data Structures and Algorithms

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Advanced Algorithms and Data Structures

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

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

Final Examination CSE 100 UCSD (Practice)

COMP Analysis of Algorithms & Data Structures

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

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Data Structures and Algorithms

Problem Score Maximum MC 34 (25/17) = 50 Total 100

DESIGN AND ANALYSIS OF ALGORITHMS

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

CSci 231 Final Review

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

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN. Sec. Name Office Office hour Mail id

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

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

END-TERM EXAMINATION

CSE : ADVANCED ALGORITHMS

Algorithm Design Techniques part I

Question Points Score TOTAL 50

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

Algorithms and Data Structures

Design and Analysis of Algorithms

Design and Analysis of Algorithms

Analysis of Algorithms, I

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

CSC 373: Algorithm Design and Analysis Lecture 8

COMP 251 Winter 2017 Online quizzes with answers

Chapter 6. Dynamic Programming

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

Computer Science E-22 Practice Final Exam

Practice Problems for the Final

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Solutions to relevant spring 2000 exam problems

AP Computer Science 4325

Virtual University of Pakistan

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

ECE250: Algorithms and Data Structures Final Review Course

Last week: Breadth-First Search

Computer Algorithms. Introduction to Algorithm

CS61BL. Lecture 5: Graphs Sorting

Chapter 9 Graph Algorithms

( D. Θ n. ( ) f n ( ) D. Ο%

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Module 2: Classical Algorithm Design Techniques

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

CS 310 Advanced Data Structures and Algorithms

Department of Information Technology

Algorithms and Data Structures, or

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Introduction to Algorithms I

Heap Order Property: Key stored at the parent is smaller or equal to the key stored at either child.

Greedy Algorithms 1 {K(S) K(S) C} For large values of d, brute force search is not feasible because there are 2 d {1,..., d}.

( ) 1 B. 1. Suppose f x

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Algorithms Interview Questions

Algorithms and Data Structures

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Algorithms and Data Structures

Algorithms and Data Structures

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Transcription:

Design and Analysis of Algorithms (I) Introduction Guoqiang Li School of Software, Shanghai Jiao Tong University

Instructor and Teaching Assistants Guoqiang LI

Instructor and Teaching Assistants Guoqiang LI Homepage: http://basics.sjtu.edu.cn/ liguoqiang Course page: http://basics.sjtu.edu.cn/ liguoqiang/teaching/galgo18/index.htm Email: li.g@outlook.com Office: Rm. 1212, Building of Software Phone: 3420-4167

Instructor and Teaching Assistants Guoqiang LI Homepage: http://basics.sjtu.edu.cn/ liguoqiang Course page: http://basics.sjtu.edu.cn/ liguoqiang/teaching/galgo18/index.htm Email: li.g@outlook.com Office: Rm. 1212, Building of Software Phone: 3420-4167 TA: Qizhe YANG: yangqizhe1994 (AT) hotmail (DOT) com Yuwei WANG: wangyuwei95 (AT) qq (DOT) com

Instructor and Teaching Assistants Guoqiang LI Homepage: http://basics.sjtu.edu.cn/ liguoqiang Course page: http://basics.sjtu.edu.cn/ liguoqiang/teaching/galgo18/index.htm Email: li.g@outlook.com Office: Rm. 1212, Building of Software Phone: 3420-4167 TA: Qizhe YANG: yangqizhe1994 (AT) hotmail (DOT) com Yuwei WANG: wangyuwei95 (AT) qq (DOT) com Office hour: Wed. 14:00-17:00 @ SEIEE 3-327 & Software Building 3203

Notification

Notification Students who take this lecture are assumed to have a solid background of algorithms.

Notification Students who take this lecture are assumed to have a solid background of algorithms. Theory of Algorithms Algorithms

Algorithm Design

Basic methodologies: Algorithm Design Algorithms on Lists, Trees and Graphs

Basic methodologies: Algorithm Design Algorithms on Lists, Trees and Graphs Divide and Conquer

Basic methodologies: Algorithm Design Algorithms on Lists, Trees and Graphs Divide and Conquer Master Theorem Recursion

Basic methodologies: Algorithm Design Algorithms on Lists, Trees and Graphs Divide and Conquer Master Theorem Recursion Advanced topics: Dynamic Programming Greedy Algorithm Linear Programming Approximation Algorithm Randomized Algorithm Bigdata Algorithms

Algorithm Analysis

Big-O Notation Algorithm Analysis

Algorithm Analysis Big-O Notation Advanced Methodology:

Algorithm Analysis Big-O Notation Advanced Methodology: Probability Analysis

Algorithm Analysis Big-O Notation Advanced Methodology: Probability Analysis Amortized Analysis

Algorithm Analysis Big-O Notation Advanced Methodology: Probability Analysis Amortized Analysis Competition Analysis

Standard Algorithms

Sorting Standard Algorithms

Standard Algorithms Sorting Searching & Hashing

Standard Algorithms Sorting Searching & Hashing Strongly connected components

Standard Algorithms Sorting Searching & Hashing Strongly connected components Finding shortest paths in graphs

Standard Algorithms Sorting Searching & Hashing Strongly connected components Finding shortest paths in graphs Minimum spanning trees in graphs

Standard Algorithms Sorting Searching & Hashing Strongly connected components Finding shortest paths in graphs Minimum spanning trees in graphs Matchings in bipartite graphs

Standard Algorithms Sorting Searching & Hashing Strongly connected components Finding shortest paths in graphs Minimum spanning trees in graphs Matchings in bipartite graphs Maximum flows in networks

Data Structure

Data Structure Link lists Trees, graphs

Data Structure Link lists Trees, graphs Kripke structure, automata

Data Structure Link lists Trees, graphs Kripke structure, automata Priority queue

Data Structure Link lists Trees, graphs Kripke structure, automata Priority queue Disjoint set

Reference Book Algorithms Sanjoy Dasgupta San Diego Christos Papadimitriou Umesh Vazirani McGraw-Hill, 2007.

Reference Book Introduction to Algorithms Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein The MIT Press (3rd edition), 2009.

Approximation Algorithms Vijay V. Vazirani Springer-Verlag, 2004 Reference Book

Reference Book The Design of Approximation Algorithms David P. Williamson David B. Shmoys Cambridge University Press, 2011.

Reference Book Randomized Algorithms Rajeev Motwani, Prabhakar Raghavan Cambridge University Press, 1995.

Scoring Policy 10% Attendance. 20% Homework. 70% Final exam.

Scoring Policy 10% Attendance. 20% Homework. Four assignments. 70% Final exam.

Scoring Policy 10% Attendance. 20% Homework. Four assignments. Each one is 5pts. 70% Final exam.

Scoring Policy 10% Attendance. 20% Homework. Four assignments. Each one is 5pts. Work out individually. 70% Final exam.

Scoring Policy 10% Attendance. 20% Homework. Four assignments. Each one is 5pts. Work out individually. Each assignment will be evaluated by A, B, C, D, F (Excellent(5), Good(5), Fair(4), Delay(3), Fail(0)) 70% Final exam.

Any Questions?

Typical Algorithms

Sorting

Sorting Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,..., a n of the input sequence such that a 1 a 2... a n

Various Sorts

Insert Sort Various Sorts

Various Sorts Insert Sort Bubble Sort

Various Sorts Insert Sort Bubble Sort Heap Sort

Various Sorts Insert Sort Bubble Sort Heap Sort Quick Sort

Various Sorts Insert Sort Bubble Sort Heap Sort Quick Sort Merge Sort

Merge Sort

The Algorithm MERGESORT(a[1... n]) An array of numbers a[1... n]; if n > 1 then return(merge(mergesort(a[1... n/2 ]), MERGESORT(a[ n/2..., n]))); else return(a); end

The Algorithm MERGESORT(a[1... n]) An array of numbers a[1... n]; if n > 1 then return(merge(mergesort(a[1... n/2 ]), MERGESORT(a[ n/2..., n]))); else return(a); end MERGE(x[1... k], y[1... l]) if k = 0 then return y[1... l]; if l = 0 then return x[1... k]; if x[1] y[1] then return( x[1] MERGE(x[2... k], y[1... l])); else return( y[1] MERGE(x[1... k], y[2... l])); end

An Iterative Version ITERTIVE-MERGESORT(a[1... n]) An array of numbers a[1... n]; Q = [ ]empty queue; for i = 1 to n do Inject(Q, [a]); end while Q > 1 do Inject (Q,MERGE (Eject (Q),Eject (Q))); end return(eject (Q));

The Time Analysis

The Time Analysis The recurrence relation: T(n) = 2T(n/2) + O(n)

The Time Analysis The recurrence relation: T(n) = 2T(n/2) + O(n) By Master Theorem: T(n) = O(n log n)

Master Theorem If T(n) = at( n/b ) + O(n d ) for some constants a > 0, b > 1 and d 0, then O(n d ) if d > log b a T(n) = O(n d log n) if d = log b a O(n log b a ) if d < log b a

Can we do better?

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 A sorting algorithm can be depicted as a decision tree.

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 A sorting algorithm can be depicted as a decision tree. The depth of the tree - the number of comparisons on the longest path from root to leaf, is the worst-case time complexity of the algorithm.

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 A sorting algorithm can be depicted as a decision tree. The depth of the tree - the number of comparisons on the longest path from root to leaf, is the worst-case time complexity of the algorithm. Assume n elements. Each of its leaves is labeled by a permutation of {1, 2,..., n}.

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 Every permutation must appear as the label of a leaf.

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 Every permutation must appear as the label of a leaf. This is a binary tree with n! leaves.

Sorting No a 1 < a 2? Yes a 1 < a 3? a 2 < a 3? a 2 < a 3? 2 1 3 a 1 < a 3? 1 2 3 3 2 1 2 3 1 3 1 2 1 3 2 Every permutation must appear as the label of a leaf. This is a binary tree with n! leaves. So,the depth of the tree - and the complexity of the algorithm - must be at least log(n!) log( π(2n + 1/3) n n e n ) = Ω(n log n)

Shortest Path in Graph

Lengths on Edges BFS treats all edges as having the same length. San Francisco 95 Sacramento 133 Reno 290 271 409 Bakersfield 445 112 291 Los Angeles 275 Las Vegas

Lengths on Edges BFS treats all edges as having the same length. In reality the unit lengths always do not exist. San Francisco 95 290 Sacramento 271 133 Reno 409 Bakersfield 445 112 291 Los Angeles 275 Las Vegas

Lengths on Edges BFS treats all edges as having the same length. In reality the unit lengths always do not exist. San Francisco 95 290 Sacramento 271 133 Reno Every edge e E with a length l e. 409 Bakersfield 445 112 291 Los Angeles 275 Las Vegas

Lengths on Edges BFS treats all edges as having the same length. In reality the unit lengths always do not exist. Every edge e E with a length l e. If e = (u, v), we will sometimes also write l(u, v) or l uv San Francisco 409 Los Angeles 95 290 Sacramento 112 Bakersfield 275 271 133 291 Reno 445 Las Vegas

An Adaption of Breadth-First Search BFS finds shortest paths in any graph whose edges have unit length.

An Adaption of Breadth-First Search BFS finds shortest paths in any graph whose edges have unit length. Q: Can we adapt it to a more general graph G = (V, E) whose edge lengths l e are positive integers?

An Adaption of Breadth-First Search BFS finds shortest paths in any graph whose edges have unit length. Q: Can we adapt it to a more general graph G = (V, E) whose edge lengths l e are positive integers? A simple trick: For any edge e = (u, v) of E, replace it by l e edges of length 1, by adding l e 1 dummy nodes between u and v. It might take time O( V + l e ) e E

An Adaption of Breadth-First Search BFS finds shortest paths in any graph whose edges have unit length. Q: Can we adapt it to a more general graph G = (V, E) whose edge lengths l e are positive integers? A simple trick: For any edge e = (u, v) of E, replace it by l e edges of length 1, by adding l e 1 dummy nodes between u and v. It might take time O( V + l e ) e E It is bad in case we have edges with high length.

0 0 1 0 5 0 0 2 Alarm Clocks G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node u. Then: G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node u. Then: The distance from s to u is T. G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node u. Then: The distance from s to u is T. For each neighbor v of u in G: G: G :

0 0 1 0 5 0 0 2 Alarm Clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node u. Then: The distance from s to u is T. For each neighbor v of u in G: If there is no alarm yet for v, set one for time T + l(u, v). If v s alarm is set for later than T + l(u, v), then reset it to this earlier time. G: G :

An Example

An Example A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: B: 4 E: C: 2

An Example A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: B: 4 E: C: 2 A 4 2 B C 2 5 4 D A: 0 D: 6 1 3 1 B: 3 E: 7 3 C: 2 E

An Example A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: B: 4 E: C: 2 A 4 2 B C 2 4 D A: 0 D: 6 1 3 1 B: 3 E: 7 3 C: 2 5 E A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: 5 B: 3 E: 6 C: 2

An Example A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: B: 4 E: C: 2 A 4 2 B C 2 4 D A: 0 D: 6 1 3 1 B: 3 E: 7 3 C: 2 5 E A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: 5 B: 3 E: 6 C: 2 A 4 2 B 1 3 C 2 5 D 4 A: 0 D: 5 1 B: 3 E: 6 3 C: 2 E

An Example A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: B: 4 E: C: 2 A 4 2 B C 2 4 D A: 0 D: 6 1 3 1 B: 3 E: 7 3 C: 2 5 E A 4 2 B 1 3 C 2 5 4 3 D E 1 A: 0 D: 5 B: 3 E: 6 C: 2 A 4 2 B 1 3 C 2 5 D 4 A: 0 D: 5 1 B: 3 E: 6 3 C: 2 E B 2 D A 1 3 2 C E

Dijkstra s Shortest-Path Algorithm DIJKSTRA(G, l, s) input : Graph G = (V, E), directed or undirected; positive edge length {l e e E}; Vertex s V output: For all vertices u reachable from s, dist(u) is the set to the distance from s to u for all u V do dist(u) = ; prev(u) = nil; end dist(s) = 0; H =makequeue(v)\\ using dist-values as keys; while H is not empty do u=deletemin(h); for all edge (u, v) E do if dist(v) > dist(u) + l(u, v) then dist(v) = dist(u) + l(u, v); prev(v) = u; decreasekey (H,v); end end end

Priority Queue Priority queue is a data structure usually implemented by heap.

Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set.

Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set. Decrease-key: Accommodate the decrease in key value of a particular element.

Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set. Decrease-key: Accommodate the decrease in key value of a particular element. Delete-min: Return the element with the smallest key, and remove it from the set.

Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set. Decrease-key: Accommodate the decrease in key value of a particular element. Delete-min: Return the element with the smallest key, and remove it from the set. Make-queue: Build a priority queue out of the given elements, with the given key values. (In many implementations, this is significantly faster than inserting the elements one by one.)

Priority Queue Priority queue is a data structure usually implemented by heap. Insert: Add a new element to the set. Decrease-key: Accommodate the decrease in key value of a particular element. Delete-min: Return the element with the smallest key, and remove it from the set. Make-queue: Build a priority queue out of the given elements, with the given key values. (In many implementations, this is significantly faster than inserting the elements one by one.) The first two let us set alarms, and the third tells us which alarm is next to go off.

Running Time Since makequeue takes at most as long as V insert operations, we get a total of V deletemin and V + E insert/decreasekey operations.

Which Heap is Best Implementation deletemin insert/decreasekey Array O( V ) O(1) Binary heap O(log V ) O(log V ) d-ary heap d log V log V O( ) O( ) log d log d Fibonacci heap O(log V ) O(1) (amortized) V deletemin+( V + E ) insert O( V 2 ) O(( V + E ) log V ) (d V + E ) log V O( ) log d O( V log V + E )

Which heap is Best A naive array implementation gives a respectable time complexity of O( V 2 ), whereas with a binary heap we get O(( V + E ) log V ). Which is preferable?

Which heap is Best A naive array implementation gives a respectable time complexity of O( V 2 ), whereas with a binary heap we get O(( V + E ) log V ). Which is preferable? This depends on whether the graph is sparse or dense.

Which heap is Best A naive array implementation gives a respectable time complexity of O( V 2 ), whereas with a binary heap we get O(( V + E ) log V ). Which is preferable? This depends on whether the graph is sparse or dense. E is less than V 2. If it is Ω( V 2 ), then clearly the array implementation is the faster. On the other hand, the binary heap becomes preferable as soon as E dips below V 2 / log V. The d-ary heap is a generalization of the binary heap and leads to a running time that is a function of d. The optimal choice is d E / V ;

Edit Distance

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by.

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case?

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case? A natural measure of the distance between two strings is the extent to which they can be aligned, or matched up.

The problem When a spell checker encounters a possible misspelling, it looks in its dictionary for other words that are close by. Q: What is the appropriate notion of closeness in this case? A natural measure of the distance between two strings is the extent to which they can be aligned, or matched up. Technically, an alignment is simply a way of writing the strings one above the other. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

The problem The cost of an alignment is the number of columns in which the letters differ.

The problem The cost of an alignment is the number of columns in which the letters differ. The edit distance between two strings is the cost of their best possible alignment.

The problem The cost of an alignment is the number of columns in which the letters differ. The edit distance between two strings is the cost of their best possible alignment. Edit distance is so named because it can also be thought of as the minimum number of edits.

The problem The cost of an alignment is the number of columns in which the letters differ. The edit distance between two strings is the cost of their best possible alignment. Edit distance is so named because it can also be thought of as the minimum number of edits. Insertions, deletions, and substitutions of characters that needed to transform the first string into the second. S N O W Y S U N N Y Cost: 3 S N O W Y S U N N Y Cost: 5

An Example Edit distance between EXPONENTIAL and POLYNOMIAL, subproblem E(4, 3) corresponds to the prefixes EXPO and POL. The rightmost column of their best alignment must be one of the following: O or Thus, E(4, 3) = min{1 + E(3, 3), 1 + E(4, 2); 1 + E(3, 2)}. (a) i 1 i m j 1 j n GOAL L (b) or O L P O L Y N O M I A L 0 1 2 3 4 5 6 7 8 9 10 E 1 1 2 3 4 5 6 7 8 9 10 X 2 2 2 3 4 5 6 7 8 9 10 P 3 2 3 3 4 5 6 7 8 9 10 O 4 3 2 3 4 5 5 6 7 8 9 N 5 4 3 3 4 4 5 6 7 8 9 E 6 5 4 4 4 5 5 6 7 8 9 N 7 6 5 5 5 4 5 6 7 8 9 T 8 7 6 6 6 5 5 6 7 8 9 I 9 8 7 7 7 6 6 6 6 7 8 A 10 9 8 8 8 7 7 7 7 6 7 L 11 10 9 8 9 8 8 8 8 7 6

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is,

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems?

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n].

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let E(i, j): the edit distance between some prefix of the first string, x[1,..., i], and some prefix of the second, y[1,..., j].

A Dynamic Programming Solution When solving a problem by dynamic programming, the most crucial question is, What are the subproblems? Our goal is to find the edit distance between two strings x[1,..., m] and y[1,..., n]. For every i, j with 1 i m and 1 j n, let E(i, j): the edit distance between some prefix of the first string, x[1,..., i], and some prefix of the second, y[1,..., j]. E(i, j) = min{1+e(i 1, j), 1+E(i, j 1), diff(i, j)+e(i 1, j 1)}, where diff(i, j) is defined to be 0 if x[i] = y[j] and 1 otherwise.

The Algorithm for i = 0 to m do E(i, 0) = i; end for j = 1 to n do E(0, j) = j; end for i = 1 to m do for j = 1 to m do E(i, j) = min{1 + E(i 1, j), 1 + E(i, j 1), diff(i, j) + E(i 1, j 1)}; end end return(e(m, n));

The Algorithm for i = 0 to m do E(i, 0) = i; end for j = 1 to n do E(0, j) = j; end for i = 1 to m do for j = 1 to m do E(i, j) = min{1 + E(i 1, j), 1 + E(i, j 1), diff(i, j) + E(i 1, j 1)}; end end return(e(m, n)); The over running time is O(m n).

Then, What is An Algorithm?

What Is An Algorithm An algorithm is a procedure that consists of a finite set of instructions which, given an input from some set of possible inputs, enables us to obtain an output through a systematic execution of the instructions that terminates in a finite number of steps.

Not Enough, Maybe

What Is An Algorithm In these problems we are searching for a solution (path, tree, matching, etc.) from among an exponential population of possibilities.

What Is An Algorithm In these problems we are searching for a solution (path, tree, matching, etc.) from among an exponential population of possibilities. All these problems could in principle be solved in exponential time by checking through all candidate solutions, one by one.

What Is An Algorithm In these problems we are searching for a solution (path, tree, matching, etc.) from among an exponential population of possibilities. All these problems could in principle be solved in exponential time by checking through all candidate solutions, one by one. The quest for algorithms is about finding clever ways to bypass this process of exhaustive search, using clues from the input in order to dramatically narrow down the search space.

Is That ALL?

Lecture Agenda NP Problem Coping with NP Completeness Linear Programming Approximation Algorithms Randomized Algorithms Bigdata Algorithms

Referred Materials [DPV07] Algorithms [CLRS09] Introduction to Algorithms [Vaz04] Approximation Algorithms [WS11] The Design of Approximation Algorithms [MR95] Randomized Algorithms

Referred Materials [DPV07] Algorithms [CLRS09] Introduction to Algorithms [Vaz04] Approximation Algorithms [WS11] The Design of Approximation Algorithms [MR95] Randomized Algorithms Content of this lecture comes from section 2.3, 4.4, 4.5 and 6.3 in [DPV07]. Suggest to read Chapter 15 of [CLRS09] and Chapter 6 in [DPV07].