Introduction to the Design and Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Stable Matching Problems with Constant Length Preference Lists Rob Irving, David Manlove, Gregg OMalley University Of Glasgow Department of Computing Science.
Advertisements

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 10 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Complexity 15-1 Complexity Andrei Bulatov Hierarchy Theorem.
Chapter 26 of CLSR Bipartite Matching By Dr. M. Sakalli, Sources: Levitin and many other CSE, Marmara Univ. May/2009.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Chapter 11: Limitations of Algorithmic Power
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
CSE 421 Algorithms Richard Anderson Lecture 3. Classroom Presenter Project Understand how to use Pen Computing to support classroom instruction Writing.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
The Stable Marriage Problem
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Chapter 10 IterativeImprovement. Iterative Improvement Algorithm design technique for solving optimization problems b Start with a feasible solution b.
1 The Stable Marriage Problem Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij.
CSC310 © Tom Briggs Shippensburg University Fundamentals of the Analysis of Algorithm Efficiency Chapter 2.
Graph Algorithms Maximum Flow - Best algorithms [Adapted from R.Solis-Oba]
Chapter 10 IterativeImprovement Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSC 421: Algorithm Design & Analysis
Matching Boys Girls A B C D E
Cybernetica AS & Tartu University
Stable Matching.
TK3043 Analysis and Design of Algorithms
Lower bound for the Stable Marriage Problem
Stable Marriage Problem
Chapter 10 Iterative Improvement
Introduction to the Design and Analysis of Algorithms
UNIT- I Problem solving and Algorithmic Analysis
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Introduction to the Design and Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Maximum Flow - Best algorithms
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
CSE 421: Introduction to Algorithms
Computation.
CS 3343: Analysis of Algorithms
Lecture 7 CSE 331 Sep 15, 2010.
Lecture 6 CSE 331 Sep 11, 2017.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Foundations II: Data Structures and Algorithms
S. Raskhodnikova; based on slides by K. Wayne
Richard Anderson Lecture 13 Divide and Conquer
Lecture 9 CSE 331 Sep 19, 2012.
CSE 421: Introduction to Algorithms
Discrete Math for CS CMPSC 360 LECTURE 9 Last time: Strong induction
CS200: Algorithms Analysis
3. Brute Force Selection sort Brute-Force string matching
Blah blah blah.
Chapter 1 Introduction: Some Representative Problems
Algorithms: the big picture
3. Brute Force Selection sort Brute-Force string matching
Bipartite Matching By Dr. M
Lecture 10 CSE 331 Sep 21, 2012.
Lecture 8 CSE 331 Sep 15, 2011.
Lecture 9 CSE 331 Sep 15, 2014.
Lecture 6 CSE 331 Sep 12, 2016.
Lecture 7 CSE 331 Sep 10, 2014.
Piyush Kumar (Lecture 3: Stable Marriage)
Lecture 7 CSE 331 Sep 11, 2013.
15th Scandinavian Workshop on Algorithm Theory
Introduction to Algorithms
The Selection Problem.
CSC 421: Algorithm Design & Analysis
Richard Anderson Lecture 14 Divide and Conquer
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
3. Brute Force Selection sort Brute-Force string matching
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Presentation transcript:

Introduction to the Design and Analysis of Algorithms ElSayed Badr Benha University September 20, 2016

Answer Model Midterm 1 Sieve of Eratosthenes is an algorithm to (A) The greatest common divisor (B) Prime numbers (C) The closest pair of points in plane (D) The shortest path in a graph (E) The inverse of a matrix 2 Pseudocode may be defined as a) Procedural solutions to problems b) A collection of connected geometric shapes containing a description of the algorithms steps c) A mixture of natural language and programming language like constructs d) A general approach to solving problems algorithmically e) Precise machine-readable description of an algorithm 3 Algorithms that require ____________ number of operations are practical for solving only problems of very small size. a) polynomial b) constant c) logarithmic d) linear e) exponential 4 A function t(n) is said to be in O(g(n)) if t(n) a) is bounded above by g(n) for all large n b) is bounded above by some constant multiple of g(n) for all large n c) is bounded both above and below by some constant multiples of g(n) d) is bounded below by some constant multiple of g(n) for all large n e) is bounded above by some function of g(n) 5 Brute force strategy of designing algorithms relies (depends) on using a) the problem statements and definitions directly b) solution of a smaller instance of the same problem c) the combined solutions of smaller sub problems d) the solution to a simpler instance of the same problem e) the solution of an instance from a different problem Answer Model Midterm

