CIT 596 Complexity. Tight bounds Is mergesort O(n 4 )? YES! But it is silly to use that as your measure More common to hear it as….? What about quicksort?

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Merge and Count Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
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]
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Insertion Sort Merge Sort QuickSort. Sorting Problem Definition an algorithm that puts elements of a list in a certain order Numerical order and Lexicographical.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.
Quicksort.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Functional Design and Programming Lecture 4: Sorting.
Data Structures Review Session 1
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Analysis of Recursive Algorithms October 29, 2014
Counting Inversions Merge and count step. n Given two sorted halves, count number of inversions where a i and a j are in different.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
CS223 Advanced Data Structures and Algorithms 1 Sorting and Master Method Neil Tang 01/21/2009.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Project 2 due … Project 2 due … Project 2 Project 2.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
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.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
Data Structures & Algorithms Recursion and Trees Richard Newman.
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.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Foundations II: Data Structures and Algorithms
Spring 2015 Lecture 2: Analysis of Algorithms
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Unit 1. Sorting and Divide and Conquer
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Data Structures Review Session
CS 3343: Analysis of Algorithms
Merge and Count Merge and count step.
Merge and Count Merge and count step.
Merge and Count Merge and count step.
Richard Anderson Lecture 14 Divide and Conquer
Algorithms Recurrences.
Richard Anderson Lecture 14, Winter 2019 Divide and Conquer
Richard Anderson Lecture 14 Divide and Conquer
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

CIT 596 Complexity

Tight bounds Is mergesort O(n 4 )? YES! But it is silly to use that as your measure More common to hear it as….? What about quicksort? Best case? Worst case? Average case? So why is quicksort so popular anyway?

Analyzing a recursive algorithm Given a recursive function, generally easy to write a recursive relationship for the complexity Solving a recursion means expressing the recursive recursion explicitly as a function of the input size T(n) = T(n-1) + 1 being converted to T(n) = n 2 Induction CS analysts are notorious for ‘guess and check and prove by induction’. T(n) <= 12n/5 + T(n/5) + T(7n/10)

Master Theorem Theorem: Let a ≥ 1 and b > 1 be constants, let f(n) be a function and let T(n) be defined on the nonnegative integers by recurrence T(n) = aT(n/b) + f(n), Then T(n) has the following asymptotic bounds. Case 1. If for some constant > 0, then Case 2. If then Case 3. If for some constant > 0, and if a f(n/b) ≤ c f(n) for some constant c < 1 and all sufficiently large n, then T(n) = Θ(f(n)).

Analyzing an iterative algorithm Nested loops What is the big-Oh of CYK parsing? What is the time taken dependent on? Size of the string that is going to be parsed! Number of rules that you have to check (pseudocode from the wikipedia implementation) for each i = 2 to n -- Length of span for each j = 1 to n-i+1 -- Start of span for each k = 1 to i-1 -- Partition of span for each rule A -> BC

Adapting an existing algorithm Finding the median of some array Remember what a median is? Median of [1,10,12,15,3]? So if we sort life is good! Is sorting = overkill? Median of medians algorithm

Adapting an existing algorithm Can we adapt quicksort to do median finding? What is the first step of quicksort? Pivot Always recurse on the larger array Remember to search for the right element! This method is called QuickSelect

public static int quickSelect(int[] data, int first, int last, int k) { if (first >= last) return first; // Pick up a random pivot and swap it to the first position int r = (int) (Math.random() * (last - first + 1)) + first; swap(data, first, r); int pivot = partition(data, first, last); int len = pivot - first;// length of the left part if (len > k) return quickSelect(data, first, pivot - 1, k); if (len < k) return quickSelect(data, pivot + 1, last, k - len - 1); // pivot - first == k return pivot; }

Complexity of quickselect Best case? What is the time taken for doing 1 partition Worst case? Horrible partitioning = … Average case analysis T(n) = n + T(n/2) (on average we can kinda/sorta assume we search half the elements Expanding this out gives you O(n)

Adapting a known algorithm Number of inversions of an array If A[1..n] is an array of distinct numbers. If i A[j] then the pair (i,j) is called an inversion of A [2,3,8,6,1] has = 5 inversions Can we do divide and conquer? How many inversions are there in this case?

How many inversions while merging? Let the ‘left half’ be represented by A ‘right half’ be represented by B We have recursively sorted A and B If A[i] > B[j] then we know that all elements in A beyond A[i] contribute an inversion

Counting Inversions Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array Total: i = 6

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. i = 6 two sorted halves 2 auxiliary array Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves 2 auxiliary array i = 6 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 6 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves 23 auxiliary array i = 5 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 5 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves 723 auxiliary array i = 4 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 4 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 3 Total: 6 6 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 3 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 3 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 3 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 2 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 1 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 1 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 0 Total: first half exhausted 6322 Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 0 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 0 Total: Counting Inversions

Merge and count step. Given two sorted halves, count number of inversions where a i and a j are in different halves. Combine two sorted halves into sorted whole. two sorted halves auxiliary array i = 0 Total: = Counting Inversions

Data representation makes a difference How to compute the running time of DFS for a graph? Does the representation of the graph matter? What graph representations have you learnt? Adjacency matrix Adjacency list Incidence matrix? Which representation is easier to use in this situation? Each vertex has to be labeled(marked as visited) Each edge has to be labeled (marked as visited) O(number of vertices + number of edges) What is it with an adjacency matrix? O(number of vertices * number of vertices)

Some common complexities No need to memorize them for this course Some tech companies might ask them, so might be worth adding it to your preparation.

How is this related to Automata/CFG etc Why do we study DFA/NFA, then CFG/PDA and then Turing Machines in that order? Reason 1 – set containment. Regular => Context Free => Turing recognizable Reason 2 - That is also the increasing order of complexity Remember JFLAP throwing alerts at you for PDAs?

Next time The class NP and NP-completeness