You are on page 1of 39

Dijkstra’s Algorithm

procedure Dijkstra(G: weighted connected simple


graph with vertices a = v0, v1, …, vn = z and
positive weights w(vi, vj), where w(vi, vj) = 
if {vi, vj} is not an edge in G)
for i := 1 to n
L(vi) := 
L(a) := 0
S := 
{the labels are now initialized so that the label of
a is zero and all other labels are , and the
distinguished set of vertices S is empty}
May 5, 2015 Applied Discrete Mathematics 1
Week 13: Boolean Algebra
Dijkstra’s Algorithm
while zS
begin
u := the vertex not in S with minimal L(u)
S := S{u}
for all vertices v not in S
if L(u) + w(u, v) < L(v) then L(v) := L(u) + w(u,
v)
{this adds a vertex to S with minimal label and
updates the labels of vertices not in S}
end {L(z) = length of shortest path from a to z}

May 5, 2015 Applied Discrete Mathematics 2


Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b  d 
5
4 6

a 1 8 2 z
0 3 
2
10
c  e 
Step 0
May 5, 2015 Applied Discrete Mathematics 3
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 4
 (a) d 
5
4 6

a 1 8 2 z
0 3 
2
10
c 2
 (a) e 
Step 1
May 5, 2015 Applied Discrete Mathematics 4
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 3
 (a,
4 (a) c) d 10 (a, c)
5
4 6

a 1 8 2 z
0 3 
2
10
c 2
 (a) e 12
 (a, c)
Step 2
May 5, 2015 Applied Discrete Mathematics 5
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 3
 (a,
4 (a) c) d 8
10(a,
(a,c,c)b)
5
4 6

a 1 8 2 z
0 3 
2
10
c 2
 (a) e 12
 (a, c)
Step 3
May 5, 2015 Applied Discrete Mathematics 6
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 3
 (a,
4 (a) c) d 8
10(a,
(a,c,c)b)
5
4 6

a 1 8 2 z
0 3 14 
(a, c, b, d)
2
10
c 2
 (a) e 12
10 (a, c)
 c, b, d)
Step 4
May 5, 2015 Applied Discrete Mathematics 7
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 3
 (a,
4 (a) c) d 8
10(a,
(a,c,c)b)
5
4 6

a 1 8 2 z
0 3 14 
(a,c,c,b,
b,d,
d)e)
13 (a,
2
10
c 2
 (a) e 12
10 (a, c)
 c, b, d)
Step 5
May 5, 2015 Applied Discrete Mathematics 8
Week 13: Boolean Algebra
Dijkstra’s Algorithm
Example:
b 3
 (a,
4 (a) c) d 8
10(a,
(a,c,c)b)
5
4 6

a 1 8 2 z
0 3 14 
(a,c,c,b,
b,d,
d)e)
13 (a,
2
10
c 2
 (a) e 12
10 (a, c)
 c, b, d)
Step 6
May 5, 2015 Applied Discrete Mathematics 9
Week 13: Boolean Algebra
The Traveling Salesman Problem
The traveling salesman problem is one of the
classical problems in computer science.
A traveling salesman wants to visit a number of cities
and then return to his starting point. Of course he
wants to save time and energy, so he wants to
determine the shortest path for his trip.
We can represent the cities and the distances
between them by a weighted, complete, undirected
graph.
The problem then is to find the circuit of minimum
total weight that visits each vertex exactly once.
May 5, 2015 Applied Discrete Mathematics 10
Week 13: Boolean Algebra
The Traveling Salesman Problem
Example: What path would the traveling salesman
take to visit the following cities?
Toronto
650 550
700
700 Boston
Chicago
200
600

New York
Solution: The shortest path is Boston, New York,
Chicago, Toronto, Boston (2,000 miles).
May 5, 2015 Applied Discrete Mathematics 11
Week 13: Boolean Algebra
Let us talk about…

