CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
MATH 224 – Discrete Mathematics
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Programming and Data Structure
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
CS 240: Data Structures Thursday, June 21 th Vector, Linked List.
Complexity Analysis (Part I)
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
C++ for Engineers and Scientists Third Edition
CS 240: Data Structures Thursday, July 12 th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL.
CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Program Performance & Asymptotic Notations CSE, POSTECH.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
CS 1704 Introduction to Data Structures and Software Engineering.
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.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Algorithm Analysis (Big O)
Searching Topics Sequential Search Binary Search.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Lecture #3 Analysis of Recursive Algorithms
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Complexity Analysis (Part I)
Chapter 2 Algorithm Analysis
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 4 C Program Control Part I
Analysis of Algorithms
CS Data Structures Chapter 8 Lists Mehmet H Gunes
COMP 53 – Week Seven Big O Sorting.
Algorithm Analysis CSE 2011 Winter September 2018.
10.3 Bubble Sort Chapter 10 - Sorting.
Big-Oh and Execution Time: A Review
Lab 03 - Iterator.
Chapter 12: Analysis of Algorithms
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
DS.A.1 Algorithm Analysis Chapter 2 Overview
The Standard Template Library
Revision of C++.
Measuring Experimental Performance
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Analysis of Algorithms
Complexity Analysis (Part I)
10.3 Bubble Sort Chapter 10 - Sorting.
Complexity Analysis (Part I)
Presentation transcript:

CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion

Uh oh… The second test is next Thursday. The second test is next Thursday. Hopefully, it won’t be as long. Hopefully, it won’t be as long. Linkedlist presentations are on Tuesday. Linkedlist presentations are on Tuesday.

Vector Vector is an STL provided sequential container. Vector is an STL provided sequential container. It provides us with similar abilities as does our templated mycontainer (lab 5). It provides us with similar abilities as does our templated mycontainer (lab 5).

Vector We declare a vector just like we do a templated mycontainer: We declare a vector just like we do a templated mycontainer: vector testvector; vector testvector; Many methods are built in: Many methods are built in: Constructor, destructor, operator = Constructor, destructor, operator = size(), capacity(), size(), capacity(), clear() //equivalent to mycontainer::empty() clear() //equivalent to mycontainer::empty() push_back(T) //equivalent to mycontainer::insert(T) push_back(T) //equivalent to mycontainer::insert(T) pop_back(T) //equivalent to mycontainer::remove(T) pop_back(T) //equivalent to mycontainer::remove(T)

Vector We can access Vector data as follows: We can access Vector data as follows: front() //gets first element front() //gets first element back() //gets last element back() //gets last element operator [unsigned int] //gets element at specified location. operator [unsigned int] //gets element at specified location.

Vector Instead of currentvalue, Vector uses iterators: Instead of currentvalue, Vector uses iterators: vector ::iterator myiterator; //T must match the vector you want to use this iterator with. vector ::iterator myiterator; //T must match the vector you want to use this iterator with. myiterator = testvector.begin(); myiterator = testvector.begin(); myiterator = testvector.end(); myiterator = testvector.end(); myiterator++;//equivalent to mycontainer::next() myiterator++;//equivalent to mycontainer::next() myiterator--;//equivalent to mycontainer::previous() myiterator--;//equivalent to mycontainer::previous() *myiterator;//equivalent to mycontainer::current() *myiterator;//equivalent to mycontainer::current() testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T);

Algorithm Efficiency What determines if an algorithm is efficient? What determines if an algorithm is efficient? How much space does it take up? How much space does it take up? How long does it take? How long does it take? We usually worry about time when we discuss efficiency – however, space issues are also important! We usually worry about time when we discuss efficiency – however, space issues are also important!

Time efficiency The time an algorithm takes has many variables: The time an algorithm takes has many variables: Size of data set Size of data set Processing speed Processing speed Compiler optimizations, effective coding Compiler optimizations, effective coding

Time Evaluation We could count how many instructions are executed. We could count how many instructions are executed. Let T(n) represent the time it takes for an algorithm to handle a data size of size n. Let T(n) represent the time it takes for an algorithm to handle a data size of size n. How long does insert() take? How long does insert() take?

Time Evaluation What about taking an average? What about taking an average? How does this vary based on SIZE? How does this vary based on SIZE? SIZE has a direct effect on the performance of this algorithm! SIZE has a direct effect on the performance of this algorithm! //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE;

Time Evaluation We refer to this as an “order of magnitude” -> Big Oh, or O() We refer to this as an “order of magnitude” -> Big Oh, or O() In this case, the algorithm is O(N), where N is the input size. In this case, the algorithm is O(N), where N is the input size. Math: Math: We say that T(N) has an order of magnitude of O(f(X)) where, We say that T(N) has an order of magnitude of O(f(X)) where, T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. f(X) = N, C >= 2 f(X) = N, C >= 2 //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE;

Big Oh Let us consider a couple of algorithms and determine their complexity. Let us consider a couple of algorithms and determine their complexity. I have no examples, you’d better think of one. I have no examples, you’d better think of one.

Recursion Recursion. Recursion. Recursion is a form of loop written in a different style. Recursion is a form of loop written in a different style. Generally, if we have a loop it can become a recursive function. Generally, if we have a loop it can become a recursive function.

Recursion Translation T & search(T searchval) { Node * i = first; for(i;i!=NULL;i=i->next){if(i->data==searchval){ return &(i->data); }} return NULL; } T & search(T searchval, Node * searchat) { if(searchat == NULL) { return NULL; }if(searchat->data==searchval){ return &(searchat->data); } return search(searchval,searchat- >next); }

Recursion: Break Down T & search(T searchval, Node * searchat) { if(searchat == NULL) { return NULL; }if(searchat->data==searchval){ return &(searchat->data); } return search(searchval,searchat->next); } Base Case Terminating Case Continuation Case

Recursion Base Case: Base Case: Termination Case: Termination Case: Continuation Case: Continuation Case: Minor difference in usage Minor difference in usage Easier to evaluate for Big Oh! Easier to evaluate for Big Oh! Too small.

Cheerios are an O with big flavor. Cheerios are an O with big flavor. Overstock.com is the “O”. Overstock.com is the “O”. So, why do we care about the Big O? So, why do we care about the Big O?

Big Oh It lets us determine how effective one algorithm is compared to another. It lets us determine how effective one algorithm is compared to another. However, it isn’t a perfect answer. But it is pretty good. However, it isn’t a perfect answer. But it is pretty good.

Sorting time! Insertion Sort! Insertion Sort! Two ways to do this… yes… two! Two ways to do this… yes… two! Bubble Sort! Bubble Sort!