may 1, 2002applied discrete mathematics week 13: graphs and trees 1news csems scholarships for cs...
TRANSCRIPT
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
1
NewsNews
CSEMS Scholarships for CS and Math students CSEMS Scholarships for CS and Math students (US citizens only)(US citizens only)
$3,125 per year$3,125 per year
Look at:Look at:
http://www.cs.umb.edu/Resources/scholarships.htmlhttp://www.cs.umb.edu/Resources/scholarships.html
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
2
Shortest Path ProblemsShortest Path ProblemsWe can assign weights to the edges of graphs, We can assign weights to the edges of graphs, for example to represent the distance between for example to represent the distance between cities in a railway network:cities in a railway network:
ChicagChicagoo
TorontTorontoo
New YorkNew York
BostonBoston
600600
700700
200200
650650
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
3
Shortest Path ProblemsShortest Path ProblemsSuch weighted graphs can also be used to Such weighted graphs can also be used to model computer networks with response times model computer networks with response times or costs as weights.or costs as weights.
One of the most interesting questions that we One of the most interesting questions that we can investigate with such graphs is:can investigate with such graphs is:
What is the What is the shortest pathshortest path between two between two vertices in the graph, that is, the path with the vertices in the graph, that is, the path with the minimal sum of weightsminimal sum of weights along the way? along the way?
This corresponds to the shortest train This corresponds to the shortest train connection or the fastest connection in a connection or the fastest connection in a computer network.computer network.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
4
Dijkstra’s AlgorithmDijkstra’s Algorithm
Dijkstra’s algorithm is an iterative procedure Dijkstra’s algorithm is an iterative procedure that finds the shortest path between to that finds the shortest path between to vertices a and z in a weighted graph.vertices a and z in a weighted graph.
It proceeds by finding the length of the It proceeds by finding the length of the shortest path from a to successive vertices and shortest path from a to successive vertices and adding these vertices to a distinguished set of adding these vertices to a distinguished set of vertices S. vertices S.
The algorithm terminates once it reaches the The algorithm terminates once it reaches the vertex z.vertex z.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
5
Dijkstra’s AlgorithmDijkstra’s Algorithmprocedureprocedure Dijkstra(G: weighted connected simple Dijkstra(G: weighted connected simple
graph with vertices a = vgraph with vertices a = v00, v, v11, …, v, …, vnn = z and = z and positive weights w(vpositive weights w(vii, v, vjj), where w(v), where w(vii, v, vjj) = ) = if {vif {vii, v, vjj} is not an edge in G)} is not an edge in G)
forfor i := 1 i := 1 toto n nL(vL(vii) := ) :=
L(a) := 0L(a) := 0S := S :=
{the labels are now initialized so that the label of {the labels are now initialized so that the label of a is zero and all other labels are a is zero and all other labels are , and the , and the distinguished set of vertices S is empty}distinguished set of vertices S is empty}
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
6
Dijkstra’s AlgorithmDijkstra’s Algorithm
whilewhile z zSSbegin begin
u := the vertex not in S with minimal L(u)u := the vertex not in S with minimal L(u)S := SS := S{u}{u}forfor all vertices v not in S all vertices v not in S
ifif L(u) + w(u, v) < L(v) L(u) + w(u, v) < L(v) thenthen L(v) := L(u) L(v) := L(u) + w(u, v)+ w(u, v)
{this adds a vertex to S with minimal label {this adds a vertex to S with minimal label and updates the labels of vertices not in S}and updates the labels of vertices not in S}
endend {L(z) = length of shortest path from a to z}{L(z) = length of shortest path from a to z}
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
7
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 0Step 0
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
8
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 1Step 1
4 (a)4 (a)
2 (a)2 (a)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
9
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 2Step 2
4 (a)4 (a)
2 (a)2 (a)
3 (a, c)3 (a, c) 10 (a, 10 (a, c)c)
12 (a, 12 (a, c)c)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
10
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 3Step 3
4 (a)4 (a)
2 (a)2 (a)
3 (a, c)3 (a, c) 10 (a, 10 (a, c)c)
12 (a, 12 (a, c)c)
8 (a, c, b)8 (a, c, b)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
11
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 4Step 4
4 (a)4 (a)
2 (a)2 (a)
3 (a, c)3 (a, c) 10 (a, 10 (a, c)c)
12 (a, 12 (a, c)c)
8 (a, c, b)8 (a, c, b)
10 (a, c, b, d)10 (a, c, b, d)
14 (a, c, b, 14 (a, c, b, d)d)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
12
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 5Step 5
4 (a)4 (a)
2 (a)2 (a)
3 (a, c)3 (a, c) 10 (a, 10 (a, c)c)
12 (a, 12 (a, c)c)
8 (a, c, b)8 (a, c, b)
10 (a, c, b, d)10 (a, c, b, d)
14 (a, c, b, 14 (a, c, b, d)d)
13 (a, c, b, d, 13 (a, c, b, d, e)e)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
13
Dijkstra’s AlgorithmDijkstra’s Algorithm
Example:Example:
aa
bb dd
zz
eecc
44
22
11
55
88
1100
22
66
3300
Step 6Step 6
4 (a)4 (a)
2 (a)2 (a)
3 (a, c)3 (a, c) 10 (a, 10 (a, c)c)
12 (a, 12 (a, c)c)
8 (a, c, b)8 (a, c, b)
10 (a, c, b, d)10 (a, c, b, d)
14 (a, c, b, 14 (a, c, b, d)d)
13 (a, c, b, d, 13 (a, c, b, d, e)e)
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
14
Dijkstra’s AlgorithmDijkstra’s Algorithm
Theorem:Theorem: Dijkstra’s algorithm finds the length Dijkstra’s algorithm finds the length of a shortest path between two vertices in a of a shortest path between two vertices in a connected simple undirected weighted graph. connected simple undirected weighted graph.
Theorem:Theorem: Dijkstra’s algorithm uses Dijkstra’s algorithm uses O(nO(n22)) operations (additions and comparisons) to find operations (additions and comparisons) to find the length of the shortest path between two the length of the shortest path between two vertices in a connected simple undirected vertices in a connected simple undirected weighted graph.weighted graph.
Please take a look at pages 492 to 496 in the Please take a look at pages 492 to 496 in the textbook for a comprehensive description and textbook for a comprehensive description and analysis of Dijkstra’s algorithm.analysis of Dijkstra’s algorithm.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
15
The Traveling Salesman ProblemThe Traveling Salesman ProblemThe The traveling salesman problemtraveling salesman problem is one of the is one of the classical problems in computer science.classical problems in computer science.
A traveling salesman wants to visit a number of A traveling salesman wants to visit a number of cities and then return to his starting point. Of cities and then return to his starting point. Of course he wants to save time and energy, so he course he wants to save time and energy, so he wants to determine the wants to determine the shortest pathshortest path for his trip. for his trip.
We can represent the cities and the distances We can represent the cities and the distances between them by a weighted, complete, between them by a weighted, complete, undirected graph.undirected graph.
The problem then is to find the The problem then is to find the circuit of circuit of minimum total weight that visits each vertex minimum total weight that visits each vertex exactly oneexactly one..
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
16
The Traveling Salesman ProblemThe Traveling Salesman ProblemExample:Example: What path would the traveling What path would the traveling salesman take to visit the following cities?salesman take to visit the following cities?
ChicagChicagoo
TorontTorontoo
New YorkNew York
BostonBoston
600600
700700
200200
650650 550550700700
Solution:Solution: The shortest path is Boston, New The shortest path is Boston, New York, Chicago, Toronto, Boston (2,000 miles).York, Chicago, Toronto, Boston (2,000 miles).
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
17
The Traveling Salesman ProblemThe Traveling Salesman ProblemQuestion:Question: Given n vertices, how many different Given n vertices, how many different cycles Ccycles Cnn can we form by connecting these can we form by connecting these vertices with edges?vertices with edges?
Solution:Solution: We first choose a starting point. Then We first choose a starting point. Then we have (n – 1) choices for the second vertex in we have (n – 1) choices for the second vertex in the cycle, (n – 2) for the third one, and so on, so the cycle, (n – 2) for the third one, and so on, so there are (n – 1)! choices for the whole cycle.there are (n – 1)! choices for the whole cycle.
However, this number includes identical cycles However, this number includes identical cycles that were constructed in that were constructed in opposite directionsopposite directions. . Therefore, the actual number of different cycles Therefore, the actual number of different cycles CCnn is is (n – 1)!/2(n – 1)!/2..
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
18
The Traveling Salesman ProblemThe Traveling Salesman Problem
Unfortunately, no algorithm solving the Unfortunately, no algorithm solving the traveling salesman problem with polynomial traveling salesman problem with polynomial worst-case time complexity has been devised worst-case time complexity has been devised yet.yet.
This means that for large numbers of vertices, This means that for large numbers of vertices, solving the traveling salesman problem is solving the traveling salesman problem is impractical.impractical.
In these cases, we can use efficient In these cases, we can use efficient approximation algorithmsapproximation algorithms that determine a that determine a path whose length may be slightly larger than path whose length may be slightly larger than the traveling salesman’s path, but the traveling salesman’s path, but
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
19
Let us talk about…Let us talk about…
TreesTrees
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
20
TreesTrees
Definition:Definition: A A treetree is a connected undirected is a connected undirected graph with no simple circuits.graph with no simple circuits.
Since a tree cannot have a simple circuit, a tree Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops.cannot contain multiple edges or loops.
Therefore, any tree must be a Therefore, any tree must be a simple graphsimple graph..
Theorem:Theorem: An undirected graph is a tree if and An undirected graph is a tree if and only if there is a only if there is a unique simple pathunique simple path between between any of its vertices.any of its vertices.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
21
TreesTrees
Example:Example: Are the following graphs trees? Are the following graphs trees?
No.No.
Yes.Yes.
Yes.Yes.
No.No.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
22
TreesTreesDefinition:Definition: An undirected graph that does not An undirected graph that does not contain simple circuits and is not necessarily contain simple circuits and is not necessarily connected is called a connected is called a forestforest..
In general, we use trees to represent In general, we use trees to represent hierarchical structureshierarchical structures..
We often designate a particular vertex of a tree We often designate a particular vertex of a tree as the as the rootroot. Since there is a unique path from . Since there is a unique path from the root to each vertex of the graph, we direct the root to each vertex of the graph, we direct each edge away from the root.each edge away from the root.
Thus, a tree together with its root produces a Thus, a tree together with its root produces a directed graphdirected graph called a called a rooted treerooted tree..
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
23
Tree TerminologyTree Terminology
If v is a vertex in a rooted tree other than the If v is a vertex in a rooted tree other than the root, the root, the parentparent of v is the unique vertex u of v is the unique vertex u such that there is a directed edge from u to v.such that there is a directed edge from u to v.
When u is the parent of v, v is called the When u is the parent of v, v is called the childchild of u.of u.
Vertices with the same parent are called Vertices with the same parent are called siblingssiblings..
The The ancestorsancestors of a vertex other than the root of a vertex other than the root are the vertices in the path from the root to this are the vertices in the path from the root to this vertex, excluding the vertex itself and including vertex, excluding the vertex itself and including the root.the root.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
24
Tree TerminologyTree Terminology
The The descendantsdescendants of a vertex v are those of a vertex v are those vertices that have v as an ancestor.vertices that have v as an ancestor.
A vertex of a tree is called a A vertex of a tree is called a leafleaf if it has no if it has no children.children.
Vertices that have children are called Vertices that have children are called internal internal verticesvertices..
If a is a vertex in a tree, then the If a is a vertex in a tree, then the subtreesubtree with with a as its root is the subgraph of the tree a as its root is the subgraph of the tree consisting of a and its descendants and all consisting of a and its descendants and all edges incident to these descendants.edges incident to these descendants.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
25
Tree TerminologyTree Terminology
The The levellevel of a vertex v in a rooted tree is the of a vertex v in a rooted tree is the length of the unique path from the root to this length of the unique path from the root to this vertex.vertex.
The level of the root is defined to be zero.The level of the root is defined to be zero.
The The heightheight of a rooted tree is the maximum of of a rooted tree is the maximum of the levels of vertices.the levels of vertices.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
26
TreesTrees
Example I:Example I: Family tree Family tree
JamesJames
ChristineChristine BobBob
FrankFrank JoyceJoyce PetraPetra
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
27
TreesTrees
Example II:Example II: File system File system
//
usrusr temptemp
binbin spoolspool lsls
binbin
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
28
TreesTrees
Example III:Example III: Arithmetic expressions Arithmetic expressions
++ --
yy zz xx yy
This tree represents the expression (y + z)This tree represents the expression (y + z)(x - y).(x - y).
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
29
TreesTreesDefinition:Definition: A rooted tree is called an A rooted tree is called an m-ary treem-ary tree if every internal vertex has no more than m if every internal vertex has no more than m children. children.
The tree is called a The tree is called a full m-ary treefull m-ary tree if every if every internal vertex has exactly m children.internal vertex has exactly m children.
An m-ary tree with m = 2 is called a An m-ary tree with m = 2 is called a binary treebinary tree..
Theorem:Theorem: A tree with n vertices has (n – 1) A tree with n vertices has (n – 1) edges.edges.
Theorem:Theorem: A full m-ary tree with i internal A full m-ary tree with i internal vertices contains n = mi + 1 vertices.vertices contains n = mi + 1 vertices.
Please look at page 536 for proofs and further Please look at page 536 for proofs and further theorems.theorems.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
30
Binary Search TreesBinary Search TreesIf we want to perform a large number of searches If we want to perform a large number of searches in a particular list of items, it can be worthwhile in a particular list of items, it can be worthwhile to arrange these items in a to arrange these items in a binary search treebinary search tree to facilitate the subsequent searches.to facilitate the subsequent searches.
A binary search tree is a binary tree in which A binary search tree is a binary tree in which each child of a vertex is designated as a right or each child of a vertex is designated as a right or left child, and each vertex is labeled with a key, left child, and each vertex is labeled with a key, which is one of the items.which is one of the items.
When we construct the tree, vertices are When we construct the tree, vertices are assigned keys so that the key of a vertex is both assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices subtree and smaller than the keys of all vertices in its right subtree.in its right subtree.
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
31
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
32
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
33
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
powerpower
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
34
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
powerpower
northnorth
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
35
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
powerpower
northnorth zoozoo
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
36
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
powerpower
northnorth zoozoodentistdentist
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
37
Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..
mathmath
computecomputerr
powerpower
northnorth zoozoodentistdentistbookbook
May 1, 2002 Applied Discrete MathematicsWeek 13: Graphs and Trees
38
Binary Search TreesBinary Search Trees
To perform a search in such a tree for an item x, To perform a search in such a tree for an item x, we can start at the root and compare its key to 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 If x is less than the key, we proceed to the left child of the current vertex, and if x is greater than child of the current vertex, and if x is greater than the key, we proceed to the right one.the key, we proceed to the right one.
This procedure is repeated until we either found This procedure is repeated until we either found the item we were looking for, or we cannot the item we were looking for, or we cannot proceed any further.proceed any further.
In a balanced tree representing a list of n items, In a balanced tree representing a list of n items, search can be performed with a maximum of search can be performed with a maximum of log(n + 1)log(n + 1) steps steps (compare with binary search)(compare with binary search)..