You are on page 1of 55

Week 5 (30/3/15 -3/4/15)

Trees and Distance(cont.)


Matrix Tree Computation
Applications of Minimum Spanning Trees, Shortest Paths, Dijkstra’s
Algorithm, and Chinese Postman Problem.
Rooted tree, Binary tree and Huffman’s Algorithm
Lab Assignment 1
Tutorial
Lab assignment
Matrix Computation
Matrix Computation
Application of minimum spanning tree
Dijkstra’s Shortest Path

The distance between two vertices is the length of a shortest walk(path) between them

Notation:
dist(x): distance from vertex s to x,
w(e) : edge-weight
P(e): P-value, (for each frontier edge e)

P( e )  dist ( x )  w( e )

Example : pg 149> Jonathan Gross


What route can I
take to avoid
going along the
Nuffield Free-Standing same street
twice?
Mathematics Activity
Chinese
postman
problems
College Open Day What route can I
take to avoid
going along the
Chris is delivering leaflets to houses same street
twice?
near the college.
The leaflets tell residents about the
college open day and apologises for
any inconvenience caused.
Chris wants to deliver the leaflets as
efficiently as possible.

This is an example of a
Chinese postman problem.

In this activity you will learn how to solve such problems.


The map shows
streets near the A

college.

College B
(C)
D
Think about
Is it possible to deliver
leaflets to the houses in
E
these streets without
travelling along the same
street twice?
F
G
The college map is an example of a graph.
Here are some others.

A graph is made up of a
collection of vertices
called nodes, joined by
edges called arcs.

A traversable graph is
one that can be drawn
without taking a pen
from the paper and
without retracing the
same edge Think about
Which of the graphs above can be traced without taking your pen from the paper?
A Swiss mathematician, Leonhard Euler (1707 to 1783),
published the first paper on graph theory in 1736.

The paper was based on


the Seven Bridges of
Konigsberg problem.

Residents of Konigsberg
wanted to know if it was
possible to find a route
which passed across each
of the bridges only once.
Euler found that the order of the vertices determines
whether or not a graph is traversable.

If it is possible to traverse a graph starting and


finishing at the same point then the graph has
an Eulerian trail.

If it is possible to traverse a graph starting at one


point and finishing at a different point then the graph
has a semi-Eulerian trail.
Odd vertices Even vertices

order 1
order 2
order 3
order 4

order 6
order 5
Vertices:
A
A order 4
B order 4
C order 5
College B
D order 2 D
E order 4
F order 4
E
G order 3
The only odd
vertices are C F
G
and G
An Eulerian trail is only possible if all vertices are even.
Think about Can you explain why?
A semi-Eulerian trail
is possible:
130m
This started at C but A
100m
ended at G. 80m

The postman needs to


60m 72m
return from G to C by B
the shortest route. College
D
The shortest route is 110m 80m 175m
GE + EC.
120m
E 82m
The total distance 100m
travelled to deliver the 200m
leaflets
190m
F
= 1691 m G

That is approximately 1.7 km


The Chinese postman algorithm
Step 1 Find all the odd vertices in the network.

Step 2 Consider all the routes joining pairs of odd vertices.


Choose the routes with the shortest total distance.

Step 3 Add in these edges again.


This will give a network with only even vertices.

Step 4 Find an Eulerian trail.


Easter Parade
G H
10 min
A order 3 11 min 6 min
B order 4 F
8 min
9 min
C order 3
E 4 min
D order 2 9 min
7 min 8 min D
E order 5 C
5 min
F order 4 9 min 5 min
6 min B
G order 3 A

H order 2
Odd vertices: A, C, E, G
Possible pairings: AC + EG = 9 + 8 = 17 mins
AG + EC = 13
+ 7 = 20 mins
AE + CG = 5
+ 11 = 16 mins
Easter Parade G
10 min H
The smallest 11 min 6 min
total is:
8 min F
AE + CG = 16 min 9 min
Add these edges to E 4 min
the network 9 min
7 min 8 min
C D
The total time in 5 min
the original
network = 97 min 9 min 5 min
6 min B
A

Shortest time = 97 + 16 = 113 minutes = 1 hour 53 minutes


Possible Eulerian trail: ABDFBEAEFHGECGCA
Chinese postman problem
Reflect on your work
An Eulerian trail is a path which starts and ends at the same vertex
and includes every edge just once. Euler discovered that such a trail
will exist if and only if all vertices are even.
Can you explain why?

In any network, the sum of the orders of the vertices is even.


Can you explain why?

In any network, the number of odd vertices must be even.


Can you explain why?
Rooted Trees
Definition: A rooted tree is a tree in which one vertex is
distinguished from the others and is called the root.
r
a b

c d
The level of a vertex is the number of edges along the unique
path between it and the root.
The height of a rooted tree is the maximum level to any vertex of
the tree.
The children of a vertex v are those vertices that are adjacent to
v and one level farther away from the root than v.
When every vertex in a rooted tree has at most two children, the
tree is called a binary tree.
Binary trees and traversals 6

• Binary Tree: a rooted tree in which each node has


at most two children and each child is designated
left child or right child.
• Thus in a binary tree, each node may have 0,1, or 2
children
• Left child—left and below parent
• Right child—right and below parent
• The left subtree of a node N in a binary tree is the
graph formed by the left child L, the descendants
of L, and the edges connecting these nodes
• Right subtree—defined similarly
Binary trees and traversals 6

• A is the root, A has two children, left child B and


right child C
• Node B has one child, left child D
• Node C has right child E but no left child.
Expression trees
• 4.6.
• Polish notation—Polish mathematician Lukasiewicz
—no parens needed
• RT4
Traversal 4

• Traversal: visit each node of a graph exactly once


• BFS/DFS—traversal of a connected graph—nodes are “visited” , i.e.,
labeled, exactly once
• A preorder traversal of a binary tree is characterized by visiting the
parent before the children and the left child before the right child
• Listing the nodes in the order they are visited is called a preorder
listing
Preorder Traversal (i.e., DFS w/ choosing left
before right) 4

• STEP 1: (Visit) Visit the root


• STEP 2:(go left) Go to the left subtree, if one exists, and do a preorder
traversal
• STEP 3: Go to the right subtree, if one exists, and do a preorder
traversal.
• 4.6.4,4.6.5
Postorder Traversal 6

• RPN—Reverse Polish Notation


• Operation sign is followed by the operands (HP
calculator)
• (2-3*4)+(4+8/2)=-2
• Pre: +-2*34+4/82 Preorder—
look for operation sign followed by two numbers
• Post: 234*-482/++
• Postorder—look for two condecutive numbers followed by
an operation
• By using a traversal called postorder, can obtain the
RPN for an expression
Postorder Traversal (child before
parent, left before right) 4

• STEP 1: (Start) Go to the root


• STEP 2:(go left) Go to the left subtree, if one exists, and do a
postorder traversal
• STEP 3: (go right) Go to the right subtree, if one exists, and do a
postorder traversal.
• Step 4: (Visit) Visit the root
• 4.6.7, 4.6.8
Binary Search Tree
• (to determine if an element a is in a binary search tree)
• Step 1(Start) Let V be the root of the binary search tree.
• Step 2 (compare) If a = V, then A is in the tree; STOP. Else, go to Step 3
• Step 3 (if smaller, go left) If V<=a, go to Step 4. Otherwise, a<=V
• If no left child of V, a is not in tree. STOP
• Else, V has left child L; let V=L and go to Step 2
• Step 4 (if larger, go right)
Huffman’s Algorithm

You might also like