Eager Prim Dijkstra.

Slides:



Advertisements
Similar presentations
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.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
1 Dijkstra's Shortest Path Algorithm Find shortest path from s to t. s 3 t
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Algorithms Minimum Spanning trees
Lecture 27 CSE 331 Nov 6, Homework related stuff Solutions to HW 7 and HW 8 at the END of the lecture Turn in HW 7.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
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.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Programming Abstractions Cynthia Lee CS106X. Graphs Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.
Prims Algorithm for finding a minimum spanning tree
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some.
Minimum Spanning Tree Graph Theory Basics - Anil Kishore.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Minimum Spanning Trees
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Greedy function greedy { S <- S0 //Initialization
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Programming Abstractions
Lecture 22 Minimum Spanning Tree
C.Eng 213 Data Structures Graphs Fall Section 3.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Eager Prim Dijkstra.
Ford-Fulkerson.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Graph Algorithm.
Minimum Spanning Tree.
Minimum Spanning Trees
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Spanning Trees.
Kruskal’s Algorithm for finding a minimum spanning tree
Chapter 23 Minimum Spanning Tree
Minimum Spanning Trees
Chapter 11 Graphs.
Minimum Spanning Trees
Fundamental Structures of Computer Science II
Algorithms: Design and Analysis
Weighted Graphs & Shortest Paths
Chapter 16 1 – Graphs Graph Categories Strong Components
Minimum Spanning Trees
Minimum spanning trees
CSE 417: Algorithms and Computational Complexity
Minimum Spanning Trees (MSTs)
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Prim’s algorithm for minimum spanning trees
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Eager Prim Dijkstra

Minimum spanning tree Minimum spanning tree (MST) Prim v.s. Kruskal is a spanning tree whose weight is no larger than any other spanning tree Prim v.s. Kruskal Prim: At each time, add an edge connecting tree vertex and non-tree vertex Minimum-weight crossing edge MinPQ, IndexMinPQ Kruskal gradually add minimum-weight edge to the MST, avoid forming cycle Union-find, MinPQ

Prim Look for the minimum-weight crossing edge Tree edge (thick black) Ineligible edge (dotted line) Crossing edge (solid line) Minimum-weight crossing edge (thick red) Minimum-weight crossing edge

Prim Look for the minimum-weight crossing edge Vertices on the MST masked[v]==true/false Edges on the tree edgeTo[v] is the Edge that connects v to the tree Crossing edges MinPQ<Edge> that compares edges by weight Minimum-weight crossing edge

Lazy Prim private void prim(EdgeWeightedGraph G, int s) { scan(G, s); while (!pq.isEmpty()) Edge e = pq.delMin(); int v = e.either(), w = e.other(v); if (marked[v] && marked[w]) continue mst.enqueue(e); weight += e.weight(); if (!marked[v]) scan(G, v); if (!marked[w]) scan(G, w); } W After putting V into to MST, both of the edges associated with W are in the priority queue private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) if (!marked[e.other(v)]) pq.insert(e); }

Lazy Prim v.s. Eager Prim Is that necessary? We already have <A,W> in the PQ, now we want to add <V,W> to the PQ. One of the edge is redundant since they connected to the same vertex W. We only need the smaller of the two since we are looking for minimum-weight crossing edge. How about this, we store <?,W> in the PQ, <?,W> denotes the minimum weight from MST to non-MST vertex W. (EdgeTo[w] and distTo[w]) After putting V into to MST, both of the edges associated with W are in the priority queue V W A

Lazy Prim v.s. Eager Prim Is that necessary? When we want to put <V,W> into the PQ, we search for index key W to see if <?,W> exists. Therefore, we need to use a IndexPriority Queue. In which the vertex numbers (e.g. W) are the index key and the weights are the sorting key. After putting V into to MST, both of the edges associated with W are in the priority queue V W A

Lazy Prim v.s. Eager Prim private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue if (e.weight() < distTo[w]) { distTo[w] = e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.changeKey(w, distTo[w]); else pq.insert(w, distTo[w]); } V W A Eager Prim keeps only the smaller of <A,W> and <V,W> in the IndexPriorityQueue

Eager Prim private void prim(EdgeWeightedGraph G, int s) { distTo[s] = 0.0; pq.insert(s, distTo[s]); while (!pq.isEmpty()) { int v = pq.delMin(); scan(G, v); } Eager Prim public PrimMST(EdgeWeightedGraph G) { edgeTo = new Edge[G.V()]; distTo = new double[G.V()]; marked = new boolean[G.V()]; pq = new IndexMinPQ<Double>(G.V()); for (int v = 0; v < G.V(); v++) distTo[v] = Double.POSITIVE_INFINITY; if (!marked[v]) prim(G, v); assert check(G); } private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue if (e.weight() < distTo[w]) { distTo[w] = e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.changeKey(w, distTo[w]); else pq.insert(w, distTo[w]); }

Dijkstra Similar idea is used in Dijkstra algorithm Dijkstra Digraph Single-source shortest paths

Dijkstra General idea Compare to Eager Prim After getting the shortest path from S to V, we want to update the distance from S to W. A previous path to W is through A, we do not not need to keep both the path from A and the path from V. We only keep the shorter one in the IndexPriority Queue. S A W Compare to Eager Prim In Eager Prim, <?, W> (distTo[w], edgeTo[w]) denotes the minimum-weight edge from the spanning tree to vertex W In Dijkstra, <?, W>. (distTo[w], edgeTo[w]) denotes the shortest path from start point S to vertex W V

Dijkstra General idea Y In the IndexPriority queue, we keep track only one path to Y, only one path to W, and so on. Y S A W V

Dijkstra private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.changeKey(w, distTo[w]); else pq.insert(w, distTo[w]); } S A W V

Prim MST v.s. Dijkstra private void relax(DirectedEdge e) { private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue if (e.weight() < distTo[w]) { distTo[w] = e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.changeKey(w, distTo[w]); else pq.insert(w, distTo[w]); } private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.changeKey(w, distTo[w]); else pq.insert(w, distTo[w]); } In Prim, distTo[w] stores the minimum-weight crossing edge connecting MST vertex to non-MST vertex W In Dijsktra, distTo[w] stores the minimum-weight from single source point S to non-explored vertex W. This is a acummulated value.