Interlude: Problem Solving

Similar documents
Jessica Su (some parts copied from CLRS / last quarter s notes)

lecture notes September 2, How to sort?

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

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

Principles of Algorithm Design

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

The vertex set is a finite nonempty set. The edge set may be empty, but otherwise its elements are two-element subsets of the vertex set.

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Midterm 1

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

CPSC 320 Midterm #1. February 4, 2015

Greedy Homework Problems

Lecture 20 : Trees DRAFT

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Lecture 3 February 9, 2010

Matchings, Ramsey Theory, And Other Graph Fun

Network Flow. November 23, CMPE 250 Graphs- Network Flow November 23, / 31

Math 3820 Project. 1 Typeset or handwritten? Guidelines

REU 2006 Discrete Math Lecture 5

CS 4349 Lecture October 18th, 2017

ISA 562: Information Security, Theory and Practice. Lecture 1

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

Kuratowski Notes , Fall 2005, Prof. Peter Shor Revised Fall 2007

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CPSC W1: Midterm #1 (Group Version) Thu

CPSC W1: Midterm #1

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

Induction Review. Graphs. EECS 310: Discrete Math Lecture 5 Graph Theory, Matching. Common Graphs. a set of edges or collection of two-elt subsets

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

CSE 417 Network Flows (pt 4) Min Cost Flows

4.7 Approximate Integration

Algorithm Design and Recursion. Search and Sort Algorithms

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Final Exam. Problem Parts Points Grade Grader Problem Parts Points Grade Grader Total 180. Name: Athena username: Joe WF3a. Joe WF2.

(x 2)(3x + 1) = 3x + 1

(Refer Slide Time: 01.26)

Lecture 25 Spanning Trees

A Random Walk through CS70

Network Flow I. Lecture Overview The Network Flow Problem

Copyright 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch.

CS 4349 Lecture August 21st, 2017

Lecture 1: Overview

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

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

CSE 417 Network Flows (pt 3) Modeling with Min Cuts

Discrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2

CS 170 Algorithms Spring 2009 David Wagner MT2

11.9 Connectivity Connected Components. mcs 2015/5/18 1:43 page 419 #427

Lecture 11: Maximum flow and minimum cut

Lecture 6: Graph Properties

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Relations I. Margaret M. Fleck. 7 November 2008

Shadows in the graphics pipeline

Math 4242 Polynomial Time algorithms, IndependentSet problem

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

Review for Midterm Exam

MC302 GRAPH THEORY Thursday, 10/24/13

CSC 325 Algorithms & Advanced Data Structures

Theorem 3.1 (Berge) A matching M in G is maximum if and only if there is no M- augmenting path.

Introduction to Algorithms December 16, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas

1 More on the Bellman-Ford Algorithm

MATH 682 Notes Combinatorics and Graph Theory II

Combinatorial Optimization

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Introduction to Programming

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

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Technical University of Denmark

Final Exam May 8, 2018

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

1 The Arthur-Merlin Story

Introduction to Algorithms 6.046J/18.401J/SMA5503

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Exercise 1: ER-Diagrams Due: November 5, Ross closing time

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

Introduction to Algorithms 6.046J/18.401J

LATIN SQUARES AND THEIR APPLICATION TO THE FEASIBLE SET FOR ASSIGNMENT PROBLEMS

Discrete Mathematics and Probability Theory Fall 2015 Rao Midterm 1

Practice Problems for the Final

14.1 Encoding for different models of computation

Outline: Search and Recursion (Ch13)

CSE-6490B Final Exam

The divide and conquer strategy has three basic parts. For a given problem of size n,

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

Lecture 11a: Maximum Matchings Lale Özkahya

Grade 7/8 Math Circles Graph Theory - Solutions October 13/14, 2015

Introduction to Algorithms

Problem Set 5 Solutions

Questions Total Points Score

Class Note #02. [Overall Information] [During the Lecture]

Treewidth and graph minors

Transcription:

