Download presentation

Presentation is loading. Please wait.

Published byAshton Arnett Modified about 1 year ago

1
Graph Algorithms

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

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

4
Shortest Path

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

6
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 ); }

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

8
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; }

9
Minimum Spanning Tree

10
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

11
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).

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

13
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.

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

15
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.

16
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.

17
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.

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

19
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 );} }

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google