graph theory ch. 2. trees and distance 1 chapter 2 trees and distance 2.1 basic properties 2.2...

84
Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Upload: marvin-lawrence

Post on 22-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance1

Chapter 2

Trees and Distance

2.1 Basic properties

2.2 Spanning tree and enumeration

2.3 Optimization and trees

Page 2: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance2

Acyclic, tree2.1.1

A graph with no cycle is acyclic A forest is an acyclic graph A tree is a connected acyclic graph A leaf (or pendant vertex ) is a vertex of degree 1

tree tree

forest

leaf

Page 3: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance3

Spanning Subgraph 2.1.1

A spanning subgraph of G is a subgraph with vertex set V(G)

A spanning tree is a spanning subgraph that is a tree

Spanning subgraph Spanning tree

Page 4: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance4

Lemma. Every tree with at least two vertices has at least two leaves. 2.1.3

Proof: (1/2)

– A connected graph with at least two vertices has an edge.

– In an acyclic graph, an endpoint of a maximal nontrivial path has no neighbor other than its neighbor on the path.

– Hence the endpoints of such a path are leaves.

Maximal path

Impossible!

Cycle occurs

Impossible!

It is Maximal.

Page 5: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance5

Proof: (2/2)– Let v be a leaf of a tree G, and that G’=G-v.

– A vertex of degree 1 belongs to no path connecting two other vertices.

– Therefore, for u, w V(G’), every u, w-path in G is also in G’.

– Hence G’ is connected.

– Since deleting a vertex cannot create a cycle, G’ also is acyclic.

– Thus G’ is a tree with n-1 vertices.

Lemma. Deleting a leaf from a n-vertex tree produces a tree with n-1 vertices. 2.1.3

Page 6: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance6

For u, w V(G’ ), every u, w-path in G is also in G’.

u w

G

G’

v

Page 7: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance7

Theorem 2.1.4. For an n-vertex graph G (with n1), the following are equivalent (and characterize the trees with n vertices) 2.1.4

A) G is connected and has no cyclesB) G is connected and has n-1 edgesC) G has n-1 edges and no cyclesD) For u, vV(G), G has exactly one u, v-path

Proof: We first demonstrate the equivalence of A, B, and C by proving that any two of {connected, acyclic, n-1 edges} together imply the third

Page 8: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance8

A{B,C}. connected, acyclic n-1 edges

Theorem 2.1.4 Continue We use induction on n. For n=1, an acyclic 1-vertex graph has no edge For n >1, we suppose that implication holds

for graphs with fewer than n vertices – Given an acyclic connected graph G, Lemma

2.1.3 provides a leaf v and states that G’=G-v also is acyclic and connected (see figure in previous proof)

– Applying the induction hypothesis to G’ yields e(G’)=n-2

– Since only one edge is incident to v, we have e(G)=n-1

Page 9: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance9

B{A, C} Connected and n-1 edges acyclic

Theorem 2.1.4 continue

If G is not acyclic, delete edges from cycles of G one by one until the resulting graph G’ is acyclic

Since no edge of a cycle is a cut-edge(Theorem 1.2.14), G’ is connected – Now the preceding paragraph implies that

e(G’) = n-1

– Since we are given e(G)=n-1, no edges were deleted

Thus G’=G, and G is acyclic

Page 10: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance10

C{A, B} n-1 edges and no cycles connected

Theorem 2.1.4 continue

Let G1,…,Gk be the components of G

Since every vertex appears in one component, in(Gi)=n

– Since G has no cycles, each component satisfies property A. Thus e(Gi) = n(Gi) - 1

– Summing over i yields e(G)=I[n(Gi)-1]= n-k

We are given e(G)=n-1,so k =1, and G is connected

Page 11: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance11

AD Connected and no cycles For u, vV(G), one and only one u, v-path exists

Theorem 2.1.4

Since G is connected, each pair of vertices is connected by a path

If some pair is connected by more than one , we choose a shortest (total length) pair P, Q of distinct paths with the same endpoints

