Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.

Slides:



Advertisements
Similar presentations
Discrete Structures CISC 2315
Advertisements

Complexity, Origami, etc. Big Oh Traditional Origami Fold and cut.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Asymptotic Growth Rate
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
CS421 - Course Information Website Syllabus Schedule The Book:
Analysis of Algorithms Review COMP171 Fall 2005 Adapted from Notes of S. Sarkar of UPenn, Skiena of Stony Brook, etc.
Analysis of Algorithms (Chapter 4)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Analysis of Algorithms
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
Data Structures Review Session 1
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithm.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
David Luebke 1 8/17/2015 CS 332: Algorithms Asymptotic Performance.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
1 Complexity Lecture Ref. Handout p
Program Performance & Asymptotic Notations CSE, POSTECH.
Week 2 CS 361: Advanced Data Structures and Algorithms
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Mathematics Review and Asymptotic Notation
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Iterative Algorithm Analysis & Asymptotic Notations
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Asymptotic Analysis-Ch. 3
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Algorithm Analysis Part of slides are borrowed from UST.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
David Luebke 1 1/6/2016 CS 332: Algorithms Asymptotic Performance.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Algorithm Analysis (Big O)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Analysis of Algorithms
CS 3343: Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Introduction to Algorithms Analysis
Data Structures Review Session
Design and Analysis of Algorithms
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis

eleg667/2001-f/Topic-1a2 Outline  Basic concepts;  Case study;  Analyzing algorithms  Comparing algorithms  Problem complexity

eleg667/2001-f/Topic-1a3 Algorithm - Definition Sequence of steps to carry out a particular task Examples: Recipe; Assembly instructions; ……

eleg667/2001-f/Topic-1a4 Characteristics of Algorithms  Algorithms are precise. Each step has a clearly defined meaning;  Algorithms are effective. The task is always done as required;  Algorithms have a finite number of steps;  Algorithms must terminate.

eleg667/2001-f/Topic-1a5 Any algorithm can be stated using the following logic constructs  Sequence: steps must follow each other in a logical sequence;  Decision: there may be alternative steps that may be taken subject to a particular condition;  Repetition: certain steps may be repeated while, or until, a certain condition is true.

eleg667/2001-f/Topic-1a6 Why study algorithms? A motivating example…

eleg667/2001-f/Topic-1a7 Example: sorting playing cards Start with one card on the left hand and the others cards facing down on the table; while there are cards left on the table do { take one card from the table; compare it with each of the cards already in the hand, from left to right, inserting it into the right position; } Sequence Decision Repetition Insertion Sort

eleg667/2001-f/Topic-1a8 Expressing computer algorithms  It should be expressed in a language more precise, less ambiguous, and more compact than a “natural language” such as English;  Algorithms are usually written in a pseudocode language and later translated to a real programming language.

eleg667/2001-f/Topic-1a9 Insertion Sort – An Intuitive Picture j QK 5 … Q i key A A1A1 …

eleg667/2001-f/Topic-1a10 Insertion Sort in Pseudocode Insertion-Sort(A) ; A is an array of numbers for j = 2 to length(A) key = A[j] i = j - 1 while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key SequenceRepetition Decision

eleg667/2001-f/Topic-1a11 Algorithms and Machine Models  Sequential algorithms and machine models  Parallel algorithms and machine models

eleg667/2001-f/Topic-1a12 Analyzing algorithms  Estimate the running time of algorithms; = F(Problem Size) = F(Input Size) = number of primitive operations used (add, multiply, compare etc)

eleg667/2001-f/Topic-1a13 Analysis for Insertion Sort Insertion-Sort(A)CostTimes ( Iterations ) 1 for j = 2 to length(A) { c 1 2 key = A[j] c 2 3 i = j – 1 c 3 4 while i > 0 and A[i] > key c 4 5 A[i+1] = A[i] c 5 6 i = i – 1 c 6 7 A[i+1] = key c 7 } n n - 1

eleg667/2001-f/Topic-1a14 Insertion Sort Analysis (cont.) Best Case: Array already sorted, tj = 1 for all j

eleg667/2001-f/Topic-1a15 Insertion Sort Analysis (cont.) Worst Case: Array in reverse order, tj = j for all j Note that

eleg667/2001-f/Topic-1a16 Insertion Sort Analysis (cont.) Average Case: Check half of array on average, tj = j/2 for all j T(n) = a.n 2 + bn + c (quadractic in n) We are usually interested in the worst-case running time

eleg667/2001-f/Topic-1a17 Insertion Sort demo…

eleg667/2001-f/Topic-1a18 Growth rates  constant growth rate: T(n) = c  linear growth rate : T(n) = c*n  logarithmic growth rate : T(n) = c*log n  quadratic growth rate : T(n) = c*n 2  exponential growth rate : T(n) = c*2 n

eleg667/2001-f/Topic-1a19 Asymptotic Analysis  Ignoring constants in T(n)  Analyzing T(n) as n "gets large" Example: The big-oh (O) Notation

eleg667/2001-f/Topic-1a20 Formally… T(n) = O(f(n)) if there are constants c and n such that T(n) < c.f(n) when n  n 0 c.f(n) T(N) n0n0 n f(n) is an upper bound for T(n)

eleg667/2001-f/Topic-1a21 O ( big-oh)  Describes an upper bound for the running time of an algorithm Upper bounds for Insertion Sort running times: worst case: O(n 2 ) T(n) = c 1.n 2 + c 2.n + c 3 best case: O(n) T(n) = c 1.n + c 2 average case: O(n 2 ) T(n) = c 1.n 2 + c 2.n + c 3 Time Complexity

