Download presentation
Presentation is loading. Please wait.
Published byHendra Hermanto Modified over 6 years ago
1
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Lecture Carrano Chapt. 15 (partial); CUSACK CHAPT 4.
2
Agenda Queues HW5: Questions / Discussion Peer Design Review
How will we grade this? Signatures of BST Peer Design Review Trees: BST If time, prop logic
3
Queues
4
A Pointer-Based Implementation
2 4 1 7 NULL front back
5
class MyQueue { public: MyQueue(); ~MyQueue(); bool isEmpty() const; int getCount() const; int Dequeue() throw (exception); void Enqueue(const int &val); private: struct Node int val; Node *next = NULL; }; Node *front; Node *back; int count;
6
bool MyQueue::isEmpty() const
{ if (front == NULL) return true; } else return false; int MyQueue::getCount() const return count; MyQueue::MyQueue() { front = NULL; back = NULL; count = 0; } MyQueue::~MyQueue() { while (getCount() > 0) Dequeue(); }
7
void MyQueue::Enqueue(const int &value)
{ Node *insNode; insNode = new Node; insNode->val = value; if (count == 0) back = insNode; front = insNode; } else back->next = insNode; count++; int MyQueue::Dequeue() throw (exception) { if (count == 0) throw exception("Queue is empty"); } Node *temp = front; int retVal = temp->val; front = front->next; delete temp; if (count == 1) back == NULL; count--; return retVal;
8
Are we done? Do we need to overload =, copy constructor?
Why or why not? Any other overloads?
9
Queue usage in OS, Network, Services
Message queues Scheduling queue TCP Flow control Payment processing
10
Unix Message Queues 1 2 Message queue (id = msgid)
struct mymesg { long mytype; char mtext[512]; } message_body; int main( void ) { int msgid = msgget( 100, IPC_CREAT ); strcpy( message_body.mtext, “hello world\n” ); msgsnd( msgid, &message_body, 512, 0 ); } struct mymesg { long mytype; char mtext[512]; } message_body; 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) 1 2 Some other process can enqueue and dequeue a message
11
Multilevel Queue Scheduling
Each queue has its own scheduling algorithm, foreground (interactive) – RR, 80% CPU time background (batch) – FCFS, 20% CPU time
12
Multilevel Feedback-Queue Scheduling
A new job enters queue Q0 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 Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.
13
Flow Control X X X X Sending application Receiving application packet
Send Socket Buffer Receive Socket Buffer send ack read blocked packet X packet packet retransmitted packet dropped Application is slow to read. X packet packet retransmitted packet dropped X X write blocked
14
Lab 5 PEER DESIGN REVIEW
15
Grading Lab5 Copy all *.cpp and *.h files into a directory
Copy BankTransIn.txt into directory Open up a command line window for compilation cl *.cpp name.exe > BankTrans.out 2> BankTrans.err
16
Trees
17
FIGURE 15-1 (a) A tree; (b) a subtree of the tree in part a
Terminology Root Parent Child Ancestor Descendent Height Subtree N-ary tree Binary Tree FIGURE 15-1 (a) A tree; (b) a subtree of the tree in part a
18
Binary Tree: Algebraic Expressions.
19
Binary Search Tree Well suited for searching
For each node n, a binary search tree satisfies the following three properties: n ’s value is greater than all values in its left subtree T L . n ’s value is less than all values in its right subtree T R . Both T L and T R are binary search trees. Well suited for searching Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
20
Binary Search Tree: Example
21
Binary Search Tree: Example
FIGURE Binary search trees with the same data as in Figure 15-13
22
Binary Search Tree: Example
FIGURE Binary search trees with the same data as in Figure 15-13 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
23
Binary Search Tree: Example
FIGURE Binary search trees with the same data as in Figure 15-13 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
24
Computer Scientist of the Week
J. Alex Halderman Professor at University of Michigan Expert on Computer security and privacy Focus on issues which impact public policy and society Coursera: Securing Digital Democracy 2016: Advised for a recount in WI, MI Explanation Popular science top-10 brightest mind: Popular Science
25
class BinarySearchTree
{ public: BinarySearchTree(); ~BinarySearchTree(); void Insert(Object *); bool Remove(const Object &); bool Retrieve(const Object &, Object * &); void Flush(); bool Contains(const Object &); void Display() const; bool isEmpty() const; int Height() const; int getCount() const; private: struct Node Object *pObj; Node *right; Node *left; }; Node *root;
26
Searching a Binary Search Tree
Data Structures and Poblem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
27
Searching a Binary Search Tree: pseudo-code
Search(Obj *root) { if (root == NULL) return NULL; else if (target == *(root->pObj)) return (root->pItem); } else if (target < *(root->pObj)) return Search(root->left); else return Search(root->right)
28
Creating a Binary Search Tree
FIGURE Empty subtree where the search algorithm terminates when looking for Frank Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
29
Efficiency of BST Operations
Retrieve: Insert: Removal: Traversal:
30
Class Bell
31
Propositional Logic
32
Text Book An Active Introduction to Discrete Mathematics and Algorithms (version 2.6), Charles Cusack, David Santos, GNU Free Software, 2016: athematics%20and%20Algorithms/ActiveIntroToDiscreteMathAndAlgorithms.2.6.pdf Chapter 4: Logic.
33
Propositions Proposition: a statement that is either true or false, but not both Examples of propositions Proposition p: All mathematicians wear sandals. Proposition r: Discrete Math is fun. Proposition v: This is not a proposition. Proposition s: The sum of the first n odd integer is equal to n2 for n >1. These are not propositions: What is your name? Go to the store and get me a steak? Propositions have a truth value: True or False.
34
Propositions Math History Programming languages
The only positive integers that divide 7 are 1 and 7 itself: (true) For every positive integer n, there is a prime number larger than n: (true) History Alejandro Inarritu won an Academy Award in 1999 for directing “Amores perros”: (false, he won in 2015 for Birdman) Seattle held the World’s Fair, Expo 62: (true ) Programming languages Boolean expressions in if-else, while, and for statements for ( index = 0; index < 100; index++ ) { …….; } A proposition Not a proposition
35
Compound propositions
Conjunction (AND) of p and q notations: p ^ q, p && q True only if both p and q are true Truth table Disjunction (OR) of p or q Notations: p v q, p || q True if either p or q or both are true truth table p q p ^ q F T p q p v q F T
36
Compound Propositions
The negation (NOT) of p Notations: not p, ¬p !p Examples P: = 3 (false) !p: !(1 + 1 = 3) ≡ ≠ 3 (true) Exclusive OR (XOR) Notations: p xor q, p !p F T p q p q F T
37
Binary Expressions in C++
How do you examine the behavior of if-else? if ( a > = 1 && a <= 100 ) true if 1 ≤ a ≤ 100 if ( a >= 1 || a <= 100 ) always true a < 1 1 <= a <= 100 100 < a a >= 1 a <= 100 false true condition proposition a < 1 1 <= a <= 100 100 < a a >= 1 a <= 100 false true condition proposition
38
Theorems for Boolean algebra
Commutative Law p v q = q v p p ^ q = q ^ p Associative Law p v (q v r) = (p v q) v r p ^ (q ^ r) = (p ^ q) ^ r Distributive Law p ^ (q v r) = (p ^ q) v (p ^ r) p v (q ^ r) = (p v q) ^ (p v r) Identity p v F = p P ^ T = p Domination p v T = T p ^ F = F
39
Theorems for Boolean algebra
Complement Law (tautologies) p ^ ¬p = F p v ¬p = T Square Law p ^ p = p p v p = p Double Negation ¬(¬p) = p Absorption p ^ (p v q) = p p v (p ^ q) = p
40
Precedence of Operators
NOT: ¬ AND: ^ XOR: OR: v Conditional: → iff: ↔
41
Prove the following Boolean equations
¬p ^ q v p ^ ¬q = (¬p v ¬q) ^ (p v q) p ^ q v p ^ q ^ r v p ^ ¬ q = p (p ^ q v r ) ^ q = p ^ q ^ ¬r v ¬p ^ q ^ r v p ^ q ^ r
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.