Software Design Analysis of Algorithms i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Marti Hearst, or Goodrich & Tamassia.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Computer Organization Computer Architecture, Data Representation, Information Theory i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst,
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts COMP-2001 L4&5 Portions.
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Computer Organization Boolean Logic and the CPU i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
I206: Distributed Computing Applications & Infrastructure Fall 2010.
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.
Review for Test 2 i206 Fall 2010 John Chuang. 2 Topics  Operating System and Memory Hierarchy  Algorithm analysis and Big-O Notation  Data structures.
Analysis of Algorithms
Analysis of Algorithms (pt 2) (Chapter 4) COMP53 Oct 3, 2007.
Software Design i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, Marti Hearst, or James Landay.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
Inter-Process Communication i206 Fall 2010 John Chuang Some slides adapted from Coulouris, Dollimore and Kindberg; Calvert and Donahoo.
Operating System & Memory Hierarchy i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Course Review i206 Fall 2010 John Chuang. 2 Outline  Test 3 topics  Course review  Course evaluation.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
The Seven Functions. Analysis of Algorithms. Simple Justification Techniques. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer Goodrich,
Elementary Data Structures and Algorithms
Analysis of Algorithms1 CS5302 Data Structures and Algorithms Lecturer: Lusheng Wang Office: Y6416 Phone:
Distributed Systems & Networks i206 Fall 2010 John Chuang Some slides adapted from Coulouris, Dollimore and Kindberg.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 11: Analysis of Algorithms, cont.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Networking and Internetworking i206 Fall 2010 John Chuang.
Analysis of Performance
1 i206: Lecture 6: Math Review, Begin Analysis of Algorithms Marti Hearst Spring 2012.
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
Analysis of Algorithms Lecture 2
Analysis of Algorithms
Analysis Tools Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Analysis of Algorithms
Analysis of Algorithms1 The Goal of the Course Design “good” data structures and algorithms Data structure is a systematic way of organizing and accessing.
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)
Data Structures Lecture 8 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Analysis of Algorithms1 Running Time Pseudo-Code Analysis of Algorithms Asymptotic Notation Asymptotic Analysis Mathematical facts.
1 i206: Lecture 18: Regular Expressions Marti Hearst Spring 2012.
Analysis of Algorithms
1 i206: Lecture 9: Review; Intro to Algorithms Marti Hearst Spring 2012.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Data Structure Introduction.
Zeinab EidAlgorithm Analysis1 Chapter 4 Analysis Tools.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Analysis of Algorithms Algorithm Input Output © 2010 Goodrich, Tamassia1Analysis of Algorithms.
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.
CSC Devon M. Simmonds University of North Carolina, Wilmington CSC231 Data Structures.
Algorithm Analysis (Big O)
Analysis Tools. Acknowledgement Ms. Krishani Abeysekera Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Dr. Michael Goodrich.
Announcement We will have a 10 minutes Quiz on Feb. 4 at the end of the lecture. The quiz is about Big O notation. The weight of this quiz is 3% (please.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
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.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
COMP9024: Data Structures and Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
GC 211:Data Structures Algorithm Analysis Tools
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
i206: Lecture 20: Memory Management; Operating Systems
i206: Lecture 8: Sorting and Analysis of Algorithms
Revision of C++.
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Software Design Analysis of Algorithms i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Marti Hearst, or Goodrich & Tamassia

John Chuang2 Analysis of Algorithms Bits & Bytes Binary Numbers Number Systems Gates Boolean Logic Circuits CPU Machine Instructions Assembly Instructions Program Algorithms Application Memory Data compression Compiler/ Interpreter Operating System Data Structures Analysis I/O Memory hierarchy Design Methodologies/ Tools Process Truth table Venn Diagram DeMorgan’s Law Numbers, text, audio, video, image, … Decimal, Hexadecimal, Binary AND, OR, NOT, XOR, NAND, NOR, etc. Register, Cache Main Memory, Secondary Storage Context switch Process vs. Thread Locks and deadlocks Op-code, operands Instruction set arch Lossless v. lossy Info entropy & Huffman code Adders, decoders, Memory latches, ALUs, etc. Data Representation Data storage Principles ALUs, Registers, Program Counter, Instruction Register Network Distributed Systems Security Cryptography Standards & Protocols Inter-process Communication Searching, sorting, Encryption, etc. Stacks, queues, maps, trees, graphs, … Big-O UML, CRC TCP/IP, RSA, … Confidentiality Integrity Authentication … C/S, P2P Caching sockets Formal models Finite automata regex

John Chuang3 Traveling Salesman Problem

John Chuang4 Which Algorithm is Better? What do we mean by “Better”?  Sorting algorithms -Bubble sort -Insertion sort -Shell sort -Merge sort -Heapsort -Quicksort -Radix sort -…  Search algorithms -Linear search -Binary search -Breadth first search -Depth first search -…

John Chuang5 Analysis  Characterizing the running times of algorithms and data structure operations  Secondarily, characterizing the space usage of algorithms and data structures

John Chuang6 Running Time  In general, running time increases with input size  Running time also affected by: -Hardware environment (processor, clock rate, memory, disk, etc.) -Software environment (operating system, programming language, compiler, interpreter, etc.) (n)

John Chuang7 Quantifying Running Time  Experimentally measure running time -Need to fully implement and execute  Analysis of pseudo- code (n)

John Chuang8 Pseudo-code Analysis Example Algorithm: arrayMax(A,n): Input: An array A storing n >= 1 integers Output: the maximum element in A currentMax = A[0] for i=1 to (n-1) do if currentMax < A[i] then currentMax = A[i] Return currentMax

John Chuang9 The Seven Common Functions  Constant  Linear  Quadratic  Cubic  Exponential  Logarithmic  n-log-n Polynomial

John Chuang10 The Seven Common Functions  Constant: f(n) = c -E.g., adding two numbers, assigning a value to some variable, comparing two numbers, and other basic operations  Linear: f(n) = n -Do a single basic operation for each of n elements, e.g., reading n elements into computer memory, comparing a number x to each element of an array of size n

John Chuang11 The Seven Common Functions  Quadratic: f(n) = n 2 -E.g., nested loops with inner and outer loops -E.g., insertion sort algorithm  Cubic: f(n) = n 3  More generally, all of the above are polynomial functions: -f(n) = a 0 + a 1 n + a 2 n 2 + a 3 n 3 + … + a n n d -where the a i ’s are constants, called the coefficients of the polynomial Brookshear Figure 5.19

John Chuang12 The Seven Common Functions  Exponential: f(n) = b n -Where b is a positive constant, called the base, and the argument n is the exponent -In algorithm analysis, the most common base is b=2 -E.g., loop that starts with one operation and doubles the number of operations with each iteration -Similar to the geometric sum: …+2 n-1 = 2 n – 1 -BTW, 2 n – 1 is the largest integer that can be represented in binary notation using n bits

John Chuang13 The Seven Common Functions  Logarithmic: f(n) = log b n -Intuition: number of times we can divide n by b until we get a number less than or equal to 1 -E.g., the binary search algorithm has a logarithmic running time -The base is omitted for the case of b=2, which is the most common in computing: -log n = log 2 n Brookshear Figure 5.20

John Chuang14 The Seven Common Functions  n-log-n function: f(n) = n log n -Product of linear and logarithmic -E.g., quicksort algorithm has n log n running time on average (but n 2 in worst case)

John Chuang15 Comparing Growth Rates  1 < log n < n 1/2 < n < n log n < n 2 < n 3 < b n

John Chuang16 Polynomials  Only the dominant terms of a polynomial matter in the long run. Lower-order terms fade to insignificance as the problem size increases.

John Chuang17 Example: Counting Primitive Operations Algorithm: arrayMax(A,n): Input: An array A storing n >= 1 integers Output: the maximum element in A currentMax = A[0] for (i=1; i<=n-1; i++) if currentMax < A[i] then currentMax = A[i] Return currentMax Two operations: indexing into array; assign value to variable Two operations repeated n times: subtract, compare One operation: assign value to variable Four or six operations repeated (n-1) times: - index and compare (for the if statement); - index and assign (for the then statement if necessary) - addition and assign (for the increment) One operation: return value of variable Total: n + 4(n-1) + 1 = 6n (best case) Total: n + 6(n-1) + 1 = 8n – 2 (worst case)

John Chuang18 Big-O Notation  Asymptotic analysis -As n becomes large and approaches infinity  Let f(n) and g(n) be functions mapping non- negative integers to real numbers  Then f(n) is O(g(n)) or “f(n) is big-Oh of g(n)”  If there is a real constant c > 0 and an integer constant n 0 >=1 such that -f(n) = n 0  Example: the function f(n)=8n–2 is O(n)  The running time of algorithm arrayMax is O(n)

John Chuang19 Example 1: Search  Given: -A physical phone book -Organized in alphabetical order -A name you want to look up -An algorithm in which you search through the book sequentially, from first page to last: Sequential Search or Linear Search -What is the order of: -The best case running time? -The worst case running time? -The average case running time? -What is: -A better algorithm? -The worst case running time for this algorithm?

John Chuang20 Example 1: Search  This better algorithm is called Binary Search  What is its running time? -First you look in the middle of n elements -Then you look in the middle of n/2 = ½*n elements -Then you look in the middle of ½ * ½*n elements -… -Continue until you find the target element, or until there is only 1 element left -Say you did this m times: ½ * ½ * ½* …*n -Then the number of repetitions is the smallest integer m such that

John Chuang21 Binary Search Pseudo-code Brookshear Figure 5.14

John Chuang22 Analyzing Binary Search -In the worst case, the number of repetitions is the smallest integer m such that -We can rewrite this as follows: Multiply both sides by Take the log of both sides Since m is the worst case time, the algorithm is O(log n)

John Chuang23 Example 2: Insertion Sort is O(n 2 ) Brookshear Figure 5.19 Brookshear Figure 5.10 Brookshear Figure 5.11

John Chuang24 Example 3: Prefix Average Compute running average of a sequence of numbers /1 15/2 30/3 50/4 75/5 105/6 There are two straightforward algorithms: The first is easy but wasteful. The second is more efficient, but requires insight into the problem.  j=0 X[j] i i+1 A[i] =

John Chuang25 1 st Algorithm Algorithm: prefixAverages1(X): Input: An n-element array X of numbers Output: An n-element array A of numbers such that A[i] is the average of elements X[0],…X[i] Let A be an array of n numbers For i  0 to n-1 do a  0 For j  0 to i do a  a+X[j] A[i]  a/(i+1) Return array A  j=0 X[j] i i+1 A[i] =

John Chuang26 2 nd Algorithm Algorithm: prefixAverages2(X): Input: An n-element array X of numbers Output: An n-element array A of numbers such that A[i] is the average of elements X[0],…X[i] Let A be an array of n numbers s  0 For i  0 to n-1 do s  s + X[i] A[i]  s/(i+1) Return array A A useful tool: store intermediate results in a variable! Uses space to save time. The key – don’t divide s. Eliminates one for loop – always a good thing to do.

John Chuang27 Summary: Analysis of Algorithms  A method for determining the asymptotic running time of an algorithm -Here asymptotic means “as n gets very large”  Useful for comparing algorithms  Useful also for determining tractability -E.g., Exponential time algorithms are usually intractable.

John Chuang28 Traveling Salesman Problem