By this extremal choice, no internal vertex of P or Q can belong to the other path

This implies that PQ is a cycle, which contradicts the hypothesis Ap

vuq

Page 12: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

DA For u, vV(G), one and only one u, v-path exists connected and no cycles Theorem

2.1.4

If there is a u,v-path for every u,vV(G), then G is connected

If G has a cycle C, then G has two u,v-paths for u,v V(G); which contradicts the hypothesis D

Hence G is acyclic (this also forbids loops).

Graph Theory

Ch. 2. Trees and Distance12

Page 13: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance13

Corollary: Every edge of a tree is a cut-edge 2.1.5

Proof:

A tree has no cycles Theorem 1.2.14 implies that every edge is

a cut-edge.

Page 14: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance14

Corollary: Adding one edge to a tree forms exactly one cycle 2.1.5

Proof:

A tree has a unique path linking each pair of vertices (Theorem2.1.4D)

Joining two vertices by an edge creates exactly one cycle

Page 15: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance15

Corollary: Every connected graph contains a spanning tree 2.1.5

Proof:

Similar to the proof of BA, C in Theorem 2.1.4 Iteratively deleting edges from cycles in a

connected graph yields a connected acyclic subgraph

Page 16: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance16

Proposition: If T, T’ are spanning trees of a connected graph G and eE(T)-E(T’), then there

is an edge e’E(T’)- E(T) such that T-e+e’ is a spanning tree of G. 2.1.6

T’T T-e+e’

ee’

G

Page 17: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance17

Proposition: If T, T’ are spanning trees of a connected graph G and eE(T)-E(T’), then there is an edge e’E(T’)- E(T) such

that T-e+e’ is a spanning tree of G. 2.1.6Proof:

Every edge of T is a cut-edge of T. Let U and U’ be the two components of T-e.

Since T’ is connected, T’ has an edge e’ with endpoints in U and U’.

Now T-e+e’ is connected, has n(G)-1 edges, and is a spanning tree of G.

T’T T-e+e’e

e’

Page 18: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance18

Distance in trees and Graphs

Assume G has a u, v-path

Then the distance from u to v, written dG(u,v) or d(u,v),

is the least length of a u,v-path. If G has no such path, then d(u,v)=

Page 19: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance19

Distance in trees and Graphs

The diameter (diam G) is maxu,vV(G) d(u,v)

– Upper bound of distance between every pair

The eccentricity of a vertex u is

(u) = maxvV(G) d(u,v)

– Upper bound of the distance from u to the others

The radius of a graph G is rad G = minuV(G) (u)

– Lower bound of the eccentricity

Page 20: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance20

Distance, Diameter, Eccentricity, and Radius

a b

c

d

e

f

g

Distance(f,c) : 2Distance(g,c): 2Distance(a,c): 3

Eccentricity(f):2Eccentricity(a): 3

Diameter: 3 Radius: 2

Page 21: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance21

If G is a simple graph, then diam G 3 diam 3 2.1.11

Proof: 1/3

Since diam G > 2, there exist nonadjacent vertices u, vV(G) with no common neighbor – If any pair of nonadjacent vertices has a

common neighbor, the distance of every pair is less than or equal to 2 and diam G = 2

G

u v

Not every pair is of this kind

A pair (u, v) of this kind exists

Page 22: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance22

If G is a simple graph, then diam G 3 diam 3 2.1.11

Proof: 2/3

Hence every xV( ) - {u,v} has at least one of {u,v} as a nonneighbor– Either u or v is not connected to x

Equivalently, this makes x adjacent to at least one of {u,v} in G

u vG

G

Everyone of these vertices has at least one of {u,v} as a nonneighbor

G

Page 23: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance23

If G is a simple graph, then diam G 3 diam 3 2.1.11

Proof: 3/3

Since also u v E( ), for every pair x, y there is an x, y-path of length at most 3 in through {u,v}. Hence diam 3

u vG

G

GG

G

Page 24: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance24

Center 2.1.12

Definition: The center of a graph G is the subgraph induced by the vertices of minimum eccentricity.

