690 likes | 809 Views
Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang). Data Abstraction. Data Abstraction. Data type A data type is a collection of objects and a set of operations that act on those objects
E N D
Bioinformatics Programming EE, NCKU Tien-Hao Chang (Darby Chang)
Data Abstraction • Data type • A data type is a collection of objects and a set of operations that act on those objects • For example, the data typeintconsists of the objects{0, +1, -1, +2, -2, …, INT_MAX, INT_MIN}and the operations+, -, *, /, and % • The data types of C • basic data types: char, int, float, and double • group data types: array and struct • pointer data type • user-defined types • Abstract data type • An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. • We know what is does, but not necessarily how it will do it.
The Stack ADT • A stackis an ordered list in which insertions and deletions are made at one end called the top • If we add the elements A, B, C, D, and E to the stack, in that order, then E is the first element we delete from the stack • A stack is also known as aLast-In-First-Out (LIFO)list
http://www.beaconelevator.com/i/elevator_myth.jpg Why we need such a data structure?
StackEvaluation of Expressions • The representation and evaluation of expressions is of great interest to computer scientists • (rear+1==front) || (rear==MAX_QUEUE_SIZE-1) (3.1) • x=a/b-c+d*e-a*c (3.2) • If we examine these expressions, we notice that they contains: • operators ==, +, -, ||, &&, ! • operands a, b, c, e • parentheses ( ) • Understanding the meaning of expressions • assume a=4, b=c=2, d=e=3 in the statement (3.2) • interpretation 1: ((4/2)-2)+(3*3)-(4*2) = 0+8+9 = 1 • interpretation 2: (4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.66666… • The challenge is to efficiently generate the machine instructions corresponding to a given expression with precedence and associative rule
Evaluation of ExpressionsPostfix Expressions • The standard wry of writing expressions is known as infix notation • binary operator in-between its two operands • Infix notation is not the one used by compilers to evaluate expressions • Actually, Java virtual machine is a stack machine • Instead compilers typically use a parenthesis-free notation referred to as postfix notation
Evaluation of ExpressionsEvaluate Postfix Expressions • Evaluating postfix expressions is much simpler than the evaluation of infix expressions • no parentheses • no precedence • There are no parentheses to consider • To evaluate an expression we make a single left-to-right scan of it • We can evaluate an expression easily by using a stack
Evaluation of ExpressionsData Representation • We now consider the representation of both the stack and the expression
Can You write a program to evaluate expressions? If not, what’s missing? A further question
Evaluation of ExpressionsInfix to Postfix • We can describe am algorithm for producing a postfix expression from an infix one as follows • fully parenthesize expression • a / b - c + d * e - a * c • ((((a / b) - c) + (d * e)) - (a * c)) • all operators replace their corresponding right parentheses • ((((a / b) - c) + (d * e)) - (a * c)) / - *+ *- • delete all parentheses • The order of operands is the same in infix and postfix
icp 13 20 12 19 13 0 isp 13 0 0 12 12 13 13 13 13
Evaluation of ExpressionsFrom Infix to Postfix • Assumptions • operators (, ), +, -, *, /, % • operands single digit integer or variable of one character • Operands are taken out immediately • Operators are taken out of the stack as long as their in-stack precedence (isp) is higher than or equal to the incoming precedence (icp) of the new operator • if (isp >= icp) pop • ‘(’ has low isp, and high icp • op ( ) + - * / % eosIsp 0 19 12 12 13 13 13 0Icp 20 19 12 12 13 13 13 0
Such two-phase strategy (a. infix to postfix and then b. evaluate postfix) is used in practice
The Queue ADT • A queueis an ordered list in which all insertion take place one end, called therearand all deletions take place at the opposite end, called thefront • If we insert the elements A, B, C, D, E, in that order, then A is the first element we delete from the queue • A stack is also known as a First-In-First-Out (FIFO)list
There might be available space when IsFullQ is true (movement is required) Answer
QueueRegard Array as Circular • We can obtain a more efficient representation if we regard the array queue[MAX_QUEUE_SIZE] as circular • front: one position counterclockwise from the first element • rear: current end • Only one space left when full
http://devcentral.f5.com/weblogs/images/devcentral_f5_com/weblogs/Joe/WindowsLiveWriter/PowerShellABCsQisforQueues_919A/queue_2.jpghttp://devcentral.f5.com/weblogs/images/devcentral_f5_com/weblogs/Joe/WindowsLiveWriter/PowerShellABCsQisforQueues_919A/queue_2.jpg Queue is much trivial in life
A Maze Problem • The most obvious choice is a 2D array • 0s the open paths and 1s the barriers • Notice that not every position has eight neighbors • To avoid checking for these border conditions we can surround the maze by a border of ones • an mp maze requires an (m+2)(p+2) array • from [1][1] to [m][p]
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 010 0 0 1 10 0 0 1 1 1 1 1 1 1 10 0 0 1 101 1 1 0 01 1 1 1 1 01 100 0 0 1 1 1 1 0 01 1 1 1 1 101 1 1 1 0 1 10 1 10 01 1 1 101 0 0 1 0 1 1 1 1 1 1 1 1 1 0 01 1 0 1 1 1 0 1 0 0 1 0 1 1 1 01 1 1 1 0 01 1 1 1 1 1 1 1 1 1 0 01 101 101 1 1 1 1 0 1 1 1 1 1 0 0 0 1 101 10 0 0 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 01 1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 01 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A Maze ProblemImplementation of Move • typedefstruct { short intvert; short inthoriz;} offsets;offsets move[8]; // array of moves for each direction • If we are at maze[row][col] and we wish to find the position of the next move, maze[next_row][next_col] • next_row = row + move[dir].vert;next_col = col + move[dir].horiz;
A Maze ProblemMaze Traversal Algorithm • Maintain a second two-dimensional array, mark, to record the maze positions already checked • Use stack to keep path history • typedefstruct { short int row; short intcol; short int dir;} element;element stack[MAX_STACK_SIZE];
Can We use queue to do the maze problem? If yes, what’s the differences ? A further question
A Maze ProblemAnalysis of path() • The worst case of computing time of path is O(mp), where m and p are the number of rows and columns of the maze respectively • The choice of add() and delete() decides the search behavior
ListOrdered List • Consider the following alphabetized list of three letter English words • bat, cat, sat, vat • If we store this list in an array • add the word mat to this list • move sat and vat one position to the right before we insert mat • remove the word cat from the list • move sat and vat one position to the left • Problems of a sequence representation (ordered list) • arbitrary insertion and deletion from arrays can be very time-consuming • waste storage
ListLinked Representation • An elegant solution of ordered list • Items may be placed anywhere in memory • Store the address, or location, of the next element for accessing elements in the correct order • Associated with each element is a node which contains both a data component and a pointer to the next item