Spanning Tree
Spanning Tree
Graphs
Minimum Spanning Tree
Key Points - Lecture 19
• Dynamic Algorithms
• Optimal Binary Search Tree
• Used when
• some items are requested more often than others
• frequency for each item is known
• Minimises cost of all searches
• Build the search tree by
• Considering all trees of size 2, then 3, 4, ....
• Larger tree costs computed from smaller tree costs
• Sub-trees of optimal trees are optimal trees!
• Construct optimal search tree by saving root of each optimal sub-tree and tracing back
• O(n3) time / O(n2) space
Key Points - Lecture 19
• Other Problems using Dynamic Algorithms
• Matrix chain multiplication
• Find optimal parenthesisation of a matrix product
• Expressions within parentheses
• optimal parenthesisations themselves
• Optimal sub-structure characteristic of dynamic algorithms
• Similar to optimal binary search tree
• Longest common subsequence
• Longest string of symbols found in each of two sequences
• Optimal triangulation
• Least cost division of a polygon into triangles
• Maps to matrix chain multiplication
Graphs - Definitions
• Graph
• Set of vertices (nodes) and edges connecting them
• Write
G = ( V, E )
where
• V is a set of vertices: V = { vi }
• An edge connects two vertices: e = ( v i , vj )
• E is a set of edges: E = { (vi , vj ) }
Vertices
Edges
Graphs - Definitions
• Path
• A path, p, of length, k, is a sequence of connected
vertices
• p = <v0,v1,...,vk> where (vi,vi+1) E
< i, c, f, g, h >
Path of length 5
< a, b >
Path of length 2
Graphs - Definitions
• Cycle
• A graph contains no cycles if there is no path
• p = <v0,v1,...,vk> such that v0 = vk
< i, c, f, g, i >
is a cycle
Graphs - Definitions
• Spanning Tree
• A spanning tree is a set of |V|-1 edges that connect all
the vertices of a graph
• Cycle determination
Kruskal’s Algorithm
• Cycle determination
• If two vertices have the same representative,
they’re already connected and adding a further
connection between them is pointless
• Procedure:
• For each end-point of the edge that you’re going to
add
• follow the lists and find its representative
• if the two representatives are equal,
then the edge will form a cycle
Kruskal’s Algorithm in operation
Choose c as its
representative
Choose b as its
representative
The rep of d is d
The rep of h is c
The rep of i is c
The rep of a is b
The rep of h is c