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

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

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

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

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

Lecture Data Structure Stack

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

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

Infix to Postfix Conversion

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

DEEPIKA KAMBOJ UNIT 2. What is Stack?

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

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

Stacks. Revised based on textbook author s notes.

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

Data Structures Week #3. Stacks

March 13/2003 Jayakanth Srinivasan,

Lecture No.04. Data Structures

Stacks, Queues (cont d)

Stack and Its Implementation

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CS 206 Introduction to Computer Science II

STACKS AND QUEUES. Problem Solving with Computers-II

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

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

Postfix (and prefix) notation

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Types of Data Structures

Content: Learning Objectives

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

Data Structure - Stack and Queue-

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Stack Abstract Data Type

Linear Data Structure

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

List, Stack, and Queues

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Chapter 4: Basic C Operators

The Stack and Queue Types

1. Algorithm. Data Structures & Algorithms. How many times you weight? Algo 2 : Half Cutting. How many times you weight? Algo.1 :Two coins at a time

Operators & Expressions

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

CSE 230 Intermediate Programming in C and C++

Lecture 12 ADTs and Stacks

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

17CS33:Data Structures Using C QUESTION BANK

Prefix/Infix/Postfix Notation

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

BBM 201 DATA STRUCTURES

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

BBM 201 DATA STRUCTURES

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

Fall, 2015 Prof. Jungkeun Park

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

Stacks, Queues and Hierarchical Collections

Stacks Fall 2018 Margaret Reid-Miller

CS W3134: Data Structures in Java

Lab 7 1 Due Thu., 6 Apr. 2017

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

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

Programming in C++ 5. Integral data types

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

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

CSE 214 Computer Science II Stack

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Stacks and their Applications

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

[CS302-Data Structures] Homework 2: Stacks

Algorithms and Data Structures

INSTITUTE OF AERONAUTICAL ENGINEERING

Draw a diagram of an empty circular queue and describe it to the reader.

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

DATA STRUCTURE UNIT I

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Code No: R Set No. 1

CS 206 Introduction to Computer Science II

Top of the Stack. Stack ADT

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

This book is licensed under a Creative Commons Attribution 3.0 License

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Linked List. April 2, 2007 Programming and Data Structure 1

Table of Contents. Chapter 1: Introduction to Data Structures... 1

12 Abstract Data Types

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

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Operators. Java operators are classified into three categories:

Constants and Variables

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

LECTURE 17. Expressions and Assignment

Top of the Stack. Stack ADT

CHAPTER 3 STACKS AND QUEUES

Discussion of project # 1 permutations

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Transcription:

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

Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types and operations and to determine what class of problems can be solved using them. 2) Implementation of these tools using already existing data types (such as arrays, lists, etc.) 2

Problem Solving Paradigm Analyze the Problem Use known Algorithms and Data Structure Abstraction (Space/Efficiency: Space-Time Trade-Off) Select an appropriate Data Structure Design Algorithm for Data Structure Implementation Translate into Target Language 3

Problem Solving Paradigm (Phases) Specify Requirements Designing Algorithm Refinement and Coding Verification Analyzing Algorithm 4

1. Specifying Requirements Understanding the given information (input), and what results to be produced. Writing a rigorous description of all the possible cases of inputs and outputs. 5

2. Designing Algorithm Can the problem be solved algorithmically? Identifying operations to be performed on several data objects. Informally writing techniques or steps involved in processing (correctness??) Modularity (structural programming) is useful in verifying correctness. Simplicity (may not be efficient) is desirable. Good for writing, verifying correctness and debugging. 6

3. Analyzing Algorithm Is the problem solvable practically? Example: Chess Playing Program: Consider all possible moves (at least 10 9 by some estimates) A program which makes a thorough examination of them would take several thousand years to execute. 7

Considerations for Practical Importance (Time and Space Requirements (complexity)): How can a given algorithms and programs be improved? How can the efficiency of algorithms be analyzed? What criteria should be used to choose between different algorithms for the same application? In what sense can algorithms be shown to be the best possible? 8

Space-Time Trade-off Trade-off - amount of time required to perform algorithm versus - amount of space Usually space constraints are less important. 9