Trees
May 5, 2015 Applied Discrete Mathematics 12
Week 13: Boolean Algebra
Trees

Definition: A tree is a connected undirected graph


with no simple circuits.
Since a tree cannot have a simple circuit, a tree
cannot contain multiple edges or loops.
Therefore, any tree must be a simple graph.

Theorem: An undirected graph is a tree if and only if


there is a unique simple path between any of its
vertices.

May 5, 2015 Applied Discrete Mathematics 13


Week 13: Boolean Algebra
Trees
Example: Are the following graphs trees?

No. Yes.

Yes. No.
May 5, 2015 Applied Discrete Mathematics 14
Week 13: Boolean Algebra
Trees
Definition: An undirected graph that does not contain
simple circuits and is not necessarily connected is
called a forest.
In general, we use trees to represent hierarchical
structures.
We often designate a particular vertex of a tree as the
root. Since there is a unique path from the root to
each vertex of the graph, we direct each edge away
from the root.
Thus, a tree together with its root produces a directed
graph called a rooted tree.
May 5, 2015 Applied Discrete Mathematics 15
Week 13: Boolean Algebra
Tree Terminology
If v is a vertex in a rooted tree other than the root, the
parent of v is the unique vertex u such that there is a
directed edge from u to v.
When u is the parent of v, v is called the child of u.
Vertices with the same parent are called siblings.
The ancestors of a vertex other than the root are the
vertices in the path from the root to this vertex,
excluding the vertex itself and including the root.

May 5, 2015 Applied Discrete Mathematics 16


Week 13: Boolean Algebra
Tree Terminology
The descendants of a vertex v are those vertices that
have v as an ancestor.
A vertex of a tree is called a leaf if it has no children.
Vertices that have children are called internal
vertices.
If a is a vertex in a tree, then the subtree with a as its
root is the subgraph of the tree consisting of a and its
descendants and all edges incident to these
descendants.

May 5, 2015 Applied Discrete Mathematics 17


Week 13: Boolean Algebra
Tree Terminology

The level of a vertex v in a rooted tree is the length of


the unique path from the root to this vertex.

The level of the root is defined to be zero.

The height of a rooted tree is the maximum of the


levels of vertices.

May 5, 2015 Applied Discrete Mathematics 18


Week 13: Boolean Algebra
Trees
Example I: Family tree

James

Christine Bob

Frank Joyce Petra

May 5, 2015 Applied Discrete Mathematics 19


Week 13: Boolean Algebra
Trees
Example II: File system

usr bin temp

bin spool ls

May 5, 2015 Applied Discrete Mathematics 20


Week 13: Boolean Algebra
Trees
Example III: Arithmetic expressions

+ -

y z x y

This tree represents the expression (y + z)(x - y).


May 5, 2015 Applied Discrete Mathematics 21
Week 13: Boolean Algebra
Trees
Definition: A rooted tree is called an m-ary tree if
every internal vertex has no more than m children.
The tree is called a full m-ary tree if every internal
vertex has exactly m children.
An m-ary tree with m = 2 is called a binary tree.
Theorem: A tree with n vertices has (n – 1) edges.
Theorem: A full m-ary tree with i internal vertices
contains n = mi + 1 vertices.

May 5, 2015 Applied Discrete Mathematics 22


Week 13: Boolean Algebra
Binary Search Trees
If we want to perform a large number of searches in a
particular list of items, it can be worthwhile to arrange
these items in a binary search tree to facilitate the
subsequent searches.
A binary search tree is a binary tree in which each
child of a vertex is designated as a right or left child,
and each vertex is labeled with a key, which is one of
the items.
When we construct the tree, vertices are assigned
keys so that the key of a vertex is both larger than the
keys of all vertices in its left subtree and smaller than
the keys of all vertices in its right subtree.

May 5, 2015 Applied Discrete Mathematics 23


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

