CS 310 Advanced Data Structures and Algorithms

Size: px
Start display at page:

Download "CS 310 Advanced Data Structures and Algorithms"

Transcription

1 CS 0 Advanced Data Structures and Algorithms Weighted Graphs July 0, 07 Tong Wang UMass Boston CS 0 July 0, 07 /

2 Weighted Graphs Each edge has a weight (cost) Edge-weighted graphs Mostly we consider only positive weights Minimum spanning trees Shortest paths Tong Wang UMass Boston CS 0 July 0, 07 /

3 Minimum Spanning Tree Given a connected undirected graph, we often want the cheapest way to connect all the nodes This is a minimum cost spanning tree A spanning tree is a tree that contains all the nodes in the graph The total cost is just the sum of the edge costs The minimum spanning tree is the spanning tree whose total cost is minimal among all spanning trees The kind of tree involved here has no particular root node, and such trees are called free trees, because they are not tied down by a root Tong Wang UMass Boston CS 0 July 0, 07 /

4 MST Example Point set in the plane Geometric spanning trees The middle one is the minimum spanning tree The one on the right has shortest paths to the central node Tong Wang UMass Boston CS 0 July 0, 07 /

5 MST Example CLRS. Tong Wang UMass Boston CS 0 July 0, 07 /

6 Prim s Algorithm for MST A greedy algorithm: Decide what to do next by selecting the immediately best option, without regard to the global structure Pseudocode Choose an arbitrary vertex s as the first tree node Add s to PrimTree while there are still nontree nodes Choose the edge of minimum weight between a tree node and a nontree node, (u, v) Add (u, v) to TreeEdge and v to PrimTree Tong Wang UMass Boston CS 0 July 0, 07 6 /

7 Prim s Algorithm for MST Choose an arbitrary node s Initialize tree T = { s } Initialize E = { } Repeat until all nodes are in T: Choose an edge (u, v) with minimum weight such that u is in T and v is not Add v to T, add (u, v) to E Output (T, E) Tong Wang UMass Boston CS 0 July 0, 07 7 /

8 Correctness of Prim s Algorithm Prim s alogrithm creates a spanning tree No cycle can be introduced by adding edges between tree and nontree vertices Why does the tree has the minimal weight? Proof by contradiction Assume Prim s algorithm does not return an MST Then there is an edge (x, y) that does not belong to the MST Instead, (x, y) is replaced by a different edge (v, ) To maintain the property of spanning, when we remove an edge, we must add another edge The cost of (x, y), however, is not greater than the cost of (v, ) Thus Prim s tree is still minimal after all Tong Wang UMass Boston CS 0 July 0, 07 8 /

9 Correctness of Prim s Algorithm If we remove (x, y), a different edge must be added In the new spanning tree, let (v, ) be the first edge along the path from x to y that is not in the tree generated by Prim s algorithm Cost(x, y) Cost(v, ) Substitute (x, y) for (v, ) back into the tree Repeat until the tree by Prim s algorithm is restored Tong Wang UMass Boston CS 0 July 0, 07 9 /

10 Naive Implementation of Prim s Algorithm Prim s algorithm grows the MST in stages, one vertex at a time Thus n iterations During each iteration, search all edges and find the minimum weight edge with one vertex in the tree and the other vertex not in the tree Thus each iteration takes O(m) time O(m n) total time Tong Wang UMass Boston CS 0 July 0, 07 0 /

11 Better Implementation of Prim s Algorithm Maintain an array Dist that stores the distances from nontree nodes to the nearest tree nodes Initialize Dist to MAXINT When vertex u is added to the tree, loop through its adjacency list Consider edge (u, v) If v is already a tree node, ignore it If v is not a tree node, and if Cost(u, v) < Dist[v] Dist[v] = Cost(u, v) parent[v] = u After updating Dist, find the node with the minimum distance to be the next tree node O(n ) total time Further improvement: Use a priority queue, so that the node with the minimum distance can be found in O(log n) time, for a total time of O(m log n) Tong Wang UMass Boston CS 0 July 0, 07 /

