Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309

Slides:



Advertisements
Similar presentations
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Greedy Algorithms Greed is good. (Some of the time)
CompSci 102 Discrete Math for Computer Science April 19, 2012 Prof. Rodger Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Minimal Spanning Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Minimal Spanning Trees Reading: Kolman, Section 7.5.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a.
GOLOMB RULERS AND GRACEFUL GRAPHS
Chapter 3 The Greedy Method 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
Discussion #36 Spanning Trees
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Graph Theory TreesAlgorithms. Graphs: Basic Definitions 4 n Let n be the number of nodes (stations) and e be the number of edges (links). n A graph is.
Chapter 9: Graphs Basic Concepts
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Let G be a pseudograph with vertex set V, edge set E, and incidence mapping f. Let n be a positive integer. A path of length n between vertex v and vertex.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
Chapter 2 Graph Algorithms.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Trees and Distance. 2.1 Basic properties Acyclic : a graph with no cycle Forest : acyclic graph Tree : connected acyclic graph Leaf : a vertex of degree.
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Chapter 9. Section 9.1 Binary Relations Definition: A binary relation R from a set A to a set B is a subset R ⊆ A × B. Example: Let A = { 0, 1,2 } and.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
CHAPTER 11 TREES INTRODUCTION TO TREES ► A tree is a connected undirected graph with no simple circuit. ► An undirected graph is a tree if and only.
Great Theoretical Ideas in Computer Science for Some.
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
COMPSCI 102 Introduction to Discrete Mathematics.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Paths and circuits.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Applied Discrete Mathematics Week 15: Trees
Basic Concepts Graphs For more notes and topics visit:
Chapter 5 : Trees.
Greedy Technique.
Trees Chapter 11.
Graph Algorithms Using Depth First Search
Chapter 9: Graphs Basic Concepts
Lectures on Graph Algorithms: searching, testing and sorting
And the Final Subject is…
Chapter 9: Graphs Basic Concepts
Presentation transcript:

Chapter 7. Trees Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University : Office : A309

School of Software  7.1. Trees  7.2. Labeled Trees  7.3. Tree Searching  7.4. Undirected Trees  7.5. Minimal Spanning Trees 2 Chapter seven: Trees

School of Software  Tree Let A be a set, and let T be a relation on A. We say that T is a tree if there is a vertex v 0 in A with the property that #1 : there exists a unique path in T from v 0 to every other vertex in A #2 : no path from v 0 to v 0 Usually, v 0 is called the root of the tree, and T is referred to as a rooted tree (denoted by (T, v 0 )) 7.1 Trees 3

School of Software  Theorem 1 Let (T,v 0 ) be a rooted tree. Then (a) There are no cycle in T (b) v 0 is the only root of T (c) Each vertex in T, other than v 0, has in-degree one, and v 0 has in-degree zero. Proof: (a) Suppose that there is a cycle q in T, beginning and ending at vertex v. By the definition of a tree, v is not v 0 and there is a path from v 0 to v. The q o p is a different path from v 0 to v, which is a contradiction of a tree. 7.1 Trees 4

School of Software  (b) If v 0 ’ is another root of T, there is a path from v 0 to v 0 ’ (since v 0 is a root) and a path q from v 0 ’ to v 0 (since v 0 ’ is a root). Then qop is a cycle from v 0 to v 0, which is a contradiction of a tree.  (c) Let w 1 be a vertex in T other than v 0. There is a unique path v 0, …, v k, w 1, from v 0 to w 1 in T. Thus (v k, w 1 ) in T, so w 1 has in-degree at least one. If the in-degree of w 1 is more than 1, then there must be distinct vertices w 2,w 3 such that (w 2,w 1 ) and (w 3,w 1 ) both in T. case #1: If w 2 & w 3 are not v 0, there are two different paths from v 0 to w 1 (v 0  w 2  w 1, v 0  w 3  w 1, contradiction) case #2: If w 2 or w 3 is v 0, e.g. w 2 =v 0, there also are two different paths from v 0 to w 1 (v 0  w 1, v 0  w 3  w 1 ) 7.1 Trees 5

School of Software  Levels, parent-offspring & siblings 7.1 Trees 6 v4v4 v0v0 v1v1 v2v2 v3v3 v5v5 v6v6 v7v7 v8v8 v9v9 Level 1 vertices: the vertices of the edges beginning at v 0 (level 0). Level k vertices: the vertices of the edges beginning at those level k-1 vertices Level 0 Level 1 Level 2 Parent-offspring: for all pairs (r 1, r 2 )in T, r 1 is called the parent of r 2, and r 2 is called the offspring of r 1 Siblings: the vertices that have the same parent Height of a tree: the largest level number of a tree. Leaves: the vertices that have no offsprings

