Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
Analysys & Complexity of Algorithms Big Oh Notation.
Data Structures Hash Tables
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 3: The Efficiency of Algorithms
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Unit III : Introduction To Data Structures and Analysis Of Algorithm 10/8/ Objective : 1.To understand primitive storage structures and types 2.To.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Data Structures Arrays Phil Tayco Slide version 1.0 Feb 02, 2015.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 2 ARRAYS.
Arrays.
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.
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.
Data Structures Simple Sorts Phil Tayco Slide version 1.0 Feb. 8, 2015.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
3.3 Complexity of Algorithms
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Copyright © 2009 Curt Hill Look Ups A Recurring Theme.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
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.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
COMP108 Algorithmic Foundations Algorithm efficiency
Introduction to complexity
Introduction to Algorithms
Introduction to Algorithms
Algorithm design and Analysis
Chapter 3: The Efficiency of Algorithms
CS 201 Fundamental Structures of Computer Science
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Data Structures Sorted Arrays
Data Structures Introduction
Data Structures Unsorted Arrays
Presentation transcript:

Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015

Introduction Why are we here? Programs are created to make our lives easier The more efficient the program, the better they perform to serve us Previous classes focus on how to create programs. Here, we analyze how to make them more efficient

Introduction What is efficient? Fast results is a key evaluation criteria by the end user There are factors to consider to measure efficiency To understand them, let ’ s look at a simple search for a key value in a list of unsorted records

Introduction Best case/Worst case In an unordered list, checking a record in a specific location is arbitrary. It doesn ’ t matter which element you select At best, you get it on the first try and at worst, you go through the entire list Can the situation be modified to improve the search time (performance factor)?

Introduction Sort the list! Sorting the records vastly improves the search using the binary search algorithm Look in the middle of the list. If you found it, great. Else, look in the middle of the section of the list where the record should be In a list of 1000 unsorted records, worst case search is If sorted, worst case is 11! (try it)

Introduction Sorting the list, though… This process takes additional time to perform Begs the question: Is the process of sorting and then searching faster than searching an unsorted list? The answer to get used to in this class: It depends

Introduction If we sort the list and save it… Pre-sorting the records eliminates that time, but requires memory space to store the indexed records (capacity factor) Sorted records need to preserve their order even after records are added and deleted (maintenance factor) Is there a configuration and algorithm that is ideal in supporting all of these factors?

Introduction That ’ s the goal! In this class we will look at different structures and algorithms that provide the best measure of efficiency based on these factors of performance time, storage space and record maintenance in the appropriate situations

Big O Notation When you code a solution… What do you use to measure how effective it is? (is it the number of lines of code?) Do you consider how it will do in other situations? (what situations do you mean?) We can address these using a notation that can be consistently and systematically applied – this is known as "Big O"

Big O Notation O, the magnitude… The O represents measuring effectiveness in terms of order of magnitude Often, algorithms are applied on data sets (a list of records, coordinates on a map, genetic sequences, …) An algorithm will perform a certain way on a set amount of data, so we want to see how that logic stands as the size of the data increases

Big O Notation Code lines as a unit of measure Examine the following code: for (int loc = 0; loc < coffeShops.length; loc++) if (coffeeShops[loc].visited == false) { coffeeShops[loc].visited = true; shopCount++; } Number of lines of code to measure an algorithm is not useful. This has 4 lines of code, but will vary in performance based on the size of the coffeeShops array

Big O Notation What is the real unit of measure? Algorithms will use many kinds of operations. Some operations take more time or memory than others –Function call (power(x, y);) –Conditional expression (x > y) –Assignment (z = 5) –Mathematical operation (area = length * width) Algorithms tend to perform repetitive sequences (i.e. loops) on these types of operations We identify the unit of measure by selecting an operation considered to be the most significant

Big O Notation Significant Operations Often this is a comparison operation or set of assignment operations (like a swap, which is 3 assignment operations) Question: In the code example, how many comparison operations are performed? Answer: It depends on the size of the array (2 * coffeeShops.length) There are 2 assignment operations in the if- statement, but are not as significant as the comparison operations

Big O Notation So we reduce the total count of key ops? At first, we are actually less interested in fine tuning the algorithm to reduce the number of significant operations that take place Big O starts with examining this performance as the list gets larger We usually look at worst case scenarios, but keep in mind that we can also analyze best and average cases as well