eleg667/2001-f/Topic-1a22 Some properties of the O notation  Fastest growing function dominates a sum O(f(n)+g(n)) is O(max{f(n), g(n)})  Product of upper bounds is upper bound for the product If f is O(g) and h is O(r)  then fh is O(gr)  f is O(g) is transitive If f is O(g) and g is O(h) then f is O(h)  Hierarchy of functions O(1), O(logn), O(n 1/2 ), O(nlogn), O(n 2 ), O(2 n ), O(n!)

eleg667/2001-f/Topic-1a23 Times on a 1-billion-steps-per-second computer

eleg667/2001-f/Topic-1a24  Simple statement sequence s 1 ; s 2 ; …. ; s k O(1) as long as k is constant  Simple loops for(i=0;i<n;i++) { s; } where s is O(1) Time complexity is n O(1) or O(n) Analyzing an Algorithm

eleg667/2001-f/Topic-1a25 Analyzing an Algorithm (cont.)  Nested loops for(i=0;i<n;i++) for(j=0;j<n;j++) { s; } Complexity is n O(n) or O(n 2 )  Recursion Search(n) if middle_element = key return it else if it’s greater search_left(n/2) else search_right(n/2) Solve recurrence equation: T(n) = T(1) + T(n/2) Complexity is O(log 2 n)

eleg667/2001-f/Topic-1a26 Reasonable and Unreasonable Algorithms  Polynomial Time algorithms An algorithm is said to be polynomial if it is O( n c ), c >1 Polynomial algorithms are said to be reasonable They solve problems in reasonable times!  Exponential Time algorithms An algorithm is said to be exponential if it is O( r n ), r > 1 Exponential algorithms are said to be unreasonable

eleg667/2001-f/Topic-1a27 Another example: Quick Sort  Uses recursion to repetitively divide list in 1/2 and sort the two halves  QuickSort partitions the list into a left and a right sublist, where all values in the left sublist are less than all values in the right sublist. Then the QuickSort is applied recursively to the sublists until each sublist has only one element.

eleg667/2001-f/Topic-1a28 Quick Sort (cont.) becomes becomes

eleg667/2001-f/Topic-1a29 Quick Sort (cont.) QuickSort (list(0,n)): If length (list) > 1 then partition into 2 sublists from 0..split-1 and from split + 1..n QuickSort (list (0,split-1)) QuickSort (list (split + 1, n)

eleg667/2001-f/Topic-1a30 1. Pick an array value, pivot. 2. Use two indices, i and j. a. Begin with i = left and j =right + 1 b. As long as i is less than j do 3 steps: i. Keep increasing i until we come to an element A[i] >= pivot. ii. Keep decreasing j until we come to an element A[j] <= pivot. iii. Swap A[i] and A[j]. Quick Sort – Partition phase

eleg667/2001-f/Topic-1a31 The Operation of Quicksort

eleg667/2001-f/Topic-1a32 Quick Sort – Complexity  Assume the list size (n) is a power of 2 and that pivot is always chosen such that it divides the list into two equal pieces  At each step we are cutting in half the size to be sorted  O(nlog 2 n)

eleg667/2001-f/Topic-1a33 Quick Sort demo…

eleg667/2001-f/Topic-1a34 Comparing the Sort Algorithms…

eleg667/2001-f/Topic-1a35 Problem complexity problems sort... InsertionShellsort Quicksort Algorithms:

eleg667/2001-f/Topic-1a36 Definitions  A problem’s upper bound refers to the best that we know how to do. It is determined by the best algorithmic solution that we have found for a given problem. Example: For the sorting problem O(n.logn) is the upper bound.

eleg667/2001-f/Topic-1a37 Definitions (cont.)  A problem’s lower bound refers to the best algorithmic solution that is theoretically possible. Example: It can be proved that sorting an unsorted list cannot be done in less than O(n.log n), unless we make assumptions about the particular input data. Thus O(n.log n) is the lower bound for sorting problems.

eleg667/2001-f/Topic-1a38 Definitions (cont.)  Upper bounds tell us the best we’ve been able to do so far;  Lower bounds tell us the best we can hope to do. For a given problem, if the upper and lower bounds are the same, then we refer to that problem as a closed problem.

eleg667/2001-f/Topic-1a39 Tractable and Intractable problems  Tractable problems have both upper and lower bounds polynomial – O(logn), O(n), O(n.logn), O(n k );  Intractable problems have both upper and lower bounds exponential - O(2 n ), O(n!), O(n n );

eleg667/2001-f/Topic-1a40 Problems that cross the line  We have found only exponential solutions (i.e. from the standpoint of our algorithms, it appears to be intractable);  We cannot prove the necessity of an exponential solution (i.e. from the standpoint of our proofs, we cannot say that it is intractable).

eleg667/2001-f/Topic-1a41 NP-complete problems  The upper bound suggests that the problem is intractable;  No proof exists that shows these problems are intractable;  Once a solution is found, it can be checked in polynomial time;  They are all reducible to one another;

eleg667/2001-f/Topic-1a42 Example  Traveling salesman: give a weighted graph (e.g., cities with distances between them) and some distance k (e.g., the maximum distance you want to travel), is there some tour that visits all the points (e.g., cities) and returns home such that distance  k?  …

eleg667/2001-f/Topic-1a43 P = NP ? Win the Turing Award and get a $1 million prize!