Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Chapter 9: Graphs Topological Sort
Chapter 9: Graphs Shortest Paths
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Greed is good. (Some of the time)
Breadth-First and Depth-First Search
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Fibonacci Heaps. Single Source All Destinations Shortest Paths
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Minimum spanning tree Prof Amir Geva Eitan Netzer.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Analysis of Algorithms
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Lecture ? The Algorithms of Kruskal and Prim
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Short paths and spanning trees
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree Neil Tang 3/25/2010
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE 373 Data Structures and Algorithms
Minimum-Cost Spanning Tree
Graphs.
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Lecture 12 Algorithm Analysis
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree Neil Tang 4/3/2008
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Priority Queues Binary Heaps
Chapter 9: Graphs Shortest Paths
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
Presentation transcript:

Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

2 Spanning Trees  Spanning Trees in Unweighted Graphs  Minimal Spanning Trees in Weighted Graphs - Prim’s Algorithm  Minimal Spanning Trees in Weighted Graphs - Kruskal’s Algorithm  Animation Animation

3 Definitions Spanning tree: a tree that contains all vertices in the graph. Number of nodes: |V| Number of edges: |V|-1

4 Spanning trees for unweighted graphs - data structures A table (an array) T size = number of vertices, Tv = parent of vertex v Adjacency lists A queue of vertices to be processed

5 Algorithm - initialization 1.Choose a vertex S and store it in a queue, set a counter = 0 (counts the processed nodes), and Ts = 0 (to indicate the root), Ti = -1,i  s (to indicate vertex not processed)

6 Algorithm – basic loop 2. While queue not empty and counter < |V| -1 : Read a vertex V from the queue For all adjacent vertices U : If Tu = -1 (not processed) Tu  V counter  counter + 1 store U in the queue

7 Algorithm – results and complexity Result: Root: S, such that Ts = 0 Edges in the tree: (Tv, V) Complexity: O(|E| + |V|) - we process all edges and all nodes

8 Example Table 0 // 1 is the root 1 // edge (1,2) 2 // edge (2,3) 1 // edge (1,4) 2// edge (2,5) Edge format: (Tv,V)

9 Minimum Spanning Tree - Prim's algorithm Given: Weighted graph. Find a spanning tree with the minimal sum of the weights. Similar to shortest paths in a weighted graphs. Difference: we record the weight of the current edge, not the length of the path.

10 Data Structures A table : rows = number of vertices, three columns: T(v,1) = weight of the edge from parent to vertex V T(v,2) = parent of vertex V T(v,3) = True, if vertex V is fixed in the tree, False otherwise

11 Data Structures (cont) Adjacency lists A priority queue of vertices to be processed. Priority of each vertex is determined by the weight of edge that links the vertex to its parent. The priority may change if we change the parent, provided the vertex is not yet fixed in the tree.

12 Prim’s Algorithm Initialization For all V, set first column to –1 (not included in the tree) second column to 0 (no parent) third column to False (not fixed in the tree) Select a vertex S, set T(s,1) = 0 (root: no parent) Store S in a priority queue with priority 0.

13 While (queue not empty) loop 1. DeleteMin V from the queue, set T(v,3) = True 2. For all adjacent to V vertices W: If T(w,3)= True do nothing – the vertex is fixed If T(w,1) = -1 (i.e. vertex not included) T(w,1)  weight of edge (v,w) T(w,2)  v (the parent of w) insert w in the queue with priority = weight of (v,w)

14 While (queue not empty) loop (cont) If T(w,1) > weight of (v,w) T(w,1)  weight of edge (v,w) T(w,2)  v (the parent of w) update priority of w in the queue remove, and insert with new priority = weight of (v,w)

15 Results and Complexity At the end of the algorithm, the tree would be represented in the table with its edges {(v, T(v,2) ) | v = 1, 2, …|V|} Complexity: O(|E|log(|V|))

16 Kruskal's Algorithm The algorithm works with : set of edges, tree forests, each vertex belongs to only one tree in the forest.

17 The Algorithm 1. Build |V| trees of one vertex only - each vertex is a tree of its own. Store edges in priority queue 2. Choose an edge (u,v) with minimum weight if u and v belong to one and the same tree, do nothing if u and v belong to different trees, link the trees by the edge (u,v) 3. Perform step 2 until all trees are combined into one tree only

18 Example Edges in Priority Queue: (1,2) 1 (3,5) 2 (2,4) 3 (1,3) 4 (4,5) 4 (2,5) 5 (1,5) 6

19 Example (cont) Forest of 5 trees Edges in Priority Queue: (1,2) 1 (3,5) 2 (2,4) 3 (1,3) 4 (4,5) 4 (2,5) 5 (1,5) 6 12 Process edge (1,2) Process edge (3,5)

20 Example (cont) Edges in Priority Queue: (2,4) 3 (1,3) 4 (4,5) 4 (2,5) 5 (1,5) 6 Process edge (2,4) Process edge (1,3) All trees are combined in one, the algorithm stops 3 2 5

21 Operations on Disjoint Sets Comparison: Are the vertices in the same tree Union: combine two disjoint sets to form one set - the new tree Implementation Union/find operations: the unions are represented as trees - nlogn complexity. The set of trees is implemented by an array.

22 Complexity of Kruskal’s Algorithm O(|E|log(|V|)Explanation: The complexity is determined by the heap operations and the union/find operations Union/find operations on disjoint sets represented as trees: tree search operations, with complexity O(log|V|) DeleteMin in Binary heaps with N elements is O(logN), When performed for N elements, it is O(NlogN).

23 Complexity (cont) Kruskal’s algorithm works with a binary heap that contains all edges: O(|E|log(|E|)) However, a complete graph has |E| = |V|*(|V|-1)/2, i.e. |E| = O(|V| 2 ) Thus for the binary heap we have O(|E|log(|E|)) = O(|E|log (|V| 2 ) = O(2|E|log (|V|) = O(|E|log(|V|))

24 Complexity (cont) Since for each edge we perform DeleteMin operation and union/find operation, the overall complexity is: O(|E| (log(|V|) + log(|V|)) = O(|E|log(|V|))

25 Discussion Sparse trees - Kruskal's algorithm : Guided by edges Dense trees - Prim's algorithm : The process is limited by the number of the processed vertices