Priority queue ADT part 1

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CS 161: Design and Analysis of Algorithms

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

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Minimum Spanning Trees

6.1 Minimum Spanning Trees

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

Announcements Problem Set 5 is out (today)!

Minimum Spanning Trees My T. UF

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Minimum-Cost Spanning Tree. Example

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

Minimum Spanning Trees

CSE 100: GRAPH ALGORITHMS

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

looking ahead to see the optimum

Minimum Spanning Trees. CSE 373 Data Structures

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

CS 310 Advanced Data Structures and Algorithms

Greedy Approach: Intro

Minimum spanning trees

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

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

Complexity of Prim s Algorithm

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

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

Chapter 5. Greedy algorithms

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

Lecture 6 Basic Graph Algorithms

Partha Sarathi Manal

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

Minimum Spanning Tree

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

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

Algorithm Design and Analysis

CS : Data Structures

Minimum Spanning Tree (5A) Young Won Lim 5/11/18

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

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

COP 4531 Complexity & Analysis of Data Structures & Algorithms

The minimum spanning tree problem

Design and Analysis of Algorithms

CS 4349 Lecture October 23rd, 2017

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

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Minimum Spanning Trees Shortest Paths

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Minimum Spanning Trees

CS 4349 Lecture October 18th, 2017

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

Lecture 25 Spanning Trees

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

Lecture 18: Implementing Graphs

2 A Template for Minimum Spanning Tree Algorithms

Lecture Summary CSC 263H. August 5, 2016

Week 11: Minimum Spanning trees

CS 561, Lecture 9. Jared Saia University of New Mexico

Algorithm Design (8) Graph Algorithms 1/2

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

COMP 355 Advanced Algorithms

CHAPTER 23. Minimum Spanning Trees

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

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

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

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

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

Lecture 4: Graph Algorithms

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

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

Minimum Spanning Trees

Data Structures and Algorithms. Werner Nutt


SPANNING TREES. Lecture 21 CS2110 Spring 2016

Announcements. HW4 due Friday. Summer 2016 CSE373: Data Structures & Algorithms 1

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

Spanning Trees. Lecture 20 CS2110 Spring 2015

CHAPTER 13 GRAPH ALGORITHMS

Three Graph Algorithms

ALGORITHM DESIGN GREEDY ALGORITHMS. University of Waterloo

1 Minimum Spanning Trees: History

Three Graph Algorithms

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

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

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

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

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

CS Final - Review material

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

Transcription:

http://www.mainjava.com/java/core-java/priorityqueue-class-in-java-with-programming-example/ Priority queue ADT part 1 CS 146 - Spring 2017

Today Dijkstra s algorithm The minimum spanning tree problem The cut property for MSTs Prim-Dijkstra-Jarnik algorithm Kruskal s algorithm

