lec16 - Introduction to Algorithms 6.046J/18.401J LECTURE...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy choice Prim’s greedy MST algorithm Prof. Charles E. Leiserson
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.2 Graphs (review) Definition. A directed graph ( digraph ) G = ( V , E ) is an ordered pair consisting of a set V of vertices (singular: vertex ), a set E V × V of edges . In an undirected graph G = ( V , E ) , the edge set E consists of unordered pairs of vertices. In either case, we have | E | = O ( V 2 ) . Moreover, if G is connected, then | E | | V | –1 , which implies that lg | E | = Θ (lg V ) . (Review CLRS, Appendix B.)
Background image of page 2
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.3 Adjacency-matrix representation The adjacency matrix of a graph G = ( V , E ) , where V = {1, 2, …, n } , is the matrix A [1 . . n , 1 . . n ] given by A [ i , j ] = 1 if ( i , j ) E , 0 if ( i , j ) E .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.4 Adjacency-matrix representation The adjacency matrix of a graph G = ( V , E ) , where V = {1, 2, …, n } , is the matrix A [1 . . n , 1 . . n ] given by A [ i , j ] = 1 if ( i , j ) E , 0 if ( i , j ) E . 2 2 1 1 3 3 4 4 A 1234 1 2 3 4 0110 0010 0000 Θ ( V 2 ) storage dense representation.
Background image of page 4
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.5 Adjacency-list representation An adjacency list of a vertex v V is the list Adj [ v ] of vertices adjacent to v . Adj [1] = {2, 3} Adj [2] = {3} Adj [3] = {} Adj [4] = {3} 2 2 1 1 3 3 4 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.6 Adjacency-list representation An adjacency list of a vertex v V is the list Adj [ v ] of vertices adjacent to v . Adj [1] = {2, 3} Adj [2] = {3} Adj [3] = {} Adj [4] = {3} 2 2 1 1 3 3 4 4 For undirected graphs, | Adj [ v ] | = degree ( v ) . For digraphs, | Adj [ v ] | = out-degree ( v ) .
Background image of page 6
November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.7 Adjacency-list representation An adjacency list of a vertex v V is the list Adj [ v ] of vertices adjacent to v . Adj [1] = {2, 3} Adj [2] = {3} Adj [3] = {} Adj [4] = {3} 2 2 1 1 3 3 4 4 For undirected graphs, | Adj [ v ] | = degree ( v ) . For digraphs, | Adj [ v ] | = out-degree ( v ) . Handshaking Lemma: v V = 2 | E | for undirected graphs adjacency lists use Θ ( V + E ) storage — a sparse representation (for either type of graph).
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon