Elementary maths for GMT. Algorithm analysis Part II

Similar documents
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Algorithms and Data Structures

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Lecture 5: Running Time Evaluation

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Notes and Answers to Homework Exam 1, Geometric Algorithms, 2017

Classic Data Structures Introduction UNIT I

Computer Science Spring 2005 Final Examination, May 12, 2005

Data Structures and Algorithms Key to Homework 1

COMP Data Structures

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

DESIGN AND ANALYSIS OF ALGORITHMS

1 Definition of Reduction

Examination Questions Midterm 2

Elementary maths for GMT. Algorithm analysis Part I

Algorithmic Analysis. Go go Big O(h)!

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Intro to Algorithms. Professor Kevin Gold

Algorithm Analysis. Part I. Tyler Moore. Lecture 3. CSE 3353, SMU, Dallas, TX

O(n): printing a list of n items to the screen, looking at each item once.

Multiple-choice (35 pt.)

Data Structures and Algorithms

Computational Geometry

Computer Science Approach to problem solving

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

CSci 231 Final Review

Algorithm Design and Time Analysis

Greedy algorithms is another useful way for solving optimization problems.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

UNIT 1 ANALYSIS OF ALGORITHMS

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Time and space behaviour. Chapter 20

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Measuring algorithm efficiency

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Sorting (Weiss chapter )

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

COSC 311: ALGORITHMS HW1: SORTING

Line segment intersection. Family of intersection problems

Course Review for Finals. Cpt S 223 Fall 2008

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Recitation 9. Prelim Review

CPS 616 TRANSFORM-AND-CONQUER 7-1

Week 12: Running Time and Performance

Computational Complexity Analysis

Sorting Algorithms. Dipartimento di Elettronica e Informazione Politecnico di Milano June 5, 2018

Recall from Last Time: Big-Oh Notation

Today. Types of graphs. Complete Graphs. Trees. Hypercubes.

Introduction to Computers & Programming

(Refer Slide Time: 1:27)

CS:3330 (22c:31) Algorithms

CS61BL. Lecture 5: Graphs Sorting

4.4 Algorithm Design Technique: Randomization

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Lower and Upper Bound Theory. Prof:Dr. Adnan YAZICI Dept. of Computer Engineering Middle East Technical Univ. Ankara - TURKEY

CS240 Fall Mike Lam, Professor. Algorithm Analysis

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Computational Geometry

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Introduction to Asymptotic Running Time Analysis CMPSC 122

1. Attempt any three of the following: 15

Review for Midterm Exam

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

CISC 1100: Structures of Computer Science

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

CSC Design and Analysis of Algorithms

Assignment 1 (concept): Solutions

Basic Combinatorics. Math 40210, Section 01 Fall Homework 4 Solutions

CmpSci 187: Programming with Data Structures Spring 2015

Complexity of Algorithms. Andreas Klappenecker

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Selection (deterministic & randomized): finding the median in linear time

Mergesort again. 1. Split the list into two equal parts

What is an Algorithm?

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Practice Problems for the Final

Analysis of Algorithms. 5-Dec-16

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

1 The sorting problem

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CPSC W2: Quiz 2 Sample Solutions

Transcription:

Elementary maths for GMT Algorithm analysis Part II

Algorithms, Big-Oh and Big-Omega An algorithm has a O( ) and Ω( ) running time By default, we mean the worst case running time A worst case O running time is a statement about all possible inputs A worst case Ω( ) running time is a statement about one input 2

Algorithms, Big-Oh and Big-Omega Consider the following BubbleSort algorithm Algorithm BubbleSort(X) Input array X of n integers Output the sorted version in array X for i 1 to n 1 do j i while ( j > 0 ) and X [j] < X [j-1] do swap X [j] and X [j-1] j j 1 return X 3

Algorithms, Big-Oh and Big-Omega If X is already sorted, then BubbleSort runs in O(n) time If X is sorted in reverse order, then BubbleSort runs in O(n 2 ) time If X is in any other permutation, the running time is somewhere in between The worst case running time is O(n 2 ) 4

Algorithms, Big-Oh and Big-Omega If X is already sorted, then BubbleSort runs in Ω(n) time we can claim Ω(n) running time in the worst case If X is sorted in reverse order, then BubbleSort runs in Ω(n 2 ) time we can claim Ω(n 2 ) running time in the worst case (which is a stronger claim) Since the running time is also O(n 2 ) in the worst case, we cannot find an even worse input 5