map bfs(graph G, vertex s) { dist = new map() queue = new FIFOqueue() for every vertex v in G dist.put(v, +inf) dist.put(s, 0) queue.enqueue(s) map dijkstra(weighted-graph G, vertex s) dist = new map() queue = new priorityqueue() for every vertex v in G dist.put(v, +inf) dist.put(s, 0) queue = new priorityqueue(dist) while queue not empty { v = queue.dequeue() for each neighbor w of v { if dist.get(w) == +inf { dist.put(w, dist.get(v) + 1) queue.enqueue(w) return dist while queue not empty { v = queue.extractmin() for each neighbor w of v { if w should be updated { dist.put(w, dist.get(v)+ weight(v,w) queue.decreasekey(w) return dist Dijkstra s algorithm: first steps (incomplete)

map dijkstra(weighted-graph G, vertex s) { dist = new map() for every vertex v in G dist.put(v, +inf) dist.put(s, 0) queue = new priorityqueue(dist) while queue not empty { v = queue.extract-min() for each neighbor w of v { if dist.get(w) > dist.get(v) + weight(v,w) { dist.put(w, dist.get(v)+ weight(v,w)) queue.decreasekey(w) return dist Dijkstra s algorithm

Example: Dijkstra s algorithm 2 2 7 2 6 1 1

map dijkstra(weighted-graph G, vertex s) { dist = new map() for every vertex v in G dist.put(v, +inf) dist.put(s, 0) queue = new priorityqueue(dist) while queue not empty { v = queue.extractmin() for each neighbor w of v { if dist.get(w) > dist.get(v) + weight(v,w) { dist.put(w, dist.get(v)+ weight(v,w)) queue.decreasekey(w) return dist relax(v->w) updates dist to w via path through edge v->w

map dijkstra(weighted-graph G, vertex s) { dist = new map() for every vertex v in G dist.put(v, +inf) dist.put(s, 0) queue = new priorityqueue(dist) while queue not empty { v = queue.extractmin() for each neighbor w of v { if dist.get(w) > dist.get(v) + weight(v,w) { dist.put(w, dist.get(v)+ weight(v,w)) queue.decreasekey(w) return dist once per vertex once per edge over the entire algorithm (not just inside loop)

Running time of Dijkstra s algorithm V+4E x time(dictionary op) 1 x time(queue.makequeue) V x time(queue.extractmin) E x time(queue.decreasekey) up to a constant factor, #queue ops = #dict ops, but dictionary ops are constant time with hash table running time is dominated by queue operations O(T(makeQueue(V)) + V x T(extractMin(V)) + E x T(decKey(V)))

Why is Dijkstra s algorithm correct? currently visiting v w s assumes that weights are non-negative visited (distances are correct) unvisited (distances are correct using only nodes that have been visited)

Shortest paths from B 2 2 7 2 6 1 1 The shortest paths from a vertex to all other nodes form a tree. Why?

B s shortest path tree 2 2 7 2 6 1 1

map augmented-dijkstra(weighted-graph G, vertex s) { dist = new map() prev = new map() < maps nodes to previous node on source s shortest path tree for every vertex v in G { dist.put(v, +inf) prev.put(v, null) dist.put(s, 0) queue = new priorityqueue(dist) while queue not empty { v = queue.extract-min() for each neighbor w of v { if dist.get(w) > dist.get(v) + weight(v,w) { dist.put(w, dist.get(v)+ weight(v,w)) queue.decreasekey(w) prev.put(w, v) return prev Retrieving the actual path step 1: record shortest path tree

2 2 7 2 6 1 1 source s shortest path tree encoded in prev map + F target node Retrieving the actual path step 2: reconstruct path to some target node B A D F

Minimum spanning trees

What is the cheapest possible power grid that will connect all the cities? https://en.wikipedia.org/wiki/minimum_spanning_tree

Observation The solution cannot contain cycles b/c removing an edge from this cycle would reduce the cost w/o compromising connectivity The solution must be a tree which we shall call

Minimum spanning tree Input: undirected connected weighted graph G = (V, E) Output: a tree T = (V, E ) with E E that minimizes weight(t )= X e2e 0 weight(e) how many can you find?

Observation minimum spanning trees are not unique! a graph can have more than one but all will be the same weight, obviously

Problem: find an algorithm for finding the minimum spanning tree of a graph

Ideas 1. from a single vertex, grow a tree by repeatedly adding a minimum-edge weight connecting a vertex not already in the tree. 2. construct tree by repeatedly adding the next lightest edge that doesn t produce a cycle Beware! The greedy approach doesn t always produce the best solution. http://newsofthesouth.com/the-monster-called-greed/

http://whatsupwithcompsci.blogspot.com/2014/07/whats-up-with-minimum-spanning-trees.html What s a graph cut? Cut = a partitioning of the vertices into 2 sets aka splitting or division formally, a pair (X, V\X) where X V, both non-empty

http://whatsupwithcompsci.blogspot.com/2014/07/whats-up-with-minimum-spanning-trees.html What s a graph cut? Edges crossing the cut: what we are really after

Cut property For a given cut, the lightest edge across the cut, if unique, is part of all MSTs. if not, the edge can be swapped out http://x-wei.github.io/algoii_week2_1.html

Prim Dijkstra Jarnik s algorithm idea: from a single vertex, grow a tree by repeatedly adding a minimum-edge weight connecting a vertex not already in the tree. Jarnik (1930), Prim (1957), Dijkstra (1959)

CLRS

Why is PDJ s algorithm correct? At each iteration, PDJ picks the lightest edge crossing the cut between the vertices already in MST and the rest. By cut property, this edge must be in MST.

map primdijkstajarnik(weighted-graph G, vertex s) { cost = new map() prev = new map() currently visiting for every vertex v in G { cost.put(v, +inf) prev.put(v, null) v dist.put(s, 0) queue = new priorityqueue(cost) w while queue not empty { v = queue.extract-min() for each neighbor w of v { if cost.get(w) > weight(v,w) { cost.put(w, weight(v,w)) queue.decreasekey(w) prev.put(w, v) return prev visited (in MST) unvisited update to cheapest edge between explored and w

map primdijkstajarnik(weighted-graph G, vertex s) { cost = new map() prev = new map() for every vertex v in G { cost.put(v, +inf) prev.put(v, null) dist.put(s, 0) queue = new priorityqueue(cost) while queue not empty { v = queue.extract-min() for each neighbor w of v { if cost.get(w) > weight(v,w) { cost.put(w, weight(v,w)) queue.decreasekey(w) prev.put(w, v) return prev running time?

Kruskal s algorithm idea: construct tree by repeatedly add the next lightest edge that doesn t produce a cycle More precisely

http://serverbob.3x.ro/ia/ddu0251.html forest tree general graph no cycle disconnected 3 connected components no cycle connected 1 connected component cycles connected 1 connected component

tree general graph cycle nected nnected ponent cycles connected 1 connected component cycles disconnected 3 connected components https://en.wikipedia.org/wiki/connected_component_(graph_theory)

Kruskal s algorithm More precisely idea: construct tree by starting with a forest of single-vertex components repeatedly adding to the forest the next lightest edge that doesn t produce a cycle

CLRS

Why is Kruskal s algorithm correct? At each iteration, Kruskal picks the lightest edge that does not create a cycle. pretend the green edge has weight 4

Why is Kruskal s algorithm correct? Put all other components to one side or the other, we have a cut. The edge chosen by Kruskal is the lightest across this cut. By cut property, this edge must be in MST.

map Kruskal(weighted graph G) { mst = new Graph() with same vertices as G and no edges sortededges = sort edges of G in order of increasing weight for each edge (u,v) in sortededges { if u and v are in different connected components of mst add (u,v) to mst return mst

E log E map Kruskal(weighted graph G) { mst = new Graph() with same vertices as G and no edges sortededges = sort edges of G in order of increasing weight for each edge (u,v) in sortededges { loops E times if u and v are in different connected components of mst add (u,v) to mst return mst E log E + E x Time(determine if in same component of MST)

How to determine if u and v are in different components? Approach 1 do DFS (tree traversal) on MST with u as starting vertex. O(V + E) each time O(E log E + E (V+E)) = O(E^2) total time

How to determine if u and v are in different components? Approach 2 use auxiliary special-purpose data structure called union-find to keep track of disjoint sets find(x): find the designated representative element of the set to which x belongs union(x,y): combine the sets to which x and y belong makeset(x): create a singleton set containing x only some operations may run slow, some fast, but k of these operations run in O(k α(n)) where n is the number of elements.

map Kruskal(weighted graph G) { mst = new Graph() with same vertices as G and no edges ds = new UnionFind() for v in G ds.makeset(v) sortededges = sort edges of G in order of increasing weight for each edge (u,v) in sortededges { if u and v are in different connected components of mst add (u,v) to mst if ds.find(u) is not ds.find(v) { add (u,v) to mst ds.union(u,v) return mst

map Kruskal(weighted graph G) { mst = new Graph() with same vertices as G and no edges ds = new UnionFind() for v in G V alpha(v) E log E ds.makeset(v) sortededges = sort edges of G in order of increasing weight for each edge (u,v) in sortededges { if u and v are in different connected components of mst add (u,v) to mst if ds.find(u) is not ds.find(v) { add (u,v) to mst 3E alpha(v) ds.union(u,v) return mst Time is O(E log E + (V+3E) alpha(v)) = O(E log V)

Priority queues: recap used in greedy algorithms where the best possible move is processed first Dijkstra s alg to solve single source shortest paths PDJ alg to solve the MST problem alternative approach: sort before processing Kruskal s alg to solve the MST problem