The center of a graph is the full graph if and only if the radius and diameter are equal.

Center

Recall: The eccentricity of a vertex u is the upper bound of the distance from u to the others

Page 25: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance25

The center of a tree is a vertex or an edge 2.1.13

Proof: We use induction on the number of vertices in a tree T.

Basis step: n(T)≦ 2. With at most two vertices, the center is the entire tree.

Page 26: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance26

Theorem. 2.1.13 Continue Induction step: n(T)>2.

– Let T’ = T- {leaves}. By Lemma 2.1.3, T’ is a tree. – Since the internal vertices on the paths between

leaves of T remain, T’ has at least one vertex.– Every vertex at maximum distance in T from a vertex

uV(T) is a leaf (otherwise, the path reaching it from u can be extended farther).

T = Green PinkT’ = Green

u

The max path from u

The end must be a leaf

Page 27: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance27

Theorem. 2.1.13 Continue– Since all the leaves have been removed and no path

between two other vertices uses a leave, T’(u)= T(u)-1 for every uV(T’).

– Also, the eccentricity of a leaf in T is greater than the eccentricity of its neighbor in T.

– Hence the vertices minimizing T(u) are the same as the vertices minimizing T’(u).

uT’(u)= T(u)-1

{v| v has min (v) in T} ={v’| v’ has min (v’) in T’}

Page 28: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance28

Theorem. 2.1.13 Continue It is shown T and T’ have the same center. By the induction hypothesis, the center of T’ is a

vertex or an edge.

T T’ T”

Page 29: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance29

Spanning Trees and Enumeration 2.2

There are 2c(n,2) simple graphs with vertex set [n]={1,…,n}.– since each pair may or may not form an

edge.

How many of these are trees?

Max number of edges = c(n,2)

Can either appear or disappear

Page 30: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance30

Enumeration of Trees 2.2

One or two vertices, then there is only one tree.

Three vertices, three trees. Four vertices, then four stars and 12 paths,

total 16. Five vertices, then there are 125 trees.

Page 31: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance31

Algorithm for tree generation by using Prűfer code 2.2.1

Algorithm. (Prűfer code) Production of f(T)=(a1,…,an-2)

Input: A tree T with vertex set S N

Iteration: At the i th step, delete the least remaining leaf, and say that ai is the neighbor of the deleted leaf

2 7 1 4 3

6 8 5

1. Delete 2 a1= 72. Delete 3 a2= 43. Delete 5 a3= 44. Delete 4 a4= 15. Delete 6 a5= 76. Delete 7 a6= 1

Page 32: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance32

Theorem: For a set SN of size n, there are nn-2 trees with vertex set S 2.2.3

Proof: (sketch) Trees with vertex set S Sn-2 of lists of

length n-2

– One prufer code one tree.

– There are nn-2 codes.

– Proved by induction

Page 33: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance33

Spanning Trees in Graphs 2.2.6

Example. A kite. To count the spanning trees: Four are path around the outside cycle in the drawing The remaining spanning trees use the diagonal edge

– Since we must include an edge to each vertex of degree 2, we obtain four more spanning trees.

The total is eight.

Page 34: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance34

Contraction 2.2.7

In a graph G, contraction of edge e with endpoints u, v is the replacement of u and v with a single vertex whose incident edges are the edges other than e that were incident to u or v

The resulting graph G·e has one less edge than G

u

e

vG G·e

Page 35: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance35

Proposition. Let (G) denote the number of spanning trees of a graph G. If eE(G) is not a loop, then (G)= (G-e)+ (G·e) 2.2.8

(G-e): The number of trees without e (G·e): The number of trees with e A spanning tree in G.e A spanning tree having e

in G

e

G

G·e

G-e

=

Page 36: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance36

Proposition. Let (G) denote the number of spanning trees of a graph G. If eE(G) is not a loop, then (G)= (G-e)+ (G·e) 2.2.8

Proof: 1/2

The spanning trees of G that omit e are precisely the spanning trees of G-e