School of Software  Theorem 2 Let (T,v 0 ) be a rooted tree on a set A. Then (a) T is irreflexive (b) T is asymmetric (c) If (a,b) in T and (b,c) in T, then (a,c) is not in T, for all a, b and c in A. The proof is left as an exercise. 7.1 Trees 7

School of Software  Example 1 Let A be the set consisting of a given woman v 0 and all of her female descendants. We now define the following relation T on A: If v 1 and v 2 are elements of A, then v 1 T v 2 if and only if v 1 is the mother of v 2. The relation T on A is a rooted three with root v Trees 8

School of Software  Example 2 Let A={v 1,v 2,…v 10 } and let T={(v 2,v 3 ), (v 2,v 1 ), (v 4,v 5 ), (v 4,v 6 ), (v 5,v 8 ), (v 6,v 7 ), (v 4,v 2 ), (v 7,v 9 ), (v 7,v 10 )}. Show that T is a rooted tree and identify the root. 7.1 Trees 9 v4v4 v6v6 v2v2 v5v5 v7v7 v9v9 v 10 v1v1 v3v3 v8v8 Root

School of Software  n-tree If n is a positive integer, we say that a tree is an n-tree if every vertex has at most n offspring. In particular, a 2-tree is called a binary tree.  Complete n-tree If all vertices of T, other than the leaves, have exactly n offspring, we say that T is a complete n-tree. A complete 2- tree is called a completed binary tree. 7.1 Trees 10

School of Software  Let (T,v 0 ) be a rooted tree on the set A, and let v be a vertex of T. Let B be the set consisting of v and all its descendants, i.e., all vertices of T that can be reached by a path beginning at v. Observe that B ⊆ A. Let T(v) be the restriction of the relation T to B, that is T ∩ (B × B). Delete all vertices that are not descendants of v and all edges that do not begin and end at any such vertex. 7.1 Trees 11 v4v4 v6v6 v2v2 v5v5 v7v7 v9v9 v 10 v1v1 v3v3 v8v8 v5v5 v8v8 T(v 5 ) v2v2 v1v1 v3v3 T(v 2 ) v6v6 v7v7 v9v9 v 10 T(v 6 )

School of Software  Theorem 3 If (T,v 0 ) is a rooted tree and v in T, then T(v) is also a rooted tree with root v. We will say that T(v) is the subtree of T beginning at v. Proof: By the definition of T(v), there is a path from v to every other vertex in T(v). If there is a vertex w in T(v) such that there are two distinct paths q and q’ form v to w, and if p is the path in T from v 0 to v, then qop and q’op would be two distinct paths in T form v 0 to w (contradiction!). Thus each path from v to another vertex w in T(v) must be unique. if q is a cycle at v in T(v), then q is also a cycle in T (contradiction!). Thus q cannot exist. Therefore, T(v) is also a rooted tree with root v. 7.1 Trees 12

School of Software  Homework Ex. 4, Ex. 10, Ex. 14, Ex. 20, Ex. 26, Ex. 27, Ex Trees 13

School of Software  Use a tree to denote the following algebraic expression (3 - ( 2 × x ) ) + ( (x – 2 ) - ( 3 + x ) ) 7.2 Labeled Trees xx2 3x x represent the vertices simply as dots 2. show the label of each vertex next to the dot representing that vertex Here leaves denote the numbers or variables

School of Software  Positional n - tree n-tree: every vertex has at most n offspring. label the offspring of a given vertex from left to right with numbers 1,2 …n 7.2 Labeled Trees L L L L L L L L R R R RR R R R R positional 3-tree positional binary tree

School of Software  Huffman code trees A variable-length code widely used in lossless data compression. Base Idea: the more frequently used letters are assigned shorter strings. 7.2 Labeled Trees R C S A E Decode the string: : E 10: A 110: S 0: E

School of Software  Homework Ex. 8, Ex. 14, Ex. 16, Ex. 25, Ex Labeled Trees 17

School of Software  Visiting Performing appropriate tasks at a vertex will be called visiting the vertex.  Tree search The process of visiting each vertex of a tree in some specific order will be called searching the tree or performing a tree search. 7.3 Tree Searching 18