12 Kruskal s algorithm This algorithm works like building the Huffman code tree Start with all the nodes separate and then join them incrementally, using a greedy approach that turns out to give the optimal solution Set up a partition, a set of sets of nodes, starting with one node in each set Then find the minimal edge to join two sets, and use that as a tree edge, and join the sets Each set in the partition is a connected component Tong Wang UMass Boston CS 0 July 0, 07 /

13 Example v v 8 v 0 6 v v v v v Tong Wang UMass Boston CS 0 July 0, 07 /

14 Example v v v v v v v v v v v v v v v Tong Wang UMass Boston CS 0 July 0, 07 / v

15 Example v v v v v v v 6 v v v v v v v 7 v 8 Tong Wang UMass Boston CS 0 July 0, 07 / v

16 Running Example The component numbers for the first few steps: Node V 0 V V V V V V 6 Set 0 6 V 0 -V ( joins 0) V -V (6 joins ) V 0 -V V -V and so on Edges are used in cost order If an edge has both to and from vertices with the same component number, it is skipped The resulting tree is defined by the edges used Tong Wang UMass Boston CS 0 July 0, 07 6 /

17 Correctness of Kruskal s Algorithm The algorithm adds n edges without creating a cycle Thus it must create a spanning tree Why does the tree has the minimal weight? Proof by contradiction Assume Kruskal s algorithm does not return an MST There is a real MST T min Then there is an edge (x, y) in the tree by Kruskal s that is not in T min Insert (x, y) in T min and create a cycle with the path P from x to y When Kruskal s adds the edge (x, y), x and y are in different components That is, one of the edges in path P is not there yet; let it be (v, ) This means Cost(x, y) Cost(v, ) Tong Wang UMass Boston CS 0 July 0, 07 7 /

18 Correctness of Kruskal s Algorithm If we add (x, y) to T min, a cycle is formed The edge (v, ) is added after (x, y) Thus Cost(x, y) Cost(v, ) By replacing (v, ) with (x, y), we would not increase the cost Repeat until T min is transformed into the tree by Kruskal s Tong Wang UMass Boston CS 0 July 0, 07 8 /

19 Kruskal s Algorithm Note that several edges are left unprocessed in the edge list These are the high-cost edges we want to avoid using Pseudocode: Put the edges in a priority queue ordered by edge weight Initialize the tree edge set T to the empty set Repeat n times Get next edge (u, v) from PQ If component[u]!= component[v] Add (u, v) to T Merge component[u] and component[v] In each iteration, test the connectivity of two trees plus an edge This can be done with BFS or DFS on a sparse graph with at most n edges and n vertices. Thus each iteration takes O(n) time O(m n) total time If we can determine the connectivity of two trees plus an edge in O(log n) time, then the total time for Kruskal s algorithm is O(m log n) Tong Wang UMass Boston CS 0 July 0, 07 9 /

20 Data Structures for Kruskal s Algorithm Need two operations SameComponent(u, v): returns true or false MergeComponent(C, C): returns the merged component Best data structure: Union-Find Tong Wang UMass Boston CS 0 July 0, 07 0 /

21 The Union-Find Data Structure Let n be the number of elements in a set A partition divides the elements into a collection of disjoint subsets Examples: SCC, vertex coloring, vertices in Kruskal s algorithm Each subset in the partition is represented by one of its elements The operation Find(u) returns the representative of the subset that u belongs to The operation Union(u, v) joins the subsets containing u and v into a new subset Tong Wang UMass Boston CS 0 July 0, 07 /

22 The Union-Find Data Structure vertex 6 7 parent rank size find(x) if (parent(x) == x) return x else return find(parent(x)) Each subset is represented by a backwards tree, where each child node has a pointer to its parent The rank of a tree is (without path compression) its height Initially, each vertex of the graph is in its own tree, and its parent pointer points to itself Find(i): Follow the parent pointers upwards until reaching a node that points to itself; return the label of this node as the label of the subset Union(i, j): Use the root of the higher-ranked tree as the root of the union it becomes the parent of the root of the lower-ranked tree Tong Wang UMass Boston CS 0 July 0, 07 /