May 5, 2015 Applied Discrete Mathematics 24


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer

May 5, 2015 Applied Discrete Mathematics 25


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer power

May 5, 2015 Applied Discrete Mathematics 26


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer power

north

May 5, 2015 Applied Discrete Mathematics 27


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer power

north zoo

May 5, 2015 Applied Discrete Mathematics 28


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer power

dentist north zoo

May 5, 2015 Applied Discrete Mathematics 29


Week 13: Boolean Algebra
Binary Search Trees
Example: Construct a binary search tree for the
strings math, computer, power, north, zoo, dentist,
book.
math

computer power

book dentist north zoo

May 5, 2015 Applied Discrete Mathematics 30


Week 13: Boolean Algebra
Binary Search Trees
To perform a search in such a tree for an item x, we
can start at the root and compare its key to x. If x is
less than the key, we proceed to the left child of the
current vertex, and if x is greater than the key, we
proceed to the right one.
This procedure is repeated until we either found the
item we were looking for, or we cannot proceed any
further.
In a balanced tree representing a list of n items,
search can be performed with a maximum of
log(n + 1) steps (compare with binary search).
May 5, 2015 Applied Discrete Mathematics 31
Week 13: Boolean Algebra
Spanning Trees

Definition: Let G be a simple graph. A spanning tree


of G is a subgraph of G that is a tree containing every
vertex of G.
Note: A spanning tree of G = (V, E) is a connected
graph on V with a minimum number of edges
(|V| - 1).
Example: Since winters in Boston can be very cold,
six universities in the Boston area decide to build a
tunnel system that connects their libraries.

May 7, 2015 Applied Discrete Mathematics 32


Week 13: Boolean Algebra
Spanning Trees
The complete graph including all possible tunnels:
Brandeis Harvard

MIT

BU Tufts

UMass
The spanning trees of this graph connect all libraries
with a minimum number of tunnels.
May 7, 2015 Applied Discrete Mathematics 33
Week 13: Boolean Algebra
Spanning Trees
Example for a spanning tree:
Brandeis Harvard

MIT

BU Tufts

UMass
Since there are 6 libraries, 5 tunnels are sufficient to
connect all of them.
May 7, 2015 Applied Discrete Mathematics 34
Week 13: Boolean Algebra
Spanning Trees

Now imagine that you are in charge of the tunnel


project. How can you determine a tunnel system of
minimal cost that connects all libraries?
Definition: A minimum spanning tree in a
connected weighted graph is a spanning tree that has
the smallest possible sum of weights of its edges.
How can we find a minimum spanning tree?

May 7, 2015 Applied Discrete Mathematics 35


Week 13: Boolean Algebra
Spanning Trees
The complete graph with cost labels (in billion $):
Brandeis 7 Harvard
8 4
2 MIT
9 5
3
9 6 3
4 Tufts
BU 4 6
5 4

UMass
The least expensive tunnel system costs $20 billion.
May 7, 2015 Applied Discrete Mathematics 36
Week 13: Boolean Algebra
Spanning Trees

Prim’s Algorithm:
• Begin by choosing any edge with smallest weight
and putting it into the spanning tree,
• successively add to the tree edges of minimum
weight that are incident to a vertex already in
the tree and not forming a simple circuit with
those edges already in the tree,
• stop when (n – 1) edges have been added.

May 7, 2015 Applied Discrete Mathematics 37


Week 13: Boolean Algebra
Spanning Trees

Kruskal’s Algorithm:
Kruskal’s algorithm is identical to Prim’s algorithm,
except that it does not demand new edges to be
incident to a vertex already in the tree.
Both algorithms are guaranteed to produce a
minimum spanning tree of a connected weighted
graph.

May 7, 2015 Applied Discrete Mathematics 38


Week 13: Boolean Algebra
The
End
May 7, 2015 Applied Discrete Mathematics 39
Week 13: Boolean Algebra

You might also like