CS 1704 Introduction to Data Structures and Software Engineering.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

MATH 224 – Discrete Mathematics
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Analysys & Complexity of Algorithms Big Oh Notation.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 1 – Basic Concepts
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Introduction to Analysis of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
COMP s1 Computing 2 Complexity
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Program Performance & Asymptotic Notations CSE, POSTECH.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Mathematics Review and Asymptotic Notation
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
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.
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
Analysis of Algorithms
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
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.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
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.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Introduction to Analysis of Algorithms CS342 S2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Algorithm Analysis Part of slides are borrowed from UST.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
Chapter 2 Algorithm Analysis
Analysis of Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Analysis of Algorithms
GC 211:Data Structures Algorithm Analysis Tools
DATA STRUCTURES Introduction: Basic Concepts and Notations
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
GC 211:Data Structures Algorithm Analysis Tools
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Analysis of Algorithms
At the end of this session, learner will be able to:
Analysis of Algorithms
Presentation transcript:

CS 1704 Introduction to Data Structures and Software Engineering

Recursion A process in which the result of each repetition is dependent upon the result of the next repetition

Recursive Version int Factorial(int n ) { if ( n > 1 ) return( n * Factorial (n-1) ); else return(1); } int Factorial(int n ) { if ( n > 1 ) return( n * Factorial (n-1) ); else return(1); }

Recursion Every recursive algorithm can be implemented non-recursively. –recursion iteration Eventually, the routine must not call itself, allowing the code to "back out". Recursive routines that call themselves continuously are termed: –infinite recursion infinite loop Problem with recursive factorial implementation? Negative numbers! Recursion is inefficient at runtime.

Coding Recursion Solve the trivial "base" case(s). Restate general case in 'simpler' or 'smaller' terms of itself. –Divide and Conquer List Example –Determine the size of a single linked list. Base Case : Empty List, size = 0 General Case : 1 + Size(Rest of List)

Types of Recursion Tail recursive –functions are characterized by the recursive call being the last statement in the function, (can easily be replaced by a loop). Head Recursive –Recursive call is the first statement (maybe after a base case check) Middle Decomposition

Recursion Underpinnings Every instance of a function execution (call) creates an Activation Record, (frame) for the function. –Activation records hold required execution information for functions: Return value for the function Pointer to activation record of calling function Return memory address, (calling instruction address) Parameter storage Local variable storage

Function Estimation If n > 10 then n 2 > 100 If n > 5 then n 2 > 5n Therefore, if n > 10 then: f(n) = 3n 2 + 5n < 3n 2 + n 2 + n 2 = 5n 2 So 5n^2 forms an “upper bound” on f(n) if n is 10 or larger (asymptotic bound). In other words, f(n) doesn't grow any faster than 5n^2 “in the long run”.

Big-Oh Defined To say f(n) is O(g(n)) is to say that f(n) is “less than or equal to” Cg(n) More formally, Let f and g be functions from the set of integers (or the set of real numbers) to the set of real numbers. Then f(x) is said to be O( g(x) ), which is read as f(x) is big-oh of g(x), if and only if there are constants C and n0 such that | f(x) | n0. Don’t be confused … –“f(n) is of Order g(n)”

The trick an k + bn k-1 + … + yn + z an k n k N(N + 1)/2 + N + N 2

Some complexity classes … ConstantO(1) LogarithmicO(log n) LinearO(n)O(n) QuadraticO(n2)O(n2) CubicO(n3)O(n3) PolynomialO(np)O(np) ExponentialO(an)O(an)

Does it matter? Let n = 1,000, & 1 ms / operation. n = 1000, 1 ms/op max n in one day n1 second86,400,000 n log 2 n10 seconds3,943,234 n2n2 17 minutes9,295 n3n3 12 days442 n4n4 32 years96 n  years 6 2n2n 1.07  years 26

Practical Curves log n n n log n n2n2

Best Case Analysis Assumes the input, data etc. are arranged in the most advantageous order for the algorithm, i.e. causes the execution of the fewest number of instructions. –E.g., sorting - list is already sorted; searching - desired item is located at first accessed position.

Worst Case Analysis Assumes the input, data etc. are arranged in the most disadvantageous order for the algorithm, i.e. causes the execution of the largest number of statements. –E.g., sorting - list is in opposite order; searching - desired item is located at the last accessed position or is missing.

Average Case Analysis Determines the average of the running times over all possible permutations of the input data. –E.g., searching - desired item is located at every position, for each search), or is missing.

Sorting Terms & Definitions Internal sorts holds all data in RAM External sorts use Files Ascending Order: –Low to High Descending Order: –High to Low Stable Sort –Maintains the relative order of equal elements, in situ. –Desirable if list is almost sorted or if items with equal values are to also be ordered on a secondary field.

Sorting Thoughts Comparison-Based Sorting –Algorithms which compare element values to each other –What is the minimum number of comparisons, required to sort N elements using a comparison-based sort? Is a Queue a type of sorting?

N log N Since the depth of all leaf nodes is >  log 2 N!  in a comparison tree, the minimal number of comparisons to sort a specific initial ordering of N elements is >  log 2 N!  Stirling’s Approximation for log 2 (N!) can be used to determine a lower bound for log 2 (N!) which is O( N log N ) No comparison based sorting algorithm can sort faster than –O( N log N )

Both Sorting & Searching Know what each algorithm does How it works Big Oh analysis of each

Function Template Example // Assumes << operator for Etype template void PrintArray(const Etype array[], const int count) { int i;//array index for ( i = 0; i < count; i++) cout << array[i] << “ “; cout << endl; }

What won’t be on the test C-Style I/O Multiple function parameters Politics TV Trivia Sports Summer vacation spots (The stuff not covered in class)