Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity

Size: px
Start display at page:

Download "Tutorial Session 5. Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1. Running Time Analysis, Asymptotic Complexity"

Transcription

1 Tutorial Session 5 Tuesday, 19 th of March 2019 Discussion of Exercise 4, Preview on Exercise 5, Preparation for Midterm 1 Running Time Analysis, Asymptotic Complexity / BIN 0.B.06

2 Agenda Outlook on Midterm 1 Discussion of Exercise 4 Midterm Preparation Preview on Exercise 5 AINF1169 Informatics II 2

3 Outlook on Midterm 1 General Information What to Expect Exam Strategy Remarks on the Cheat Sheet

4 Date and Location Date, time, location: Hints: Friday, March 22 nd, h 90 minutes exam time in HAH-E-03 and HAH-F-01 (both at Häldeliweg 2, at Zentrum) seating plan will be published on OLAT Don t forget your student ID (Legi). Either this midterm or the next midterm will count 25% towards your final grade. Things will probably be significantly more relaxed if you score a good grade now Failure to show up leads to grade 1 in this midterm. AINF1169 Informatics II 4

5 General Information: Topics and Allowed Auxiliary Materials Topics covered: SL01 to SL03 complete Allowed auxiliary materials: one A4 sheet (both-sided), notes can be handwritten or printed or photocopied pocket calculator with no communication capabilities and no storage / memory, e.g. TI-30 XII B/S AINF1169 Informatics II 5

6 What to Expect There might (or might not) be some changes to the style of the midterm when compared to earlier years since the midterms now count towards the final grade and are no longer graded on a pass/fail basis. I expect the passing grade threshold (grade 4.0) to remain at about 50% of reachable points and I also expect the amount of tasks to stay about the same, i.e. a total 60 points, distributed on 3 to 4 exercises worth 10 to 20 points each. Standard tasks of midterm 1 include: Asymptotic complexity and recurrences Running time analysis Recursion / Divide and conquer algorithm Have a look at previous year s midterms which are available on the tutorial web page. Also, old assignments with solutions are available on the tutorial web page and can be used for additional practice. AINF1169 Informatics II 6

7 What to Expect Some data from midterm 1 of previous semesters (numbers without no-shows, maximum 60 points each): Semester Pass Fail Median points FS % FS % FS % % % % Average points Almost full point range was used in previous years. Points to pass Points distribution in midterm 1 of FS 2018: number of participants points reached AINF1169 Informatics II 7

8 Exam Strategy Make sure, you always at least write something. Note down observations and thoughts for example. If you struggle with formal notation, use natural language additionally (or alternatively). Start with tasks which you feel comfortable to solve. Never give up. AINF1169 Informatics II 8

9 Remarks on the Cheat Sheet The cheat sheet can be a nice help. It saves you from learning stupid things by heart. It may protect from «blackout situations» where you suddenly can t remember things that you actually know. Although: The cheat sheet is useless and can t save you, if you don t understand the content and did not practice its application. Preparing the cheat sheet may be a good preparation and repetition. I suggest, though, to not spend too much time into preparing the perfect «God cheat sheet» solving example tasks (e.g. old midterms and assignments) is far more important and rewarding in my opinion. AINF1169 Informatics II 9

10 Last Minute Q&A Session For people who want to ask questions, I will be available this Friday, 22 rd of March from h here in BIN 0.B.06 (until about at the latest) AINF1169 Informatics II 10

11 Review of Exercise 4 Task 1: Ternary Search Visualization Task 2: Ternary Search Implementation Task 3: Ternary Search Analysis Task 4: Towers of Hanoi: Recurrence Relation Task 5: Towers of Hanoi: Implementation Task 6: Recursion Tree & Substitution Method Task 7: Master Method

12 Exercise 4 Task 1: Ternary Search Task Description: «Based on the three way max finder description, draw a tree to illustrate the process of finding the array s maximum value, given A = [24, 7, -18, 9, -7, -6, 15, 2, 1]. Also write the recurrence relation for this three way max finder algorithm.» left boundary right boundary A = AINF1169 Informatics II 12

13 Exercise 4 Task 1: Ternary Search Visualizaion AINF1169 Informatics II 13

14 Exercise 4 Task 2: Ternary Search: Implementation left boundary right boundary current maximum value base case as long as boundaries are valid, i.e. r might not be smaller than l if the left and right boundary have met (single remaining element) compare value of single remaining element to current maximum value calculate boundary indices for subarrays recursive calls on subarrays AINF1169 Informatics II 14

15 Exercise 4 Task 2: Ternary Search: Implementation in C int ternarymaxsearch(int A[], int l, int r, int k) { int mid1, mid2; int val, max1, max2, max3; if (r >= l) { if (l == r) { if (A[l] > k) { return A[l]; } else { return k; } } } } mid1 = l + (r - l) * 1 / 3; mid2 = l + (r - l) * 2 / 3; max1 = ternarymaxsearch(a, l, mid1, k); max2 = ternarymaxsearch(a, mid1 + 1, mid2, k); max3 = ternarymaxsearch(a, mid2 + 1, r, k); return max(max1, max2, max3); return -1; AINF1169 Informatics II 15

16 Exercise 4 Task 3: Ternary Search: Analysis The recurrence relation describing the ternary search is: T(n) = 3 T(n/3) + 3 What would be the recurrence relation if the algorithm would split the input array into four partitions instead of three? How would this affect the asymptotic complexity of the algorithm? New recurrence relation: T(n) = 4 T(n/4) + 4 No change in asymptotic complexity: remains Ο(n). AINF1169 Informatics II 16

