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



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

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

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!

Ordered Lists and Binary Trees

Data Structures and Algorithms

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:

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

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

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

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Sequential Data Structures

Algorithms and Data Structures

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

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Module 2 Stacks and Queues: Abstract Data Types

DATA STRUCTURES USING C

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

Analysis of a Search Algorithm

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

10CS35: Data Structures Using C

St S a t ck a ck nd Qu Q eue 1

CmpSci 187: Programming with Data Structures Spring 2015

Chapter 3: Restricted Structures Page 1

Common Data Structures

Introduction to Data Structures and Algorithms

CompSci-61B, Data Structures Final Exam

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

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

The ADT Binary Search Tree

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

Symbol Tables. Introduction

Binary Search Trees (BST)

Data Structures and Algorithms Stacks and Queues

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

Binary Search Trees CMPSC 122

Lecture Notes on Binary Search Trees

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

Data Structures and Algorithms(5)

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

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

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

Data Structure with C

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

TREE BASIC TERMINOLOGIES

Analysis of Algorithms, I

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

Data Structure [Question Bank]

Cpt S 223. School of EECS, WSU

Sample Questions Csci 1112 A. Bellaachia

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

Exam study sheet for CS2711. List of topics

Data Structures Using C++

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

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.

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

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

6 March Array Implementation of Binary Trees

Linked Lists Linked Lists, Queues, and Stacks

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

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

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Structural Design Patterns Used in Data Structures Implementation

Abstract Data Types. Chapter 2

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

Data Structures and Data Manipulation

DATA STRUCTURE - STACK

3 Abstract Data Types and Search

Pseudo code Tutorial and Exercises Teacher s Version

PES Institute of Technology-BSC QUESTION BANK

Data Structures UNIT III. Model Question Answer

Universidad Carlos III de Madrid

Data Structures. Level 6 C Module Descriptor

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

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures Fibonacci Heaps, Amortized Analysis

14.1 Rent-or-buy problem

Converting a Number from Decimal to Binary

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

Java Software Structures

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

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

CS 2412 Data Structures. Chapter 2 Stacks and recursion

DATA STRUCTURE - QUEUE

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

International Journal of Software and Web Sciences (IJSWS)

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

STACKS,QUEUES, AND LINKED LISTS

Lecture Notes on Binary Search Trees

Fast nondeterministic recognition of context-free languages using two queues

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

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

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

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Dynamic Programming. Lecture Overview Introduction

7.1 Our Current Model

Transcription:

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

Content of this Lecture Stacks and Queues Tree Traversal Towers of Hanoi Ulf Leser: Alg&DS, Summer semester 2011 2

Stacks and Queues Recall these two fundamental ADTs type stack( T) import bool; operators isempty: stack bool; push: stack x T stack; pop: stack stack; top: stack T; type queue( T) import bool; operators isempty: queue bool; enqueue: queue x T queue; dequeue: queue queue; head: queue T; Properties Stacks always add / remove the first element Add and remove from right - LIFO Queues always add the first element and remove the last element Add from right, remove from left - FIFO Ulf Leser: Alg&DS, Summer semester 2011 3

Implementation Stacks Always add / remove at the front Efficiently supported by linked lists or double-linked lists Queues Always add at the front and remove from the back Efficiently supported by double-linked lists with pointer to first and last element Adding a last pointer to a (single) linked list is also enough How? Array Linked list Doublelinked l. Insert O(n) O(n) O(n) InsertAfter O(n) O(1) O(1) Delete O(n) O(n) O(n) DeleteThis O(n) O(n) O(1) How? Search O(n) O(n) O(n) Add to start O(n) O(1) O(1) Add to end O(1) O(n) O(1) Ulf Leser: Alg&DS, Summer semester 2011 4

Content of this Lecture Stacks and Queues Tree Traversal Application Depth-First using Stacks Breadth-First using Queues Towers of Hanoi Ulf Leser: Alg&DS, Summer semester 2011 5