Need to show that G has (G·e) spanning trees containing e– It must be shown that contraction of e

defines a bijection from the set of spanning trees of G containing e to the set of spanning trees of G·e

Page 37: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance37

Proposition. Let (G) denote the number of spanning trees of a graph G. If eE(G) is not a loop, then (G)= (G-e)+ (G·e)

2.2.8

Proof:2/2

When contract e in a spanning tree having e, obtain a spanning tree of G·e because

– The resulting subgraph of G·e is spanning and connected

– It has the right number of edges

– The other edges maintain their identity under contraction

So no two trees are mapped to the same spanning tree of G·e by this operation.

Page 38: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance38

A Matrix Tree computation. 2.2.12

Given a loopless graph G with vertex set v1, …., vn

Let aij be the number of edges with endpoints vi and vj

Let Q be the matrix in which – entry (i, j) is –ai,j when i j

and is d(vi) when i=j

Let Q* is a matrix obtained by deleting row s and column t of Q, then

(G) = (-1)s+t detQ*

Page 39: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance39

A Matrix Tree computation. 2.2.11

Theorem 2.2.12 instructs us– Form a matrix by putting the vertex degrees on

the diagonal and subtracting the adjacency matrix

– Delete a row and a column and take the eterminant

Page 40: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance40

A Matrix Tree computation. 2.2.11

Consider the kite of Example 2.2.9, – The vertex degrees are 3,3,2,2

– We form the matrix on the left below and then

– We take the determinant of the matrix in the middle

– The result is the number of spanning trees

Page 41: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance41

Example 2.2.11

2011

0211

1131

1113

201

021

113

8

v1

v2

v3

v4

v1 v2 v3 v4v1 v2 v3 v4

Page 42: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance42

Minimum Spanning Tree 2.3

In a connected weighted graph of possible communication links, all spanning trees have n-1 edges; we seek one that minimizes or maximizes the sum of the edge weights.

– Kruskal’s Algorithm

– Prim’s Algorithm

Page 43: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance43

Kruskal’s Algorithm for Minimum Spanning Tree 2.3.1

Input: A weighted connected graph Idea:

– Maintain an acyclic spanning subgraph H.

– Enlarging it by edges with low weight to form a spanning tree.

– Consider edges in nondecreasing order of weight.

Page 44: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance44

Kruskal’s Algorithm for Minimum Spanning Tree 2.3.1

Initialization: Set E(H)=. Iteration: If the next cheapest edge joins

two components of H, then include it; otherwise, discard it. Terminate when H is connected.

HJoin Two vertices in one component. Cycle occurs.

Not Allowed!

Join two components. It works

Page 45: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance45

Example of using Kruskal’s Algorithm

1

9

4

3

12

11

7

8 2

6

510

1

9

4

3

12

11

7

8 2

6

510

1

9

4

3

12

11

7

8 2

6

510

1

9

4

3

12

11

7

8 2

6

510

1

9

4

312

11

78 2

6

510

1

9

4

3

12

11

78 2

6

510

Page 46: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance46

Theorem: In a connected weighted graph G, Kruskal’s Algorithm constructs a minimum-weight spanning tree. 2.3.3

Proof: 1/3

We show first that the algorithm produces a tree

We never choose an edge that completes a cycle

If the final graph has more than one component, then there is no edge joining two of them and G is not connected

– Since G is connected, some such edge exists and we considered it.

Thus the final graph is connected and acyclic, which makes it a tree.

Page 47: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance47

Theorem 2.3.3 Continue

Proof: continue Let T be the resulting tree, and let T* be a spannig tree of

minimum weight. If T=T*, we are done. If TT*, let e be the first edge chosen for T that is not in

T*. Adding e to T* creates one cycle C. Since T has no cycle, C has an edge e’E(T). Consider the spanning tree T*+e-e’

T: …………. e, …………T*: …………. e*,…………

Identical

The first edge chosen for T that is not in T*

Page 48: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance48

Theorem 2.3.3 Continue

Proof: continue Since T* contains e’ and all the edges of T chosen

