[Hernandez 1213A] Cs30 day21

Page 1

Trees

CS 30 : Discrete Mathematics for Computer Science First Semester, AY 2012-2013

https://sites.google.com/a/dcs.upd.edu.ph/nhsh_classes/cs 30

sablay-logo

Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman nshernandez@dcs.upd.edu.ph updilseal

Day 21

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph Theory

sablay-logo

1

Trees Graph-theoretic Properties Application of Trees

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

A tree is a graph that is connected and contains no cycles. More generally, a forest is a graph that contains no cycles; each component of a forest is a tree.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

A tree is a graph that is connected and contains no cycles. More generally, a forest is a graph that contains no cycles; each component of a forest is a tree. 1 4

2

sablay-logo

3

5 6

7

d a Forest

updilseal

x of Figure 10.1 is a tree. We can ntains no cycles. ox of Figure 10.1 is a forest that

Discrete Mathematics for Computer Science

dcs-logo

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

A tree is a graph that is connected and contains no cycles. More generally, a forest is a graph that contains no cycles; each component of a forest is a tree. 1 4

2

sablay-logo

3

5 6

7

A pendant vertex (a vertex of degree 1) is called a leaf. The vertices of degree at least 2 are said to be internal vertices. d a Forest

x of Figure 10.1 is a tree. We can ntains no cycles. ox of Figure 10.1 is a forest that

Discrete Mathematics for Computer Science

updilseal

dcs-logo

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Theorem Let G be a graph. If G is a tree, then there is a unique path between any pair of vertices in G .

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Theorem Let G be a graph. If G is a tree, then there is a unique path between any pair of vertices in G .

sablay-logo

Theorem Every tree on two or more vertices has at least two leaves.

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Theorem Let G be a graph. If G is a tree, then there is a unique path between any pair of vertices in G .

sablay-logo

Theorem Every tree on two or more vertices has at least two leaves.

Theorem Let G

= (V , E )

be a graph. If G is a tree, then

|E | = |V | − 1. updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Spanning Trees A spanning tree for a connected graph G = (V , E ) is a subgraph = (V , F ) that is a tree on the same vertex set V . It may be speci ed uniquely by its edge set F . More generally, a subgraph of a graph G is called a spanning forest if its intersection with each component of G is a spanning tree for that component. H

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


subgraph of a graph G is called a component of G is a spanning tree Spanning Trees Trees

Graph-theoretic Properties Application of Trees

the left-hand box of Figure 10.7 is A spanning for a connected f Figure 10.7, a tree spanning forest graph for G = (V , E ) is a subgraph H = (V , F ) that is a tree on the same vertex set V . It may be speci ed graphuniquely is notbyconnected, its edge set Fit. cannot that is connected at least More generally, ahas subgraph of a one graph G is called a spanning forest if its intersection with each component of G is a spanning tree for that component.

ee and a Spanning Forest

sablay-logo

updilseal

dcs-logo

G has a spanning tree. Discrete Mathematics for Computer Science

CS 30

acl-logo


subgraph of a graph G is called a component of G is a spanning tree Spanning Trees Trees

Graph-theoretic Properties Application of Trees

the left-hand box of Figure 10.7 is A spanning for a connected f Figure 10.7, a tree spanning forest graph for G = (V , E ) is a subgraph H = (V , F ) that is a tree on the same vertex set V . It may be speci ed graphuniquely is notbyconnected, its edge set Fit. cannot that is connected at least More generally, ahas subgraph of a one graph G is called a spanning forest if its intersection with each component of G is a spanning tree for that component.

ee andTheorem a Spanning Forest Let G

= (V , E )

sablay-logo

updilseal

be a graph. If G is connected, then G has a spanning

tree.

dcs-logo

G has a spanning tree. Discrete Mathematics for Computer Science

CS 30

acl-logo


rs in a Directory Rooted Trees System

Trees

Graph-theoretic Properties Application of Trees

A rooted tree is a pair (T , v ), where T is a tree and v is a vertex of T . The distinguished vertex v is said to be the root of T .

where T is a tree and v is a vertex of T. The distinlevel of a vertex u in T is its distance from v . he rootThe of T.

A child of a vertex u in T is a neighbor of u at a level one more than ure 10.8that hasofthe u . structure of a rooted tree with root The parent of a vertex u inalways T is theleaves unique and neighbor of rooted trees that files are in- of u with level one less than that of u . The root v iscan thealso onlybe vertex in T without a parent. ders. However, a folder, if empty, a leaf.

sablay-logo

The height of T is the maximum level among its vertices. v1 v4