bool anagrams(string s, string t) { return s.sort() == t.sort() } B) (5 points) (Anagram checking). Design an algorithm for checking whether two given words are anagrams, i.e., whether one word can be obtained by permuting the letters of the other. For example, the words tea and eat are anagrams. Solution: Method 1: Sort both the strings and then compare the strings. Time Complexity is O(nlogn). bool anagrams(string s, string t) { return s.sort() == t.sort() }

Q2) (5 points) A) Consider the following algorithm: ALGORITHM ComparisonCountingSort (A[0..n − 1]) // Sorts an array by comparison counting // Input: Array A [0..n − 1] of orderable values // Output: Array S [0..n − 1] of A’s elements sorted in nondecreasing order for i ← 0 to n − 1 do Count[i]← 0 for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do if A[i]< A[j ] Count[j ]← Count[j ]+ 1 else Count[i]← Count[i]+ 1 for i ←0 to n − 1 do S[Count[i]]← A[i] return S a. Apply this algorithm to sorting the list 60, 35, 81, 98, 14, 47. b. Prove that its complexity = O( n2 ) ? Solution: a) b) A 60 35 81 98 14 47 COUNT 3 2 4 5 S

B): (5 points)  Design an algorithm that reads as its inputs the (x, y) coordinates of the endpoints of two line segments P1Q1 and P2Q2 and determines whether the segments have a common point. Solution: The main idea : Two segments ab and cd intersect if and only if a) the endpoints a and b are on opposite sides of the line and b) the endpoints c and d are on opposite sides of the line   Intersect(a; b; c; d): if CCW(a; c; d) = CCW(b; c; d) return False else if CCW(a; b; c) = CCW(a; b; d) else return True

Stable Marriage Problem There is a set Y = {m1,…,mn} of n men and a set X = {w1,…,wn} of n women. Each man has a ranking list of the women, and each woman has a ranking list of the men (with no ties in these lists). A marriage matching M is a set of n pairs (mi, wj). A pair (m, w) is said to be a blocking pair for matching M if man m and woman w are not matched in M but prefer each other to their mates in M. A marriage matching M is called stable if there is no blocking pair for it; otherwise, it’s called unstable. The stable marriage problem is to find a stable marriage matching for men’s and women’s given preferences.

Instance of the Stable Marriage Problem An instance of the stable marriage problem can be specified either by two sets of preference lists or by a ranking matrix, as in the example below. men’s preferences women’s preferences 1st 2nd 3rd 1st 2nd 3rd Bob: Lea Ann Sue Ann: Jim Tom Bob Jim: Lea Sue Ann Lea: Tom Bob Jim Tom: Sue Lea Ann Sue: Jim Tom Bob ranking matrix Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 2,1 1,2 {(Bob, Ann) (Jim, Lea) (Tom, Sue)} is unstable {(Bob, Ann) (Jim, Sue) (Tom, Lea)} is stable

Stable Marriage Algorithm (Gale-Shapley) Step 0 Start with all the men and women being free Step 1 While there are free men, arbitrarily select one of them and do the following: Proposal The selected free man m proposes to w, the next woman on his preference list Response If w is free, she accepts the proposal to be matched with m. If she is not free, she compares m with her current mate. If she prefers m to him, she accepts m’s proposal, making her former mate free; otherwise, she simply rejects m’s proposal, leaving m free Step 2 Return the set of n matched pairs

Example Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Bob proposed to Lea Lea accepted Free men: Bob, Jim, Tom Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Free men: Jim, Tom Jim proposed to Lea Lea rejected

Example (cont.) Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Jim proposed to Sue Sue accepted Free men: Jim, Tom Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Free men: Tom Tom proposed to Sue Sue rejected

Example (cont.) Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Tom proposed to Lea Lea replaced Bob with Tom Free men: Tom Ann Lea Sue Bob 2,3 1,2 3,3 Jim 3,1 1,3 2,1 Tom 3,2 Free men: Bob Bob proposed to Ann Ann accepted

Analysis of the Gale-Shapley Algorithm The algorithm terminates after no more than n2 iterations with a stable marriage output The stable matching produced by the algorithm is always man-optimal: each man gets the highest rank woman on his list under any stable marriage. One can obtain the woman-optimal matching by making women propose to men A man (woman) optimal matching is unique for a given set of participant preferences The stable marriage problem has practical applications such as matching medical-school graduates with hospitals for residency training