Algorithms, Big-Oh and Big-Omega Since the worst case running time is Ω(n 2 ) and O n 2, the running time is also Θ(n 2 ) in the worst case The worst case running time bound is tight if the upper bound and lower bound match Is it possible that an algorithm has a worst case running time of Ω(n 2 ) and O n 3? n 3 O(n 3 ) Ω(n 2 ) n 2 6

Algorithms, Big-Oh and Big-Omega Since the worst case running time is Ω(n 2 ) and O n 2, the running time is also Θ(n 2 ) in the worst case The worst case running time bound is tight if the upper bound and lower bound match Is it possible that an algorithm has a worst case running time of Ω(n 2 log n) and O n 2? Ω(n 2 log n) n 2 log n n 2 O(n 2 ) 7

Basic algorithm problems The problem of sorting a set of numbers is perhaps the most fundamental algorithmic problem InsertionSort and BubbleSort are simple incremental algorithms that take Θ(n 2 ) time in the worst case MergeSort and QuickSort are based on a divide-andconquer approach and take Θ(n log n) time in the worst case CountingSort takes Θ(n) time but only works for integers that are not too large Is it possible that any sorting algorithm is even faster than Θ(n) time? 8

Basic algorithm problems The problem of storing a set of numbers for efficient searching is the most fundamental data structuring problem A sorted array allows for binary search, which take Θ(log n) time (binary search is a search algorithm for a single number in a sorted set) In an unsorted array, searching cannot be faster than Θ(n) time Hash tables are specifically organized arrays that allow searching in Θ(1) time in practice, but not as a worst case bound 9

Recall: important functions Function Time Usage in Constant O 1 initialization of a variable Logarithmic O log n searching in a sorted set Linear O n A full scan over the input N-Log-N O n log n sorting a set Quadratic O n 2 nested loops Cubic O n 3 one deeper nesting Exponential O 2 n all subsets of a set Factorial O n! all ordering of a set 10

Different steps in an algorithm Consider the problem: given a set of n numbers, are any two equal? Example: 4, 6, 14, 3, 7, 97, 56, -4, 89, 34, 8, 14, -23, 88 Solution 1 The intuitive way: consider all pairs of numbers and test each pair Result in a O(n 2 ) algorithm (nested loops) Solution 2 The sort&search approach: sort the numbers with MergeSort or QuickSort (step 1) and then scan (step 2) to see if two adjacent numbers are equal Step 1 takes O(n log n) time and step 2 takes O(n) time In total O(n log n) + O n = O(n log n + n) = O(n log n) time 11

Different steps in an algorithm An algorithm has different steps if it has subtasks and each subtask is completely finished before the next one begins We analyze each subtask separately and add up their running times With Big-Oh notation and removal of constants and lowerorder terms, this implies that the most time expensive subtask determines the efficiency of the whole algorithm 12

Different steps in an algorithm Compare the two following algorithms Algorithm Loops1(X) Input array X of n integers Output irrelevant for i 1 to n do some computations for j 1 to n do some computations return something Algorithm Loops2(X) Input array X of n integers Output irrelevant for i 1 to n do some computations for j 1 to n do some computations return something What is their running time? 13

More example problems Given a set of n numbers, can we split them in two subsets with the same summed value? set: -18, 4, 22, 14, 2, 7, 97, 56, -6, 88, 34, 9, 17, -23, 69 total sum is 372, half is 186 One solution: -23,2,22,88,97 and -18,-6,4,7,9,14,17,34,56,69 14

Another nested-loops example Analyze the following algorithm Algorithm SumOccurs(X, m) Input array X of n integers and an integer m Output true if X[i] + X[j] = m for some i!= j MergeSort(X) i 0 j n - 1 while (i < j) do while (X[i] + X[j] > m) do j j - 1 if (X[i] + X[j] = m) then return true i i +1 return false 15

Another nested-loops example Nested loops (both over the input size) do not always give a worst case quadratic running time When not, you need a different argument to bound the number of times the inner loop is executed This involves understanding what the algorithm precisely does If you designed the algorithm, you (should) understand what it does Otherwise, applying the algorithm to some example input helps to understand how the algorithm works 16

