Data Structures Review Session 1

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Divide and Conquer Strategy
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
Quicksort
Cmpt-225 Sorting – Part two. Idea of Quick Sort 1) Select: pick an element 2) Divide: partition elements so that x goes to its final position E 3) Conquer:
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Chapter 12 Recursion, Complexity, and Searching and Sorting
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CSC 211 Data Structures Lecture 13
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Algorithms Analysis and Design
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Quicksort
Quicksort 1.
CS 3343: Analysis of Algorithms
Unit-2 Divide and Conquer
Data Structures Review Session
CS200: Algorithms Analysis
Sub-Quadratic Sorting Algorithms
Quicksort.
Quicksort.
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Data Structures Review Session 1 Ramakrishna, PhD student. Grading Assistant for this course CS 307 Fundamentals of Computer Science

Java exercises For the following program, what are the possible outputs ? Public class WhatIsX { public static void f(int x) /* body unknown */ } public static void main(String[ ] args) int x = 0; f(x); System.out.println(x); Output - 0 CS 307 Fundamentals of Computer Science

Java exercises Output – 1 2 3 4 What is the output of this code ? public class xyz { public static void resize(int[ ] arr) int[ ] old = arr; arr = new int[old.length*2 + 1]; for(int i=0;i<old.length;i++) arr[i]=old[i]; } public static void main(String[ ] args) int arr[]={1,2,3,4}; xyz.resize(arr); for(int i=0;i<arr.length;i++) System.out.println(arr[i]); }}} Output – 1 2 3 4 CS 307 Fundamentals of Computer Science

Problems Give an efficient algorithm to determine whether an integer i exists such that A[i] equals i in an array of increasing integers. What is the running time of your algorithm ? Naïve Algorithm ? Smart Algorithm ? Linear search – O(n) Binary search – O(logn) CS 307 Fundamentals of Computer Science

Quick Sort Review PARTITIONING A key step in the Quick sort algorithm is partitioning the array We choose some (any) number p in the array to use as a pivot We partition the array into three parts: p numbers less than p p numbers greater than or equal to p CS 307 Fundamentals of Computer Science

Best case Partitioning at various levels CS 307 Fundamentals of Computer Science

Analysis of Quick Sort— Best Case Suppose each partition operation divides the array almost exactly in half Then the depth of the recursion in log2n Because that’s how many times we can halve n However, there are many recursions! How can we figure this out? We note that Each partition is linear over its sub array All the partitions at one level cover the array CS 307 Fundamentals of Computer Science

Best case II So the depth of the recursion in log2n At each level of the recursion, all the partitions at that level do work that is linear in n O(log2n) * O(n) = O(n log2n) Hence in the average case, quicksort has time complexity O(n log2n) What about the worst case? CS 307 Fundamentals of Computer Science

Worst case partitioning CS 307 Fundamentals of Computer Science

Worst case In the worst case, partitioning always divides the size n array into these three parts: A length one part, containing the pivot itself A length zero part, and A length n-1 part, containing everything else We don’t recur on the zero-length part Recurring on the length n-1 part requires (in the worst case) recurring to depth n-1 CS 307 Fundamentals of Computer Science

Worst case for quick sort In the worst case, recursion may be n levels deep (for an array of size n) But the partitioning work done at each level is still n O(n) * O(n) = O(n2) So worst case for Quick sort is O(n2) When does this happen? When the array is sorted to begin with! CS 307 Fundamentals of Computer Science

Typical case for quick sort If the array is sorted to begin with, Quick sort is terrible: O(n2) It is possible to construct other bad cases However, Quick sort is usually O(n log2n) The constants are so good that Quick sort is generally the fastest algorithm known Most real-world sorting is done by Quick sort CS 307 Fundamentals of Computer Science

Problems on Quick Sort (1) What is the running time of QUICKSORT when a. All elements of array A have the same value ? b. The array A contains distinct elements and is sorted in descending order ? Assume that you always use the last element in the sub array as pivot. ANSWER ? CS 307 Fundamentals of Computer Science

Answer (1) A) Whatever pivot you choose in each sub array it would result in WORST CASE PARTITIONING and hence the running time is O(n2). B) Same is the case. Since you always pick the maximum element in the sub array as the pivot each partition you do would be a worst case partition and hence the running time is O(n2) again !. CS 307 Fundamentals of Computer Science