Application Information systems are a class of software systems that is concerned with managing (and analyzing) data Customers of a company, calls of a telecom company, stock manment, human resources, enrolled students, etc. Managing means Storing Being fail-safe Allowing concurrent read and write access Offering comfortable (and fast) ways of accessing the data Give me all older than 55 which purchased goods worth more than 30K Euro in the last 6 months and they did never before buy a Rolex See course on Databases Ulf Leser: Alg&DS, Summer semester 2011 6

Data Models Data mand within a database needs to be modeled Which data do we store? One particularly comfortable data model is called XML XML: Extended Markup Langu Allows to model (and define) hierarchical data structures There is much more to say about XML; we only scratch the surface There are things you cannot model easily in XML, but still E.g.: g:students enroll_to courses Central elements: Elements and values Elements e sae are names esof values auesor of groups goupsof values aues Elements have an opening and a closing tag (<x></x>) Values store the actual data values Ulf Leser: Alg&DS, Summer semester 2011 7

Example Elements and Values <> <customer> <> Müller </> <> Peter </> <> 25 </> </customer> <customer> <> Meier </> <> Stefanie </> <> 27 </> </customer> </> XML is verbose But can be compressed well Not necessarily a model for stor Ulf Leser: Alg&DS, Summer semester 2011 8

Example <> <customer> <> Müller </> <> Peter </> <> 25 </> </customer> <customer> <> Meier </> <> Stefanie </> <> 27 </> </customer> </> Production rules -> cust cust -> customer cust -> customer, cust customer ->,, M i -> * -> * -> * Ulf Leser: Alg&DS, Summer semester 2011 9

Data A Tree The data items of an XML database form a tree <> <customer> <> Müller </> <> Peter </last name> customer customer _ <> 25 </> </customer> <customer> <> Meier </> <> Stefanie </> <> 27 </> Müller Peter Meier Stefanie </customer> </> 25 27 Ulf Leser: Alg&DS, Summer semester 2011 10

Implementing a Tree class element { value: String; children: list_of_element; } <> <customer> <> Müller </> <> Peter </> <> 25 </> </customer> <customer> <> Meier </last _ name> <> Stefanie </> <> 27 </> </customer> </> class XMLDoc { root: element; func void init() func element getroot() func String printtree() {? How? } } customer customer Müller Peter 25 Meier Stefanie 27 Ulf Leser: Alg&DS, Summer semester 2011 11

Two Strategies For both cases, we need to traverse the tree Start from root and recursively follow pointer to children Fortunately, we cannot run into cycles But they require different traversal strategies customer Depth-first: From root, always follow the left-most child until you reach a leaf; then follow second-left-most Müller Breadth-first: From root, first <> Peter <customer> <> Müller look at all children, then on all </> grand-children, then <> Peter </> <> 25 (always from left to right) </> </customer> <customer> <> Meier </> <> Stefanie </> <> 27 </> </customer> </> 25 Meier customer Stefanie customer customer Müller Peter 25 Meier Stefanie 27 Ulf Leser: Alg&DS, Summer semester 2011 12 27

Content of this Lecture Stacks and Queues Tree Traversal Application Depth-First using Stacks Breadth-First using Queues Towers of Hanoi Ulf Leser: Alg&DS, Summer semester 2011 13

Depth-First Traversal (no indentation) func String printdfs (t Tree) { s := new Stack(); o := ; node : treeelement; s.put( t.getroot()); while not s.isempty() do node := s.get(); o := o+node.getvalue()+ \lf ; c := node.getchildren(); if (c null) then foreach x in c do s.put( c); end for; end if; end while; return o; } We assume that elements have their name as value Müller customer Peter Meier customer Stefanie 25 27 <> <customer> <> Müller </last _ name> <> Peter </> <> 25 </> </customer> <customer> <> Meier </> <> Stefanie </> <> 27 </> </customer> </> Ulf Leser: Alg&DS, Summer semester 2011 14