v2 v3

updilseal

v5 dcs-logo

v6 Discrete Mathematics for Computer Science

CS 30

acl-logo


v1 ) displayed in Figure 10.9. Vertex v2 is at level 1, Rooted Trees3, since dist(v , v ) = 3. Vertex v has s at level 1 6 2 nt of v2 is v1 . The height of T is 3, and T is a binary lanced. shows a then fullweand balanced If eachFigure vertex of 10.10 has at most children, say that is an . If each internal vertex has exactly children, then is said ght 2.to be a . A 2-ary tree is also called a . Trees

T

Graph-theoretic Properties Application of Trees

m

T

m-ary tree

m

full m-ary tree

T

binary tree

sablay-logo

If T has height h and all of its leaves are at levels h or h − 1, then T is said to be balanced. v1

v2 v5

v3 v6

v4

v7

updilseal

ull and Balanced Rooted 3-ary Tree Discrete Mathematics for Computer Science

CS 30

dcs-logo

acl-logo


v1 ) displayed in Figure 10.9. Vertex v2 is at level 1, Rooted Trees3, since dist(v , v ) = 3. Vertex v has s at level 1 6 2 nt of v2 is v1 . The height of T is 3, and T is a binary lanced. shows a then fullweand balanced If eachFigure vertex of 10.10 has at most children, say that is an . If each internal vertex has exactly children, then is said ght 2.to be a . A 2-ary tree is also called a . Trees

T

Graph-theoretic Properties Application of Trees

m

T

m-ary tree

m

full m-ary tree

T

binary tree

sablay-logo

If T has height h and all of its leaves are at levels h or h − 1, then T is said to be balanced. v1

v2 v5

v3 v6

v4

v7

updilseal

ull and Balanced Rooted 3-ary Tree Discrete Mathematics for Computer Science

CS 30

dcs-logo

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Ordered Rooted Tree An ordered rooted tree is a rooted tree in which the set of children of each internal vertex is ordered. An ordered binary tree is a binary tree in which each child of an internal vertex is designated as either the left child or the right child, but not both. Moreover, at each internal vertex, the corresponding left subtree (respectively, right subtree) is the subtree rooted at its left child (respectively, right child).

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


xample. Note that this ordering agrees with the left-to-right placement ertices in the picture Ordered Rooted Treein Figure 10.10. In fact, the convention for displayin ed rooted trees is to place the children in order from left to right. he convention displaying ordered binary trees is to place the left child An orderedfor rooted tree is a rooted tree in which the set of children of ft of its parent and the isright child to the right. Consequently, there is mo each internal vertex ordered. one way in which we might provide the binary tree in Figure 10.9 with th binary tree is a binary which each child of an internal ture ofAn anordered ordered binary tree. Thetree twoinare shown in Figure 10.11; they a vertex is designated as either the left child or the right child , butleft not or the rig nguished by the choice of regarding vertex v6 as either the Moreover, at each internal vertex, the corresponding left subtree of its both. parent v5 . (respectively, right subtree) is the subtree rooted at its left child (respectively, right child). Trees

Graph-theoretic Properties Application of Trees

sablay-logo

v1 v4

v2 v3

v1 v4

v2

v5

v3

v6

v5

updilseal

v6

Figure 10.11 Two Different Ordered Binary Trees Discrete Mathematics for Computer Science

CS 30

dcs-logo

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Trees as Models Graphs can be used to represent molecules, where atoms are represented by vertices and bonds between them by edges.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Trees as Models Graphs can be used to represent molecules, where atoms are represented by vertices and bonds between them by edges. Saturated Hydrocarbons

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Trees as Models

An Organization Tree for a Computer Company

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Trees as Models

A Computer File System

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Trees as Models

A Tree-Connected Network of Seven Processors

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree.

sablay-logo

Suppose there are seven coins, all with the same weight, and a counterfeit coin that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the eight coins is the counterfeit one?

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree.

sablay-logo

Suppose there are seven coins, all with the same weight, and a counterfeit coin that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the eight coins is the counterfeit one?

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees

We can also use decision trees to model sorting algorithms.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees

We can also use decision trees to model sorting algorithms. A Decision Tree for Sorting Three Distinct Elements

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Decision Trees