17 Exercise 4 Task 4: Towers of Hanoi: Example with Four Disks Let s number the disks of the tower from 1 to N, starting with the smallest disk and let s denote the three places as A, B and C. Thus the starting configuration looks like this: place A place B place C We now are given the task to move the four disks from place A to place C without having a disk with a higher number above a disk with a lower number at any point in time. AINF1169 Informatics II 17

18 Exercise 4 Task 4: Towers of Hanoi: Example with Four Disks The problem of shifting four disks from place A to place C can be solved in three steps as follows: place A place B place C Move the tower consisting of disks 1 to 3 to place B 4 place A place B place C Move disk 4 to place C place A place B place C Move the tower consisting of disks 1 to 3 to place C place A place B 4 place C So far so good, but now we need a way to move a tower of three disks from one position to another How can we solve this? By applying the same strategy recursively! AINF1169 Informatics II 18

19 Exercise 4 Task 4: Towers of Hanoi: Generalized Recursive Approach From this, we have a generalized, recursive solution to move a tower with k disks (i.e. disks from 1 to k) from place A to place C: 1) Move the tower with disks 1 to k 1 from place A to place B 2) Move disk k (biggest disk of the tower of size k) on place C (only remaining valid place) 3) Move the tower with disks 1 to k 1 from place B to place C Steps 1) and 3) are applied recursively. The base case is reached when the tasks has reduced to shifting a tower of size 1, i.e. shifting a single disk. AINF1169 Informatics II 19

20 Exercise 4 Task 4: Towers of Hanoi: Example with Five Disks Graphical depiction of the recursive calls (recursion tree) for the example of five disks: Move tower with 5 disks Move tower with 4 disks Move disk number 5 Move tower with 4 disks Move tower with 3 disks Move disk number 4 Move tower with 3 disks Move tower with 2 disks Move disk number 3 Move tower with 2 disks Move disk number 1 (base case) Move disk number 1 (base case) AINF1169 Informatics II 20

21 Exercise 4 Task 4: Towers of Hanoi: Pseudocode AINF1169 Informatics II 21

22 Exercise 4 Task 4: Towers of Hanoi: C Code Implementation Possible implementation of a program which prints a sequence of movements which will solve the Towers of Hanoi problem: void printhanoioperations(int numberofdisks, char fromplace, char viaplace, char toplace) { if (numberofdisks == 1) { printf("%c -> %c\n", fromplace, toplace); } else { printhanoioperations(numberofdisks - 1, fromplace, toplace, viaplace); printhanoioperations(1, fromplace, viaplace, toplace); printhanoioperations(numberofdisks - 1, viaplace, fromplace, toplace); } } AINF1169 Informatics II 22

23 Exercise 4 Task 4: Towers of Hanoi: Recurrence Relation Thus, the recursive solution to the Towers of Hanoi problem can be modeled by the following recurrence relation: T(n) = 2 T(n 1) + 1 This recurrence can be solved by repeated backward substitution: Therefore, this recurrence and hence the running time of the algorithm to solve the Towers of Hanoi problem is in Θ(2 n ). AINF1169 Informatics II 23

24 Exercise 4 Task 6a: Solving a Recurrence Using Recursion Tree The branches of the recursion tree are longer on the very left side (where n gets always divided by 3). To estimate the total work in its nodes, we will assume the tree has the height of the leftmost branch everywhere and this will yield an upper bound for the total work. On the left side, the tree grows until h = log 3 (n). Therefore an upper bound for the total work is: log 3 (n) T(n) < n i = i AINF1169 Informatics II 24

25 Exercise 4 Task 6a: More Detailed Calculation for Guess h The sum 11 is a finite geometric series, i.e. a sum of form 11 0 a where a = and x = h = log 3 (n). 18 k 18 0 i = 0 1 a x+1 We know that the geometric series sums up to. 1 a Thus: T(n) < n With h = log 3 (n) we get: 0 i h i = = n h + 1 = n T(n) < n 2 h bb = = n log 0 1 n bb = 0 n h+1 i x k = Since log 3 (11/18) 0.45, n 1+log 3(11/18) n 0.55 which is asymptotically smaller than the linear term of the first summand. Therefore, T(n) is in Ο(n). h n n log n 0 AINF1169 Informatics II 25 h+1 h + 1 bb 0 = = 0 n 7 h+1 log 3 (n) bb 11 1 = 18 0 n 1 + log

26 Exercise 4 Task 6b: Proofing a Recurrence Using Substitution AINF1169 Informatics II 26

27 Exercise 4 Task 7: Master Method a) T(n) = 16 T(n/4) + 6n 2 a = 16, b = 4, f(n) = 6n 2 x = log b (a) = log 4 (16) = 2; n x = n log₄(16) = n 2 compare n x to f(n): same growth rate case 2 T(n) = Θ(n 2 log(n)) b) T(n) = T(n/2) + 1 a = 1, b = 2, f(n) = 1 x = log b (a) = log 2 (1) = 0; n x = n 0 = 1 compare n x to f(n): same growth rate case 2 T(n) = Θ(log(n)) AINF1169 Informatics II 27

28 Exercise 4 Task 7: Master Method c) T(n) = 4 T(n/2) + 2n a = 4, b = 2, f(n) = 2n x = log b (a) = log 2 (4) = 2; n x = n log₄(4) = n 2 compare n x to f(n): n x grows faster case 1 T(n) = Θ(n 2 ) d) T(n) = 2 T(n 2) + n master method not applicable: parsing fails applying repeated substitution yields: T(n) = Ο(n sqrt(2) n ) AINF1169 Informatics II 28

