Data Structure (Part II) - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Data Structure (Part II)

Description:

Stacks and Queues – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 31
Provided by: 9381
Category:

less

Transcript and Presenter's Notes

Title: Data Structure (Part II)


1
Data Structure (Part II)
  • Stacks and Queues

2
3.5 A Mazing Problem
3
A Maze
  • Define a m p maze as follows
  • A two-dimensional array with height m2 and
    width p2.
  • To avoid boundary condition, the maze is surround
    by 1s.
  • For 1 ?i ?m and 1 ?j ?p,
  • Walk mazeij 0.
  • Wall If mazeij 1.

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4
Mazing Problem
  • Determine whether there exists a path from the
    entrance maze11 to the exit mazemp.

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
entrance
exit
5
Allowable Moves
  • For we are at (i, j), there are eight possible
    directions we can move forward.

NW i-1j-1 N i-1j NE i-1j1
W ij-1 ij1 E
i1j-1 SW i1j S i1j1 SE
x
ij
The position (i, j) is marked X when it is
visited.
6
Allowable Moves
  • Table of moves

q offset on i moveq.a Offset on j moveq.b
0 N -1 0
1 NE -1 1
2 E 0 1
3 SE 1 1
4 S 1 0
5 SW 1 -1
6 W 0 -1
7 NW -1 -1
class offsets public int a,
b enum directions N, NE, E, SE, S, SW, W,
NW offsets move8
7
Program 3.16
maze
mark
to mark visited positions
Stack
0 1 2 3 4 5 6 7
0
1 0 1 0 0 0 1
2 1 0 0 0 1 1
3 0 1 1 0 0 0
4 1 1 0 1 1 1
5 1 1 0 0 0 0
6 0 1 1 1 0 0
7

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

1
(1, 1, E)
lt i, j, nextDir. gt
Initial State
8
Program 3.16
maze
mark
Stack
0 1 2 3 4 5 6 7
0
1 0 1 0 0 0 1
2 1 0 0 0 1 1
3 0 1 1 0 0 0
4 1 1 0 1 1 1
5 1 1 0 0 0 0
6 0 1 1 1 0 0
7

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

1
1
1
1
1
1
(1, 5, SW)
(1, 4, E)
(1, 3, E)
(2, 2, NE)
(1, 1, E)
lt i, j, nextDir. gt
(1, 1, E) ?
(1, 2)
Wall
(1, 1, SE) ?
(2, 2)
Walk
Can walk? (not wall and not visited)
Move forward
9
Program 3.16
maze
mark
Stack
0 1 2 3 4 5 6 7
0
1 0 1 0 0 0 1
2 1 0 0 0 1 1
3 0 1 1 0 0 0
4 1 1 0 1 1 1
5 1 1 0 0 0 0
6 0 1 1 1 0 0
7

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

1
1
1
1
1
1
1
1
1
(3, 5, W)
(2, 4, SE)
(1, 5, SW)
(1, 4, E)
(1, 3, E)
(2, 2, NE)
(1, 1, E)
Move backward
10
Program 3.16
maze
mark
Stack
0 1 2 3 4 5 6 7
0
1 0 1 0 0 0 1
2 1 0 0 0 1 1
3 0 1 1 0 0 0
4 1 1 0 1 1 1
5 1 1 0 0 0 0
6 0 1 1 1 0 0
7
(5, 6, S)

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

(5, 5, E)
1
1
1
1
(5, 4, E)
(4, 3, SE)
1
1
(3, 4, SW)
1
1
1
(3, 5, W)
(2, 4, SE)
1
(1, 5, SW)
1
1
1
(1, 4, E)
(1, 3, E)
(2, 2, NE)
(1, 1, E)
Result
11
Analysis of Program 3.16
  • Space complexity
  • An extra 2D array mark is used O(mp)
  • Time complexity
  • The inner while-loop executes at most eight times
    for each direction. Each iteration takes O(1)
    time. Therefore, the inner loop totally takes
    O(1) time.
  • The outer while loop executes until the stack is
    empty.
  • If the number of zeros in the maze is z, at most
    z positions can be marked.
  • Since z is bounded above by mp, the computing
    time is O(mp).

12
3.6 Evaluation of Expression
13
Evaluation of Expression
  • Expressions
  • An expression is made up of operands, operators
    and delimiters.
  • Example
  • A / B C D E A C
  • Operands A, B, C, D, E (or constants).
  • Operators /, , , .
  • Delimiter (, ).

14
How to Evaluate an Expression in Right Order?
  • Assign each operator a priority
  • Priority of operators in C
  • How to evaluate A B / C? Evaluate the
    expression from left to right.
  • Use parentheses to define computation priority.

Priority Operator
1 Unary minus, !
2 , /,
3 , -
4 lt, lt, gt, gt
5 , !
6
7
High priority
Low priority
15
Postfix Notation
  • Each operator appears after its operands.
  • The way of how compiler evaluate an expression.
  • Example

Infix Notation Postfix Notation
A B A B
A B / C A B C /
A/ B C D E A C A B / C D E A C
16
3.6.3 Infix to Postfix
  • Use parentheses to group operands according to
    the operator they use.
  • Change the position of operator.
  • Remove all the parentheses.
  • Example

A / B C D E A C
Step 1.
( ( ( ( A / B ) C ) ( D E )) ( A C ) )
( ( ( ( A / B ) C ) ( D E )) ( A C ) )
Step 2.
( ( ( ( A B ) / C ) ( D E ) ) ( A C ) )
Step 3.
A B / C D E A C
17
Exercises
  • Convert the following infix expression into
    postfix notation.
  • A B / C
  • A / B C D E A C