We can also use decision trees to model sorting algorithms. A Decision Tree for Sorting Three Distinct Elements

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes Consider the problem of using bit strings to encode the letters of the English alphabet. We can represent each letter with a bit string of length ve. (why?)

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes Consider the problem of using bit strings to encode the letters of the English alphabet. We can represent each letter with a bit string of length ve. (why?) The total number of bits used to encode data is ve times the number of characters in the text when each character is encoded with ve bits. Is it possible to nd a coding scheme of these letters such that, when data are coded, fewer bits are used?

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes Consider the problem of using bit strings to encode the letters of the English alphabet. We can represent each letter with a bit string of length ve. (why?) The total number of bits used to encode data is ve times the number of characters in the text when each character is encoded with ve bits. Is it possible to nd a coding scheme of these letters such that, when data are coded, fewer bits are used?

sablay-logo

Consider using bit strings of di erent lengths to encode letters. Letters that occur more frequently should be encoded using short bit strings, and longer bit strings should be used to encode rarely occurring letters. updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes Consider the problem of using bit strings to encode the letters of the English alphabet. We can represent each letter with a bit string of length ve. (why?) The total number of bits used to encode data is ve times the number of characters in the text when each character is encoded with ve bits. Is it possible to nd a coding scheme of these letters such that, when data are coded, fewer bits are used? Consider using bit strings of di erent lengths to encode letters. Letters that occur more frequently should be encoded using short bit strings, and longer bit strings should be used to encode rarely occurring letters. When letters are encoded using varying numbers of bits, some method must be used to determine where the bits for each character start and end

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes Consider the problem of using bit strings to encode the letters of the English alphabet. We can represent each letter with a bit string of length ve. (why?) The total number of bits used to encode data is ve times the number of characters in the text when each character is encoded with ve bits. Is it possible to nd a coding scheme of these letters such that, when data are coded, fewer bits are used? Consider using bit strings of di erent lengths to encode letters. Letters that occur more frequently should be encoded using short bit strings, and longer bit strings should be used to encode rarely occurring letters. When letters are encoded using varying numbers of bits, some method must be used to determine where the bits for each character start and end (e.g. if e were encoded with 0, a with 1 and t with 01, then the bit string 0101 could correspond to ...)

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the rst part of the bit string for another letter. Codes with this property are called pre x codes.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the rst part of the bit string for another letter. Codes with this property are called pre x codes. (e.g. if e were encoded with 0, a with 10 and t with 11, then the bit string 10110 corresponds to ...)

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Pre x Codes One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the rst part of the bit string for another letter. Codes with this property are called pre x codes. (e.g. if e were encoded with 0, a with 10 and t with 11, then the bit string 10110 corresponds to ...)

sablay-logo

A pre x code can be represented by a binary tree.

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Binary Search Trees Searching for items in a list is one of the most important tasks that arises in computer science. Our primary goal is to implement a searching algorithm that nds items e ciently when the items are totally ordered.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Binary Search Trees Searching for items in a list is one of the most important tasks that arises in computer science. Our primary goal is to implement a searching algorithm that nds items e ciently when the items are totally ordered. This can be accomplished through the use of a binary search tree.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Binary Search Trees Searching for items in a list is one of the most important tasks that arises in computer science. Our primary goal is to implement a searching algorithm that nds items e ciently when the items are totally ordered. This can be accomplished through the use of a binary search tree.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Tree Traversal

We consider two algorithms that yield spanning trees by traversing a given connected graph from a speci ed vertex. Breadth-First Search (BFS) Depth-First Search (DFS)

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


CHAPTER 10

Trees

■ Graph-theoretic Properties Application of Trees

5

Trees and Algorithms

Breadth-First Search ALGORITHM 10.1 Breadth-First Search Let G = (V, E) be a connected graph on n vertices and v a vertex of G. Algorithm. Let M = [ ], L = [v], and F = ∅. While |F | < n − 1,

sablay-logo

\begin

Let t be the first vertex in L and not in M. Add t to the end of M. While t has neighbors that are not in L,

\begin

Let e be an edge such that one end is t and the other is a vertex u outside of L. Let F = F ∪ {e}. Add u to the end of L.

\end. \end.

Return T = (V, F ).

updilseal

dcs-logo

as a rooted tree with root v. In Breadth-First Search, the neighbors of v are visi Discrete Mathematics for Computer Science

CS 30

acl-logo


need not agree with the original given ordering. Graph-theoretic Properties Trees

â–

Application of Trees

Breadth-First Search

In our second example, we apply Breadth-First Search to a graph that is not a tree. Thus, the particular spanning tree produced should also be a focus of our attention.

10.6

Use Breadth-First Search to find the breadth-first search tree for the pictured graph starting from the vertex v1 , given the initial ordering v1 , v2 , v3 , v4 , v5 , v6 , v7 of its vertices. v1