Big O Notation Big O Types There are four major ways to categorize the Big O performance of an algorithm Consider the program example which is essentially recording a count of the number of coffee shops visited Suppose we also want to record that count in a database There are many ways to do this, some more effective than others. Big O provides a standard notation to categorize it

Big O Notation Algorithm 1 Start at coffee shop 1. If it has already been visited, go to the next coffee shop. Repeat until you ’ ve examined all coffee shops Meanwhile, if the current shop has not been visited, stop the visiting process (i.e. exit the loop) Add 1 to the coffee shop count Log on to the database and update the coffee shop count record Repeat the coffee shop visiting process starting at shop 1

Big O Notation Code for Algorithm 1 int shopCount = 0; int loc; while(true) { for (loc = 0; loc < coffeeShops.length; loc++) if (coffeeShops[loc].visited == false) { coffeeShops[loc].visited = true; break; } updateDatabase(++shopCount); if (loc == coffeeShops.length) break; }

Big O Notation Algorithm 2 Visit all coffee shops starting at shop 1 If the current shop has not been visited, mark it as visited and add 1 to the coffee shop count After all coffee shops have been examined, log on to the database and update the coffee shop count record

Big O Notation Code for Algorithm 2 int shopCount = 0; for (int loc = 0; loc < coffeeShops.length; loc++) if (coffeeShops[loc].visited == false) { coffeeShops[loc].visited = true; shopCount++; } updateDatabase(++shopCount);

Big O Notation Analysis It’s intuitively clear that the second algorithm is more efficient than the first, but let’s use Big O to formally confirm this We must first determine an operation type. Usually, this is the most expensive operation to consider Using the comparison operation and the worst case scenario of if all coffee shops were unvisited, examine the counts for algorithm 1: –10 shops: … = 120 –20 shops : … = 410 –30 shops : … = 930

Big O Notation Algorithm 1 Plot

Big O Notation Exponential growth Notice with this graph that as the number of elements in the list increases, the count of operations grows exponentially A list of n elements will have something to the effect of (n 2 + C) comparison counts The exact formula can be derived but what matters more (at this point) is the rate of growth and not the actual number Big O categorizes this exponential growth as O(n 2 )

Big O Notation What about algorithm 2? Using the same operation and worst case scenario, the counts for algorithm 2: –10 elements: 2 * 10 = 20 –20 elements: 2 * 20 = 40 –30 elements: 2 * 30 = 60 –n elements: 2 * n The count is significantly smaller than algorithm 1

Big O Notation Algorithm 1 and 2 Plots

Big O Notation Further analysis The rate of growth in relation to size n is linear. We capture this linear growth as O(n) Comparing between orders makes the actual counts and formulas less significant O(n ) will be better than O(n 2 ) because as n increases, linear growth eventually wins over exponential Question 1: What are the Big Os of the 2 algorithms if the operation to consider is calling the database? Question 2: Do the Big Os change if we consider best case scenario (i.e. if all coffee shops were already visited)?

Big O Notation The 4 main Big O groups From worst to best: –Exponential: O(n 2 ) –Linear: O(n) –Logarithmic: O(log n) –Constant: O(1) Logarithmic we will see more later. This plot line has a flatter growth rate than linear Constant is ideal where no matter how much n increases, the number of operations performed is constant Some algorithms at lower values of n will have better counts than the Big O suggests. Remember that the measure is not for all values of n, but to show you performance as n increases

Big O Notation Algorithm analysis procedure Identify the operation type to use for your unit of measure Identify the scenario(s) you want to examine (worst case, best case and/or average) Examine the algorithm performance focusing on that unit of measure and how its value changes as the data set the algorithm is applied to gets larger Determine its Big O and repeat the process with other algorithms as needed noting: –Which algorithm has the best Big O –If the best solutions are the same order, examine the performance in more detail to see if there's a significant difference such as O(n) versus O(2n)

Structure Considerations The common operations When all is said and done, all programs tend to focus on performing four main functions –Search: Finding a record of significance –Insert: Adding new data to the record set –Update: Performing a search and making a change to that record in the set –Delete: Removing data from the record set When all functions are performed, keeping the design intent of the structure intact must be considered

Structure Considerations Key values and duplicates In most data structures, a key value is used to support performing operations Data structures are evaluated based on the performance of these functions and value considerations along with the storage and maintenance factors discussed earlier Duplicate key values may be allowed which need to be considered in the use of the structure