recursion, O(n), linked lists 6/14



Similar documents
Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Algorithms. Margaret M. Fleck. 18 October 2010

Node-Based Structures Linked Lists: Implementation

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Sequential Data Structures

3. Mathematical Induction

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

14.1 Rent-or-buy problem

Lecture Notes on Binary Search Trees

Data Structures. Algorithm Performance and Big O Analysis

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Data Structures and Algorithms

Lecture Notes on Linear Search

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Data Structures and Algorithms Lists

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Data Structures and Algorithms

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Binary Search Trees CMPSC 122

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Ordered Lists and Binary Trees

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data storage Tree indexes

Sequences in the C++ STL

Lecture 12 Doubly Linked Lists (with Recursion)

Lecture Notes on Binary Search Trees

Linked Lists: Implementation Sequences in the C++ STL

Small Maximal Independent Sets and Faster Exact Graph Coloring

DATA STRUCTURES USING C

Physical Data Organization

Analysis of a Search Algorithm

Persistent Binary Search Trees

Guessing Game: NP-Complete?

Binary search algorithm

Review of Hashing: Integer Keys

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

LINKED DATA STRUCTURES

Heaps & Priority Queues in the C++ STL 2-3 Trees

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

CSE 326: Data Structures B-Trees and B+ Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

CS/COE

Symbol Tables. Introduction

Introduction to Hypothesis Testing. Hypothesis Testing. Step 1: State the Hypotheses

Binary Heap Algorithms

APP INVENTOR. Test Review

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Lecture 11: Tail Recursion; Continuations

Data Structures Fibonacci Heaps, Amortized Analysis

Not agree with bug 3, precision actually was. 8,5 not set in the code. Not agree with bug 3, precision actually was

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Introduction to Data Structures

Converting a Number from Decimal to Binary

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Cpt S 223. School of EECS, WSU

Chapter 8: Bags and Sets

Near Optimal Solutions

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

Computer Science 210: Data Structures. Searching

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Introduction to data structures

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

The Purchase Price in M&A Deals

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Pitfalls of Object Oriented Programming

Pattern Insight Clone Detection

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

361 Computer Architecture Lecture 14: Cache Memory

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

10CS35: Data Structures Using C

Name: 1. CS372H: Spring 2009 Final Exam

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

6. Standard Algorithms

Lecture 1: Course overview, circuits, and formulas

Cours de C++ Utilisations des conteneurs

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

There are a number of factors that increase the risk of performance problems in complex computer and software systems, such as e-commerce systems.

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Towards running complex models on big data

Chapter 13: Query Processing. Basic Steps in Query Processing

Data Structures and Algorithms

Data Structures and Data Manipulation

Data Structures. Level 6 C Module Descriptor

Module 2 Stacks and Queues: Abstract Data Types

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Transcription:

recursion, O(n), linked lists 6/14

recursion reducing the amount of data to process and processing a smaller amount of data example: process one item in a list, recursively process the rest of the list related to proof by induction related to iteration

recursion typically used for linked data structures examples of recursion on lists: process one element, recursively process the rest look at a couple elements, recursively process half of the list

how fast does this run? given some implementation, how do we know how fast it is? can just run it, time it caution: disk access way slower than memory access way slower than CPU operations compare to another implementation (if available)

empirical analysis first priority: make sure an algorithm is working correctly if it s incorrect, doesn t matter much if it s fast or slow

empirical analysis second challenge: how does the input affect things? actual data random data perverse data (ideal data)

empirical analysis actual data get to know how good/bad an algorithm/ implementation is for real usage don t always have actual data, or enough actual data gives an expectation of actual run time

empirical analysis random data gives an overall sense of the algorithm if RNG is seeded with time, time tests aren t repeatable

empirical analysis perverse data measures worst-case performance provides guarantee - the code performs no worse than this

empirical analysis ideal data considering or testing ideal data is sometimes helpful algorithms that perform better on ideal data and the same worst-case will do better on average

theory and practice algorithms vs implementation measuring runtime evaluates implementation directly, algorithm indirectly two implementations of the same algorithm may take vastly different runtimes

theory good code should: work correctly run fast be easy to debug/develop/maintain use little memory

practice tradeoffs cpu vs memory performance vs readability/maintainability common mistakes ignoring performance of alg or implementation (runs too slow) focusing too much on performance (susceptible to difficult bugs, hard to tweak for new uses)

computational complexity a (rough) way to describe the runtime of an algorithm or implementation concerned only with rate of growth i.e., how well will this work with a huge input?

computational complexity algorithms have runtime that is some function of the input array input: function of the size of the array single number input: function of the actual number we call this number n

computational complexity given some algorithm or implementation: upper bound on runtime lower bound on runtime

Big-O suppose some code takes time f(n): O(n) is true if we can say c*n > f(n) above some base value of n and some value c O(n2 ) is true if we can say c*n 2 > f(n) above some base value of n and some value c same for any other function

Big-O O(?) is an upper bound on the runtime is trivially true if you overestimate tight bound - it s the slowest-growing function for which O(?) is true

others lower bound - same idea, use omega theta - the upper and lower bounds are the same functions

notes logarithms are common for problems that reduce the size of the input by a fraction at each step assume base 2 unless otherwise specified

examples prime numbers searching an array for a value computing a logarithm

ADTs difference between an abstract data type and an implemented data type ADT specifies what it is, what sorts of functions should be defined doesn t usually define the runtime characteristics

ADTs example: ADT: List implementations: linked lists, arrays

arrays vs linked lists what s the order of insert/remove for an array? at the end? at the beginning? at the middle?

linked lists a list where the data for each item in the list isn t necessarily stored consecutively in memory each element (node) in the list has a pointer to the next node why a pointer and not a non-pointer type?

linked lists basic singly-linked list is implemented with a struct like so: struct Node { int data; // could be any data type really Node* next; };

linked lists generally, initialize the next pointer to NULL basic linked list is just a pointer to the first node how do we: insert at the beginning? insert at the end? insert in the middle?

linked lists performance of insert/remove performance of accessing an arbitrary element performance of traversing a list how to traverse a singly-linked list

dummy nodes dummy nodes or sentinel values are common: common to have the first node in the chain be a sentinel/dummy value why? removes some if statements, speeds up runtime at the cost of some memory

doubly-linked lists advantage vs singly-linked lists: easier to remove elements disadvantage vs singly-linked lists: using even more memory common to have both a front and back sentinel/dummy node