Discussion of project # 1 permutations

Similar documents
STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Data Structure - Stack and Queue-

STACKS AND QUEUES. Problem Solving with Computers-II

Lecture Data Structure Stack

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

DATA STRUCTURES AND ALGORITHMS

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

March 13/2003 Jayakanth Srinivasan,

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

Name CPTR246 Spring '17 (100 total points) Exam 3

Stacks and their Applications

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Lecture No.04. Data Structures

The Stack and Queue Types

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Fall, 2015 Prof. Jungkeun Park

CS 211 Programming Practicum Spring 2017

// The next 4 functions return true on success, false on failure

Abstract Data Types 1

BBM 201 DATA STRUCTURES

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

BBM 201 DATA STRUCTURES

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

CS 211. Project 5 Infix Expression Evaluation

Postfix (and prefix) notation

[CS302-Data Structures] Homework 2: Stacks

Lecture 4 Stack and Queue

Infix to Postfix Conversion

Arrays and Linked Lists

Stack and Its Implementation

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Largest Online Community of VU Students

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Ashish Gupta, Data JUET, Guna

Programming Abstractions

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Monday, November 13, 2017

CS 211 Programming Practicum Fall 2018

.:: UNIT 4 ::. STACK AND QUEUE

CS 211 Programming Practicum Spring 2018

Linear Data Structure


Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Data Structures and Algorithms

Data Structures and Algorithms

UNIT VI. STACKS AND QUEUES

An Introduction to Queues With Examples in C++

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Foundations of Data Structures

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

csci 210: Data Structures Stacks and Queues

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Programming Abstractions

12 Abstract Data Types

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

infix expressions (review)

Stacks. Revised based on textbook author s notes.

Data Structures. Lecture 5 : The Queues

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Stacks, Queues and Hierarchical Collections

Formal Languages and Automata Theory, SS Project (due Week 14)

Data Structures Week #3. Stacks

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Problem with Scanning an Infix Expression

List, Stack, and Queues

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CS W3134: Data Structures in Java

Chapter 2. Stack & Queues. M hiwa ahmad aziz

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Data Structures and Algorithms Winter Semester

Stacks Fall 2018 Margaret Reid-Miller

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Largest Online Community of VU Students

CSE 230 Intermediate Programming in C and C++

Data Structures. Lecture 4 : The Queues. Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Transcription:

Discussion of project # 1 permutations Algorithm and examples. The main function for generating permutations is buildp. We don t need a separate class for permutations since the permutations objects are essentially the same as combination objects (in structure), i.e., vector of pointers to lists of integers. So, you can make buildp a function in the combination class.