4. Refinement and Coding Selection of a Data Structure Selection of Language (host consideration) Suggestive approach is that all processing, which is independent of data structure must be written out first. 10

5. Verification Correctness or Program Proving Using Theorems and Lemmas Example: Validity of the Gauss elimination method for solving of linear equations, depends upon a number of theorems in linear algebra. Using Mathematical Induction Testing Creating sample data and run the program Debugging 11

Abstract Data Type A method (concept) for interpreting the memory contents of a computer (which are bits) Example: contents may represent integers, real numbers, characters, words, etc. 1) Logical properties, and, 2) Legal operations need to be specified for proper interpretation. 12

Abstract Data Type - Declaration - Operations - Implementation A Semiformal Approach 1. Value Definition abstract type condition (optional) 2. Operator(s) Definition(s) abstract function pre-condition (optional) post-condition (specifies what the operation does) 13

What Basic Data Structure(s) Should be used to Implement Higher Level ADT? Arrays (static) Pointers/Liked List (dynamic) 14

1-D Case: Example: Array as an ADT abstract typedef<<eletype, ub>> ARRTYPE (ub, eltype); condition type(ub) == int, Example of ARRTYPE: int a[10]; Possible Functions: Get a value (Extraction via index, e.g. x=a[i] ) Store a value (assignment statement a[3]=x ) ADT Representation: Internally as linear chain of memory. 15

Implementation of Arrays (Logical to Physical Mapping) int b[10]; Allocate 10 successive memory locations, where the base address is the first location. Each element is of a particular size (memory units having a unique address), esize. esize b[0] base b[1] base + esize b[i] Address of b[i] base + i* esize b[9] base +9* esize 16

Implementation of 2-D Arrays (Logical to Physical Mapping) int b[2][4]; b[0][0] b[0][1] b[0][2] : : b[1][0] b[1][1] : : b[2][4] 17

Two Ways to Map 2-D Arrays (Logical to Physical Mapping) Row Major Indexing First row occupies the first set of memory locations, then the second row and so on. Also known as lexicographic order. Column Major Indexing First column occupies the first set of memory locations, then the second column and so on. 18

Row Major Indexing int b[r1][r2]; Physical Address of b[i][j]= base + [i* r2 + j] *esize How about Column Major Indexing?? Generalization to Multidimensional Arrays 19

Some Higher Level Abstract Data Structures Stacks (Chapter 2) Queues (Chapter 4) Usage: to hold ordered lists. Examples: - Math expression 2 * 3 + 4 4 + 2 * 3 Order (priority) of execution of operators - Time events (arrivals, departures,... ) - Values in a deck of cards 20

What is Stack? Insertion (push) Deletion (pop) M Growth 21

What is Stack? Last In First Out (LIFO) (i) Queue 1) Type of Element 2) Size (ii) Operations PUSH > Test Overflow pre-condition POP > Test Underflow pre-condition 22

Stacks Operations (i) Empty: to check for empty (ii) PUSH (+ Test) (iii) POP (test for empty) (iv) Stack top What is its value of the top element without popping? 23

Stacks (cont.) Implementation - Array - Pointers An Application of Stack: Prefix, Infix, Postfix Representation of Expressions 24

Formal Definition - structure STACK (item) 1 declare CREATE ( ) stack 2 ADD (item, stack) stack 3 DELETE (stack) stack 4 TOP (stack) item 5 ISEMTS (stack) boolean; 6 for all S stack, i item let 7 ISEMTS (CREATE) ::= true 8 ISEMTS (ADD (i,s) ) ::= false 9 DELETE (CREATE) ::= error 10 DELETE (ADD (i,s)) ::= S 11 TOP (CREATE) ::= error 12 TOP (ADD (i,s)) ::= I 13 end 14 end STACK 25

Representing Stacks in C Implementation using 1-D Array Both are ordered collection of items Problems: 1. Stack is dynamic, Array is static 2. Access modes are different Solutions: 1. Make bounds large 2. Use an index variable to track top of stack (track boundary conditions) 26