Problems on Sorting What are the worst-case and average-case running times for insertion sort, merge sort and quick sort ? Answer: Insertion sort is O( n2 ) in both cases. Merge sort is O( n log n ) in both cases. Quick-sort is O( n log n ) on average, O(n2 ) in the worst case. In instances where the worst-case and average-case differ, give an example of how the worst case occurs ? Answer: In Quick-sort, the worst case occurs when the partition is repeatedly degenerate. CS 307 Fundamentals of Computer Science

Problems on Sorting Which of the following take average linear execution time ? Insertion sort Merge sort Quick sort Quick select None of the above CS 307 Fundamentals of Computer Science

Problems on Sorting When all elements are equal, what is the running time of – Insertion sort – O(n) ( best case) Merge sort – O(n logn) Quick sort - O(n2) ( worst case ) When the input has been sorted, what is the running time of – Quick sort - O(n2) ( worst case ) CS 307 Fundamentals of Computer Science

Problems on Sorting When the input has been reverse sorted, what is the running time of – Insertion sort – O(n2) ( worst case ) Merge sort – O(n logn) Quick sort - O(n2) ( worst case ) CS 307 Fundamentals of Computer Science

Searching Problems Write down the time complexities (best, worst and average cases) of performing Linear-Search and Binary-Search in a sorted array of n elements. Solution ? CS 307 Fundamentals of Computer Science

Linear Search (1) Method : Scan the input list and compare the input element with every element in the list. If a match is found, return. Worst case Time Complexity: The obvious worst case is that the input element is not available in the list. In this case go through the entire list and hence the worst case time complexity would be O(n). So this search doesn’t really exploit the fact that the list is sorted !! CS 307 Fundamentals of Computer Science

Linear Search (2) Average case Time Complexity: Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half and hence its time complexity is Θ(n/2) which is Θ (n) similar to the worst case ignoring the constant. Best Case Time complexity: The obvious best case is that we find the first element of the array equal to the input element and hence the search terminates there itself. Hence the best case is Ω(1). CS 307 Fundamentals of Computer Science

Binary Search (1) Binary search can only be performed on a sorted array. In binary search we first compare the input element to the middle element and if they are equal then the search is over. If it is greater than the middle element, then we search in the right half recursively, otherwise we search in the left half recursively until the middle element is equal to the input element. Algorithm complexity is O(log2n). Best, Worst and Average case complexities are : Θ (log2 n) CS 307 Fundamentals of Computer Science

Problem on Complexity Let f (n), g (n) be asymptotically nonnegative. Show that max( f (n), g (n)) = Θ(f (n) + g (n)). SOLUTION: According to the definition, f (n) = Θ(g (n)) if and only if there exists positive constants c1, c2 and n0 such that 0<=c1f(n)<=g (n)<=c2f(n) for all n>=n0 So to prove this, we need to find positive constants c1,c2 and n0 such that 0 <= c1(f(n) + g (n)) <= max (f (n), g (n)) <= c2(f(n) + g (n)) for all n >= n0. Selecting c2 = 1 clearly shows the third inequality since the maximum must be smaller than the sum. c1 should be selected as 1/2 since the maximum is always greater than the average of f (n) and g (n). Also we can select n0=1. CS 307 Fundamentals of Computer Science

Master Theorem CS 307 Fundamentals of Computer Science

Solving Recurrence relations Solve the recurrence equation: T (n) = 16T(n/4) + n2. Now we solve this recurrence using the master theorem. This recurrence relation can be solved using the master’s theorem. When this recurrence relation is compared to the general equation: T(n)=aT(n/b) + f(n), we get: a=16, b=4 and f (n) = n2 which is a quadratic function of n. Here nlogba = nlog416 =n2 (Since 16=42 we get nlog416 to be equal to n2 as log44 =1). CS 307 Fundamentals of Computer Science

Continued.. Checking case 1, f(n)=O(nlogba-ε) for ε>0. f(n)=O(n2-ε) for ε>0 not true since f(n)=n2 Checking case 2, f(n)=Ɵ(nlogba.logkn) for k>0. f(n)=Ɵ(n2.logkn) for k=0 is true since f(n)=n2 Need not check case 3 since case 2 applies. Hence, T(n)= Ɵ(nlogba.logk+1n)= Ɵ(n2.logn) CS 307 Fundamentals of Computer Science