Lecture 4 Interlude: Problem Solving Supplemental reading in CLRS: None This lecture was originally given as a pep talk before the take-home exam. In note form, this chapter will be light reading, a break in which we look back at the course material as veterans. 4. What to Bring to the Table In most technical undergraduate courses, questions are posed in such a way that all solutions are equally valid; if a student is able to come up with any solution to a given problem, she is considered to be in good shape. In 6.046, this is not the case. For most algorithmic problems, there is an obvious but inefficient solution; your task is to find a better solution. It is worth appreciating that this way of thinking may be new to you, and taking a moment to reflect metacognitively on strategies to help you solve algorithmic problems. There is no recipe for solving problems, but there are many things you can do which often, eventually, lead to good solutions. Knowledge. In academic research, one will usually pick up the relevant background material over the course of working on a given problem. In an exam setting, you should come to the table with as complete an understanding of the background material as possible; you will not have time to learn many new concepts. In either case, you should strive to understand the relevant background as deeply as possible. You should be able to implement a given algorithm correctly in your computer language of choice. You should be able to answer CLRS-style exercises about the pseudocode: What would be the effect on performance if we changed this line? Would the algorithm still be correct? What would be the effect on performance if we used a different data structure? Belief. Be optimistic. Remind yourself that you might solve this problem, even if it doesn t look tractable at first. Motivation. There is no substitute for a strong motivation to solve the problem at hand. Given that you re trying to learn algorithms from a set of lecture notes, I probably don t even need to tell you this.

4.2 How to Attack a Problem We ll now discuss several tips that might help you to crack a problem open. We ll keep the following concrete example in the back of our mind: Problem 4. (Bipartite Matching). In a group of n heterosexual people, each woman has a list of the men she is interested in marrying, and each man has a list of the women he is interested in marrying. Find an algorithm that creates the largest possible number of weddings. (Each person can marry at most one other person. In a couple, each member must be on the other member s list.) 4.2. Understand the Problem Note everything that is given. Find some upper bounds and lower bounds. For example: If the algorithm has to read the entire input, then the running time is at least Ω(n). (This is not always the case, however! For example, binary search runs in O(lg n) and does not read the entire input.) If the output has to output k things, then the running time is at least Ω(k). If the algorithm can be used to perform a comparison-based sort, then the running time is at least Ω(nlg n). There are usually a finite number of possibilities for the output. In the case of our current problem, arrange the men in a line (by alphabetical order of last name, say). For each man who has a wife, place his wife behind him. Place the unmarried women at the front of the line (by alphabetical order of last name, say). In this way, each possible matching gives a different line. Thus, the total number of possible matchings is at most n!, the number of ways to arrange all n people in a line. Usually there is an obvious algorithm that runs in exponential time. Sometimes it runs faster. Sometimes it is easy to reduce the problem to another problem whose solution is wellknown. (Be careful, though your special case may be amenable to a more efficient solution than the general problem!) Whatever your solution is, its running time must of course fall within the lower and upper bounds you find. Is there a useful diagram? A comparison-based sorting algorithm is a sorting algorithm which makes no assumptions about the items being sorted except that there is a well-defined notion of less than and that, for objects a and b, we can check whether a < b in constant time. The fact we are using here is that any comparison-based sorting algorithm takes at least Ω(nlg n) time. The proof is as follows. Suppose we have a comparison-based sorting algorithm A which takes as input a list L = a,..., a n and outputs a list of indices I = i,..., i n such that a i a in. Let k be the number of comparison operations performed by A on a worst-case input of length n (where k depends on n). Because each comparison is a yes-or-no question, there are at most 2 k possible sets of answers to the at most k comparison operations. Since the output of a comparison-based sorting algorithm must depend only on these answers, there are at most 2 k possible outputs I. Of course, if A is correct, then all n! permutations of the indices,..., n must be possible, so 2 k n!. Thus k lg(n!). Finally, Stirling s approximation tells us that lg(n!) = Θ(nlg n). Thus the running time of A is Ω(k) = Ω(nlg n). Lec 4 pg. 2 of 5