29 Exercise 4 Task 7: Master Method e) T(n) = 2 T(n/4) + n lg(n) a = 2, b = 4, f(n) = n log 2 (n) x = log 4 (2) = log 4 (2) = 0.5; n x = n log₄(2) = n 0.5 = sqrt(n) compare n x to f(n): f(n) grows faster case 3 Regularity check: we need to find a constant c < 0 such that a f(n/b) c f(n) for any n > n 0. a f(n/b) = 2 f(n/4) = 2 n/4 log 2 (n/4) = 0.5 n (log 2 (n) log 2 (4)) = 0.5 n (log 2 (n) 2) = = 0.5 n log 2 (n) n Thus we re looking for a c which fulfills: 0.5 n log 2 (n) n c n log 2 (n) Obviously, the above equation is certainly true for any c 0.5 and any n > 1 = n 0. T(n) = Θ(n log(n)) AINF1169 Informatics II 29

30 Exercise 4 Task 7: Master Method d) T(n) = 2 T(n 2) + n master method not applicable: parsing fails; repeated substitution yields: T(n) = Ο(n sqrt(2) n ) AINF1169 Informatics II 30

31 Summations: Arithmetico-Geometric Series By combining the arithmetic series and the geometric series by a term-by-term multiplication, we get a summation of the form n (a 0 + k d) b k = a 0 + (a 0 + d) b + (a d) b 2 + (a d) b (a 0 n d) b n k = 0 (with 0 < b 1, d = const and an integer n > 0). This is called a (finite) arithmetico-geometric series. As it is written above (i.e. starting at k = 0) sums up to a 0 1 b (a 0 + (n 1) d) b n 1 b + d b (1 bn 1 ) (1 b) 2 The infinite arithmetico-geometric series sums up to: (a 0 + k d) b k = k = 0 a 0 1 b + d b (1 b) 2 AINF1169 Informatics II 31

32 Midterm Preparation Examples Solved General Principles

33 Methods for Determining the Number of Executions of for Loops in Asymptotic Running Time Analysis Multiplicative combination of algorithm parts (and using «end start + 1» et al.) «Table method»: write down the evolution of the loop variable in a table to gain an understanding about the number of executions (probably not possible for more than two levels of nesting) Write loop as a nested multiple sum and solve it (requires a bit of knowledge how to manipulate sums). AINF1169 Informatics II 33

34 Algorithm Running Time Analysis: Another Example Exercise 2.3 from midterm 1 of spring semester 2016: Perform an exact analysis of the running time of the following algorithm and determine its asymptotic complexity. Algorithm: alg(a, n) 1 for i = 1 to n / 2 do 2 min = i 3 max = n i if A[min] > A[max] then 5 exchange A[min] and A[max] 6 for j = i + 1 to n i do 7 if A[j] < A[min] then 8 min = j 9 if A[j] > A[max] then 10 max = j 11 exchange A[i] and A[min] 12 exchange A[n i + 1] and A[max] AINF1169 Informatics II 34

35 Algorithm Running Time Analysis: «Table Method» Example The following example shows how to analyse the number executions of the body of the inner loop (lines 7 to 10) of the algorithm alg(a, n) using a table to track the evolution of the number of executions depending on the value of the outer loop variable. Outer loop variable value i = Inner loop variable range j = i+1 n i Number of executions of the inner loop body 1 2 n 1 n n 2 n n 3 n 6 k k+1 n k n 2 k n/2 n/2 +1 n/2 0 AINF1169 Informatics II 35

36 Algorithm Running Time Analysis: Yet Another Example Task 1 of Assignment 2 from FS 2018: AINF1169 Informatics II 36

37 Summation Rules and Examples Decomposing Sums Changing Summation Indexes Double Sums / Multiple Sums / Nested Sums

38 Summations: Decomposing Sums Indexes of sums can be rewritten / sums can be «taken apart» as in the following example: n x n x x 1 k = k k k = x k = 1 k = 1 sum up «too much», i.e. start summing up from 1 instead of starting from x remove what has been summed up too much, i.e. numbers from 1 to x 1 This is useful in solving sums stemming from nested loops with interacting loop variables like for example: <pseudo code> Im Falle, dass über eine Konstante aufsummiert wird, gilt einfach ENDE ANFANG + 1 AINF1169 Informatics II 38

39 Recursion Tree Example (Task 5a of assignment 2 from FS 2018) Solve the following recurrence using the tree method: T(n) = T(n/6) + T(n/2) + n T(1) = 1 AINF1169 Informatics II 39

40 Trees Nomenclature Binary Trees Binary Tree Traversals Binary Search Trees

41 Trees: Nomenclature height of tree = 3 height (of node) level siblings e.g. B and C are siblings root A B C D internal node e.g. D, G, B E F G H parent (ancestor / father) e.g. D is parent of H child (son) e.g. H is child of D 0 3 I J K L M N sub-tree leaf (external node), e.g. K, C, F AINF1169 Informatics II 41

42 Binary Tree Traversals: Introduction and Overview Tree traversals are rules on how to visit each node of a binary tree exactly once. There are three famous kinds of depth-first traversals and one kind of breadth-first traversal (visiting neighbors first). Depth-first traversals: Inorder: left root right Preorder: root left right Postorder: left right root Breadth-first traversal: Levelorder: from left to right, from root level to leaf level AINF1169 Informatics II 42

43 Binary Tree Traversals: Remarks Note that the sequence of nodes stemming from a particular traversal method in general does not allow to unambiguously reconstruct the original tree. An exception is the preorder traversal which allows to reconstruct the original tree in an unambiguous and efficient manner; therefore, the preorder traversal can be used (and considered as) as representational structure for trees, e.g. in order to store a tree in a file. AINF1169 Informatics II 43

44 Binary Tree Traversals Recursively apply the principle stated before to all nodes of the tree by beginning at the root node. For example: inorder traversal: Left Root Right Pseudo code: inorder(tree T) { inorder(left_subtree(t)); visit(root); inorder(right_subtree(t)); } AINF1169 Informatics II 44