School of Software  Algorithm PREORDER Step 1: visit v Step 2: If v L exists, then apply this algorithm to (T(v L ), v L ). Step 3: If v R exists, then apply this algorithm to (T(v R ), v R ) Recursion: Recursion is the process of repeating items in a self-similar way. Refer to Tree Searching 19

School of Software  Example Tree Searching 20 A B C DF E GJ I L K H ABCDEFGHIJKL

School of Software  Example 2 (a – b) × (c + ( d / e) ) 7.3 Tree Searching 21 × - ab e / + c d ×—a b +c/de

School of Software  Prefix or Polish form × - a b + c / d e (a=6, b=4, c=5, d=2, e=2) 1. × - / × / 2 2 replacing by 2 since 6-4=2 3. × replacing / 2 2 by 1 since 2/2=1 4. × 2 6 replacing by 6 since 5+1= replacing×2 6 by12 since 2×6= Tree Searching 22

School of Software  Algorithm INORDER Step 1: Search the left subtree (T(v L ), v L ), if it exists; Step 2: Visit the root v; Step 3: Search the right subtree (T(v R ), v R ), if it exists.  Algorithm POSTORDER Step 1: Search the left subtree (T(v L ), v L ), if it exists; Step 2: Search the left subtree (T(v R ), v R ), if it exists; Step 3: Visit the root v. 7.3 Tree Searching 23

School of Software  Traveling the tree using INORDER & POSTORDER 7.3 Tree Searching 24 × - ab e / + c d INORDER: a - b × c + d / e (a – b) × (c + ( d / e) ) POSTORDER: a b – c d e / + ×

School of Software  Infix notation a - b × c + d / e (a – b) × (c + ( d / e) ) or a – (b × ((c + d) / e) )  Postfix or reverse polish a b – c d e / + × (if a=2,b=1,c=3,d=4,e=2) – / + × / + × replacing 2 1 – with 1 since 2-1= × replacing 4 2 / with 2 since 4/2= × replacing with 5 since 3+2= replacing 1 5× with 5 since 1×5 =5 7.3 Tree Searching 25

School of Software  Let T be any order tree and A be the set of vertices in T Define a binary positional tree B(T) on A: if v in A, the left offspring v L of v in B(T): the first offspring of v in T, if exists. the right offspring v R of v in B(T): the next sibling of v in T, if exists 7.3 Tree Searching

School of Software  Homework Ex. 16, Ex. 17, Ex. 18, Ex. 24, Ex. 26, Ex. 32, Ex Tree Searching 27

School of Software  Undirected Tree An undirected tree is simply the symmetric closure of a tree, that is, it is the relation that results from a tree when all the edges are made bidirectional. 7.4 Undirected Trees 28 b c e a f d g An undirected diagram T abef c d g An ordinary tree T 2 Note: T is the symmetric closure of T1 and T2. Thus T is a undirected tree. a d g f e b An ordinary tree T 1 c

School of Software  Simple Let R be a symmetric relation, and let p: v 1,v 2,…v n be a path in R. we say p is simple if no two edges of p correspond to the same undirected edge. If, in addition, v 1 equals v n, we will call p a simple cycle.  Acyclic A symmetric relation R is acyclic if it contains no simple cycles.  Connected A symmetric relation R is connected if there is a path in R from any vertex to others. 7.4 Undirected Trees 29

School of Software  Example 2 Path: a, b, c, e, d Path: f, e, d, c, d, a Path: f, e, a, d, b, a, f & d, a, b, d Path: f, e, d, c, e, f 7.4 Undirected Trees 30 a f b e d c Simple Not simple, since d, c & c, d correspond to the same undirected edge. Simple cycles Not a simple cycle

School of Software  Theorem 1 Let R be a symmetric relation on a set A. Then the following statements are equivalent. (a) R is an undirected tree (b) R is connected and acyclic 7.4 Undirected Trees 31

School of Software  Theorem 2 Let R be a symmetric relation on a set A. Then R is an undirected tree if and only if either of the following statements is true (a) R is acyclic, and if any undirected edge is added to R, the new relation will not be acyclic; (b) R is connected, and if any undirected edge is removed from R, the new relation will not be connected. 7.4 Undirected Trees 32

School of Software  Theorem 3 A tree with n vertices has n-1 edges Proof: Because a tree is connected, there must be at least n-1 edges to connect the n vertices. Support that there are more than n-1 edges. Then either the root has in-degree 1 or some other vertex has in-degree at least 2. But by Theorem 1, Section 7.1, this is impossible. Thus there are exactly n-1 edges. 7.4 Undirected Trees 33 Theorem 1 (c) Each vertex in T, other than v 0, has in-degree one, and v 0 has in-degree zero.