before e, both e’ and e are available when the algorithm chooses e, and hence w(e)w(e’)

Thus T*+e-e’ is a spanning tree with weight at most T* that agrees with T for a longer initial list of edges than T* does.

T: …………. e, ………… T*: …………. e*,…………

Identical

T: …………… e, ………… T*+e-e’: …………… e, …………

Identical

Page 49: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance49

Theorem 2.3.3 Continue

Proof: continue Repeating this argument eventually yields a minimum-

weight spanning tree that agrees completely with T. Phrased extremely, we have proved that the minimum

spanning tree agreeing with T the longest is T itself.

Page 50: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance50

Shortest Paths

How can we find the shortest route from one location to another?

Page 51: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance51

Dijkstra’s Algorithm2.3.5

Input: A graph (or digraph) with nonnegative edge weights and a starting vertex u. – The weight of edge xy is w(xy)

– Let w(xy)= if xy is not an edge

u

a d

e

cb

1

5

2

6

44

5

3

Page 52: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance52

Dijkstra’s Algorithm2.3.5

Idea: – Maintain the set S of vertices to which a

shortest path from u is known

– Enlarge S to include all vertices. • To do this, maintain a tentative distance t(z)

from u to each zS, being the length of the shortest u, v-path yet found.

u

S

u

S

The one nearest to u

v

Page 53: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance53

Dijkstra’s Algorithm2.3.5

Initialization: Set S={u}; t(u)=0; t(z)=w(uz) for zu

Iteration:

1. Select a vertex v outside S such that t(v)=minzS t(z).

2. Add v to S.

3. Explore edges from v to update tentative distance: for each edge vz with zS, update t(z) to min{t(z), t(v)+w(vz)}

The iteration continues until S=V(G) or until t(z)= for every zS. At the end, set d(u, v)=t(v) for all v.

Page 54: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance54

Tentative distance v.s. Shortest distance 2.3.5

The tentative distance of a vertex may not be the shortest distance, for example:

– t(d ) = 6 and not the minimum

– Actually d(d ) = 5

u

ad

e

b

1

5

2

6

2

3d=0

d=1

t=3

t=6

u

ad

e

b

1

5

2

6

2

3

d=0

d=1t=5

d=3

t=9

Page 55: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance55

Example: Find Shortest paths by using Dijkstra’s Algorithm 2.3.6

u

a d

e

cb

15

2

6

44

53

u

a d

e

cb

15

2

6

44

5

3d=0

t=1

t=3

Page 56: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance56

Example: Find Shortest paths by using Dijkstra’s Algorithm continue 2.3.6

u

ad

e

c

b

1

5

2

6

4

45

3d=0

d=1

t=3

t=6

t=5

u

a

d

e

c

b

1 5 2

6

4

4

53

d=0

d=1

d=3 t=6

t=5

Page 57: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance57

Example: Find Shortest paths by using Dijkstra’s Algorithm continue 2.3.6

u

a

d

e

c

b

15

2

64

4

5

3

d=0

d=1

d=3

t=11

d=5

t=6

u

a

d

e

c

b

15

2

64

4

5

3

d=0

d=1

d=3

t=8

d=5

d=6

Page 58: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance58

Theorem: Given a (di)graph G and a vertex uV(G), Dijkstra’s algorithm compute d(u, z) for every z V(G) 2.3.7

Proof: We prove the stronger statement that at

each iteration,1) For zS, t(z)=d(u, z), and 2) For zS, t(z) is the least length of a u, z-path

reaching z directly from S.

Page 59: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance59

Theorem 2.3.7 Continue

We use induction on k=|S|. Basis step: k=1 From the initialization,

– S={u}, d(u, u)=t(u)=0,– the least length of a u, z-path reaching z

directly from S is t(z)=w(u,z), which is infinite when uz is not an edge

Page 60: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance60

Theorem 2.3.7 Continue

Induction step: Suppose that when |S|=k, (1) and (2) are true. – Let v be a vertex among zS such that t(z) is smallest.