45 Binary Tree Traversals: Example What are the inorder, preorder, postorder and levelorder traversals of the following binary tree? Inorder: 10, 14, 19, 27, 31, 35, 42 Preorder: 27, 14, 10, 19, 35, 31, 42 Postorder: 10, 19, 14, 31, 42, 35, 27 Levelorder: 27, 14, 35, 10, 19, 31, 42 AINF1169 Informatics II 45

46 Binary Tree Traversals: Visual Help preorder inorder postorder AINF1169 Informatics II 46

47 Binary Tree Traversals: Iterative vs. Recursive Implementation Instead of applying a recursive algorithm, the depth-first traversals (inorder, preorder, postorder) can also be implemented iteratively using a (explicit) stack. Similarly, a levelorder traversal can be implemented iteratively using a queue. 1 Step Stack content Output of function «next» AINF1169 Informatics II 47

48 Tree Traversals: Applications What are these tree traversal strategies useful for? Preorder: easy and efficient way to store binary search trees with the possibility to reconstruct them unambigously Inorder: retrieving the node values of a binary search tree in ascending order Postorder: important for compiler design (in assembly: operand operand opcode) Levelorder: used in heapsort algorithm AINF1169 Informatics II 48

49 Binary Search Trees A binary search tree (BST) is a binary tree where all descendent nodes to the left are smaller or equal than the ancestor node and all descendants to the right are bigger or equal than the ancestor node. Example: 19 Subtree with node values less than Subtree with node values bigger than AINF1169 Informatics II 49

50 Heaps Heap Conditions Representation of a Heap as an Array Heap Operations Heapsort

51 Heap Conditions A heap is a binary tree. All levels except the lowest are fully filled and the lowest level is occupied starting from left without gaps (= «nearly complete tree»). Keys in nodes of tree are ordered: in a max-heap: for all nodes, the key is bigger than or equal to all its children largest element at root in a min-heap: for all nodes, the father node is always smaller than or equal to his sons smallest element at root AINF1169 Informatics II 51

52 Representation of a Heap as an Array level Indexing starts at Indexing starts at Level Note: This kind of tree traversal is called level order. AINF1169 Informatics II 52

53 Addressing Parent and Child Nodes k n 2 n 1 n n 3 n 2 n 1 Getting to parent of node with index k Getting to left child of node with index k Getting to right child of node with index k Indexing starts at 1 k / 2 2 k 2 k + 1 Indexing starts at 0 (k 1) / 2 2 k k + 2 AINF1169 Informatics II 53

54 Heap Conditions: Exercise The following arrays shall each represent a binary heap. Do they fulfill the conditions of a max-heap? A: Not a heap, because the first element is neither the maximum nor the minimum of the elements 9 B: By drawing the corresponding heap, we can see that elements at indices 5 and 8 (starting from zero) violate the heap condition. Therefore it is not a heap AINF1169 Informatics II 54

55 Heaps: Building a Heap How to get to a heap from an unordered array of values? Let s assume that for a certain node, we knew that both its subtrees are already heaps: In this case, we can apply the same procedure which we used before to trickle down the nodes. Thus, we just start at the last node which has at least one child node and then work our way up to the root while making sure for each node, that everything below (its subtrees) are nice heaps. AINF1169 Informatics II 55

56 Heaps: Sorting Algorithm 1) Transform input data into heap. 2) Successively remove the root element and reorganize the remaining heap. AINF1169 Informatics II 56

57 Heaps: Demonstration: Removing the Root AINF1169 Informatics II 57

58 Heaps: Demonstration: Building a Heap AINF1169 Informatics II 58

59 Heaps: Comprehension Question Why do we iterate from n/2 to 1 and not from 1 to n/2 in the BuildHeap algorithm? (Does this even matter?) AINF1169 Informatics II 59

60 Heaps, Heapsort: DIY / Game 1) Form groups of two to three people. 2) Get a deck of playing cards. 3) (If necessary: Familiarize yourself with the ordering of the cards.) 4) Shuffle the cards. 5) Lay out 12 cards in form of a binary three before you on the desk. (If you got the Joker, replace it through another card.) 6) Transform the card into a max- or min-heap. 7) Get a sorted set of cards by successively removing the root element. 8) If you feel comfortable with heapsort or if you get bored: Take 8 random cards of your deck and perform bubble sort, selection sort, insertion sort, quicksort, mergesort, on it. AINF1169 Informatics II 60

61 Preview on Exercise 5

62 Exercise 5 Task 1: Heap and Heapsort: Implementation in C Look at the respective slides from the lecture: SL04, pp AINF1169 Informatics II 62

63 Exercise 5 Tasks 2 & 3: Heap Construction and Heapsort by Hand Look at the respective slides from the lecture: SL04, pp AINF1169 Informatics II 63

64 Exercise 5 Task 4: Quicksort Example by Hand Look at the respective slides from the lecture: SL04, pp AINF1169 Informatics II 64

65 Wrap-Up Summary Outlook Questions

66 Outlook Midterm 1: Friday, , 12:15 13:45 h Last minute Q&A: Friday, , 09:00 11:00 h Next tutorial: Tuesday, , BIN 0.B.06 Topics: Review of Exercise 5 Review of Midterm 1 Heapsort Quicksort Preview to Exercise 6 (your wishes) AINF1169 Informatics II 66

67 Questions? AINF1169 Informatics II 67

68 Thank you for your attention. AINF1169 Informatics II 68

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003 University of New Mexico Department of Computer Science Midterm Examination CS 361 Data Structures and Algorithms Spring, 2003 Name: Email: Print your name and email, neatly in the space provided above;

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees (Exercises with hints for solution)

