rossella lau lecture 5, dco20105, semester a,2005-6 dco 20105 data structures and algorithms ...

21
Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO20105 Data structures and algorithms Lecture 5: Deque Comparison of sequence containers Deque Applications considerations Comparison of sequence containers -- By Rossella Lau

Post on 20-Dec-2015

225 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

DCO20105 Data structures and algorithms

Lecture 5: Deque Comparison of sequence containers

Deque Applications considerations Comparison of sequence containers

-- By Rossella Lau

Page 2: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Deque

Doubly-ended-queue allows objects to be added to the front or back efficiently compromises the advantages and disadvantages of list and

vector

An example of deque: Deque.h a linked list of blocks which are arrays (or vectors)

…… …… …………

Page 3: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

The example structure of DequeBlock

DequeBlock<T> similar to Node<T> use of a static array is to avoid unwanted array re-size in

STL’s vector

template <class T>class DequeBlock { T elm[BLOCK_SIZE]; friend class Deque<T>;};

Page 4: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

The example structure of Deque<T>

use STL’s list which is a doubly linked list length stores the number of elements in Deque indexInHead and IndexInTail store the first position of the

first block and the last index of the last block

template <class T> class Deque { list<DequeBlock<T>> blockList; size_t length; size_t indexInHead; size_t indexInTail;};

Page 5: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Constraints of Deque<T>

Middle blocks are full The first block usually stores data at its last part while the

last block usually stores data at its beginning part

…… …… ……

Page 6: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Adding elements into deque

push_back()

……

……

push_back()

push_back()

……

……

……

…………

push_front()

……

…… ……

Page 7: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Identifying an element in deque

…… …… ……

[0] [1][1+BLOCK_SIZE-1]

posInBlock() for deque[i] = i – [sizeInHead() + (previous blocks – 1) * BLOCK_SIZE ]

dequeBlock[pos] is posInBlock() and its corresponding deque [i] = sizeInHead() + (n-2) * BLOCK_SIZE + posInBlock() - 1

Page 8: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Exercises on using Deque<T>

Assume that a deque has the structures as in Deque.h and the BLOCK_SIZE is 3 (instead of 10 as in the program). Depict the diagrams, or write down the answer, after each set of the following operations is performed:

push_back("a"), push_back("b"), push_back("c"), push_back("d"), push_front("e"), push_front("f"), push_front("g"), push_front("h"), push_back("i")

identify the value of deque[5] , the block number of the value located, and the array index of the block

pop_back(), pop_front(), pop_front() identify the value of deque[5], the block number of the value located, and the

array index of the block. pop_back(), pop_back(), pop_front(), pop_front(), pop_front()

Page 9: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Another implementation of deque

Collin’s 5:82 : An array (map) of blocks with pointers

map start

finish

yes

true now

good

loveclear

right

Page 10: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

The iterator classmap start

finish

yestrue

now

good

loveclear

right

Collin’s 5:85-86: first, last, current, node

Page 11: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Sequence containers

Vector, list, and deque are sequence containers

deque is a compromise of vector and list Creation on demand for a block to save the overhead of

creation for each node Shift operations involve only a block or at most two

blocks for Collin’s structure Allow random access and thus allow for efficient search It also has the disadvantages of vector and list but they

are limited to a lower degree with some space and computation overhead

Page 12: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Container for order re-visit

Will “order” use deque as its container better? Use vector

• A small vector causes resizing for “long” orders

• A large vector results in wasting space for “short” orders Using list solves the above problem but creation for each node is

not efficient Use deque

• The one in Deque.h avoids resizing the array by simply creating another block and each block can be defined as a “short” block

• The one in Collin’s is similar with more flexibility in insertion/removal but more space overhead

Page 13: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Application considerations

A typical order system in a restaurant usually includes 2 containers:

Menu: All the dishes provided in the restaurant. Typical contents of each item are the dish name and its price.

Table Order: All the dishes ordered by a table/customer. Typical contents of a Table Order includes the table id or a customer name and all the orders of the table. Each order includes at least the dish name and the quantity.

Before analysis, assumptions should be made for Types of operations and their frequency

Page 14: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Analysis of using which containers

Define (Make the assumptions) the attributes of the containers in the following areas:

Elements adding frequency Elements removing frequency Search frequency Update frequency

Page 15: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Comparisons – Storage

Deque Vector (or array) List

For Collin’s structure, some space in the front/back blocks is wasted

Unused slots are wasted Efficient because of create on demand

Overhead is required for links or the class of Iterator

No overhead for each element

A pointer is the overhead of each element

Page 16: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Comparisons – Adding/Removing elements

Deque Vector (or array) List

Efficient for both adding/removing at the end or at the front

Efficient when appending while there is still room Adding to the front causes lots of operations

fast when adding an element in any position if the position is set

Insert involves fewer “shift” operations than vector that depend on the size of a block for Collin’s structure

Insert may involve a lot of shift operations which depend on number of data

Insert/remove causes only a fixed number of operations

Page 17: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Comparisons – Access

Deque Vector (or array) List

Random access Random assess No random access

Can apply Binary Search

Can apply Binary search Can only apply linear search

Traversal is in between Vector and List

Traversal is simple and efficient since data are stored contiguous

Traversal is not complicated but a bit slower than vector

Page 18: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Comparisons – Memory allocation

Deque Vector (or array) ListActions are required when a block is full

Actions are only required when the array needs to be re-sized

Actions are required for each add/delete operation on a list

Size of a new block is fixed and usually much smaller than the one in vector

Size of a new block depends on number of data

Size of a new node is fixed and usually the size is small

No additional action is required for creation of a new block

Copying of the original elements is required

No additional action is required for creation of a new node

Page 19: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

No appropriate container?

It seems that it is difficult to find a suitable container for “order”

Possible application re-design? Consider an "Order" in a local super market, it only

allows for an item to be added to an order and there is no modification. Although an order may consist of more than one identical item, the only “append” operation simplifies the requirements in choosing an efficient container for the application

Page 20: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Summary

Two typical deque structures are studied: a list of vector and a map of blocks

Deque compromises the advantages and disadvantages of linked list and array and uses a node to store an array to reduce the times of using new/delete

There is not a sequence container that can always be the best for the frequent operations: add, remove, update, and search

Page 21: Rossella Lau Lecture 5, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque

Rossella Lau Lecture 5, DCO20105, Semester A,2005-6

Reference

Ford: 6, 9.1-4, Collin: 5.4

STL online references http://www.sgi.com/tech/stl http://www.cppreference.com/

Example programs: Deque.h (v.8)

-- END --