– The algorithm now choose v; let S’=S{v}. We first argue that d(u, v)=t(v). A shortest u, v-path must exit S before reaching v. The induction hypothesis states that the length of the shortest path going directly to v from S is t(v). The induction hypothesis and choice of v also guarantee that a path visiting any vertex outside S and later reaching v has length at least t(v).

– Hence d(u, v)=t(v), and (1) holds for S’.

Page 61: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance61

Theorem 2.3.7 Continue

To prove (2) for S’, let z be a vertex outside S other than v.

– By the hypothesis, the shortest u, v-path reaching z directly from S has length t(z) ( if there is no such path).

– When we add v to S, we must also consider paths reaching z from v. Since we have now computed d(u, v)=t(v), the shortest such path has length t(v)+w(vz), and we compare this with the previous value of t(z) to find the shortest path reaching z directly from S’.

We have verified that (1) and (2) hold for the new set S’ of size k+1; this completes the induction step.

Page 62: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance62

Prim’s Algorithmfor Minimum Spanning Tree

Input: A graph (or digraph) with nonnegtive edge weights and a starting vertex u. The weight of edge xy is w(xy); let w(xy)= if xy is not an edge.

Idea: Maintain a tree T including u at the beginning, enlarging T to include all vertices. To do this, select the cheapest edge from the edges E={(x,v) |x T, vT }.

Page 63: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance63

Prim’s Algorithm

Initialization: Set T={u}.

Iteration:

– Select a vertex v outside T such that w(x,v)=minx T, vT w(x,v)

– Add v to T

– The iteration continues until V(T )=V(G).

Page 64: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance64

Dijkstra’s Algorithm v.s. Prim’s Algorithm

u

ad

ec

bd=0

d=1

t=3

t=6

t=5

The vertex which is the nearest to u

8

The end vertex of the cheapest

edge

3

6

Page 65: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance65

Algorithm - Breadth First Search 2.3.8

Input: An unweighted graph (or digraph) and a start vertex u.

Idea:

Maintain a set R of vertices that have been reached but not searched and a set S of vertices that have been searched

The set R is maintained as a First-In First-Out list (queue), so the first vertices found are the first vertices explored

Page 66: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance66

Algorithm - Breadth First Search 2.3.8

Initialization: R={u}, S=, d(u, u)=0

Iteration:

1. As long as R, we search from the first vertex v of R

2. The neighbors of v not in SR are added to the back of R and assigned distance d(u, v)+1

3. Then v is removed from the front of R and placed in S

Page 67: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance67

Example of BFS

a

bc d

e f g h i j

Order of BFS: a, b, c, d, e, f, g, h, i, j, …

a, c, b, d, f, h, e, g, i, j, …

b, a, g, f, e, c, d, … h, i, j, …

Page 68: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance68

Breadth-F-S vs. Depth-F-S

a

bc d

e f g h i j

a

bc d

e f g h i j

a,b,c,d,e,f,g,h,i,j, … a,b,e,f, …c, h,…

Page 69: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance69

Application: Chinese Postman Problem 2.3.9

A mail carrier must traverse all edges in a road network, starting and ending at the Post Office.

– The edges has nonnegative weights representing distance or time.

– We seek a closed walk of minimum total length that uses all the edges

– This is the Chinese Postman Problem,• named in honor of the Chinese mathematician

Guan Meigu [1962], who proposed it.

Page 70: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance70

Application: Chinese Postman Problem continue 2.3.9

If every vertex is even, then the graph is Eulerian and the answer is the sum of the edge weights.

Otherwise, we must repeat edges. Every traversal is an Eulerian circuit of a graph obtained by duplicating edges.

Finding the shortest traversal is equivalent to finding the minimum total weight of edges whose duplication will make all vertex degrees even .

Page 71: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance71

Application: Chinese Postman Problem continue 2.3.9

We say “duplication” because we need not use an edge more than twice

If we use an edge three or more times in making all vertices even, then deleting two of those copies will leave all vertices even

There may be many ways to choose the duplicated edges

Page 72: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance72

