Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
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:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE 326: Data Structures Spanning Trees Ben Lerner Summer 2007.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
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 CS 146 Prof. Sin-Min Lee Regina Wang.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
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.
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Greedy Algorithms General principle of greedy algorithm
Minimum Spanning Trees
Introduction to Algorithms
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSC 172 DATA STRUCTURES.
Minimum Spanning Trees
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Chapter 11 Graphs.
CMSC 341 Lecture 20.
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Topological Sort Algorithm
Minimum Spanning Trees
Minimum Spanning Trees
Fundamental Structures of Computer Science II
Greedy Algorithms Comp 122, Spring 2004.
Fundamental Structures of Computer Science II
CMSC 341 Graphs 2.
Lecture 12 Algorithm Analysis
Prim’s algorithm for minimum spanning trees
GRAPH – Definitions A graph G = (V, E) consists of
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

Graph Algorithms

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 3 Graph Representation Adjacency list Adjacency matrix

Shortest Path

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 5 Unweighted shortest path v1v1 v3v3 v4v4 v5v5 v7v7 v6v6 v2v

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 6 Unweighted Shortest Path Queue q;Vertex v, w; q = new Queue(); q.enqueue( s );s.dist = 0; // start with s While ( !q.isEmpty() ) {v = q.dequeue(); for each w adjacent to v {if (w.dist == INFINITY ) {w.dist = v.dist + 1;w.path = v; q.enqueue( w ); }

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 7 Weighted Shortest Path v1v1 v3v3 v4v4 v5v5 v7v7 v6v6 v2v

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 8 Dijkstra’s Algorithm vertex v, w; s.dist = 0; // start with s v = smallest unknown distance vertex While ( v != null ) {v.known = true; for each w adjacent to v {if ( !w.known ) if (v.dist + c(v,w) < w.dist ) {w.dist = v.dist + c(v,w); w.path = v; }

Minimum Spanning Tree

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 10 Definitions Let G = (V, E) be an undirected graph. T is a minimum spanning tree of G if T ⊆ E is an acyclic subset that connects all of the vertices and whose total weight w(T ) =  w(u, v) is minimized. (u,v) ∈ T

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 11 Prim’s Algorithm Prim’s algorithm has the property that the edges in the set A always form a single tree. The tree starts from an arbitrary root vertex r. Grow the tree until it spans all the vertices in V. –At each step, a light edge is added to the tree A that connects A to an isolated vertex of G A = (V, A).

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 12 Example of Prim’s Algorithm

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 13 Definitions Let G = (V, E) be an undirected graph. A cut (S, V − S) of G is a partition of V. An edge (u, v)  E crosses the cut (S, V − S) if one of its endpoints is in S and the other is in V − S. A cut respects a set A of edges if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 14 Light Edge u v S V-S A light edge

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 15 Definitions Let G = (V, E) be an undirected graph. T is a minimum spanning tree of G if T ⊆ E is an acyclic subset that connects all of the vertices and whose total weight w(T ) =  w(u, v) is minimized. (u,v) ∈ T Let A be a subset of some minimum spanning tree. An edge (u, v) is a safe edge for A if A  {(u, v)} is also a subset of a minimum spanning tree.

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 16 Corollary Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E, –A be a subset of E that is included in some minimum spanning tree for G, and –C = (V C, E C ) be a connected component (tree) in the forest G A = (V, A). If (u, v) is a light edge connecting C to another tree in G A, then (u, v) is safe for A.

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 17 Kruskal’s Algorithm Concept –Build a forest of minimum spanning trees. –Repeatedly connect the trees to create a subset of a minimum spanning tree, until all nodes are covered. –In connecting two trees, choose the edge of the least weight. Let C 1 and C 2 denote the two trees that are connected by (u, v). Since (u, v) must be a light edge connecting C 1 to some other tree, we need to prove that (u, v) is a safe edge for C 1.

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 18 Example of Kruskal’s Algorithm

Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 19 Kruskal’s Algorithm int edgesAccepted;Set s, uset, vset; PriorityQueue h;Vertex u, v;Edge e; h = readGraphIntoHeapArrayAndBuild( ); s = new Set( NUM_VERTICES ); edgesAccepted = 0; While( edgesAccepted < NUM_VERTICES – 1) {e = h.deletedMin( ); // e= (u,v) uset = s.find( u );vset = s.find( v ); if( uset != vset ) {edgesAccepted++;s.union( uset, vset );} }