More information

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes Computational Complexity Lecture 02 - Basic Complexity Analysis Tom Kelsey & Susmit Sarkar School of Computer Science University of St Andrews http://www.cs.st-andrews.ac.uk/~tom/ twk@st-andrews.ac.uk

More information

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts) Introduction to Algorithms October 13, 2010 Massachusetts Institute of Technology 6.006 Fall 2010 Professors Konstantinos Daskalakis and Patrick Jaillet Quiz 1 Solutions Quiz 1 Solutions Problem 1. We

More information

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 MA008 p.1/37 MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3 Dr. Markus Hagenbuchner markus@uow.edu.au. MA008 p.2/37 Exercise 1 (from LN 2) Asymptotic Notation When constants appear in exponents

More information

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms - Using Asymptotic Bounds - Analysis of Algorithms - Using Asymptotic Bounds - Andreas Ermedahl MRTC (Mälardalens Real-Time Research Center) andreas.ermedahl@mdh.se Autumn 004 Rehersal: Asymptotic bounds Gives running time bounds

More information

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM Sorting Chapter 11-1 - Sorting Ø We have seen the advantage of sorted data representations for a number of applications q Sparse vectors q Maps q Dictionaries Ø Here we consider the problem of how to efficiently

More information

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu Analysis of Algorithm Efficiency Dr. Yingwu Zhu Measure Algorithm Efficiency Time efficiency How fast the algorithm runs; amount of time required to accomplish the task Our focus! Space efficiency Amount

More information

Central Algorithmic Techniques. Iterative Algorithms

