Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · February 23, 2012 4:05:36 PM
AlgorithmsF O U R T H E D I T I O N
R O B E R T S E D G E W I C K K E V I N W A Y N E
1.3 DIJKSTRA'S TWO-STACK ALGORITHM
click to begin demo
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
value stack operator stack
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
2
Dijkstra's two-stack algorithm
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
operand operator
infix expression(fully parenthesized)
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
value stack operator stack
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
3
Dijkstra's two-stack algorithm
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
4
Dijkstra's two-stack algorithm
+ ( ( 2 + 3 ) * ( 4 * 5 ) ) )1
value stack operator stack
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
5
Dijkstra's two-stack algorithm
+ ( ( 2 + 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
6
Dijkstra's two-stack algorithm
( ( 2 + 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
7
Dijkstra's two-stack algorithm
( ( 2 + 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
8
Dijkstra's two-stack algorithm
( ( 2 + 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
9
Dijkstra's two-stack algorithm
2 + 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
(
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
10
Dijkstra's two-stack algorithm
+ 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
2
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
11
Dijkstra's two-stack algorithm
+ 3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
2
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
12
Dijkstra's two-stack algorithm
2
3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
13
Dijkstra's two-stack algorithm
2
3 ) * ( 4 * 5 ) ) )
1
value stack operator stack
+
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
14
Dijkstra's two-stack algorithm
2 +
) * ( 4 * 5 ) ) )
1
value stack operator stack
+
3
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
15
Dijkstra's two-stack algorithm
2 +
) * ( 4 * 5 ) ) )
1
value stack operator stack
+
3
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
16
Dijkstra's two-stack algorithm
2 +
3
* ( 4 * 5 ) ) )
1
value stack operator stack
+
)
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
17
Dijkstra's two-stack algorithm
2+3
* ( 4 * 5 ) ) )
1
value stack operator stack
+
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
18
Dijkstra's two-stack algorithm
* ( 4 * 5 ) ) )
1
value stack operator stack
+
2+3 = 5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
19
Dijkstra's two-stack algorithm
* ( 4 * 5 ) ) )
1
value stack operator stack
+
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
20
Dijkstra's two-stack algorithm
( 4 * 5 ) ) )
1
value stack operator stack
+
5
*
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
21
Dijkstra's two-stack algorithm
( 4 * 5 ) ) )
1
value stack operator stack
+
5 *
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
22
Dijkstra's two-stack algorithm
*
( 4 * 5 ) ) )
1
value stack operator stack
+
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
23
Dijkstra's two-stack algorithm
*
* 5 ) ) )
1
value stack operator stack
+
5
4
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
24
Dijkstra's two-stack algorithm
*
* 5 ) ) )
1
value stack operator stack
+
5
4
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
25
Dijkstra's two-stack algorithm
*
4
* 5 ) ) )
1
value stack operator stack
+
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
26
Dijkstra's two-stack algorithm
*
4 *
5 ) ) )
1
value stack operator stack
+
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
27
Dijkstra's two-stack algorithm
*
4 *
) ) )
1
value stack operator stack
+
5
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
28
Dijkstra's two-stack algorithm
*
4 *
) ) )
1
value stack operator stack
+
5
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
29
Dijkstra's two-stack algorithm
*
4 *
5
) )
1
value stack operator stack
+
5
)
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
30
Dijkstra's two-stack algorithm
*
4*5
) )
1
value stack operator stack
+
5
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
31
Dijkstra's two-stack algorithm
*
4*5
) )
1
value stack operator stack
+
5
= 20
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
32
Dijkstra's two-stack algorithm
*
) )
1
value stack operator stack
+
5
20
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
33
Dijkstra's two-stack algorithm
*
)
1
value stack operator stack
+
5
20
)
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
34
Dijkstra's two-stack algorithm
*
)
1
value stack operator stack
+
520
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
35
Dijkstra's two-stack algorithm
*
)
1
value stack operator stack
+
520 = 100
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
36
Dijkstra's two-stack algorithm
)
1
value stack operator stack
+
100
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
37
Dijkstra's two-stack algorithm
)
1
value stack operator stack
+
100
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
38
Dijkstra's two-stack algorithm
1
value stack operator stack
+100
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
39
Dijkstra's two-stack algorithm
1
value stack operator stack
+100 = 101
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
40
Dijkstra's two-stack algorithm
value stack operator stack
101
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
41
Dijkstra's two-stack algorithm
value stack operator stack
101
Value: push onto the value stack.Operator: push onto the operator stack.Left parenthesis: ignore.Right parenthesis: pop operator and two values; push the result ofapplying that operator to those values onto the operand stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
42
Dijkstra's two-stack algorithm
result
101