Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu Department of Computer Science Aalborg University Fall 2007
This Lecture Minimum spanning trees Definitions Kruskal s algorithm Prim s algorithm Example Course conclusion 2
Spanning Tree Revisit Given a graph G=(V, E), a spanning tree of it is a subgraph of G which Covers all vertices No vertex is omitted Forms a tree No cycle is allowed T=(V, E ) E E We have seen BFS spanning tree DFS spanning tree Shortest path spanning tree We will see minimum spanning tree 3
Motivation Example Given a number of cities, connect them by an electric power network of minimum cost Each city is a vertex Whenever a direct link between two cities is feasible, the corresponding vertices are connected by an edge e of cost c(e) The total cost of links really deployed is to be minimized E.g. a 2 d 1 2 4 Minimum spanning tree/forest How to find such a solution? b e 3 6 5 3 c f 5 a 2 d 1 2 b e 3 3 c f 4
General Principle G=(V, E) is a connected graph; X, Y are two sets of edges Loop Invariant: T=(V, X), X E, X Y=E There exists a minimum spanning tree T of G which contains all edges of X, but none of the edges of Y. Idea: add edges to X while maintaining the invariant Theorem E.g. If X and Y obey the loop invariant, C is a connected component of G. Let J(C)={{u,v} E-Y u C and v C}. Suppose e is a minimum cost edge in J(C), then X {e} and Y also obey the loop invariant. X={{a,b}, {b,c}} Y={} C covers a, b, c J(C)={{a,d},{a,e},{b,e},{c,e},{c,f}} a 2 d 1 2 b e c f a 2 d 1 2 4 b e 3 6 5 3 c f 5 5
Kruskal s Algorithm Input: connected graph G=(V,E) X:=Ø; Y:=Ø; Q:=E while Q Ø do Take an edge {u, v} of minimum cost out of Q if u and v are in different connected components of (V, X) then else X:=X {{u,v}} Y:=Y {{u,v}} return (V, X) Q should be a priority queue to ease the operation Can be used to find minimum spanning forests Different components are tried 6
Prim s Algorithm Idea: Loop invariant + staying within one connected component throughout the algorithm X:=Ø; Y:=Ø; Q:=Ø // set of edges out of the component C pick a V; insert all edges <a, w> into Q // treated as directed while Q Ø do remove from Q edge <u, v> with minimum cost insert {u, v} into X for each vertex w adjacent to v do if w is not visited then insert <v, w> into Q else if a path connects w to a in (V, X) then do nothing else // there must be an edge <x, w> Q if c(v, w) < c(x, w) then replace <x, w> in Q by <v, w> insert {x, w} into Y else insert <v, w> into Y 7
Example Kruskal Prim Pick a first Pick b first Pick c first Pick d first a 15 d 11 2 b 10 8 9 12 e c Pick e first 8
Algorithm Complexity Kruskal Dominant cost: insert and delete m edges into and out of priority queue q Prim O(m log m) for any implementation of priority queue Similar to Dijkstra s algorithm delete from priority queue: n times insert into priority queue: n times distance update: m times Total: O(n log n + m) Sparse graphs m=θ(n) Dense graphs m=θ(n 2 ) Kruskal O(n log n) O(n 2 log n) Prim O(n log n) O(n 2 ) 9
Course Conclusion Algorithmic problems Algorithms Data structures The expectation of attending course is to get prepared for attacking algorithmic problems The tools we learn in this course are algorithms and data structures They are closely correlated to each other Choose proper data structures and algorithms based on the characteristics of given problems To analyze the complexity of your own algorithm 10
Algorithmic Problems Easy (polynomial time complexity) Binary search Sorting Shortest path Minimum spanning tree Hard (exponential time complexity) Traveling salesperson problem Unsolvable Halting problem 11
Traveling Salesperson Problem Given a number of cities and the costs of traveling from any city to any other city, what is the least-cost round-trip route that visits each city exactly once and then returns to the starting city? All known algorithms are exponential 12
Abstract Data Type (ADT) ADTs are used to describe algorithms in an understandable way. Any ADT has two sides Operations Implementation Interface for users X Built by creators For a user, you can see and use the operations only Behaviors of operations are specified using pre- and postconditions One ADT can have many different implementations with different complexities As a user, we can analyze our algorithms without knowing the implementation details of the ADTs we use 13
Topics in the Course (1) Introduction Search in sequences Sequential scan vs. binary search Correctness of algorithms Partial vs. total correctness Pre- and post-conditions Iteration (assertions) vs. recursion (induction) Efficiency of algorithms Time and space complexity Worst case vs. average case Asymptotic complexity (O, Ω, Θ notations) Recurrence equations Repeated substituion Proof by math induction 14
Topics in the Course (2) Basic abstract data types Set, List, Stack, Queue Sorting Bubble, Selection, Insertion, Merge, Quick Radix sort Trees, binary trees, binary search trees Properties, ADT, traversals Symbol tables Different implementations Hashing Priority queue Special operations Used in graph algorithms 15
Topics in the Course (3) Algorithm design techniques Incremental Simple recursive, backtracking algorithms Divide-and-conquer Dynamic programming Greedy algorithms Graph Properties, Digraph ADT Adjacent matrix vs. adjacent list implementations Topological sorting, critical paths BFS, DFS Shortest paths (Bellman-Moore alg., Dijkstra s alg.) Minimum spanning trees (Kruskal s alg., Prim s alg) 16
Preparation for the Exam You are allowed to take all lecture notes, exercises and textbooks to the exam Go over each lecture one by one Read the relevant portions in the textbook or references Go through slides, understand and digest the contents No need to memorize algorithm codes, but do remember the ideas/rationales behind Re-solve by yourself exercise questions and check your solutions with those given online Try to solve some problems from exams of previous years Available online You are welcome to approach me if you get any problem about this course and the exam 17