Implementation and Operations on Stacks #define STACKSIZE 100 #define INTGR 1 #define FLT 2 #define STRING 3 Struct stackelement { int etype /* etype equals INTGR, FLT, or STRING */ /* depending on the type of the */ /* corresponding element. */ union { int ival; float fval; char *pval; /* pointer to a string */ } element; }, struct stack { int top; struct stackelement items [STACKTOP]; }; 27

Summary of Pros and Cons for Using Linked Lists Advantages: Flexibility due to dynamic storage No overflow problem Changes (Insertion and Deletions) Disadvantages: Space Use Random Access Programming Inconvenience 28

Application: The Polish Notation Representing Arithmetic Expressions that are easy to process by the computer using some simple data structure such as stack. Prefix, Infix, Postfix Representation of Expressions Evaluation of Expressions Conversion (Bottom-up Parsing) 29

Priority of Operations To understand an infix expressions meaning, we need to know the priority of operations! e.g. 2 + 3 * 2 Does this mean: (a) (2 + 3) * 2 or (b) 2 + (3 * 2) Priorities: (Perform highest priority operations first) 30

Priority of Operations (cont.) Operator Priority ^, all unary operators, eg, sign (+,-) 6 x, /, % 5 + - (binary) 4 ==!= 3 < <= > >= 2 && 1 = 0 31

Left Versus Right Association A + B + C is understood as (A + B) + C Left > Right If we indicate exponentiation as ^, then for: A ^ B ^ C we assume: A^(B ^ C) 2^2^3 = 2^8 32

Polish Expressions: Postfix, Prefix and Infix Postfix (Reverse Polish Notation or Suffix Form): All operators appear after their operand(s): AB+ (HP calculators) Prefix: Every operator is written before its operand(s): +AB Infix: Customary way A+B Example: Infix Postfix Prefix A + B < C A B + C< <+A BC 33

Advantages of Postfix: No more parenthesizing Priority of each operator is no more relevant (easy to implement in the computer) 34

Evaluation of Postfix Expression Using a Stack (can be implemented recursively) Scan the Postfix expression from Left to Right. While there are tokens (symbols) in the expression: *Get the next token. *If token is an operand, push it on the stack. *Otherwise, remove from the stack, the correct number of operands for the operator, evaluate and push the result back on the stack. 35

Example 36

Stack-Based Algorithm 37

Conversion from Infix to Postfix 1. Fully parenthesize the infix expression by taking precedence into account. 2. Move all operators so that they replace their corresponding right parenthesis. 3. Delete all parentheses. A + B + C ===> AB+C+ A^B^C ===> ABC^^ 38

Conversion Algorithm: Infix to Postfix (cont d) Convert the parenthesized expression first (recursively), that is, within a parenthesis perform this step. Operations with the highest precedence are converted first. Once an operation is converted, treat the operator and its operands as a single operand. Take into account the associativity rule. 39

Example 40

Algorithm for Infix Prefix This algorithm is similar to infix to postfix, except operators come before the operand. Ex: A^B*C-D+E/F/(G+H) 41

Implementation Infix > Postfix: Problem with this algorithm: requires two passes. But, we have learned that the order of the operands is unaffected by the conversion process!! We can scan an expression for the first time, passing operands to output. Store operators on stack until the correct moment to output. 42

43

A Priority Based Algorithm Using Stack Two priorities for operators: in-stack-priority (isp) in-coming-priority (icp) For all operators: icp = isp = given in the table except for ( isp of ( is the lowest = 0 icp of ( is the highest = 20 44

A Priority Based Algorithm Using Stack 1. Pass operands immediately to output (do not push on the stack). 2. Operators in the stack are taken out to the output expression as long as their isp s are greater than or equal to the icp of incoming operator. After popping out these operators, the incoming operator is pushed on the stack. 3. If incoming token is ), keep on popping the operators until a ( is encountered to eliminate the pair of parentheses. 45

Example: Example 1: A x (B + C) x D - Next Token Stack Output 1. none empty A 2. A empty A 3. x x A 4. ( x ( A 5. B x ( AB 6. + x ( + AB 7. C x ( + ABC 8. ) x ABC+ 9. x x ABC+x 10. D x ABC+xD - Since there is no more expression, pop all the operators giving: - A B C + x D x 46