CSS 342 DATA S TR U CTURES ALGO

  • Slides: 34
Download presentation
CSS 342 DATA S TR U CTURES, ALGO RITHMS , AND DISCRETE MATHEMATICS I

CSS 342 DATA S TR U CTURES, ALGO RITHMS , AND DISCRETE MATHEMATICS I LECTU RE 16. 161129. CHAPT ER 13, 14.

Agenda • STL • Queues • Lab 5 Intro and peer design requirements

Agenda • STL • Queues • Lab 5 Intro and peer design requirements

STL Sequence Containers: the Big 3 (recap) • Vector • • Flexibly sized array

STL Sequence Containers: the Big 3 (recap) • Vector • • Flexibly sized array Access any element in constant time (index into array) Add/Remove from the end of array Data kept contiguous in memory • Deque • • Double ended queue Can add/look from front or back Access any element in constant time Not guaranteed to be contiguous in memory • List • Linked list • Need iterator to traverse • Can add anywhere in list in constant time

Recall the stack • Last In First Out (LIFO) • We implemented with following

Recall the stack • Last In First Out (LIFO) • We implemented with following structures: push • Array • Linked List • (a. Stack. push(new. Item)). pop() is equal to a. Stack • STL has a stack implementation as a Container Adapter • Container adapter on vector, deque, or list • Default is deque • Functions: empty, size, push, pop, top pop

#include <vector> #include <stack> #include <deque> int main( ) { stack<int> a. Stack; stack<int,

#include <vector> #include <stack> #include <deque> int main( ) { stack<int> a. Stack; stack<int, vector<int>> b. Stack; for (int i = 0; i < 3; i++) { b. Stack. push(i); } cout << "stack size is: " << b. Stack. size() << endl; cout << "top element is: " << b. Stack. top() << endl; b. Stack. pop(); cout << "Popped! " << endl; cout << "top element is: " << b. Stack. top() << endl; return 0; }

Big-O Challenge for (int i = 1; i <= n; i++) { int j

Big-O Challenge for (int i = 1; i <= n; i++) { int j = n; while (i * i < j) { j--; My. Big. Task(); } }

Queues

Queues

Queue • First in First Out (FIFO) • Can be implemented with Time =

Queue • First in First Out (FIFO) • Can be implemented with Time = 0 • Array • Linked List • (a. Q. push(new. Item)). pop() != new. Item Time = 12 • Uses • Many: powerful data structures for impls. • Often used for short lived (in-memory) or persistent applications (written to disk) • Modeling: Rich field on queueing theory/modeling CSS 342: QUEUES Time = 20 Time = 38 8

Queue Specification • STL has a stack implementation as a Container Adapter • Container

Queue Specification • STL has a stack implementation as a Container Adapter • Container adapter on vector, deque, or list • Default is deque • Functions: empty, size, push, pop, back, front pop( ): remove and get the front item push() to the back(): get the back item but do not remove it Front(): get the front item but do not remove it

int main( ) { queue<int> a. Q; for (int i = 0; i <

int main( ) { queue<int> a. Q; for (int i = 0; i < 5; i++) { a. Q. push(i); } cout << "Queue size is: " << a. Q. size() << endl; cout << "Front element is: " << a. Q. front() << endl; cout << "Back element is: " << a. Q. back() << endl; a. Q. pop(); cout << "Popped! " << endl; cout << "Front element is: " << a. Q. front() << endl; cout << "Back element is: " << a. Q. back() << endl; return 0; }

Comparison of Stack and Queue Operations • size, empty, push, pop, =, comparators are

Comparison of Stack and Queue Operations • size, empty, push, pop, =, comparators are all common syntax • Both are adapters which can be built on container classes • Differences: • stack: top • queue: front, back • Semantics: LIFO, FIFO

Queue implementation • Array Based • Pointer based • Can we make a queue

Queue implementation • Array Based • Pointer based • Can we make a queue from lists? • How can we make a queue Persistent?

In-Class Code • Create a queue using an array as an underlying data structure

In-Class Code • Create a queue using an array as an underlying data structure

An Array-Based Implementation 0 k front back 47 2 4 1 0 1 2

An Array-Based Implementation 0 k front back 47 2 4 1 0 1 2 49 front back …. . 1 2 …. . k …. . 0 7 the. Array. size( )-1 4 10 7 47 48 49 the. Array. size( )-1 1) Shift left on each removal? 2) Shift left when end of queue is reached? 3) Never Shift but maintain moving front and back pointers? 4) Allocate new array when full?

Circular-array implementation front 0 the. Array. size( )-1 2 4 1 7 1 2

Circular-array implementation front 0 the. Array. size( )-1 2 4 1 7 1 2 3 back

MAX_Q-1 (1) 0 front back (2) 7 count = 0 back 2 count =

MAX_Q-1 (1) 0 front back (2) 7 count = 0 back 2 count = 2 3 3 MAX_Q-1 0 (4) 1 4 2 9 1 6 Enqueue(7) Enqueue(6) 2 Enqueue(3) back Enqueue(8) Enqueue(9) Dequeue() Enqueue(2) Enqueue(4) Dequeue() count = 5 front 0 1 Initial state (3) MAX_Q-1 8 3 3 2 front MAX_Q-1 0 while (the. Q. size()) back { front Dequeue() } 1 2 count = 0 3