v6

v2

sablay-logo

v7 v3

v5

v4

Shade the edge set F for the resulting breadth-first search tree, and report the new ordering L imposed on the vertices.

Solution. The final value of the vertex list is L = [v1 , v2 , v6 , v3 , v4 , v5 , v7 ],

updilseal

dcs-logo

and the breadth-first search tree is shown in bold in the right-hand picture. Discrete Mathematics for Computer Science

CS 30

acl-logo


rooted tree with root v. Trees

Graph-theoretic Properties Application of Trees

Depth-First Search

ALGORITHM 10.2 Depth-First Search Let G = (V, E) be a connected graph on n vertices and v a vertex of G. Algorithm. Let M = [v], L = [ ], and F = ∅. Let t = v. While t = " null,

sablay-logo

\begin

While t has neighbors that are not in M,

\begin

Let e be an edge such that one end is t and the other is a vertex u outside of M. Let F = F âˆŞ {e}. Add u to the end of M. Let t = u.

\end.

updilseal

Add t to the end of L. Replace t by the parent of t.

\end.

dcs-logo

Return T = (V, F ). Discrete Mathematics for Computer Science

CS 30

acl-logo


Graph-theoretic Properties Application of Trees

Trees

Depth-First Search

CHAPTER 10

â–

Trees and Algorithms

579

In our second example, we apply Depth-First Search to the graph on which Breadth-First Search was earlier applied in Example 10.6. Here we see that a different spanning tree results. 10.8

Use Depth-First Search to find the depth-first search tree for the pictured graph starting from the vertex v1 , given the initial ordering v1 , v2 , v3 , v4 , v5 , v6 , v7 of its vertices. v1

v6

v2

sablay-logo

v7 v3

v5

v4

Shade the edge set F for the resulting depth-first search tree, and report the new ordering L imposed on the vertices.

Solution. The final value of the vertex list is

updilseal

L = [v4 , v7 , v6 , v3 , v5 , v2 , v1 ],

and the depth-first search tree is shown in bold in the right-hand picture. v1 v

v6

v7 v

2 3 Discrete Mathematics for Computer Science

v1 v

CS 30 2

v6

dcs-logo

v7 v3

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Minimum Spanning Trees

Let G be a weighted graph. (a) The weight of a subgraph is the sum of the weights of the edges in that subgraph. (b) A minimum spanning tree for G is a spanning tree with the minimum weight among all spanning trees.

sablay-logo

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


y” than others, then some choices may now be considered Trees

Graph-theoretic Properties Application of Trees

Minimum Spanning Trees ph is a graph G = (V, E) for which each edge has been asal number called the weight of the edge. Formally, this asy a function ω : E → R+ . In the examples we consider, the ed by attaching them asgraph. labels on the edges in a drawing of Let G be a weighted 0.20 displays an example of a weighted In all of the (a) The weight of a subgraph is the sum ofgraph. the weights of the edges in subgraph. er, thethat weights are positive integers. (b) A minimum spanning tree for G is a spanning tree with the minimum weight among all spanning trees. 11 3

4 7

9

12 8

2

sablay-logo

6 14

updilseal

Weighted Graph (for a Company Setting up a Network) dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Kruskal's Algorithm

Graph-theoretic Properties Application of Trees

CHAPTER 10

Trees and Algorithms

58

ALGORITHM 10.3 Kruskal’s Algorithm Let G = (V, E) be a weighted connected graph on n > 1 vertices. Algorithm.

sablay-logo

Let F = {e}, where e is a non-loop edge of minimum possible weight. While |F | < n − 1,

\begin

Let e be an edge of minimum possible weight among all edges in E\F for which F ∪ {e} contains no cycle. Let F = F ∪ {e}.

\end.

Return T = (V, F ).

updilseal

At any stage of an implementation of Kruskal’s Algorithm, if there is a ti among edges that may be chosen next, then any one such choice will suffic However, if the edge set additionally comes with some ordering, then that o dering can be used to force a unique choice at each stage. For example, it can b helpful to pre-sort the edges into some order of nondecreasing weight. Henc dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

10.22 represents an apartment complex for which electric Kruskal's Algorithm plied. Each vertex represents an apartment building, each The graphpower below represents an apartment for which otential direct cable between twocomplex buildings, andelectric power must be supplied. Each vertex represents an apartment building, to an edge reflects the cost of establishing that link. each edge represents a potential direct power cable between two heapest power grid that connects all of the apartment buildings, and the weight assigned to an edge re ects the cost of d by aestablishing minimumthat spanning link. Whattree. would be the cheapest power grid that connects all of the apartment buildings? v1