Is there a graph? Try examples! 4.2.2 Try to Solve the Problem Can we solve the problem under a set of simplifying assumptions? (What if every vertex has degree 2? What if the edge weights are all?) Are there useful subproblems? Will a greedy algorithm work? Divide-and-conquer? Dynamic programming? Can this problem be reduced to a known problem? Is it similar to a known problem? Can we modify the known algorithm to suit it to this version? Can the problem be solved with convolution? If so, the fast Fourier transform will improve efficiency. Is there any further way to take advantage of the given information? Can randomization help? It is often better to find a fast algorithm with a small probability of error than a slower, correct algorithm. Remember that a hard problem usually cannot be solved in one sitting. Taking breaks and changing perspective help. In the original setting of a pre take-home exam pep talk, it was important to remind students that incubation time is completely necessary and should be part of the exam schedule. Thus, students should start the test early the away-time when they are not actively trying to solve test questions is crucial. 4.2.3 Work Out the Details What is the algorithm? (Write the algorithm down in as much detail as possible it is easy to end up with an incorrect algorithm or an incorrect running time analysis because you forgot about a few of what seemed like unimportant implementation details.) Why is it correct? What is its asymptotic running time? 4.2.4 Communicate Your Findings Don t be shy about writing up partial solutions, solutions to simplified versions of the problem, or observations. This is the most common sort of writing that occurs in academia: often, the complete solution to a problem is just the finishing touch on a large body of prior work by other scientists. And even more often, no complete solution is found all we have are observations and solutions to certain tractable special cases. (In an exam setting, this sort of writing could win you significant partial credit.) Lec 4 pg. 3 of 5

To be especially easy on your reader, you can format your algorithm description as an essay, complete with diagrams and examples. The essay should begin with a high-level executive summary of how the algorithm works. Now that you have been thinking about the bipartite matching problem for a while, here is a solution: Algorithm: Let M be the set of men and let W be the set of women; let s and t be objects representing a source and a sink. Create a bipartite directed graph G with vertex set M W {s, t}. Draw an edge from s to each man and an edge from each woman to t. Next, for each man m and each woman w, draw an edge from m to w if m is on w s list and w is on m s list. Give all edges capacity. The graph G is now a flow network with source s and sink t (see Figure 4.). Note that there is a bijection between valid matchings and integer flows in G. In one direction, given a matching, we can saturate each edge between a man and a woman who have been matched. The flows out of s and into t are then uniquely determined. In the other direction, given an integer flow f in G, the fact that each edge out of s or into t has capacity means that there is at most one edge out of each man and at most one edge into each woman. Thus, we obtain a valid matching by wedding a man m to a woman w if and only if f (m, w) =. The magnitude of f is equal to the number of couples, so the number of couples in any matching is bounded above by the maximum flow in G. This bound is tight if and only if there exists a maximum flow f in G which is an integer flow. Such a flow does exist, and is found by the Ford Fulkerson algorithm, by Proposition 3.3. Since the number of couples is at most O(n), we have f = O(n), and the running time of the Ford Fulkerson algorithm is O (E f ) = O ( n 2 n ) = O ( n 3). Actually, assuming each person has a relatively small list of acceptable spouses, we ought to be more granular about this bound: we have and the running time is at most E = O T = O ( ( n n + ( n + n i= ( ) ) size of the ith, person s list n i= ( ) )) size of the ith. person s list A graph G = (V, E) is called bipartite if the vertex set V can be written as the union of two disjoint sets V = V V 2 such that every edge in E has one endpoint in V and one endpoint in V 2. 4.2.5 Reflect and Improve Can we achieve a better running time? 2 2 For the bipartite matching problem, there exist better algorithms than the one given above. Still, I love this solution. It shows how useful flow networks are, even in contexts that seem to have nothing to do with flow. There are many more examples in which this phenomenon occurs. Lec 4 pg. 4 of 5

s t Figure 4.. In this flow network, the nodes on the left (other than s) represent women and the nodes on the right (other than t) represent men. All edges have capacity. A maximum flow is highlighted in black; it shows that the maximal number of couples is 3. Can randomization help? Can amortization help? Can we use less space? 4.3 Recommended Reading There are many great references and exercise books for algorithmic problem solving and mathematical problem solving in general. Two particular selections are: George Pólya, How to Solve It The Princeton Companion to Mathematics, VIII.: The Art of Problem Solving. Another good source of algorithms practice is web sites designed to help people prepare for technical interviews. It is common for tech companies to ask algorithm questions to job applications during interviews; students who plan to apply for software jobs may especially want to practice. Even for those who aren t looking for jobs, web sites like TechInterview.org can be a useful source of challenging algorthmic exercises. Lec 4 pg. 5 of 5

MIT OpenCourseWare http://ocw.mit.edu 6.046J / 8.40J Design and Analysis of Algorithms Spring 202 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.