23 Runtime Analysis of Union-Find Runtime of Union and Find depends on the heights of the trees When two trees of rank r are merged, the rank of the resulting tree is r + This is the only way to increase the rank How high can the trees become? The smallest tree of rank 0 has node The smallest tree of rank has nodes Merging a rank-0 tree to a rank- tree is still a rank- tree Merging two rank- trees gives a rank- tree The smallest tree of rank has nodes The smallest tree of rank k has k nodes by induction The maximum height is O(log n) O(log n) time for union or find Tong Wang UMass Boston CS 0 July 0, 07 /

24 Union-Find with Path Compression Path compression find(x) if (parent(x)!= x) parent(x) = find(parent(x)) return parent(x) Amortized runtime per operation of union-find with path compression is O() Tong Wang UMass Boston CS 0 July 0, 07 /

25 Back to Kruskal s Algorithm Initialize all vertices as singleton trees for Union-Find Keep all edges in a priority queue Loop through the edges out of PQ (at most m iterations) For edge (u, v), use Union-Find to determine whether u and v are in separate trees O(log n) time If u and v are in the same tree, move on to the next edge If not, join the two trees Total runtime O(m log n) Tong Wang UMass Boston CS 0 July 0, 07 /

26 Unweighted Single-Source Shortest-Path Problem Find the shortest path (measured by number of edges) from a designated vertex s to every vertex A special case of the weighted shortest-path problem by giving weight to all edges This problem has an efficient solution: BFS The problem becomes more complicated when (different) weights are allowed Tong Wang UMass Boston CS 0 July 0, 07 6 /

27 Positive-Weighted, Single-Source, Shortest-Path Problem Many times the edges have weights indicating that the relations between nodes are not always equal Example: distances between cities In this case BFS does not work Goal: Find the shortest path (measured by total cost) from a designated vertex s to every vertex in a weighted graph Weights (costs) are assigned to edges All edge costs are positive Tong Wang UMass Boston CS 0 July 0, 07 7 /

28 Dijkstra s Algorithm A greedy technique that works well Let weight(u, v) be the cost for edge (u, v) If the shortest path from s to t passes through x, then the path from s to x must be shortest as well Starting from vertex s, we set up an array to hold all the running min costs D i from s to vertex i Being greedy, we visit a new node v with the least cost from s that has not been visited yet Then we update the min cost D i by passing through v D i = min{d i, D v + weight(v, i)} Repeat by visiting the next node with the lowest cost D i that has not been visited Tong Wang UMass Boston CS 0 July 0, 07 8 /

29 Running Example, Starting From V 0 0 v v 8 v 0 6 v 0 v v 8 v 0 6 v 0 9 v v 8 v 0 6 v 0 9 v v 8 v 0 6 v Tong Wang UMass Boston CS 0 July 0, 07 9 /

30 Running Example, Starting From V v v 8 v 0 6 v 0 8 v 6 v 8 v 0 6 v 0 6 v 7 v 8 v 0 6 v 0 6 v 8 v 8 v 0 6 v Tong Wang UMass Boston CS 0 July 0, 07 0 /

31 Path with Minimum Weights from V 0 0 v 8 v visiting D 0 D D D D D D 6 start 0 V 0 V 9 V 9 V 9 V 8 V 6 6 V final: 0 6 v 0 6 v Tong Wang UMass Boston CS 0 July 0, 07 /

32 Pseudocode for Dijkstra s Algorithm Input: a directed graph G and vertex s D i : an array of distances from s to each node Initialize D i = For each edge (s, i) in the adjacency list of s Set D i = weight(s, i) Initialize tree T = { s } Repeat until all nodes are in T Choose a node v not in T that minimizes D v Add v to T For each edge (v, w) such that w T D w = min(d w, D v + weight(v, w)) Tong Wang UMass Boston CS 0 July 0, 07 /

33 Prim and Dijkstra Are Very Similar Prim s Algorithm: Choose an arbitrary node s Initialize tree T = { s } Initialize E = { } Repeat until all nodes are in T: Choose an edge (u, v) with minimum weight such that u is in T and v is not Add v to T, add (u, v) to E Output (T, E) Dijkstra s Algorithm: Input: a directed graph G and source node s D i : an array of distances from s to each node Initialize D i = For each edge (s, i) in the adjacency list of s Set D i = weight(s, i) Initialize tree T = { s } Repeat until all nodes are in T Choose a node v not in T that minimizes D v Add v to T For each edge (v, w) such that w T D w = min(d w, D v + weight(v, w)) Tong Wang UMass Boston CS 0 July 0, 07 /