DFS-2 Output o: s.push( root); while not s.isempty() do node := s.get(); o := o+node.getvalue(); # print s, o; c := node.getchildren(); if (c null) then foreach x in c do s.put( c); end for; end if; # print s, 0; end while; customer2 customer2 customer2 Stack s: customer2 customer1 customer1 customer1 customer1 Ulf Leser: Alg&DS, Summer semester 2011 15

DFS-3 s.push( root); while not s.isempty() do node := s.get(); o := o+node.getvalue(); # print s, o; c := node.getchildren(); if (c null) then foreach x in c do s.put( c); end for; end if; # print s, 0; end while; customer2 customer2 25 Peter Müller customer2 25 Peter Müller customer1 customer2 25 Peter Müller customer1 25 customer1 customer1 Ulf Leser: Alg&DS, Summer semester 2011 16

Adding Indentation We need to also store the depth of a node on the stack We assume a generic, type-independent stack s.put( root); s.put( 1); while not s.isempty() do depth := s.get(); node := s.get(); o := o+ SPACES(depth) +node.getvalue(); c := node.getchildren(); if (c null) then foreach x in c do s.put( c); s.put( depth+1); end for; end if; end while; customer2 25 Peter Müller customer1 Ulf Leser: Alg&DS, Summer semester 2011 17

Reverting Order We create customer2 customer1 but we wanted customer1 customer2 The order of children is reverted by the stack Remedy Push children in reverted order Can be achieved by a FOREACH which can traverse the list in reverted order Easy, if a double-linked linked list is used Ulf Leser: Alg&DS, Summer semester 2011 18

Content of this Lecture Stacks and Queues Tree Traversal Application Depth-First using Stacks Breadth-First using Queues Towers of Hanoi Ulf Leser: Alg&DS, Summer semester 2011 19

Breadth-First Traversal Func String printbfs (t Tree) { s := new Queue(); o := ; node : element; s.put( t.getroot()); while not s.isempty() do node := s.get(); o := o+node.getvalue(); c := node.getchildren(); if (c null) then foreach x in c do s.put( c); end for; end if; end while; } Müller customer Peter 25 Meier customer Stefanie customer customer Müller Peter 25 Meier Stefanie 27 27 Ulf Leser: Alg&DS, Summer semester 2011 20

BFS-2 customer1 customer1 customer1 customer2 customer2 customer1 customer2 customer2 Ulf Leser: Alg&DS, Summer semester 2011 21

BFS-3 customer1 customer1 customer1 customer2 customer2 customer2 customer1 customer2 customer1 customer2 Müller Peter Müller If we add information about the depth of a node, we can put elements of same depth at the same line of the output Ulf Leser: Alg&DS, Summer semester 2011 22

Time Complexity The complexity of the traversal is O(n) in both cases n = number of nodes in the tree Each node is pushed (enqueued) once and popped (dequeued) once Thus, the foreach loop is passed by (n-1) times altogether The style of argument is different from what we had so far Recall SelectionSort We have two nested loops in both algorithms SelectionSort: for i = 1..n-1 do for j = i+1..n do end for; end for; printbfs: while not s.isempty() do foreach x in c do end for; end while; Ulf Leser: Alg&DS, Summer semester 2011 23

Time Complexity printbfs: while not s.isempty() do foreach x in c do end for; end while; SelectionSort: for i = 1..n-1 do for j = i+1..n do end for; end for; In printbfs, we do not know how often the inner loop is passed-through for a specific iteration of the outer loop We could not easily estimate this number depends on the number of children, not on the concrete iteration of the outer loop But we can directly count how often the inner loop is passed- through over all iterations of the outer loop This is possible because we know that no element is touched twice In SelectionSort, we do know how often the inner loop is passed-through for every iteration of the outer loop Obviously, n-i-1 times But we have no simple estimation for the number of times the inner loop is passed-through over all iterations of the outer run This is because we touch elements multiple times Ulf Leser: Alg&DS, Summer semester 2011 24