School of Software  Spanning Tree If R is a symmetric, connected relation on a set A, we say that a tree T on A is a spanning tree for R if T is a tree with exactly the same vertices as R and which can be obtained from R by deleting some edges of R. For instance, 7.4 Undirected Trees 34 a b c d e f (a) R a b c d e f (b) T’ a b c d e f (c) T’’

School of Software  Undirected spanning Tree An undirected spanning tree is the symmetric closure of a spanning tree, and it is useful in some applications. 7.4 Undirected Trees 35 a b c d e f (c) T’’ a b c d e f (c) Undirected Tree

School of Software  Example Undirected Trees 36 a b c d e f (a) a b c d e f (b) a b c d e f (e) a b c d e f (c) a b c d e f (d) a b c d e f (f) Theorem 2(b) & Theorem 3 suggests a simple algorithm for finding an undirected spanning tree for a symmetric relation R.

School of Software  Merging the vertices Let R be a relation on a set A, and let a, b in A. Let A 0 =A- {a, b}, and A’=A 0 U {a’}, where a’ is some new element not in A. Define a relation R’ on A’ as follows. Support u, v in A’, u and v are not a’. Let (a’, u) in R’ iff (a,u) in R or (b,u) in R (u, a’) in R’ iff (u,a) in R or (u,b) in R (u, v) in R’ iff (u,v) in R We say that R’ is a result of merging the vertices a and b 7.4 Undirected Trees 37

School of Software  Example Undirected Trees 38 v0v0 v1v1 v2v2 (a) R v6v6 v5v5 v4v4 v3v3 v’ 0 v2v2 (b) Merging v 1 & v 0 into v’ 0 v6v6 v5v5 v4v4 v3v3 v’’ 0 v6v6 (c) Merging v’ 0 & v 2 into v’’ 0 v5v5 v4v4 v3v3

School of Software Suppose that vertices a and b of a relation R are merged into a new vertex a’ that replaces a and b to obtain the relation R’. To determine the matrix of R’, we proceed as follows.  Step 1: Let row i represent vertex a and row j represent vertex b. Replace row i by the join of rows i and j. Note: The join of two n-tuples of 0’s and 1’s has 1 in some position exactly when either of those two n-tuples has a 1 in that position.  Step 2: Replace column i by the join of columns i and j  Step 3: Restore the main diagonal to its original values in R  Step 4: Delete row j and column j. 7.4 Undirected Trees 39

School of Software  Example Undirected Trees 40 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v’ 0 v2v2 v3v3 v4v4 v5v5 v6v6 v2v2 v3v3 v4v4 v5v5 v6v6 v ’’0 v3v3 v4v4 v5v5 v6v6 v3v3 v4v4 v5v5 v6v6 (a)(b)(c)

School of Software The Prim’s algorithm for finding a spanning tree for a symmetric, connected relation R on A={v 1,v 2,…,v n }  Step 1: Choose v 1 of R, and arrange the matrix of R so that the first row corresponds to v 1  Step 2: Choose a vertex v 2 of R such that (v 1,v 2 ) in R, merge v 1 and v 2 into a new vertex v’ 1, representing {v 1,v 2 }, and replace v 1 by v’ 1. compute the matrix of R’. Call the vertex v’ 1 a merged vertex.  Step 3: Repeat Step 1& 2 on R’ until a relation with a single vertex is obtained. At each stage, keep a record of the set of original vertices that represented by each merged vertex.  Step 4: Construct the spanning tree. At each stage, when merging vertices a & b, select an edge in R from one of the original vertices represented by a to one of the original vertices represented by b. 7.4 Undirected Trees 41

School of Software  Example Undirected Trees 42 ab cd ba c d Matrix Original & merged vertices New vertex to be merged a b cd a b c d a’ b d b da’’ d d c a’  {a, c} b d a’’  {a, c, b} a’’’ a’’’  {a, c, d, b}

School of Software  Homework Ex. 6, Ex. 12, Ex. 14, Ex. 16, Ex. 21, Ex Undirected Trees 43

School of Software  Weighted graph A weighted graph is a graph for which each edges is labeled with a numerical value called its weight. For instance vertices: towns edges: the distance between two connected towns 7.5 Minimal Spanning Trees 44 BD C A F G H E

School of Software  Example Minimal Spanning Trees 45 B C IA H G J F E D Vertices: relay stations Edges: the cost for upgrading 4.4

