News CSEMS Scholarships for CS and Math students

  • Slides: 38
Download presentation
News CSEMS Scholarships for CS and Math students (US citizens only) $3, 125 per

News CSEMS Scholarships for CS and Math students (US citizens only) $3, 125 per year Look at: http: //www. cs. umb. edu/Resources/scholarships. html May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 1

Shortest Path Problems We can assign weights to the edges of graphs, for example

Shortest Path Problems We can assign weights to the edges of graphs, for example to represent the distance between cities in a railway network: Toronto 650 700 Chicago 600 Boston 200 New York May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 2

Shortest Path Problems Such weighted graphs can also be used to model computer networks

Shortest Path Problems Such weighted graphs can also be used to model computer networks with response times or costs as weights. One of the most interesting questions that we can investigate with such graphs is: What is the shortest path between two vertices in the graph, that is, the path with the minimal sum of weights along the way? This corresponds to the shortest train connection or the fastest connection in a computer network. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 3

Dijkstra’s Algorithm Dijkstra’s algorithm is an iterative procedure that finds the shortest path between

Dijkstra’s Algorithm Dijkstra’s algorithm is an iterative procedure that finds the shortest path between to vertices a and z in a weighted graph. It proceeds by finding the length of the shortest path from a to successive vertices and adding these vertices to a distinguished set of vertices S. The algorithm terminates once it reaches the vertex z. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 4

Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices a = v 0,

Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices a = v 0, v 1, …, 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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 5

Dijkstra’s Algorithm while z S begin u : = the vertex not in S

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 6

Dijkstra’s Algorithm Example: b 5 4 a 0 8 1 2 Step 0 May

Dijkstra’s Algorithm Example: b 5 4 a 0 8 1 2 Step 0 May 1, 2002 d 6 2 3 z 10 c e Applied Discrete Mathematics Week 13: Graphs and Trees 7

Dijkstra’s Algorithm Example: b 4 (a) 5 4 a 0 1 8 2 Step

Dijkstra’s Algorithm Example: b 4 (a) 5 4 a 0 1 8 2 Step 1 May 1, 2002 d 6 2 3 z 10 c 2 (a) e Applied Discrete Mathematics Week 13: Graphs and Trees 8

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1 8 2 Step 2 May 1, 2002 d 10 (a, c) 6 2 3 z 10 c 2 (a) e 12 (a, c) Applied Discrete Mathematics Week 13: Graphs and Trees 9

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1

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

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1 8 2 Step 4 May 1, 2002 d 8 10(a, c, c)b) 6 2 3 z 14 (a, c, b, d) 10 c 2 (a) e 12 10 (a, c, b, d) (a, c) Applied Discrete Mathematics Week 13: Graphs and Trees 11

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1

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

Dijkstra’s Algorithm Example: b 4 3 (a, (a) c) 5 4 a 0 1

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

Dijkstra’s Algorithm Theorem: Dijkstra’s algorithm finds the length of a shortest path between two

Dijkstra’s Algorithm Theorem: Dijkstra’s algorithm finds the length of a shortest path between two vertices in a connected simple undirected weighted graph. Theorem: Dijkstra’s algorithm uses O(n 2) operations (additions and comparisons) to find the length of the shortest path between two vertices in a connected simple undirected weighted graph. Please take a look at pages 492 to 496 in the textbook for a comprehensive description and analysis of Dijkstra’s algorithm. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 14

The Traveling Salesman Problem The traveling salesman problem is one of the classical problems

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 one. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 15

The Traveling Salesman Problem Example: What path would the traveling salesman take to visit

The Traveling Salesman Problem Example: What path would the traveling salesman take to visit the following cities? Toronto 650 Chicago 700 600 550 Boston 200 New York Solution: The shortest path is Boston, New York, Chicago, Toronto, Boston (2, 000 miles). May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 16

The Traveling Salesman Problem Question: Given n vertices, how many different cycles Cn can

The Traveling Salesman Problem Question: Given n vertices, how many different cycles Cn can we form by connecting these vertices with edges? Solution: We first choose a starting point. Then we have (n – 1) choices for the second vertex in the cycle, (n – 2) for the third one, and so on, so there are (n – 1)! choices for the whole cycle. However, this number includes identical cycles that were constructed in opposite directions. Therefore, the actual number of different cycles Cn is (n – 1)!/2. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 17

The Traveling Salesman Problem Unfortunately, no algorithm solving the traveling salesman problem with polynomial

The Traveling Salesman Problem Unfortunately, no algorithm solving the traveling salesman problem with polynomial worst-case time complexity has been devised yet. This means that for large numbers of vertices, solving the traveling salesman problem is impractical. In these cases, we can use efficient approximation algorithms that determine a path whose length may be slightly larger than the traveling salesman’s path, but May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 18

Let us talk about… Trees May 1, 2002 Applied Discrete Mathematics Week 13: Graphs

Let us talk about… Trees May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 19

Trees Definition: A tree is a connected undirected graph with no simple circuits. Since

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 20

Trees Example: Are the following graphs trees? May 1, 2002 No. Yes. No. Applied

Trees Example: Are the following graphs trees? May 1, 2002 No. Yes. No. Applied Discrete Mathematics Week 13: Graphs and Trees 21

Trees Definition: An undirected graph that does not contain simple circuits and is not

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 22

Tree Terminology If v is a vertex in a rooted tree other than the

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 23

Tree Terminology The descendants of a vertex v are those vertices that have v

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 24

Tree Terminology The level of a vertex v in a rooted tree is the

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 25

Trees Example I: Family tree James Christine Frank May 1, 2002 Bob Joyce Petra

Trees Example I: Family tree James Christine Frank May 1, 2002 Bob Joyce Petra Applied Discrete Mathematics Week 13: Graphs and Trees 26

Trees Example II: File system / usr bin May 1, 2002 bin spool temp

Trees Example II: File system / usr bin May 1, 2002 bin spool temp ls Applied Discrete Mathematics Week 13: Graphs and Trees 27

Trees Example III: Arithmetic expressions + y - z x y This tree represents

Trees Example III: Arithmetic expressions + y - z x y This tree represents the expression (y + z) (x - y). May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 28

Trees Definition: A rooted tree is called an m-ary tree if every internal vertex

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. Please look at page 536 for proofs and further theorems. May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 29

Binary Search Trees If we want to perform a large number of searches in

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 30

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 31

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 32

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer May 1, 2002 power Applied Discrete Mathematics Week 13: Graphs and Trees 33

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power north May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 34

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power north May 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees zoo 35

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math power computer dentist May 1, 2002 north Applied Discrete Mathematics Week 13: Graphs and Trees zoo 36

Binary Search Trees Example: Construct a binary search tree for the strings math, computer,

Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math power computer book May 1, 2002 dentist north Applied Discrete Mathematics Week 13: Graphs and Trees zoo 37

Binary Search Trees To perform a search in such a tree for an item

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 1, 2002 Applied Discrete Mathematics Week 13: Graphs and Trees 38