Analysis of a Search Algorithm



Similar documents
DATA STRUCTURES USING C

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

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

10CS35: Data Structures Using C

Sequential Data Structures

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

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

PES Institute of Technology-BSC QUESTION BANK

Analysis of Binary Search algorithm and Selection Sort algorithm

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.

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

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

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

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

Binary Heap Algorithms

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

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

DATA STRUCTURE - STACK

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:

STACKS,QUEUES, AND LINKED LISTS

Linked Lists Linked Lists, Queues, and Stacks

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

Introduction to Data Structures and Algorithms

Data Structures and Algorithms

Programming with Data Structures

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Data Structures Fibonacci Heaps, Amortized Analysis

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

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

Data Structure [Question Bank]

CmpSci 187: Programming with Data Structures Spring 2015

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

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

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

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Node-Based Structures Linked Lists: Implementation

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Lecture 12 Doubly Linked Lists (with Recursion)

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

- 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

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

Binary search algorithm

Universidad Carlos III de Madrid

Common Data Structures

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

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

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Section IV.1: Recursive Algorithms and Recursion Trees

LINKED DATA STRUCTURES

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Ordered Lists and Binary Trees

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Introduction to Data Structures

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

Computer Science 210: Data Structures. Searching

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

Algorithms and Data Structures

6. Standard Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures

Data Structures and Algorithms Lists

Sample Questions Csci 1112 A. Bellaachia

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Data Structures. Level 6 C Module Descriptor

Data Structures and Data Manipulation

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Stacks. Linear data structures

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

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

7.1 Our Current Model

Data Structures and Algorithms

DATA STRUCTURE - QUEUE

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Lecture Notes on Binary Search Trees

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Symbol Tables. Introduction

Abstract Data Types. Chapter 2

Lecture Notes on Binary Search Trees

ADTs,, Arrays, Linked Lists

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

Chapter 8: Bags and Sets

Binary Heaps. CSE 373 Data Structures

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Unordered Linked Lists

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Converting a Number from Decimal to Binary

Persistent Binary Search Trees

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

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

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

The ADT Binary Search Tree

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

Transcription:

CSE 326 Lecture 4: Lists and Stacks 1. Agfgd 2. Dgsdsfd 3. Hdffdsf 4. Sdfgsfdg 5. Tefsdgass We will review: Analysis: Searching a sorted array (from last time) List ADT: Insert, Delete, Find, First, Kth, etc. Array versus Linked List implementations Stacks Focus on running time (big-oh analysis) Covered in Chapter 3 of the text 1 Analysis of a Search Algorithm Problem: Search for an item X in a sorted array A. Return index of item if found, otherwise return 1. Brainstorming: What is an efficient way of doing this A -4-3 5 7 12 35 56 98 101 124 X=101 2