Combinations buildp (int[] A, int j) { // generate all the permutations of // A[0], A[1],, A[j 1] if (j == 0) { Combinations C = new Combinations(1); // constructor generates one empty list return C; else { temp = buildp(a, j-1); result = Combinations(0); // result has 0 lists tempcopy = copy(temp); for (k=0; k <j; ++k) { insert A[j-1] in position k of each list of tempcopy; result = merge(result, tempcopy); tempcopy = copy(temp); return result; Example: A = { 1, 2, 3, j = 3

Implementation of exp evaluation token class: class token { private: int op_type; double value; public: token(int x, int y) { op_type = x; op_value = y; int get_op_type() { return op_type; double get_value() { return value; void set_op_type(int x) { op_type = x; void set_value(double y) {value = y; ; Op_type: 1 + 2-3 * 4 / 5 ** 6 operand 1 token represents end of expression op_value: value of the operand.

Input Look at the main program: int main(void) { string str = "908 100 200+ 23 19 * +/ 123 *"; Expr ex(str, 0); double rslt = ex.eval(); cout << "The result of evaluation is " << rslt << endl; return 0; ; There must be a space between successive operands. There need not be a space when an operand follows an operator, and after an operator. There can be more than one space after any token, including the last.

Implementation of expression evaluation double eval() { // assumes that the postfix expression is correct // also unary minus is not allowed. Operands have to be integers // although the final result can be non-integral Stack st(max_size); token tok = get_token(); //gets the next token while (tok.get_op_type()!= -1) { if (tok.get_op_type() == 6) st.push(tok.get_value()); else { double opd2 = st.pop(); double opd1 = st.pop(); double op = apply(tok.get_op_type(), opd1, opd2); st.push(op); current++; tok = get_token(); double result = st.pop(); return result; // eval ; // end Expr

token get_token() { Code for get_token token tok( -1, 0); if (current > exp.length() - 1) return tok; while (exp[current] == ' ') current++; if (current > exp.length() 1) return tok; if (exp[current] == '+') tok.set_op_type(1); else if (exp[current] == '-') tok.set_op_type(2); else if (exp[current] == '/') tok.set_op_type(4); else if (exp[current] == '*') { if (exp[current+1]!= '*') tok.set_op_type(3); else {tok.set_op_type(5); current++; else { // token is an operand double temp = 0.0; while (!(exp[current] == ' ') &&!optr(exp[current])) { temp= 10*temp+val(exp[current]); current++; return tok; //end get_token if (optr(exp[current])) current--; tok.set_op_type(6); tok.set_value(temp);

Converting infix to Postfix expression Recall the postfix notation from last lecture. Example: a b c + * It represents a*(b + c) What is the postfix form of the expression a + b*(c+d)? Answer: a b c d + * + Observation 1: The order of operands in infix and postfix are exactly the same. Observation 2: There are no parentheses in the postfix notation.

Example : (a) Infix: 2 + 3 4 Postfix: 2 3 + 4 (b) Infix: 2 + 3 * 4 Postfix: 2 3 4 * + The operators of the same priority appear in the same order, operator with higher priority appears before the one with lower priority. Rule: hold the operators in a stack, and when a new operator comes, push it on the stack if it has higher priority. Else, pop the stack off and move the result to the output until the stack is empty or an operator with a lower priority is reached. Then, push the new operator on the stack.

Applying the correct rules on when to pop Assign a priority: ( * and / have a higher priority than + and etc.) Recall: Suppose st.top() is + and next token is *, then * is pushed on the stack. However, ( behaves differently. When it enters the stack, it has the highest priority since it is pushed on top no matter what is on stack. However, once it is in the stack, it allows every symbol to be pushed on top. Thus, its in-stack-priority is lowest. We have two functions, ISP (in-stack-priority) and ICP (incoming-priority).

In-stack and in-coming priorities icp isp +, 1 1 *, / ** 2 2 3 3 ( 4 0

Dealing with parentheses An opening parenthesis is pushed on the stack (always). It is not removed until a matching right parenthesis is encountered. At that point, the stack is popped until the matching ( is reached. Example: (a + b * c + d)* a Stack: ( Output: a Stack: ( + Output: a Stack: ( + Output: a b Stack: ( + * Output: a b Stack: ( + * Output: a b c Stack: ( + + Output: a b c * Stack: ( + + Output: a b c * d Stack Output: a b c * d + + Stack * Output: a b c * d + + Stack * Output: a b c * d + + a Stack Output: a b c * d + + *

Code for conversion (infix to postfix) string postfix() { Stack st(100); string str =""; token tok = get_token(); string cur = tok.get_content(); while (tok.get_op_type()!= -1) { if (tok.get_value() == 1) str+= cur + " "; else if (cur == ")") { while (st.top()!= "(") str += st.pop() +" "; string temp1 = st.pop();

else if (!st.isempty()) { string temp2 = st.top(); while (!st.isempty() && icprio(cur) <= isprio(temp2)) { str+= temp2 + " "; string temp = st.pop(); if (!st.isempty()) temp2 = st.top(); if (tok.get_value()!= 1 && tok.get_content()!= ")") st.push(cur); current++; tok = get_token(); cur = tok.get_content(); while (!st.isempty()) { str += st.pop() + " "; cout << "string at this point is " << str << endl; return str;

Queue Overview Queue ADT FIFO (first-in first-out data structure) Basic operations of queue Insert, delete etc. Implementation of queue Array

Queue ADT Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served.

Insert and delete Primary queue operations: insert and delete Like check-out lines in a store, a queue has a front and a rear. insert - add an element at the rear of the queue delete remove an element from the front of the queue Remove front rear Insert

Implementation of Queue Queues can be implemented as arrays Just as in the case of a stack, queue operations (insert delete, isempty, isfull, etc.) can be implemented in constant time

Implementation using Circular Array When an element moves past the end of a circular array, it wraps around to the beginning, e.g. OOOOO7963 (insert(4)) 4OOOO7963 How to detect an empty or full queue, using a circular array? Use a counter for the number of elements in the queue. size == ARRAY_SIZE means queue is full Size == 0 means queue is empty.

Queue Class Attributes of Queue front/rear: front/rear index size: number of elements in the queue Q: array which stores elements of the queue Operations of Queue IsEmpty(): return true if queue is empty, return false otherwise IsFull(): return true if queue is full, return false otherwise Insert(k): add an element to the rear of queue Delete(): delete the element at the front of queue

class queue { private: point* Q[MSIZE]; int front, rear, size; public: queue() { // initialize an empty queue front = 0; rear = 0; size = 0; for (int j=0; j < MSIZE; ++j) Q[j] = 0; void insert(point* x) { if (size!= MSIZE) { front++; size++; if (front == MSIZE) front = 0; Q[front] = x;

point delete() { if (size!= 0) { rear++; if (rear == MSIZE) rear = 0; point temp(q[rear]->getx(), Q[rear]->gety()); size--; return temp; ; bool isempty() { return (size == 0); bool isfull() { return (size == MSIZE); ;

Breadth-first search using a queue BFS: application that can be implemented using a queue. Our application involves finding the number of distinct letters that appear in an image and draw bounding boxes around them. Taken from the output of the BFS algorithm