Contract (. h) class My. Queue { public: My. Queue(); ~My. Queue(); bool is.

Contract (. h) class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int get. Count() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: vector<int> arr; int front; int back; int count; void double. Queue. Size(); };

My. Queue: : My. Queue() { arr. resize(ARRAY_START_SIZE); front = 0; back = arr.

My. Queue: : My. Queue() { arr. resize(ARRAY_START_SIZE); front = 0; back = arr. size() - 1; count = 0; } void My. Queue: : double. Queue. Size() { } int My. Queue: : get. Count() const { return count; } bool My. Queue: : is. Empty() const { if (count == 0) { return true; } else { return false; } }

void My. Queue: : Enqueue(const int &val) { if (count == arr. size()) {

void My. Queue: : Enqueue(const int &val) { if (count == arr. size()) { double. Queue. Size(); } back = (back + 1) % arr. size(); arr[back] = val; count++; } int My. Queue: : Dequeue() throw (exception) { int ret. Val; if (count > 0) { ret. Val = arr[front]; front = (front + 1) % arr. size(); count--; return ret. Val; } else { throw exception("Queue Empty"); } }

Lab 5 Intro

Lab 5 Intro

Lab 5 • The Jolly Banker • Purpose: • Learn Queues • Learn Binary

Lab 5 • The Jolly Banker • Purpose: • Learn Queues • Learn Binary Search Trees • Practice class design • Two phase project • Design Review: Thursday, 12/1 • Lab Due: 12/9.

Computer Scientist of the Week Margaret Hamilton Developed on-board flight software for Apollo space

Computer Scientist of the Week Margaret Hamilton Developed on-board flight software for Apollo space program Director of MIT Instruments Lab Just awarded Presidential Medal of Freedom Contributions to async software, priority scheduling and priority Display • Foundations for ultra-reliable software design • Apollo 11 Landing: design helped to successfully land ship amidst Overloading • •

Computer Scientists who were awarded Presidential Medal of Freedom this week Margaret Hamilton Grace

Computer Scientists who were awarded Presidential Medal of Freedom this week Margaret Hamilton Grace Hopper Robert De. Niro

A Pointer-Based Implementation 2 front 4 1 7 back NULL

A Pointer-Based Implementation 2 front 4 1 7 back NULL

Bell Rang

Bell Rang

class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int

class My. Queue { public: My. Queue(); ~My. Queue(); bool is. Empty() const; int get. Count() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: struct Node { int val; Node *next = NULL; }; Node *front; Node *back; int count; };

My. Queue: : My. Queue() { front = NULL; back = NULL; count =

My. Queue: : My. Queue() { front = NULL; back = NULL; count = 0; } My. Queue: : ~My. Queue() { while (get. Count() > 0) { Dequeue(); } } bool My. Queue: : is. Empty() const { if (front == NULL) { return true; } else { return false; } } int My. Queue: : get. Count() const { return count; }

void My. Queue: : Enqueue(const int &value) { Node *ins. Node; ins. Node =

void My. Queue: : Enqueue(const int &value) { Node *ins. Node; ins. Node = new Node; ins. Node->val = value; if (count == 0) { back = ins. Node; front = ins. Node; } else { back->next = ins. Node; back = ins. Node; } count++; } int My. Queue: : Dequeue() throw (exception) { if (count == 0) { throw exception("Queue is empty"); } Node *temp = front; int ret. Val = temp->val; front = front->next; delete temp; if (count == 1) { back == NULL; } count--; return ret. Val; }

Are we done? • Do we need to overload =, copy constructor? • Why

Are we done? • Do we need to overload =, copy constructor? • Why or why not? • Any other overloads?

Queue usage in OS, Network, Services • Message queues • Scheduling queue • TCP

Queue usage in OS, Network, Services • Message queues • Scheduling queue • TCP Flow control • Payment processing

Unix Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main(

Unix Message Queues struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body. mtext, “hello worldn” ); msgsnd( msgid, &message_body, 512, 0 ); } int main( void ) { int msgid = msgget( 100, IPC_CREAT ); msgrcv( msgid, &message_body, 512, 0, 0 ); cout << message_body. mtext << endl; } Message queue (id = msgid) 0 1 2 Some other process can enqueue and dequeue a message

Multilevel Queue Scheduling • Each queue has its own scheduling algorithm, – foreground (interactive)

Multilevel Queue Scheduling • Each queue has its own scheduling algorithm, – foreground (interactive) – RR, 80% CPU time – background (batch) – FCFS, 20% CPU time

Multilevel Feedback-Queue Scheduling • • A new job enters queue Q 0 which is

Multilevel Feedback-Queue Scheduling • • A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2.

Flow Control Sending application Receiving application packet Send Socket Buffer Receive Socket Buffer send

Flow Control Sending application Receiving application packet Send Socket Buffer Receive Socket Buffer send ack read blocked X packet retransmitted packet dropped packet retransmitted packet dropped X write blocked X X Application is slow to read.