Central Algorithmic Techniques. Iterative Algorithms Central Algorithmic Techniques Iterative Algorithms Code Representation of an Algorithm class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) throws Exception { for (int i = 1; i < a.length;

More information

MIDTERM I CMPS Winter 2013 Warmuth

MIDTERM I CMPS Winter 2013 Warmuth test I 1 MIDTERM I CMPS 101 - Winter 2013 Warmuth With Solutions NAME: Student ID: This exam is closed book, notes, computer and cell phone. Show partial solutions to get partial credit. Make sure you

More information

Week 5: Quicksort, Lower bound, Greedy

Week 5: Quicksort, Lower bound, Greedy Week 5: Quicksort, Lower bound, Greedy Agenda: Quicksort: Average case Lower bound for sorting Greedy method 1 Week 5: Quicksort Recall Quicksort: The ideas: Pick one key Compare to others: partition into

More information

Divide-and-Conquer Algorithms Part Two

Divide-and-Conquer Algorithms Part Two Divide-and-Conquer Algorithms Part Two Recap from Last Time Divide-and-Conquer Algorithms A divide-and-conquer algorithm is one that works as follows: (Divide) Split the input apart into multiple smaller

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17 2.1 Notes Homework 1 will be released today, and is due a week from today by the beginning

More information

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms Prof. Gregory Provan Department of Computer Science University College Cork 1 Lecture Outline CS 4407, Algorithms Growth Functions

More information

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read.

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR  ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read. ECE608 Midterm Spring 2009 Please fill in the following information. THEN PUT YOUR EMAI ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Email (please put complete address): Neatness counts. We will not grade

More information

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3 Banner ID: Name: Midterm Exam CS 3110: Design and Analysis of Algorithms June 20, 2006 Group 1 Group 2 Group 3 Question 1.1 Question 2.1 Question 3.1 Question 1.2 Question 2.2 Question 3.2 Question 3.3

More information

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence A. V. Gerbessiotis CS 610-102 Spring 2014 PS 1 Jan 27, 2014 No points For the remainder of the course Give an algorithm means: describe an algorithm, show that it works as claimed, analyze its worst-case

More information

CSCI 3110 Assignment 6 Solutions

CSCI 3110 Assignment 6 Solutions CSCI 3110 Assignment 6 Solutions December 5, 2012 2.4 (4 pts) Suppose you are choosing between the following three algorithms: 1. Algorithm A solves problems by dividing them into five subproblems of half

More information

Data selection. Lower complexity bound for sorting

Data selection. Lower complexity bound for sorting Data selection. Lower complexity bound for sorting Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 12 1 Data selection: Quickselect 2 Lower complexity bound for sorting 3 The

More information

CS 361, Lecture 14. Outline

CS 361, Lecture 14. Outline Bucket Sort CS 361, Lecture 14 Jared Saia University of New Mexico Bucket sort assumes that the input is drawn from a uniform distribution over the range [0, 1) Basic idea is to divide the interval [0,

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 9 Divide and Conquer Merge sort Counting Inversions Binary Search Exponentiation Solving Recurrences Recursion Tree Method Master Theorem Sofya Raskhodnikova S. Raskhodnikova;

More information

Divide and Conquer. Recurrence Relations

Divide and Conquer. Recurrence Relations Divide and Conquer Recurrence Relations Divide-and-Conquer Strategy: Break up problem into parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. 2 MergeSort Mergesort.

More information

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ; 1 Trees The next major set of data structures belongs to what s called Trees. They are called that, because if you try to visualize the structure, it kind of looks like a tree (root, branches, and leafs).

More information

Asymptotic Algorithm Analysis & Sorting

Asymptotic Algorithm Analysis & Sorting Asymptotic Algorithm Analysis & Sorting (Version of 5th March 2010) (Based on original slides by John Hamer and Yves Deville) We can analyse an algorithm without needing to run it, and in so doing we can

More information

Insertion Sort. We take the first element: 34 is sorted

Insertion Sort. We take the first element: 34 is sorted Insertion Sort Idea: by Ex Given the following sequence to be sorted 34 8 64 51 32 21 When the elements 1, p are sorted, then the next element, p+1 is inserted within these to the right place after some

More information

CS Analysis of Recursive Algorithms and Brute Force

CS Analysis of Recursive Algorithms and Brute Force CS483-05 Analysis of Recursive Algorithms and Brute Force Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/

More information

Introduction to Divide and Conquer

Introduction to Divide and Conquer Introduction to Divide and Conquer Sorting with O(n log n) comparisons and integer multiplication faster than O(n 2 ) Periklis A. Papakonstantinou York University Consider a problem that admits a straightforward

More information

Asymptotic Analysis and Recurrences

Asymptotic Analysis and Recurrences Appendix A Asymptotic Analysis and Recurrences A.1 Overview We discuss the notion of asymptotic analysis and introduce O, Ω, Θ, and o notation. We then turn to the topic of recurrences, discussing several

More information

CS 161 Summer 2009 Homework #2 Sample Solutions

CS 161 Summer 2009 Homework #2 Sample Solutions CS 161 Summer 2009 Homework #2 Sample Solutions Regrade Policy: If you believe an error has been made in the grading of your homework, you may resubmit it for a regrade. If the error consists of more than

More information

Fundamental Algorithms

Fundamental Algorithms Chapter 2: Sorting, Winter 2018/19 1 Fundamental Algorithms Chapter 2: Sorting Jan Křetínský Winter 2018/19 Chapter 2: Sorting, Winter 2018/19 2 Part I Simple Sorts Chapter 2: Sorting, Winter 2018/19 3

More information

Fundamental Algorithms

Fundamental Algorithms Fundamental Algorithms Chapter 2: Sorting Harald Räcke Winter 2015/16 Chapter 2: Sorting, Winter 2015/16 1 Part I Simple Sorts Chapter 2: Sorting, Winter 2015/16 2 The Sorting Problem Definition Sorting

More information

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February) Algorithms and Data Structures 016 Week 5 solutions (Tues 9th - Fri 1th February) 1. Draw the decision tree (under the assumption of all-distinct inputs) Quicksort for n = 3. answer: (of course you should

More information

data structures and algorithms lecture 2

data structures and algorithms lecture 2 data structures and algorithms 2018 09 06 lecture 2 recall: insertion sort Algorithm insertionsort(a, n): for j := 2 to n do key := A[j] i := j 1 while i 1 and A[i] > key do A[i + 1] := A[i] i := i 1 A[i

More information

1 Closest Pair of Points on the Plane

1 Closest Pair of Points on the Plane CS 31: Algorithms (Spring 2019): Lecture 5 Date: 4th April, 2019 Topic: Divide and Conquer 3: Closest Pair of Points on a Plane Disclaimer: These notes have not gone through scrutiny and in all probability

More information

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

CMPSCI 311: Introduction to Algorithms Second Midterm Exam CMPSCI 311: Introduction to Algorithms Second Midterm Exam April 11, 2018. Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more

More information

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms Chapter 3 Data Structures and Algorithms Chapter 3 1. Divide and conquer 2. Merge sort, repeated substitutions 3. Tiling 4. Recurrences Recurrences Running times of algorithms with recursive calls can be described

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture II: Chapter 2 R. Paul Wiegand George Mason University, Department of Computer Science February 1, 2006 Outline 1 Analysis Framework 2 Asymptotic

More information

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005 CSCE 750 Final Exam Answer Key Wednesday December 7, 2005 Do all problems. Put your answers on blank paper or in a test booklet. There are 00 points total in the exam. You have 80 minutes. Please note

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu What is an Algorithm?

More information

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis Lecture 14 Class URL: http://vlsicad.ucsd.edu/courses/cse21-s14/ Lecture 14 Notes Goals for this week Big-O complexity

More information

Problem Set 2 Solutions

Problem Set 2 Solutions Introduction to Algorithms October 1, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 9 Problem Set 2 Solutions Reading: Chapters 5-9,

More information

CS361 Homework #3 Solutions

CS361 Homework #3 Solutions CS6 Homework # Solutions. Suppose I have a hash table with 5 locations. I would like to know how many items I can store in it before it becomes fairly likely that I have a collision, i.e., that two items

More information

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Inf 2B: Sorting, MergeSort and Divide-and-Conquer Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh The Sorting Problem Input: Task: Array A of items with comparable

More information

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency Lecture 2 Fundamentals of the Analysis of Algorithm Efficiency 1 Lecture Contents 1. Analysis Framework 2. Asymptotic Notations and Basic Efficiency Classes 3. Mathematical Analysis of Nonrecursive Algorithms

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3 Algorithms ROBRT SDGWICK KVIN WAYN 2.4 PRIORITY QUUS Algorithms F O U R T H D I T I O N Fundamentals and flipped lectures Priority queues and heaps Heapsort Deeper thinking ROBRT SDGWICK KVIN WAYN http://algs4.cs.princeton.edu

More information

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida Algorithms Jordi Planes Escola Politècnica Superior Universitat de Lleida 2016 Syllabus What s been done Formal specification Computational Cost Transformation recursion iteration Divide and conquer Sorting

More information

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms Chapter 3 1 Data Structures and Algorithms Chapter 3 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Recursion: Introduction and Correctness

Recursion: Introduction and Correctness Recursion: Introduction and Correctness CSE21 Winter 2017, Day 7 (B00), Day 4-5 (A00) January 25, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Today s Plan From last time: intersecting sorted lists and

More information

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 1: Asymptotics, Recurrences, Elementary Sorting Lecture 1: Asymptotics, Recurrences, Elementary Sorting Instructor: Outline 1 Introduction to Asymptotic Analysis Rate of growth of functions Comparing and bounding functions: O, Θ, Ω Specifying running

More information

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018 CS17 Integrated Introduction to Computer Science Klein Contents Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018 1 Tree definitions 1 2 Analysis of mergesort using a binary tree 1 3 Analysis of

More information

CS 2110: INDUCTION DISCUSSION TOPICS

CS 2110: INDUCTION DISCUSSION TOPICS CS 110: INDUCTION DISCUSSION TOPICS The following ideas are suggestions for how to handle your discussion classes. You can do as much or as little of this as you want. You can either present at the board,

More information

Math 391: Midterm 1.0 Spring 2016

Math 391: Midterm 1.0 Spring 2016 Math 391: Midterm 1.0 Spring 2016 James Skon skonjp@kenyon.edu Test date: October 5 Submission Instructions: Give all your assumptions. Show all your work. Be as complete as possible. Grading Problem 1

More information

Practical Session #3 - Recursions

Practical Session #3 - Recursions Practical Session #3 - Recursions Substitution method Guess the form of the solution and prove it by induction Iteration Method Convert the recurrence into a summation and solve it Tightly bound a recurrence

More information

Fall 2016 Test 1 with Solutions

Fall 2016 Test 1 with Solutions CS3510 Design & Analysis of Algorithms Fall 16 Section B Fall 2016 Test 1 with Solutions Instructor: Richard Peng In class, Friday, Sep 9, 2016 Do not open this quiz booklet until you are directed to do

More information

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt Lecture 3 The Analysis of Recursive Algorithm Efficiency What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency

More information

CS Data Structures and Algorithm Analysis

CS Data Structures and Algorithm Analysis CS 483 - Data Structures and Algorithm Analysis Lecture VII: Chapter 6, part 2 R. Paul Wiegand George Mason University, Department of Computer Science March 22, 2006 Outline 1 Balanced Trees 2 Heaps &

More information

IS 709/809: Computational Methods in IS Research Fall Exam Review

IS 709/809: Computational Methods in IS Research Fall Exam Review IS 709/809: Computational Methods in IS Research Fall 2017 Exam Review Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Exam When: Tuesday (11/28) 7:10pm

More information

Asymptotic Running Time of Algorithms

Asymptotic Running Time of Algorithms Asymptotic Complexity: leading term analysis Asymptotic Running Time of Algorithms Comparing searching and sorting algorithms so far: Count worst-case of comparisons as function of array size. Drop lower-order

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 10, 2018 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. 1 /

More information

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016 University of Toronto Department of Electrical and Computer Engineering Final Examination ECE 345 Algorithms and Data Structures Fall 2016 Print your first name, last name, UTORid, and student number neatly

More information

Sorting and Searching. Tony Wong

Sorting and Searching. Tony Wong Tony Wong 2017-03-04 Sorting Sorting is the reordering of array elements into a specific order (usually ascending) For example, array a[0..8] consists of the final exam scores of the n = 9 students in

More information

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself is easy to understand Λ a complete analysis can often

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 4 - Jan. 14, 2019 CLRS 1.1, 1.2, 2.2, 3.1, 4.3, 4.5 University of Manitoba Picture is from the cover of the textbook CLRS. COMP

More information

CSC236 Week 4. Larry Zhang

CSC236 Week 4. Larry Zhang CSC236 Week 4 Larry Zhang 1 Announcements PS2 due on Friday This week s tutorial: Exercises with big-oh PS1 feedback People generally did well Writing style need to be improved. This time the TAs are lenient,

More information

CS 231: Algorithmic Problem Solving

CS 231: Algorithmic Problem Solving CS 231: Algorithmic Problem Solving Naomi Nishimura Module 4 Date of this version: June 11, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides

More information

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows: Outline Computer Science 331 Quicksort Mike Jacobson Department of Computer Science University of Calgary Lecture #28 1 Introduction 2 Randomized 3 Quicksort Deterministic Quicksort Randomized Quicksort

More information

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14 Divide and Conquer Algorithms CSE 101: Design and Analysis of Algorithms Lecture 14 CSE 101: Design and analysis of algorithms Divide and conquer algorithms Reading: Sections 2.3 and 2.4 Homework 6 will

More information

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts] Prelim [Solutions]. Short Answer [8 pts] (a) [3 pts] In a model/view/controller, Swing is likely to be part of (there may be more than one): i. the model ii. the view iii. the controller The view and the

More information

CS 4349 Lecture August 30th, 2017

CS 4349 Lecture August 30th, 2017 CS 4349 Lecture August 30th, 2017 Main topics for #lecture include #recurrances. Prelude I will hold an extra office hour Friday, September 1st from 3:00pm to 4:00pm. Please fill out prerequisite forms

More information

CPS 616 DIVIDE-AND-CONQUER 6-1

CPS 616 DIVIDE-AND-CONQUER 6-1 CPS 616 DIVIDE-AND-CONQUER 6-1 DIVIDE-AND-CONQUER Approach 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger)

More information

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: The maximum-subarray problem Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm: Brute force algorithm: At best, θ(n 2 ) time complexity 129 Can we do divide

More information

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation: Data structures Exercise 1 solution Question 1 Let s start by writing all the functions in big O notation: f 1 (n) = 2017 = O(1), f 2 (n) = 2 log 2 n = O(n 2 ), f 3 (n) = 2 n = O(2 n ), f 4 (n) = 1 = O

More information

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101 A design paradigm Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/17 112 Multiplying complex numbers (from Jeff Edmonds slides) INPUT: Two pairs of integers, (a,b),

More information

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation: CS 124 Section #1 Big-Oh, the Master Theorem, and MergeSort 1/29/2018 1 Big-Oh Notation 1.1 Definition Big-Oh notation is a way to describe the rate of growth of functions. In CS, we use it to describe

More information

Methods for solving recurrences

Methods for solving recurrences Methods for solving recurrences Analyzing the complexity of mergesort The merge function Consider the following implementation: 1 int merge ( int v1, int n1, int v, int n ) { 3 int r = malloc ( ( n1+n

More information

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009 CPSC 320 (Intermediate Algorithm Design and Analysis). Summer 2009. Instructor: Dr. Lior Malka Final Examination, July 24th, 2009 Student ID: INSTRUCTIONS: There are 6 questions printed on pages 1 7. Exam

More information

Solutions. Prelim 2[Solutions]

Solutions. Prelim 2[Solutions] Prelim [Solutions]. Short Answer [ pts] (a) [ pts] A traversal of an expression tree produces the string + + 3. i. What kind of traversal is it? Preorder; the operator is printed before the operands. ii.

More information

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

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 5 Divide and Conquer CLRS 4.3 Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 Divide-and-Conquer Divide-and-conquer. Break up problem into several parts. Solve

More information

Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109. Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101

Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109. Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101 Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109 Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101 http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ March 30, 2016 Sorting (or Ordering)

More information

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b) Introduction to Algorithms October 14, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Srini Devadas and Constantinos (Costis) Daskalakis Quiz 1 Solutions Quiz 1 Solutions Problem

More information

Data Structures and Algorithms Winter Semester

Data Structures and Algorithms Winter Semester Page 0 German University in Cairo December 26, 2015 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Wael Abouelsadaat Data Structures and Algorithms Winter Semester 2015-2016 Final

More information

An analogy from Calculus: limits

An analogy from Calculus: limits COMP 250 Fall 2018 35 - big O Nov. 30, 2018 We have seen several algorithms in the course, and we have loosely characterized their runtimes in terms of the size n of the input. We say that the algorithm

More information

5. DIVIDE AND CONQUER I

5. DIVIDE AND CONQUER I 5. DIVIDE AND CONQUER I mergesort counting inversions closest pair of points randomized quicksort median and selection Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

Algorithms and Data Structures 2014 Exercises week 5

Algorithms and Data Structures 2014 Exercises week 5 Algorithms and Data Structures 014 Exercises week 5 October, 014 Exercises marked by ( ) are hard, but they might show up on the exam. Exercises marked by ( ) are even harder, but they will not be on the

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Based

More information

Lecture 4. Quicksort

Lecture 4. Quicksort Lecture 4. Quicksort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018 Networking

More information

Analysis of Algorithms

Analysis of Algorithms September 29, 2017 Analysis of Algorithms CS 141, Fall 2017 1 Analysis of Algorithms: Issues Correctness/Optimality Running time ( time complexity ) Memory requirements ( space complexity ) Power I/O utilization

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 8 Analyzing Quick Sort Sofya Raskhodnikova and Adam Smith Reminder: QuickSort Quicksort an n-element array: 1. Divide: Partition the array around a pivot

More information

15.1 Introduction to Lower Bounds Proofs

15.1 Introduction to Lower Bounds Proofs 15 Lower Bounds How do I know if I have a good algorithm to solve a problem? If my algorithm runs in Θ(n log n) time, is that good? It would be if I were sorting the records stored in an array. But it

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Sorting Algorithms (contd.) Outline Sorting Algorithms (contd.) 1 Sorting Algorithms (contd.) Analysis of Quicksort Time to sort array of length

More information

Algorithms Exam TIN093 /DIT602

Algorithms Exam TIN093 /DIT602 Algorithms Exam TIN093 /DIT602 Course: Algorithms Course code: TIN 093, TIN 092 (CTH), DIT 602 (GU) Date, time: 21st October 2017, 14:00 18:00 Building: SBM Responsible teacher: Peter Damaschke, Tel. 5405

More information

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th CSE 3500 Algorithms and Complexity Fall 2016 Lecture 10: September 29, 2016 Quick sort: Average Run Time In the last lecture we started analyzing the expected run time of quick sort. Let X = k 1, k 2,...,

More information

Data Structures in Java

Data Structures in Java Data Structures in Java Lecture 20: Algorithm Design Techniques 12/2/2015 Daniel Bauer 1 Algorithms and Problem Solving Purpose of algorithms: find solutions to problems. Data Structures provide ways of

More information

Advanced Analysis of Algorithms - Midterm (Solutions)

Advanced Analysis of Algorithms - Midterm (Solutions) Advanced Analysis of Algorithms - Midterm (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV {ksmani@csee.wvu.edu} 1 Problems 1. Solve the following recurrence using substitution:

More information

Searching. Sorting. Lambdas

Searching. Sorting. Lambdas .. s Babes-Bolyai University arthur@cs.ubbcluj.ro Overview 1 2 3 Feedback for the course You can write feedback at academicinfo.ubbcluj.ro It is both important as well as anonymous Write both what you

More information

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005 V. Adamchi Recurrences Victor Adamchi Fall of 00 Plan Multiple roots. More on multiple roots. Inhomogeneous equations 3. Divide-and-conquer recurrences In the previous lecture we have showed that if the

More information

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs Krishnendu Chatterjee Rasmus Ibsen-Jensen Andreas Pavlogiannis IST Austria Abstract. We consider graphs with n nodes together

More information

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Divide-and-conquer: Order Statistics. Curs: Fall 2017 Divide-and-conquer: Order Statistics Curs: Fall 2017 The divide-and-conquer strategy. 1. Break the problem into smaller subproblems, 2. recursively solve each problem, 3. appropriately combine their answers.

More information

Analysis of Algorithms CMPSC 565

Analysis of Algorithms CMPSC 565 Analysis of Algorithms CMPSC 565 LECTURES 38-39 Randomized Algorithms II Quickselect Quicksort Running time Adam Smith L1.1 Types of randomized analysis Average-case analysis : Assume data is distributed

More information