34 Correctness of Dijkstra The argument of the correctness of Dijkstra s algorithm is similar to that of Prim s algorithm By contradiction Assume there is a shorter path An edge (u, v) in Dijkstra s tree is absent in the shorter path Add (u, v) and get a cycle One of the edges in the cycle is not there when (u, v) is added by Dijkstra (u, v) is no worse than this edge Tong Wang UMass Boston CS 0 July 0, 07 /

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree Algorithms and Theory of Computation Lecture 5: Minimum Spanning Tree Xiaohui Bei MAS 714 August 31, 2017 Nanyang Technological University MAS 714 August 31, 2017 1 / 30 Minimum Spanning Trees (MST) A

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees CIS 121 Data Structures and Algorithms Minimum Spanning Trees March 19, 2019 Introduction and Background Consider a very natural problem: we are given a set of locations V = {v 1, v 2,..., v n }. We want

More information

Lecture 6 Basic Graph Algorithms

Lecture 6 Basic Graph Algorithms CS 491 CAP Intro to Competitive Algorithmic Programming Lecture 6 Basic Graph Algorithms Uttam Thakore University of Illinois at Urbana-Champaign September 30, 2015 Updates ICPC Regionals teams will be

More information

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs Graphs and Network Flows ISE 411 Lecture 7 Dr. Ted Ralphs ISE 411 Lecture 7 1 References for Today s Lecture Required reading Chapter 20 References AMO Chapter 13 CLRS Chapter 23 ISE 411 Lecture 7 2 Minimum

More information

CS 161: Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with

More information

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, ) Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 1. if >. 2. error new key is greater than current key 3.. 4.. 5. if NIL and.

More information

looking ahead to see the optimum

looking ahead to see the optimum ! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

Greedy Algorithms Part Three

Greedy Algorithms Part Three Greedy Algorithms Part Three Announcements Problem Set Four due right now. Due on Wednesday with a late day. Problem Set Five out, due Monday, August 5. Explore greedy algorithms, exchange arguments, greedy

More information

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem of the Day Your job is to

More information

6.1 Minimum Spanning Trees

6.1 Minimum Spanning Trees CS124 Lecture 6 Fall 2018 6.1 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8

Union Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8 Union Find and Greedy Algorithms CSE 101: Design and Analysis of Algorithms Lecture 8 CSE 101: Design and analysis of algorithms Union find Reading: Section 5.1 Greedy algorithms Reading: Kleinberg and

More information

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 9 Greedy Technique Copyright 2007 Pearson Addison-Wesley. All rights reserved. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that

More information

Lecture 13: Minimum Spanning Trees Steven Skiena

Lecture 13: Minimum Spanning Trees Steven Skiena Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Your job

More information

Greedy Approach: Intro

Greedy Approach: Intro Greedy Approach: Intro Applies to optimization problems only Problem solving consists of a series of actions/steps Each action must be 1. Feasible 2. Locally optimal 3. Irrevocable Motivation: If always

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, 2003 Notes 8 for CS 170 1 Minimum Spanning Trees A tree is an undirected graph that is connected

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 8 Data Structures for Disjoint Sets Thanks to the text authors who contributed to these slides Data Structures for Disjoint Sets Also

More information

Minimum Spanning Trees

Minimum Spanning Trees CS124 Lecture 5 Spring 2011 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29 Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Greedy Algorithms. At each step in the algorithm, one of several choices can be made. Greedy Algorithms At each step in the algorithm, one of several choices can be made. Greedy Strategy: make the choice that is the best at the moment. After making a choice, we are left with one subproblem

More information

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016 CS125 Lecture 2 Fall 2016 2.1 Greedy Algorithms We will start talking about methods high-level plans for constructing algorithms. One of the simplest is just to have your algorithm be greedy. Being greedy,

More information

Week 12: Minimum Spanning trees and Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601