((A B) / C)
((A B) / C)
((A B) C) /
A B C /
18
Reasons to Convert Expressions to Postfix
Notation
  • Parentheses are eliminated.
  • Example
  • Easier than infix evaluation.
  • The priority of the operators is no longer
    relevant.

Infix Notation Postfix Notation
A (B C) A B C
A/ B (C D) E A B / C D E
19
Evaluate an Infix Expression
  • Issues
  • How to convert infix notation to the postfix?
  • How to evaluate an postfix expression?
  • Clue
  • Using stack.

20
Convert from Infix to Postfix
  • Observation
  • The order of operands is unchanged
  • Output operands immediately.
  • A B C D ? A B C D
  • The order of A, B, C and D is unchanged.
  • Stack operators until it is time to pass them to
    the output.

21
Example 1
A B C
Stack
Next Stack Output
none empty none
Next Stack Output
none empty none
A empty A Output operand
Next Stack Output
none empty none
A empty A Output operand
A Stack operator
Next Stack Output
none empty none
A empty A Output operand
A Stack operator
B AB
Next Stack Output
none empty none
A empty A Output operand
A Stack operator
B AB
AB has a priority higher than .
Next Stack Output
none empty none
A empty A Output operand
A Stack operator
B AB
AB has a priority higher than .
C ABC
Next Stack Output
none empty none
A empty A Output operand
A Stack operator
B AB
AB has a priority higher than .
C ABC
empty ABC Clear the stack
Init.


Pop out stacked operators that has higher
priority.
A
B
C


Output
22
Observation
  • The stack is used for operators.
  • The more upper the operator is, the higher its
    priority is.
  • When a new operator p is coming, operators that
    has higher priority than p will be popped out
    first before p is pushed.

23
Example 2
( A B ) C
Next Stack Output
none empty none
Next Stack Output
none empty none
( ( none ( is pushed directly
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
B ( AB
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
B ( AB
) empty AB ) pops out all operators until encountering the first ( parentheses will not be output.
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
B ( AB
) empty AB ) pops out all operators until encountering the first ( parentheses will not be output.
AB
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
B ( AB
) empty AB ) pops out all operators until encountering the first ( parentheses will not be output.
AB
C empty ABC
Next Stack Output
none empty none
( ( none ( is pushed directly
A ( A
( A No operator except ) can pop (
B ( AB
) empty AB ) pops out all operators until encountering the first ( parentheses will not be output.
AB
C empty ABC
ABC
Stack
Init.

(

A
B
C


Output
24
Example 3
A ( B C ) D
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
C ( ABC
) ABC
ABC The same priority. Pop out the old one and push the new one.
D ABCD
ABCD
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
C ( ABC
) ABC
ABC The same priority. Pop out the old one and push the new one.
D ABCD
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
C ( ABC
) ABC
ABC The same priority. Pop out the old one and push the new one.
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
C ( ABC
) ABC
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
C ( ABC
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
( AB
Next Stack Output
none empty none
A empty A
A
( ( A
B ( AB
Next Stack Output
none empty none
A empty A
A
( ( A
Next Stack Output
none empty none
A empty A
A
Next Stack Output
none empty none
A empty A
Next Stack Output
none empty none
Init.
Stack

(

A
B
C


Output

D
25
Algorithm
  • void InfixToPostfix (expression e)
  • Stack S
  • for (i0 ei is not the end of string i)
  • if (ei is an operand)
  • output ei
  • else if (ei is ()
  • S.Push(ei)
  • else if (ei is not ))
  • while (S is not empty)
  • y S.Pop()
  • if (y is not ( and the
    priority of y gt the priority of ei)
  • output y
  • else
  • S.Push(y)
  • break
  • S.Push(ei)

26
Analysis of InfixToPostfix
  • Suppose the input expression has length of n.
  • Space complexity O(n).
  • The stack used to buffer operators at most
    requires O(n) elements.
  • Time complexity
  • The function make only a left-to-right pass
    across the input.
  • The time spent on each operand is O(1).
  • The time spent on each operator is O(1).
  • Each operator is stacked and unstacked at most
    once.
  • Thus, the time complexity of InfixToPostfix() is
    O(n).

27
Evaluate a Postfix Expression
  • Strategy
  • Scan left to right.
  • Stack operands.
  • Evaluate operators using the top two operands on
    the stack and then push back the result.

28
Program 3.18
  • A / B C D E A C

A B / C D E A C
Operations Postfix Notation






The Stack is used for operands when evaluating
the expression.
T1 A / B
T1 C D E A C
T2 D E A C
T2 T1 C
T3 D E
T2 T3 A C
If encountering operator, the top two are popped
to perform computation. The result is pushed
back.
T4 T2 T3
T4 A C
E
C
T5 A C
T4 T5
B
C
D
T3
A
T5
A
T1
T6
T2
T4
T6 T4 T5
T6
Stack
29
Program 3.18
  • Algorithm to evaluate a postfix expression
  • void Eval (expression e)
  • Stack S
  • for (i0 ei is not the end of string
    i)
  • if (ei is an operand)
  • S.Push(ei)
  • else //ei is an
    operator
  • t1 S.Pop()
  • t2 S.Pop()
  • Perform the operation of ei with t1 and
    t2 and store the result in X
  • S.Push(X)

30
Analysis of Eval()
  • Suppose the input expression has length of n.
  • Space complexity O(n).
  • The stack used to buffer operands at most
    requires O(n) elements.
  • Time complexity
  • The function make only a left-to-right pass
    across the input.
  • The time spent on each operand is O(1).
  • The time spent on each operator to perform the
    operation is O(1).
  • Thus, the time complexity of Eval() is O(n).
Write a Comment
User Comments (0)
About PowerShow.com