Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "Introduction to the Design and Analysis of Algorithms"— Presentation transcript:

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

2 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

3 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() }

4 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

5 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

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

7 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 st 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

8 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

9 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

10 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

11 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

12 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


Download ppt "Introduction to the Design and Analysis of Algorithms"

Similar presentations


Ads by Google