You are on page 1of 35

LECTURE 29 & 30

Infix and Postfix notation

Shortest Path Problems

Minimum Spanning Trees


Infix and Postfix notation
Given the following prefix expressions:

(a)  /   x 6 4 z 3 x 9
(b)    d c 8 / d 4 7

What is the postfix expression of each?


Solution:

 /  x 6 4  z  3 x 9

 /   x 6 4  z (3  x) 9

 /   x 6 4 ( z  (3  x)) 9
x 6  4  z 3 x  / 9 
 /  ( x  6) 4 ( z  (3  x)) 9

 / (( x  6)  4) ( z  (3  x)) 9

 (( x  6)  4) / ( z  (3  x)) 9

((( x  6)  4) / ( z  (3  x)))  9
Solution:

   d c 8 / d 4 7

((d  c)  8)  ((d  4) / 7)

d c  8  d 4  7 /
Given the following postfix expressions:

(a) r s 3 r 64 * 
(b) y 5  4  x y 3 */ 9 

What is the prefix expression of each?


Solution:

r s  3  r 6  4*
(r  s ) 3  r 6  4 * 

((r  s)  3) r 6  4 * 
   r s 3 * r 6 4
((r  s )  3) (r  6) 4 * 

((r  s)  3) ((r  6) * 4) 

((r  s)  3)  ((r  6) * 4)
Solution:

y 5  4  x y 3  */ 9 

((( y  5)  4) / ( x * ( y  3)))  9

 /   y 5 4* x  y 3 9
Shortest Path Problems
• Many problems can be modeled using graphs
with weights assigned to their edges.

• Consider how an airline system can be


modeled. We can use a basic graph model by
representing edges for mileage or fares or time
and vertices for cites.

• A trucking company or a train company could


model their systems similarly.
Shortest Path Problems

Los Angeles
MILEAGE 2451
789
1855
Atlanta 760
833 346
Denver Miami
182
908 1090
Boston 860 834
191
New York
722
Chicago
Shortest path by observation:
observation What is the length of
the shortest path between a and z?

4 2
3

2 1

Ans: a, d, e, z
Dijkstra’s algorithm
• There are several algorithms that find the
shortest path between two vertices.

• However we would look at Dijkstra’s


algorithm only.

• We will not go into the actual algorithm,


but we will use its techniques to arrive at
the answer (shortest path).
Shortest Path Problems

• Dijkstra’s algorithm calculates the shortest


(lease) distance from a starting vertex a to
a destination vertex z from a weighted
graph
What is the shortest path between a and z?
{a, c, b, d, e, z} = 13
Example: What is the shortest path from
a to z?
• a: (0, empty)
• c: (a, 3)
• b: (a, 4)
• d: (c, 3)
• e: (d, 1)
• f: (d, 5)
• g: (e, 5)
• z: (g, 4)
• Work backwards z, g, e, d, c, a
• Ans: {a, c, d, e, g, z } = 16
Example: What is the shortest path from F to H?
Solution
• F: (0, empty)
• E: (F, 1)
• D: (E, 1)
• C: (D, 1)
• G: (D, 1)
• B: (C, 2)
• A: (B, 1)
• H: (B, 2)
• Work backwards H, B, C, D, E, F
• Ans: {F, E, D, C, B, H} = 7
A: (0, EMPTY)
B: (A, 3)
D: (A, 5)
C: (D, 7)
F: (D, 8)
G: (F, 1)
E: (B, 11)
I : (E, 3) ANS { A, D, F, G, J, K, M } = 26
H: (E, 4)
J: (G, 4)
L: (H, 6)
K: (J, 7)
M: (K, 1)
Find the Shortest Path from A to H.

a: (0, empty)
b: (a,3)
f: (b, 1) {a, b, f, d, g, h} = 8
c: (b, 1)
d: (f, 1)
e: (f, 2)
g: (d, 2)
h: (g, 1)
• Spanning Trees
• Definition: Let G be a simple graph. A
spanning tree of G is a subgraph of G that has a
tree containing every vertex of G.

• Example The graph below shows the network of


roads which connects 6 towns. Hurricane debris
has covered the roads. What is the minimum set
of roads that should be cleared to keep the towns
connected?
Find a spanning tree of the simple graph.
$1000 New York
Chicargo
$1200
$2000
San Francisco
$1300 $800
$1600
$900
$700
$2200
Denver

$1400 Atlanta
• Minimum Spanning Trees
• Definition
• A minimum spanning tree in a connected
weighted graph is a spanning tree that has the
smallest possible sum of the weights of the
edges in G.
• We will examine Prim’s Algorithm and
Kruskal’s Algorithm for finding the
minimum spanning tree.
• Prim’s Algorithm

1. Choose an edge with the smallest weight


putting it into the spanning tree.

2. Add to the tree edges of minimum weight that


are incident on a vertex already in the tree and
not forming a simple circuit.

3. Repeat step 2 until n – 1 edges have been


added to the spanning tree.
$1000 New York
Chicargo
$1200
$2000
San Francisco
$1300 $800
$1600
$900
$700
$2200
Denver

$1400 Atlanta
Solution
• Choice Edge Cost
• 1 {Chicago, Atlanta} $700
• 2 {Atlanta, New York} $800
• 3 {Chicago, San Francisco} $1200
• 4 {San Francisco, Denver} $ 900
Total: $3600

You might also like