More information

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14 CS200: Graphs Rosen Ch. 9.1-9.4, 9.6, 10.4-10.5 Walls and Mirrors Ch. 14 Trees as Graphs Tree: an undirected connected graph that has no cycles. A B C D E F G H I J K L M N O P Rooted Trees A rooted tree

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples. . Greedy Technique The greedy technique (also known as greedy strategy) is applicable to solving optimization problems; an optimization problem calls for finding a solution that achieves the optimal (minimum

More information

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (IX) Yijia Chen Shanghai Jiaotong University Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Minimum-Cost Spanning Tree. Example

Minimum-Cost Spanning Tree. Example 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 cost Example 2 4 12 6 3 Network has 10 edges.

More information

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies 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 cost Example 2 4 12 6 3 Network has 10 edges.

More information

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley Lecture II: Minimium Spanning Tree Algorithms Dr Kieran T. Herley Department of Computer Science University College Cork April 016 Spanning Tree tree formed from graph edges that touches every node (e.g.

More information

Three Graph Algorithms

Three Graph Algorithms Three Graph Algorithms Shortest Distance Paths Distance/Cost of a path in weighted graph sum of weights of all edges on the path path A, B, E, cost is 2+3=5 path A, B, C, E, cost is 2+1+4=7 How to find

More information

Three Graph Algorithms

Three Graph Algorithms Three Graph Algorithms Shortest Distance Paths Distance/Cost of a path in weighted graph sum of weights of all edges on the path path A, B, E, cost is 2+3=5 path A, B, C, E, cost is 2+1+4=7 How to find

More information

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013 CSE373: Data Structures & Algorithms Lecture 7: Minimum Spanning Trees Dan Grossman Fall 03 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such

More information

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms Introduction to Parallel & Distributed Computing Parallel Graph Algorithms Lecture 16, Spring 2014 Instructor: 罗国杰 gluo@pku.edu.cn In This Lecture Parallel formulations of some important and fundamental

More information

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445 CS 5 Union/Find Aka: Disjoint-set forest Alon Efrat Problem definition Given: A set of atoms S={1, n E.g. each represents a commercial name of a drugs. This set consists of different disjoint subsets.

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Disjoin Sets and Union-Find Structures CLRS 21.121.4 University of Manitoba 1 / 32 Disjoint Sets Disjoint set is an abstract data type

More information

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg CSE 1 Greedy Alg: Union Find/Dijkstra s Alg Shayan Oveis Gharan 1 Dijkstra s Algorithm Dijkstra(G, c, s) { d s 0 foreach (v V) d[v] //This is the key of node v foreach (v V) insert v onto a priority queue

More information

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

Parallel Graph Algorithms

Parallel Graph Algorithms Parallel Graph Algorithms Design and Analysis of Parallel Algorithms 5DV050 Spring 202 Part I Introduction Overview Graphsdenitions, properties, representation Minimal spanning tree Prim's algorithm Shortest

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4) Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.

More information

Chapter 5. Greedy algorithms

Chapter 5. Greedy algorithms Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem

More information

Minimum spanning trees

Minimum spanning trees Carlos Moreno cmoreno @ uwaterloo.ca EI-3 https://ece.uwaterloo.ca/~cmoreno/ece5 Standard reminder to set phones to silent/vibrate mode, please! During today's lesson: Introduce the notion of spanning

More information

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees CSE1 Introduction to Algorithms Lecture 1 Minimum Spanning Trees Antoine Vigneron antoine@unist.ac.kr Ulsan National Institute of Science and Technology July 11, 201 Antoine Vigneron (UNIST) CSE1 Lecture

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 9: Minimum Spanning Trees Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: MST cut and cycle properties Prim, Kruskal greedy algorithms

More information

Chapter 23. Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees Chapter 23. Minimum Spanning Trees We are given a connected, weighted, undirected graph G = (V,E;w), where each edge (u,v) E has a non-negative weight (often called length) w(u,v). The Minimum Spanning

More information

2 A Template for Minimum Spanning Tree Algorithms

2 A Template for Minimum Spanning Tree Algorithms CS, Lecture 5 Minimum Spanning Trees Scribe: Logan Short (05), William Chen (0), Mary Wootters (0) Date: May, 0 Introduction Today we will continue our discussion of greedy algorithms, specifically in

More information

CS 6783 (Applied Algorithms) Lecture 5

CS 6783 (Applied Algorithms) Lecture 5 CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)

More information

Parallel Graph Algorithms

Parallel Graph Algorithms Parallel Graph Algorithms Design and Analysis of Parallel Algorithms 5DV050/VT3 Part I Introduction Overview Graphs definitions & representations Minimal Spanning Tree (MST) Prim s algorithm Single Source

More information

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast

More information

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck Theory of Computing Lecture 10 MAS 714 Hartmut Klauck Data structures: Union-Find We need to store a set of disjoint sets with the following operations: Make-Set(v): generate a set {v}. Name of the set

More information

Week 11: Minimum Spanning trees

Week 11: Minimum Spanning trees Week 11: Minimum Spanning trees Agenda: Minimum Spanning Trees Prim s Algorithm Reading: Textbook : 61-7 1 Week 11: Minimum Spanning trees Minimum spanning tree (MST) problem: Input: edge-weighted (simple,

More information

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees CS : Advanced Algorithms Minimum Spanning Trees Ali Ebnenasir Department of Computer Science Michigan Technological University Acknowledgement Eric Torng Moon Jung Chung Charles Ofria Minimum Spanning

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Algorithms and Theory of Computation. Lecture 7: Priority Queue Algorithms and Theory of Computation Lecture 7: Priority Queue Xiaohui Bei MAS 714 September 5, 2017 Nanyang Technological University MAS 714 September 5, 2017 1 / 15 Priority Queues Priority Queues Store

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

CS Final - Review material

CS Final - Review material CS4800 Algorithms and Data Professor Fell Fall 2009 October 28, 2009 Old stuff CS 4800 - Final - Review material Big-O notation Though you won t be quizzed directly on Big-O notation, you should be able

More information

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees CSE 33 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 0 August 3, 0 CSE 33 Data Abstractions, Summer 0 Making Connections You have a set of nodes (numbered -9)

More information

Minimum Spanning Trees. CSE 373 Data Structures

Minimum Spanning Trees. CSE 373 Data Structures Minimum Spanning Trees CSE 373 Data Structures Reading Chapter 3 Section 3.7 MSTs 2 Spanning Tree Given (connected) G(V,E) a spanning tree T(V,E ): Spans the graph (V = V) Forms a tree (no cycle); E has

More information

Algorithms for Minimum Spanning Trees

Algorithms for Minimum Spanning Trees Algorithms & Models of Computation CS/ECE, Fall Algorithms for Minimum Spanning Trees Lecture Thursday, November, Part I Algorithms for Minimum Spanning Tree Sariel Har-Peled (UIUC) CS Fall / 6 Sariel

More information

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND CSE 373 MAY 0 TH SPANNING TREES AND UNION FIND COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend HW5 out tomorrow

More information

Kruskal's MST Algorithm

Kruskal's MST Algorithm Kruskal's MST Algorithm In Wednesday's class we looked at the Disjoint Set data structure that splits a bunch of data values into sets in such a way that each datum is in exactly one set. There are 2 primary

More information

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes Page no: Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-00) Subject Notes Unit- Greedy Technique. Introduction: Greedy is the most straight forward design technique.

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f CS6: Algorithm Design and Analysis Recitation Section 8 Stanford University Week of 5 March, 08 Problem 8-. (Graph Representation) (a) Discuss the advantages and disadvantages of using an adjacency matrix

More information

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Dijkstra s algorithm for shortest paths when no edges have negative weight. Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

More information

COMP 355 Advanced Algorithms

COMP 355 Advanced Algorithms COMP 355 Advanced Algorithms Algorithms for MSTs Sections 4.5 (KT) 1 Minimum Spanning Tree Minimum spanning tree. Given a connected graph G = (V, E) with realvalued edge weights c e, an MST is a subset

More information

Weighted Graph Algorithms Presented by Jason Yuan

Weighted Graph Algorithms Presented by Jason Yuan Weighted Graph Algorithms Presented by Jason Yuan Slides: Zachary Friggstad Programming Club Meeting Weighted Graphs struct Edge { int u, v ; int w e i g h t ; // can be a double } ; Edge ( int uu = 0,

More information

CHAPTER 13 GRAPH ALGORITHMS

CHAPTER 13 GRAPH ALGORITHMS CHAPTER 13 GRAPH ALGORITHMS SFO LAX ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 00) AND SLIDES FROM NANCY

More information

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee Algorithm Analysis Graph algorithm Chung-Ang University, Jaesung Lee Basic definitions Graph = (, ) where is a set of vertices and is a set of edges Directed graph = where consists of ordered pairs

More information

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD UNIT 3 Greedy Method GENERAL METHOD Greedy is the most straight forward design technique. Most of the problems have n inputs and require us to obtain a subset that satisfies some constraints. Any subset

More information

Minimum Spanning Trees

Minimum Spanning Trees Chapter 23 Minimum Spanning Trees Let G(V, E, ω) be a weighted connected graph. Find out another weighted connected graph T(V, E, ω), E E, such that T has the minimum weight among all such T s. An important

More information

Minimum spanning trees

Minimum spanning trees Minimum spanning trees [We re following the book very closely.] One of the most famous greedy algorithms (actually rather family of greedy algorithms). Given undirected graph G = (V, E), connected Weight

More information

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness Prim and Dijkstra: Efficiency and Correctness Luay Nakhleh 1 Prim s Algorithm In class we saw Prim s algorithm for computing a minimum spanning tree (MST) of a weighted, undirected graph g. The pseudo-code

More information

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 1 Possible implementations Sorted linear implementations o Appropriate if

More information

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo CSE 431/531: Analysis of Algorithms Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast algorithms to solve

More information

The Algorithms of Prim and Dijkstra. Class 19

The Algorithms of Prim and Dijkstra. Class 19 The Algorithms of Prim and Dijkstra Class 19 MST Introduction you live in a county that has terrible roads a new county commissioner was elected based on her campaign promise to repave the roads she promised

More information

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1 Unit #9: Graphs CPSC 1: Algorithms and Data Structures Will Evans 01W1 Unit Outline Topological Sort: Getting to Know Graphs with a Sort Graph ADT and Graph Representations Graph Terminology More Graph

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 4.5 Minimum Spanning Tree Minimum Spanning Tree Minimum spanning tree. Given a connected

More information

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7 Minimum Spanning Trees and Union Find CSE 101: Design and Analysis of Algorithms Lecture 7 CSE 101: Design and analysis of algorithms Minimum spanning trees and union find Reading: Section 5.1 Quiz 1 is

More information

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63 CSci 3110 Greedy Algorithms 1/63 Greedy Algorithms Textbook reading Chapter 4 Chapter 5 CSci 3110 Greedy Algorithms 2/63 Overview Design principle: Make progress towards a solution based on local criteria

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets 22c:1 Algorithms Kruskal s Algorithm for MST Union-Find for Disjoint Sets 1 Kruskal s Algorithm for MST Work with edges, rather than nodes Two steps: Sort edges by increasing edge weight Select the first

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

CS3383 Unit 2: Greedy

CS3383 Unit 2: Greedy CS3383 Unit 2: Greedy David Bremner January 31, 2018 Contents Greedy Huffman Coding MST Huffman Coding DPV 5.2 http://jeffe.cs.illinois.edu/teaching/algorithms/ notes/07-greedy.pdf Huffman Coding is covered

More information

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees

Chapter 9. Greedy Algorithms: Spanning Trees and Minimum Spanning Trees msc20 Intro to lgorithms hapter. Greedy lgorithms: Spanning Trees and Minimum Spanning Trees The concept is relevant to connected undirected graphs. Problem: Here is a diagram of a prison for political

More information

Minimum Spanning Trees

Minimum Spanning Trees CSMPS 2200 Fall Minimum Spanning Trees Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 11/6/ CMPS 2200 Intro. to Algorithms 1 Minimum spanning trees Input: A

More information

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Chapter 23: Minimal Spanning Trees. Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Definition: A selection of edges from T E such that (V, T ) is a tree is called a spanning

More information

Maintaining Disjoint Sets

Maintaining Disjoint Sets Maintaining Disjoint Sets Ref: Chapter 21 of text. (Proofs in Section 21.4 omitted.) Given: A collection S 1, S 2,..., S k of pairwise disjoint sets. Each set has a name, its representative, which is an

More information

Minimum Spanning Trees My T. UF

Minimum Spanning Trees My T. UF Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication

More information