Space Complexity Time complexity is the same for DFS and BFS, but space complexity is different Let d be the depth of the tree Length of longest path Let b be the breadth of the tree Maximal number of nodes with same depth over all levels In DFS, the stack holds at most d elements In BFS, the queue holds at most b elements That s a big difference in typical database settings Little nesting (small d), but hundreds of thousands of (large b) Ulf Leser: Alg&DS, Summer semester 2011 25

Content of this Lecture Stacks and Queues Tree Traversal Towers of Hanoi Ulf Leser: Alg&DS, Summer semester 2011 26

Rules of the Game 1 2 3 Move stack from stick 1 to stick 2 Always move only one disc at a time Never place a larger disc on a smaller one Ulf Leser: Alg&DS, Summer semester 2011 27

Solution for 3 Discs Source: Informatik Didaktik, U Potsdam Ulf Leser: Alg&DS, Summer semester 2011 28

4 Discs We have seen this part before Ulf Leser: Alg&DS, Summer semester 2011 29

4 Discs And this part as well We have seen this part before Ulf Leser: Alg&DS, Summer semester 2011 30

Idea The problem can be solved easily (with little program code) using the following observations Suppose you know how to solve the problem for n-1 discs Then solving it for n discs is simple: 1. Move the (n-1) top-part of the tower to stick 3 2. Move the n th (largest) disc to stick 2 3. Move the (n-1) tower from stick 3 to stick 2 Furthermore, we know how to solve the problem for n=1 Done Ulf Leser: Alg&DS, Summer semester 2011 31

Algorithm We want an algorithm which prints the series of moves that solve the problem for size n We encode a move as a quadruple (n, a, b, c) which means: Move n discs from stick a to b using c We build a stack of tasks When we pop a task from the stack, we can do either Task is easy (n=1): Print next move Task is difficult (n>1): Push three new tasks s: stack; s.push( n, 1, 2, 3); while not s.isempty() do (n, a, b, c) := s.pop(); if (n=1) then print Move +a+ -> +b; else s.push( n-1, c, b, a); s.push( 1, a, b, c); s.push( n-1, a, c, b); end if; end while; Ulf Leser: Alg&DS, Summer semester 2011 32

Example s: stack; s.push( n, 1, 2, 3); while not s.isempty() do p (n, a, b, c) := s.pop(); p(); if (n=1) then print Move +a+ -> +b; else s.push( n-1, c, b, a); s.push( 1, a, b, c); s.push( n-1, a, c, b); end if; end while; 3,1,2,3 2,1,3,2 1123 1,1,2,3 2,3,2,1 1,1,2,3 1132 1,1,3,2 1,2,3,1 1,1,2,3 2,3,2,1 Move 1->2 Move 1->3 Move 2->3 Move 1->2 1312 1,3,1,2 Move 3->1 1,3,2,1 Move 3->2 1123 1,1,2,3 Move 1->2 Ulf Leser: Alg&DS, Summer semester 2011 33

Complexity How often do we pop from the stack? For a task of size n, we pop once and create two tasks of size n-1 and one task of size 1 For a task of size 1, we pop once and create no further task This gives 1+2+1+4+1+8+1+ +2 n-1 = O(2 n ) tasks altogether Recall that 2 i =2 n+1-1 The algorithm has complexity O(2 n ) Ulf Leser: Alg&DS, Summer semester 2011 34

Optimality We can also derive: For solving a problem of size n, the algorithm creates 2 n -1 moves As every pop yields one move As no algorithm can create 2 n -1 moves in less than 2 n -1 operations, the algorithm is optimal for such sequences Question: Is there a shorter sequence of moves that also solves the problem? Answer: No Second example of an exponential problem Ulf Leser: Alg&DS, Summer semester 2011 35

Recursion Doesn t this fiddling around with a stack look overly complex? Recursive formulation This program will create more or the less the same stack -on the program stack func void solve( n, a, b, c) { if (n=1) then print Move +a+ -> +c; else solve( n-1,a, c, b); solve( 1, a, b, c); } solve( n-1, c, b, a); end if; A stack can be used to de-recursivy a recursive algorithm Which doesn t mean that the program gets easier to understand Ulf Leser: Alg&DS, Summer semester 2011 36