1

7 v4

v2

6

7

v3 2

6

1

v6 1

v5 5

sablay-logo

4

updilseal

3 1

v7

dcs-logo

v8

22 Possible Power Grid Connections with Costs Discrete Mathematics for Computer Science

CS 30

acl-logo


13

Graph-theoretic Properties ever, unlike in Kruskal’s Algorithm, here the stronger requirement that the se Trees Application of Trees of chosen edges always forms a tree further restricts our possible choices. The Prim's Algorithm process continues until this growing tree forms a spanning tree.

ALGORITHM 10.4 Prim’s Algorithm Let G = (V, E) be a weighted connected graph on n > 1 vertices. Algorithm.

sablay-logo

Let F = {e}, where e is a non-loop edge of minimum possible weight. While |F | < n − 1,

\begin

Let e be an edge of minimum possible weight among all edges in E\F that connect an endpoint of an edge from F to a vertex that is not an endpoint of an edge from F . Let F = F ∪ {e}.

\end.

Return T = (V, F ).

updilseal

dcs-logo The graph in Figure 10.23 represents the same apartment complex considered in Example 10.12, but now the weighted edges reflect potential gas line connections and their costs. So here a minimum spanning tree reflects theacl-logo Discrete Mathematics for Computer Science

CS 30


Trees

Graph-theoretic Properties Application of Trees

Prim's Algorithm The graph below represents the same apartment complex considered in the previous example, but now the weighted edges re ect potential gas A P T E R 1 0 â– Trees and Algorithms 591 line connectionsC Hand their costs. What would be the cheapest possible system of gas lines joining the apartment buildings?

sablay-logo

v1

3

2 v4

v2

7

1

v3 6

4

8

v6 5

v5 8

8

2

updilseal

2 v7

v8

dcs-logo

23 Possible Gas Line Connections with Costs Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

Shortest Path Trees

Let G = (V , E ) be a weighted graph. (a) The weighted distance between two vertices u and v in G , denoted DistG (u , v ), is the minimum weight of a path in G from u to v . If there is no path in G from u to v , then we assign DistG (u , v ) = ∞. When G is clear in context, the subscripts may be dropped.

sablay-logo

(Note that capital D is used here in the notation for the weighted distance where lower-case d was used for ordinary distance.)

(b) Suppose that a vertex v is speci ed in G . A shortest path tree for G from v is a spanning tree T such that, for each vertex w in G , the path in T from v to w has the minimum weight among all paths in G from v to w .

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Trees

Graph-theoretic Properties Application of Trees

The graph below again represents the apartment complex considered in the previous example, but now the edges re ect potential water lines, and the weights re ect the relative di culty of pumping water between buildings. A large pumping station has been installed beneath the apartment building represented by vertex v . Hence, water must be pumped from there to the other buildings. Consequently, the system that accomplishes this with the lowest loss of water pressure for each building is re ected by a shortest path tree. 1

v1

2

1 v4

v2

3

1

v3 2

2

3

v6 2

v5 3

sablay-logo

updilseal

3

1 1

v7

dcs-logo

v8

Discrete Mathematics for Computer Science

CS 30

acl-logo


15

Having seen the utility of a shortest path tree, we now consider an algorithm Graph-theoretic Properties Trees Application of Trees for finding one that was discovered by the Dutch computer scientist Edsger W. Dijkstra (1930–2002). Dijkstra’s Algorithm is presented in Algorithm 10.5, and Dijkstra's Algorithm the proof of its correctness is given at the end of this section. ALGORITHM 10.5 Dijkstra’s Algorithm Let G be a weighted connected graph on n > 1 vertices and v a vertex of G. Algorithm. Let F = {e}, where e is a non-loop edge incident with v of minimum possible weight. While |F | < n − 1,

sablay-logo

\begin

Let e be an edge such that one endpoint t is an endpoint of an edge from F , the other endpoint is not, and DistT (v, t) + ω(e) is as small as possible. Let F = F ∪ {e}.

\end.

Return T = (V, F ).

updilseal

The graph in Figure 10.25 again represents the apartment complex consid- dcs-logo ered in Example 10.12, but now the edges reflect potential water lines, and the weights reflect the relative difficulty of pumping water between buildings. acl-logo A large pumping station has been installed Discrete Mathematics for Computer Science CS 30 beneath the apartment building


Trees

Questions?

sablay-logo

See you next meeting!

updilseal

dcs-logo

Discrete Mathematics for Computer Science

CS 30

acl-logo


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.