School of Software  Distance between vertices The weight of an edge (v i,v j ) is sometimes referred to as the distance between vertices v i and v j.  Nearest neighbor A vertex u is nearest neighbor of vertex v if u and v are adjacent and no other vertex is joined to v by an edge of lesser weight than (u, v) (Note: may have more than one nearest neighbor) 7.5 Minimal Spanning Trees 46

School of Software  Example Minimal Spanning Trees 47 BD C A F G H E The nearest neighbor of A is C The nearest neighbor of F is E & G 3

School of Software  The nearest neighbor of a set of vertices A vertex v is a nearest neighbor of a set vertices V={v 1,v 2,…,v k } in a graph if v is adjacent to some member v i of V and no other vertex adjacent to a member of V is joined by an edge of lesser weight than (v,v i ). Note: This vertex v may belong to V 7.5 Minimal Spanning Trees 48 The nearest neighbor of {C,E,J} is D. What is the nearest neighbor of {D,E,F}? Answer: D & F B C IA H G J F E D

School of Software  Minimal spanning tree A n undirected spanning tree in which the total weight of the edges is as small as possible, is called a minimal spanning tree. Prim’s algorithm (Section 7.4, p. 293) can easily be adapted to produce a minimal spanning tree for a weighted graph by using the greedy algorithm. Greedy algorithm makes the locally optimal choice at each stage, and hopes to find the global optimum Minimal Spanning Trees 49

School of Software Algorithm: Prim’s ALGORITHM  Step 1: Choose a vertex v 1 of R. Let V={v 1 } and E={};  Step 2: Choose a nearest neighbor v i of V that is adjacent to v j, v j in V, and for which the edge (v i, v j ) does not form a cycle with members of E. Add v i to V and add (v i, v j ) to E.  Step 3: Repeat Step 2 until |E|=n-1. Then V contains all n vertices of R, and E contains the edges of a minimal spanning tree for R. 7.5 Minimal Spanning Trees 50

School of Software  Theorem 1 Prim’s algorithm produces a minimal tree for the relation. Proof … 7.5 Minimal Spanning Trees 51

School of Software  Example Minimal Spanning Trees 52 BD C A F G H E BD C A F G H E Kilometers

School of Software  Example Minimal Spanning Trees 53 B C IA H G J F E D

School of Software  Weight graph & Matrix representation 7.5 Minimal Spanning Trees 54 A B C D A BCD A B C D

School of Software Algorithm PRIM’S ALGORITHM (Matrix version) Let R be a symmetric, connected relation with n vertices and M be the associated matrix of weights.  Step 1 Choose the smallest entry in M, say m ij. Let a be the vertex that is represented by row I and b the vertex represented by column j.  Step 2 Merge a with b as follows: Replace row i with 7.5 Minimal Spanning Trees 55

School of Software Replace column i with Replace the main diagonal with the original entries of M. Delete row j and column j. Call the resulting matrix M’.  Step 3 Repeat Step 1 & 2 on M’ and subsequent matrices until a single vertex is obtained. At each stage, keep a record of the set of original vertices that is represented by each merged vertex. 7.5 Minimal Spanning Trees 56

School of Software  Step 4 Construct the minimal spanning tree as follows: At each stage, when merging vertices a and b, select the edge represented by the minimal weight from one of the original vertices represented by a to one of the original vertices represented by b. 7.5 Minimal Spanning Trees 57

School of Software  Example Minimal Spanning Trees 58 Either 2 may be selected as m ij. We choose m 3,4 and merge C and D, This produces A A B C D BCD A A B C’ B With C’  {C,D} and the first edge (C,D), Repeat 1 and 2 On M’ using m 1,3 =3. This gives A’ B B With A’  {A,C,D} and the edge (A,C) A B C D A final merge yields the edge (B,D)

School of Software Algorithm KRUSKAL’S ALGORITHM Let R be a symmetric, connected relation with n vertices and let S={e 1,e 2,…,e k } be the set of weighted edges of R.  Step 1 Choose an edge e 1 in S of least weight. Let E={e 1 }. Replace S with S-{e 1 }  Step 2 Select an edge e i in S of least weight that will not make a cycle with members of E. Replace E with E U {e i } and S with S-{e i }  Step 3 Repeat Step 2 until |E|=n Minimal Spanning Trees 59

School of Software  Example Minimal Spanning Trees 60 BD C A F G H E Kilometers

School of Software  Example Minimal Spanning Trees 61 B A F C E 20 D G

School of Software  Homework Ex. 4, Ex. 8, Ex. 13, Ex. 15, Ex. 20, Ex Minimal Spanning Trees 62