Application: Chinese Postman Problem continue 2.3.9

If there are only two odd vertices, then – We can use Dijkstra’s Algorithm to find the

shortest path between them and solve the problem

If there are 2k odd vertices, then – Use Dijkstra’s algorithm to find the shortest paths

connecting each pair of odd vertices

– Use these lengths as weights on the edges of K2k

– Find the minimum total weight of k edges that pair up these 2k vertices. This is a maximum matching problem.

Page 73: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance73

Application: Chinese Postman Problem continue 2.3.9

• A vertex: An odd vertex • An edge between u and v: The shortest path between u and v in the given graph

16

20

40

15

20

10A matching in a general graph with minimum total weight

Page 74: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance74

Rooted Tree 2.3.11

A rooted tree is a tree with one vertex r chosen as root.

For each vertex v, let P(v) be the unique v, r-path.

– The parent of v is its neighbor on P(v);

– Its children are its other neighbors.

– Its ancestors are the vertices of P(v)-v.

– Its descendants are the vertices u such that P(u) contains v.

– The leaves are the vertices with no children.

– A rooted plane tree or planted tree is a rooted tree with a left-to–right ordering specified for the children of each vertex.

Page 75: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance75

Rooted Tree 2.3.11

root

v

Parent of v,Ancester of v

A leaf,A child of v

Page 76: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Binary Tree 2.3.12

A binary tree is a rooted plane tree where each vertex has at most two children, and each child of a vertex is designated as its left child or right child

The subtrees rooted at the children of the root are the left subtree and the right subtree of the tree

A k-ary tree allows each vertex up to k children

Graph Theory

Ch. 2. Trees and Distance76

Page 77: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance77

Huffman’s Algorithm 2.3.13

Input: Weights (frequencies or probabilities) p1,…,pn

Output: Prefix-free code (equivalently, a binary tree)

Idea: Infrequent items should have longer codes; put infrequent items deeper by combining them into parent nodes

Page 78: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance78

Huffman’s Algorithm 2.3.13

Initial case: When n=2, the optimal length is one, with 0 and 1 being the codes assigned to the two items (the tree has a root and two leaves; n=1 can also be used as the initial case)

Page 79: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance79

Huffman’s Algorithm 2.3.13

Recursion:

Replace the two least likely items p, p’ with a single item q of weight p+p’ when n>2

Treat the smaller set as a problem with n-1 items.

After solving it, give children with weights p, p’ to the resulting leaf with weight q. – Equivalently, replace the code computed for

the combined item with its extensions by 1 and 0, assigned to the items that were replaced.

Page 80: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance80

Example of Huffman coding 2.3.14

Consider eight items with frequencies 5, 1, 1, 7, 8, 2, 3, 6

Algorithm 2.3.13 combines items according to the tree on the left below, working from the bottom up

– First the two items of weight 1 combine to from one of weight 2

2

5 1 1 7 8 2 3 6

Page 81: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance81

Huffman coding 2.3.14

42

5 1 1 7 8 2 3 6

– Now this and the original item of weight 2 are the least likely and combine to form an item of weight 4.

Page 82: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance82

Huffman coding 2.3.14

2

5 1 1 7 8 2 3 6

42

5 1 1 7 8 2 3 6

7

4

2

5 1 1 7 8 2 3 6

7

114

2

5 1 1 7 8 2 3 6

Page 83: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance83

Huffman coding 2.3.14

33

19 14

7

11 4

2

5 1 1 7 8 2 3 6

6:101

8:11

5:100

7:01

3:001

2:0001

1:000011:00000

0 1

0

0

0

1

1

1

0 1

0

1

10

Page 84: Graph Theory Ch. 2. Trees and Distance 1 Chapter 2 Trees and Distance 2.1 Basic properties 2.2 Spanning tree and enumeration 2.3 Optimization and trees

Graph Theory

Ch. 2. Trees and Distance84

Theorem 2.3.15

Given a probability distribution {pi} on n items, Huffman’s Algorithm produces the prefix-free code with minimum excepted length