Binary Search Problem: Search for an item X in a sorted array A. Return index of item if found, otherwise return 1. Idea: Compare X with middle item A[mid], go to left half if X < A[mid] and right half if X > A[mid]. Repeat. A -4-3 5 7 12 35 56 98 101 124 X=101 Mid X > A[Mid] Mid A[Mid]=X Found! Return Mid = 8 3 Binary Search A -4-1 5 7 12 35 56 98 101 124 public static int BinarySearch( int [ ] A, int X, int N ) { int Low = 0, Mid, High = N - 1; while( Low <= High ) { Mid = ( Low + High ) / 2; // Find middle of array if ( X > A[ Mid ] ) // Search second half of array Low = Mid + 1; else if ( X < A[ Mid ] ) // Search first half High = Mid - 1; else return Mid; // Found X! } return NOT_FOUND; } 4

Running Time of Binary Search Given an array A with N elements, what is the worst case running time of BinarySearch What is the worst case A -4-3 5 7 12 35 56 98 101 124 X=100 Mid X > A[Mid] Mid Mid 5 Running Time of Binary Search Worst case is when item X is not found. How many iterations are executed before Low > High int Low = 0, Mid, High = N - 1; while( Low <= High ) { Mid = ( Low + High ) / 2; // Find middle of array if ( X > A[ Mid ] ) // Search second half of array Low = Mid + 1; else if ( X < A[ Mid ] ) // Search first half High = Mid - 1; else return Mid; // Found X! } 6

Running Time of Binary Search Worst case is when item X is not found. How many iterations are executed before Low > High After first iteration: N/2 items remaining 2 nd iteration: (N/2)/2 = N/4 remaining Kth iteration: 7 Running Time of Binary Search How many iterations are executed before Low > High After first iteration: N/2 items remaining 2 nd iteration: (N/2)/2 = N/4 remaining Kth iteration: N/2 K remaining Worst case: Last iteration occurs when N/2 K 1 and N/2 K+1 < 1 item remaining 2 K N and 2 K+1 > N [take log of both sides] Number of iterations is K log N and K > log N - 1 Worst case running time = Θ(log N) 8

Lists What is a list An ordered sequence of elements A1, A2,, AN Elements may be of arbitrary type, but all are the same type List ADT: Common operations are: Insert, Find, Delete, IsEmpty, IsLast, FindPrevious, First, Kth, Last Two types of implementation: Array-Based Linked List We will compare worst case running time of ADT operations 9 Lists: Array-Based Implementation Basic Idea: Pre-allocate a big array of size MAX_SIZE Keep track of first free slot using a variable N Empty list has N = 0 Shift elements when you have to insert or delete 0 1 2 3 N-1 MAX_SIZE A_1 A_2 A _ 3 A _ 4 A _ N Example: Insert(List L, ElementType E, Position P) 10

Lists: Array-Based Implementation public void insert(list L, ElementType X, Position P) // Example: Insert X after position P = 1 0 1 2 3 N-1 MAX_SIZE A_1 A_2 A_3 A_4 A_N Basic Idea: Shift existing elements to the right by one slot and insert new item 0 1 2 3 N-1 N MAX_SIZE A_1 A_2 X A_3 A_N-1 A_N Running time for insert into N element array-based list = 11 Lists: Array-Based Insert Operation Basic Idea: Shift existing elements to the right by one slot and insert new item 0 1 2 3 N-1 MAX_SIZE A_1 A_2 A_3 A_4 A_N Running time for N elements = Worst case is when you insert at the beginning of list must shift all N items 12

Lists: Linked List Implementation Insert the value X after P header A_1 A_2 NULL P 13 Lists: Linked List Implementation Insert the value X after P: 1. Create new containing X 2. Update pointers header A_1 A_2 NULL P X (Go through the Java/C++ code in Chap. 3 of your text) 14

Lists: Linked List Implementation of Insert Insert the value X after P: 1. Create new containing X 2. Update pointers header A_1 A_2 NULL P Running Time = X 15 Lists: Linked List Implementation of Insert A_1 A_2 NULL header P X Running Time = Θ(1) Insert takes constant time does not depend on input size N Comparison: Array implementation takes time 16

Caveats with Linked List Implementation Whenever you break a list, your code should fix the list up as soon as possible Draw pictures of the list to visualize what needs to be done Pay special attention to boundary conditions: Empty list Single item same item is both first and last Two items first, last, but no middle items Three or more items first, last, and middle items 17 Header Node in Linked List Implementation Why use a header If List points to first item, any change in first item changes List itself Need special checks if List pointer is NULL (e.g. is invalid) Solution: Use header at beginning of all lists (see text) List always points to header, which points to first item 18

Other List Operations: Run time analysis Operation isempty Insert FindPrev Delete Array-Based List Linked List 19 Other List Operations: Run time analysis Operation isempty Insert FindPrev Delete Find Find Array-Based List Linked List 20

Other List Operations: Run time analysis Operation isempty Insert FindPrev Delete Find Find First Kth Last Length Array-Based List Linked List 21 Other List Operations: Run time analysis Operation isempty Insert FindPrev Delete Find Find First Kth Last Length Array-Based List Linked List 22

Delete Operation using a Linked List A_11 A_12 A_13 NULL P Problem: To delete the pointed to by P, need a pointer to the previous (= ) 23 Doubly Linked Lists FindPrev (and hence Delete) is because we cannot go to previous Solution: Keep a back-pointer at each Doubly Linked List header prev prev prev Advantages: Delete and FindPrev become operations Disadvantages: More space (double the number of pointers at each ) More book-keeping for updating two pointers at each 24

Circularly Linked Lists header Set the pointer of the last to first instead of NULL Useful when you want to iterate through whole list starting from any No need to write special code to wrap around at the end A circular and doubly linked list speeds up both the Delete and Last operations (first and last s point to each other) time for both instead of 25 Applications of Lists Polynomial ADT: store and manipulate single variable polynomials with non-negative exponents E.g. 10X 3 + 4X 2 + 7 = 10X 3 + 4 X 2 + 0 X 1 + 7 X 0 Data structure: stores coefficients C i and exponents i Array Implementation: C[i] = C i E.g. C[3] = 10, C[2] = 4, C[1] = 0, C[0] = 7 ADT operations: Input polynomials in arrays A and B Addition: C[i] = Multiplication: 26

Applications of Lists: Polynomials Polynomial ADT: store and manipulate single variable polynomials with non-negative exponents E.g. 10X 3 + 4X 2 + 7 = 10X 3 + 4 X 2 + 0 X 1 + 7 X 0 Array Implementation: C[i] = C i E.g. C[3] = 10, C[2] = 4, C[1] = 0, C[0] = 7 ADT operations: Input polynomials in arrays A and B Addition: C[i] = A[i] + B[i]; Multiplication: initialize C[i] = 0 for all i for each i, j pair: C[i+j] = C[i+j] + A[i]*B[j]; 27 Applications of Lists: Polynomials Polynomial ADT: store and manipulate single variable polynomials with non-negative exponents E.g. 10X 3 + 4X 2 + 7 = 10X 3 + 4 X 2 + 0 X 1 + 7 X 0 Array Implementation: C[i] = C i E.g. C[3] = 10, C[2] = 4, C[1] = 0, C[0] = 7 Problem with Array implementation: Sparse polynomials E.g. 10X 3000 + 4 X 2 + 7 Waste of space and time (C i are mostly 0s) Use singly linked list, sorted in decreasing order of exponents 28

Applications of Lists: Radix Sort Bucket sort: Sort N integers A 1,, A N which are in the range 0 through B-1 Initialize array Count with B slots ( buckets ) to 0 s Given an input integer A i, Count[A i ]++ Time: O(B+N) (= if B is Θ(N)) Radix sort = Bucket sort on digits of integers Each digit in the range 0 through 9 Bucket-sort from least significant to most significant digit Use linked list to store numbers that are in same bucket Takes O(P(B+N)) time where P = number of digits 29 Stacks In Array implementation of Lists Insert and Delete took time (need to shift elements) What if we avoid shifting by inserting and deleting only at the beginning of the list Both operations take time! Stack: Same as list except that Insert/Delete allowed only at the beginning of the list (the top). LIFO Last in, First out Push: Insert element at top Pop: Delete and Return top element 30

class: Queues and Trees To do this week: Homework no. 2 on the Web (due next Monday) Read Chapters 3 and 4 31