More Problems on Sorting Describe and analyze an efficient method for removing all duplicates from a collection A of n elements. Solution ? CS 307 Fundamentals of Computer Science

Smart Algorithm Pseudo Code: Sort array A using an O (n log n) sorting algorithm. // n elements in A So now since the array is sorted if we have any duplicates at all, they will be adjacent to one another. Let B the resulting array without duplicates. B[1] = A[1]; For I = 2 to n If A[I] is same as the recently added element in B, then skip it, else add A[I] in B Time Complexity : O (nlogn) + O(n) = O(nlogn) CS 307 Fundamentals of Computer Science

More Problems on Sorting Given a set A with n elements and a set B with m elements, describe an efficient algorithm for computing A XOR B, which is the set of elements that are in A or B, but not in both. Solution ? CS 307 Fundamentals of Computer Science

Naïve Algorithm Pseudo Code: For each element x in A // n elements in A For each element y in B // m elements in B Compare x and y. If both are same mark them. Go through A and B and find the elements that are not marked. These are the elements in the XOR set. Time Complexity : O (n*m) + O(n)+ O(m) = O(n*m) CS 307 Fundamentals of Computer Science

Smart Algorithm Pseudo Code: Sort array A using an O (n log n) sorting algorithm. // n elements in A For each element x in B // m elements in B Perform a binary search for x in A. If a match is found, mark x in A ; else add x in the XOR set. Go through A and copy unmarked elements to the XOR set. Time Complexity : O (nlogn) + O(mlogn) +O(n) = O((m+n)logn) CS 307 Fundamentals of Computer Science

Merge Sort Approach Performance Partition list of elements into 2 lists Recursively sort both lists Given 2 sorted lists, merge into 1 sorted list Examine head of both lists Move smaller to end of new list Performance O( n log(n) ) average / worst case CS 307 Fundamentals of Computer Science

Merge Example 2 4 5 2 7 4 5 8 7 8 2 2 4 5 7 7 4 5 8 8 2 4 2 4 5 7 8 7 5 8 CS 307 Fundamentals of Computer Science

Merge Sort Example 7 2 8 5 4 2 4 5 7 8 7 2 8 5 4 2 7 4 5 8 7 2 8 5 4 7 2 8 4 5 5 4 5 4 Split Merge CS 307 Fundamentals of Computer Science

Problems on Merge Sort Let S be a sequence of n elements. An inversion in S is a pair of elements x and y such that x appears before y in S but x>y. Describe an algorithm running in O (n log n) time for determining the number of inversions in S. Solution ? CS 307 Fundamentals of Computer Science

Simple Naïve Algorithm Pseudo Code: For I = 1 to n // n elements in A For J = I+1 to n Compare A[I] and A[J]. If A[I] > A[J] then inversions++; Time Complexity : O (n2) CS 307 Fundamentals of Computer Science

Smart Algorithm Hint: Modify the MERGE sub procedure to solve this problem efficiently !. Original Merge Sort Approach Partition list of elements into 2 lists Recursively sort both lists Given 2 sorted lists, merge into 1 sorted list Examine head of both lists Move smaller to end of new list Performance O( n log (n) ) average / worst case CS 307 Fundamentals of Computer Science

Merge Example 2 4 5 2 7 4 5 8 7 8 2 2 4 5 7 7 4 5 8 8 2 4 2 4 5 7 8 7 5 8 CS 307 Fundamentals of Computer Science

Merge Sort Example 7 2 8 5 4 2 4 5 7 8 7 2 8 5 4 2 7 4 5 8 7 2 8 5 4 7 2 8 4 5 5 4 5 4 Split Merge CS 307 Fundamentals of Computer Science

Modified Merge Sort Modified Merge Sort Approach Performance Partition list of elements into 2 lists Recursively sort both lists Given 2 sorted lists, merge into 1 sorted list Examine the head of the left list and the right list If head of left list is greater than the head of right list we have an inversion Performance O( n log (n) ) average / worst case CS 307 Fundamentals of Computer Science