Graphs and representations A graph G = (V, E) consists of a set V of vertices and a set E of edges Abstractly speaking, vertices are elements and edges are pairs of elements One can draw a graph by giving coordinates to the vertices, but any graph exists without coordinates Example V = {1, 2, 3, 4, 5, 6, 7} E = { 1,2, 1,3, 2,3, 2,5, 3,5, 4,5, 3,6, 3,7, (5,6)} 1 4 2 5 3 6 7 17

Graphs and representations The (input) size of a graph is expressed as the number of vertices and the number of edges: V = n and E = m Question: what is the minimum and maximum number of edges a graph with n vertices can have? 18

Graphs and representations A graph G = (V, E) is planar if it can be drawn in the plane without any edge-edge intersections Is this graph planar? For planar graphs, it is known that m 3n 5 In other words, the number of edges of a planar graph with n vertices is O(n) Big-Oh notation is not used only for running time statements 1 4 2 5 3 6 7 19

Graphs and representations Subdivisions of the plane can be represented with graphs, if we give coordinates to each vertex Road networks are also graphs that have vertices with coordinates 20

Graphs and representations A common representation of a graph is the adjacency matrix, a n x n matrix of zeroes and ones with a one at (i,j) if and only if (i,j) is an edge in E 1 4 2 5 V = {1, 2, 3, 4, 5, 6, 7} E = { 1,2, 1,3, 2,3, 2,5, 3 6 3,5, 4,5, 3,6, 3,7, (5,6)} 7 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 21

Graphs and representations Some questions Suppose that a graph G with n vertices and m edges is given. How much storage space does the adjacency matrix representation of G need? What if G is planar? Can we use Big-Oh notation to state this? Is the adjacency matrix representation suitable for representing planar graphs? 22

Graphs and representations A different common representation for graphs is the adjacency list representation It consists of an array A[1 n], with one entry for each vertex, with access to a list of neighbors of that vertex 1 2 3 7 1 2 3 4 5 6 7 2 1 1 5 3 3 6 5 4 V = {1, 2, 3, 4, 5, 6, 7} 3 3 5 2 5 2 3 5 E = { 1,2, 1,3, 2,3, 2,5, 6 4 3,5, 4,5, 3,6, 3,7, (5,6)} 7 6 23

Graphs and representations What are the storage requirements of an adjacency list representation of a graph G with n vertices and m edges? O(n + m) Do we really need the n and the m in the storage bound (for example, would O(n) or O(m) be correct)? We really need both a graph with n vertices and n(n 1) Θ m = Θ n 2 storage, and this is not O(n) 2 edges (all possible edges) needs a graph with n vertices but no edges needs Θ n storage, and this is not O(m) since m = 0 24

Graphs and representations What are the advantages and the disadvantages of the adjacency matrix and adjacency list representations? 25

Graphs and representations Graphs often have weighted edges The weight may represent the distance between the incident vertices, the travel time, the capacity, the cost... In an adjacency matrix, we can simply store the weight of an edge (i,j) in the matrix (if no edge is present, we need to use a special value that does not occur as a weight) In an adjacency list, we store twice the weight of an edge with j in the list of i with i in the list of j 26

Graphs and representations 1 4 2 2.5 2.0 3 3.4 3.3 2.1 3.2 5 2.2 6 1 2 3 4 5 6 2.5 3.4 2.5 2.0 3.3 3.4 2.0 2.1 3.2 3.3 3.2 2.2 2.1 2.2 2 2.5 1 2.5 1 3.4 5 3.2 2 3.3 3 2.1 3 3.4 3 2.0 2 2.0 4 3.2 5 2.2 5 3.3 6 2.1 6 2.2 27

Graphs and representations The most important algorithmic problem on (weighted) graphs is computing shortest paths (sequences of edges with minimum sum of weights) A famous algorithm is Dijkstra s algorithm (1959), where a shortest path between two given vertices in a given weighted graph is computed in O(n + m log m) time What graph representation is assumed when we state this time bound? 28

Some graph problems Given a graph decide if a tour exists that visits every edge exactly once decide if a tour exists that visit every vertex exactly once find the largest completely interconnected sub-graph find the largest non-connected sub-graph determine the minimum number of colors to color the vertices so that neighbors have different colors Given a planar graph, determine if the vertices can be colored using two/three/four colors so that neighbors have different colors 29

A geometric problem Assume that a computer (model) can do additions, subtractions, multiplications, divisions and memory reads and writes in constant time each Given a simple polygon with n vertices, is it algorithmically easier to compute its area or its perimeter? 30