Computing Spans Given an an array X, the span S[i] of X[i] is

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Advertisements

Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Hash Tables1  
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
Data Structure Introduction.
Stacks And Queues Chapter 18.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Advanced Sorting 7 2  9 4   2   4   7
Stacks 1/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Sorting With Priority Queue In-place Extra O(N) space
Stacks 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Review Array Array Elements Accessing array elements
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Data Structures Using C, 2e
Queues.
COSC160: Data Structures: Lists and Queues
Stacks Stacks.
Data Structures I (CPCS-204)
Data Structure Interview Question and Answers
Introduction to complexity
Bucket-Sort and Radix-Sort
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Stacks and Queues.
Queues Queues Queues.
Data Structures Interview / VIVA Questions and Answers
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
The Greedy Method and Text Compression
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
STACKS.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Analysis & Design of Algorithms (CSCE 321)
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Queues 11/9/2018 6:32 PM Queues.
Stacks © 2013 Goodrich, Tamassia, Goldwasser Stacks.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Stacks.
Array-Based Sequences
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
© 2013 Goodrich, Tamassia, Goldwasser
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

Computing Spans Given an an array X, the span S[i] of X[i] is the maximum number of consecutive elements X[j] immediately preceding X[i] such that X[j]  X[i] Spans have applications to financial analysis E.g., stock at 52-week high X 6 3 4 5 2 1 S © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 1 For each index i Array size is N Scan backward to find a higher value Array size is N Worst-case time complexity? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Quadratic Algorithm Algorithm spans1(X, n) Input array X of n integers Output array S of spans of X # S  new array of n integers n for i  0 to n  1 do n s  1 n while s  i  X[i - s]  X[i] 1 + 2 + …+ (n  1) s  s + 1 1 + 2 + …+ (n  1) S[i]  s n return S 1 Algorithm spans1 runs in O(n2) time © 2014 Goodrich, Tamassia, Goldwasser Stacks

Computing Spans with a Stack We keep in a stack the indices of the last element that is taller when “looking back” We scan the array from left to right Let i be the current index We pop indices from the stack until we find index j such that X[i]  X[j] We set S[i]  i - j We push i onto the stack © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Keep on the stack the last building that is taller than the current one Root top that can’t be seen Ie. While a building is shorter on the stack Pop the building © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] 0,6 [pop 5,2; push 6] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 While shorter building on the stack pop the building Push current building Stack (top on the right) 0 [push 0] 0,1 [push 1] 0,2 [pop 1, push 2] 0,2,3 [push 3] 0,2,4 [pop 3, push 4] 0,2,5 [pop 4, push 5] 0,6 [pop 5,2; push 6] 0,6,7 [push 7] © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 0] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 How to get the span? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Algorithm 2 Stack (top on the right) Span 0 [push 1] 1 0,1 [push 1] 1 0,2 [pop 1, push 2] 2 0,2,3 [push 3] 1 0,2,4 [pop 3, push 4] 2 0,2,5 [pop 4, push 5] 3 0,6 [pop 5,2; push 6] 6 0,6,7 [push 7] 1 What is the time complexity (stack operations)? © 2014 Goodrich, Tamassia, Goldwasser Stacks

Linear Time Algorithm Each index of the array Is pushed into the stack exactly one Is popped from the stack at most once The statements in the while-loop are executed at most n times Algorithm spans2 runs in O(n) time Algorithm spans2(X, n) # S  new array of n integers n A  new empty stack 1 for i  0 to n  1 do n while (A.isEmpty()  X[A.top()]  X[i] ) do n A.pop() n if A.isEmpty() then n S[i]  i + 1 n else S[i]  i - A.top() n A.push(i) n return S 1 © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks

Worst-case Analysis N data items Algorithm 1 Algorithm 2 Time Complexity O(N2) O(N) Space Complexity © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? Suppose that intList is a variable of type Collection<Integer>, i.e., intList is a list of integer elements. Write a code segment that uses an iterator to compute the sum of all the integer values in the collection. Write a second code segment that does the same thing using a for-each loop. © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? For each of the following scenarios choose the “best” data structure from the following list or a combination of data structures: an unsorted array, linked list, Doubly Linked List, circular Linked List, stack, queue. In each case, justify your answer briefly. © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? Suppose that a grocery store decided that customers who come first will be served first © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? A list must be maintained so that any element can be accessed randomly © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? A program needs to remember operations it performed in opposite order © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? The size of a file is unknown. The entries need to be entered as they come in. Entries must be deleted when they are no longer needed. It is important that structure has flexible memory management © 2014 Goodrich, Tamassia, Goldwasser Stacks

Which Data Structure? A list must be maintained so that elements can be added to the beginning or end in O(1) © 2014 Goodrich, Tamassia, Goldwasser Stacks