introduction to graphs and traversal
play

Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, - PDF document

Notes Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, Dallas, TX March 7, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For


  1. Notes Introduction to Graphs and Traversal Tyler Moore CSE 3353, SMU, Dallas, TX March 7, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see http://www.cs.sunysb.edu/~skiena/ Introduction to Graphs Notes Many problems can be represented using graphs Once we reduce a problem to a graph, we can often use standard graph algorithms to navigate the graph A graph G = ( V , E ) is defined by a set of vertices V , and a set of edges E consisting of ordered or unordered pairs of vertices from V Reading for today’s lecture: ADM 145-152 AND Python Algorithms Ch 2. pp. 23–31 and Ch. 5 pp. 101–107, availble from http://lyle.smu.edu/~tylerm/courses/cse3353/pyalg.html Shameless plug: Jailbreaking Information: The Legacy of Hacktivist Aaron Swartz , Fondren Library Mezzanine, TONIGHT 6:30pm-8pm 2 / 28 Social Network as a Graph Notes Nodes: people Alice Edges: friendship Bob Charlie Dawn Eve George Fred 3 / 28 Road Network as a Graph Notes Nodes: intersections Main St Edges: roads 2nd Ave 1st Ave Main St Elm St 3rd Ave Aspen Ave Main St 5th Ave Park St 5th Ave 4 / 28

  2. Electronic Circuits as a Graph Notes 10Ω A v x S 20Ω 5Ω 5 v x B Vertices: junctions Edges: components 5 / 28 World Wide Web as a Graph Notes Nodes: websites Edges: links NYT SMU Google Foo DMN 6 / 28 Course Dependencies as a Graph Notes CSE 1341 CSE 1342 CSE 2353 CSE 2341 Nodes: courses Edges: prerequisites CSE 3353 CSE 5338 7 / 28 Maze as a Graph Notes Nodes: rooms; edges: doorways 8 / 28

  3. Flavors of Graphs Notes The first step in any graph problem is recognizing that you have a graph problem It’s not always obvious! The second step in any graph problem is determining which flavor of graph you are dealing with Learning to talk the talk is an important part of walking the walk The flavor of graph has a big impact on which algorithms are appropriate and efficient 9 / 28 Directed vs. Undirected Graphs Notes A Undirected A Directed B C B C D E D E F G F G A graph G = ( V , E ) is undirected if edge ( x , y ) ∈ E implies that ( y , x ) is also in E Road networks between cities are typically undirected Street networks within cities are almost always directed because of one-way streets What about online social networks? 10 / 28 Weighted vs. Unweighted Graphs Notes Unweighted Weighted A A 10 4 B C B C 1 7 5 8 19 D E D E 4 10 6 8 F G F G In weighted graphs, each edge (or vertex) of G is assigned a numerical value, or weight The edges of a road network graph might be weighted with their length, drive-time or speed limit In unweighted graphs, there is no cost distinction between various edges and vertices 11 / 28 Simple vs. Non-simple Graphs Notes Simple Non-simple A A B C B C D E D E F G F G Certain types of edges complicate the task of working with graphs. A self-loop is an edge ( x , x ) involving only one vertex. An edge ( x , y ) is a multi-edge if it occurs more than once in the graph. Any graph without self-loops and multi-edges is called simple . Which of the earlier example graphs can be non-simple? 12 / 28

  4. Sparse vs. Dense Graphs Notes Sparse A A Dense B C B C D E D E F G F G Graphs are sparse when only a small fraction of the possible number of vertex pairs actually have edges defined between them. Graphs are usually sparse due to application-specific con- straints Road networks must be sparse because of road junctions Typically dense graphs have a quadratic number of edges while sparse graphs are linear in size 13 / 28 Cyclic vs. Acyclic Graphs Notes Cyclic Acyclic A A B C B C D D F G F G An acyclic graph does not contain any cycles Trees are connected, acyclic, undirected graphs Directed acyclic graphs are called DAGs DAGs arise naturally in scheduling problems, where a directed edge ( x , y ) indicates that x must occur before y . 14 / 28 Labeled vs. Unlabeled Graphs Notes A Labeled Unlabeled B C D E F G In labeled graphs, each vertex is assigned a unique name or identifier to distinguish it from all other vertices. An important graph problem is isomorphism testing : determining whether the topological structure of two graphs are in fact identical if we ignore any labels. 15 / 28 More Graph Terminology Notes A path is a sequence of edges connecting two vertices Two common problems: (1) does a path exist between A and B ? (2) what is the shortest path between A and B ? A graph is connected if there is a path between any two vertices A directed graph is strongly connected if there is a directed path between any two vertices. The degree of a vertex is the number of edges adjacent to it 16 / 28

  5. Graph Data Structures Notes Option 1: Adjacency Sets a , b , c , d , e , f , g , h = range (8) N = [ { b , c , d , e , f } , # a { c , e } , # b { d } , # c g c b { e } , # d { f } , # e { c , g , h } , # f { f , h } , # g a f { f , g } # h ] > b i n N[ a ] > > d e h # Neighborhood membership True > l e n (N[ f ] ) # Degree > > 3 17 / 28 Graph Data Structures Notes Option 2: Adjacency Lists a , b , c , d , e , f , g , h = range (8) N = [ [ b , c , d , e , f ] , # a [ c , e ] , # b [ d ] , # c g c b [ e ] , # d [ f ] , # e [ c , g , h ] , # f [ f , h ] , # g a f [ f , g ] # h ] > b i n N[ a ] > > d e h # Neighborhood membership True > l e n (N[ f ] ) # Degree > > 3 17 / 28 Graph Data Structures Notes Option 3: Adjacency Dictionaries w/ Edge Weights a , b , c , d , e , f , g , h = range (8) N = [ { b : 2 , c : 1 , d : 3 , e : 9 , f :4 } , #a { c : 4 , e : 3 } , #b { d : 8 } , #c { e : 7 } , #d 4 c g b { f : 5 } , #e 2 { c : 2 , g : 2 , h : 2 } , #f 1 2 2 { f : 1 , h : 6 } , #g 1 { f : 9 , g :8 } #h 4 a 8 3 f ] 8 6 2 9 3 5 > b i n N[ a ] 9 > > # Neighborhood membership 7 e d h True > l e n (N[ f ] ) # Degree > > 3 > N[ a ] [ b ] > > # Edge weight f o r ( a , b ) 2 17 / 28 Graph Data Structures Notes Option 4: Dictionaries w/ Adjacency Sets N = { ’ a ’ : s e t ( ’ bcdef ’ ) , ’ b ’ : s e t ( ’ ce ’ ) , ’ c ’ : s e t ( ’ d ’ ) , g c b ’ d ’ : s e t ( ’ e ’ ) , ’ e ’ : s e t ( ’ f ’ ) , ’ f ’ : s e t ( ’ cgh ’ ) , ’ g ’ : s e t ( ’ fh ’ ) , a f ’ h ’ : s e t ( ’ fg ’ ) } > ’ b ’ i n N[ ’ a ’ ] > > e d h # Neighborhood membership True > l e n (N[ ’ f ’ ] ) # Degree > > 3 17 / 28

  6. Graph Data Structures Notes Option 5: Adjacency Matrix (using nested lists) a , b , c , d , e , f , g , h = range (8) # a b c d e f g h N = [ [ 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 ] , # a g c [ 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , # b b [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , # c [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , # d [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , # e [ 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 ] , # f a f [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 ] , # g [ 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 ] ] # h > a , b , c , d , e , f , g , h = range (8) e > > d h > N[ a ] [ b ] > > # Neighborhood membership 1 > sum(N[ f ] ) # Degree > > 3 17 / 28 Graph Data Structures Notes Option 6: Weighted Adjacency Matrix (using nested lists) a , b , c , d , e , f , g , h = range (8) = f l o a t ( ’ i n f ’ ) # a b c d e f g h W = [ [ 0 , 2 , 1 , 3 , 9 , 4 , , ] , # a [ , 0 , 4 , , 3 , , , ] , # b 4 g c [ , , 0 , 8 , , , , ] , # c b [ , , , 0 , 7 , , , ] , # d 2 1 [ , , , , 0 , 5 , , ] , # e 2 2 1 [ , , 2 , , , 0 , 2 , 2 ] , # f 4 [ , , , , , 1 , 0 , 6 ] , # g a 8 3 f 8 6 [ , , , , , 9 , 8 , 0 ] ] # h 2 9 5 3 9 > i n f = f l o a t ( ’ i n f ’ ) > > 7 > W[ a ] [ b ] < i n f e > > d h # Neighborhood membership True > W[ c ] [ e ] < i n f > > # Neighborhood membership F a l s e 17 / 28 > sum(1 f o r w i n W[ a ] i f w < i n f ) − 1 > > # Degree 5 Tradeoffs Between Adjacency Lists and Adjacency Matrices Notes When the ADM refers to adjacency lists, Skiena means adjacency linked lists Adjacency linked lists use less memory for sparse graphs than matrices Finding vertex degree is Θ(1) vs. Θ( n ) in matrices In Python, adjacency lists are technically adjacency dynamic arrays (respectively sets and dictionaries for other representations) Python adjacency lists/sets/dictionaries use less memory than adjacency matrices implemented in Python They are also Θ(1) to find vertex degree Within Python, adjacency sets offer expected Θ(1) membership checking vs. Θ(1) for lists For graph traversal, adjacency lists execute faster than sets. 18 / 28 Graph Traversal Notes One of the most fundamental graph problems is to traverse every edge and vertex in a graph. For correctness, we must do the traversal in a systematic way so that we dont miss anything. For efficiency, we must make sure we visit each edge at most twice. Since a maze is just a graph, such an algorithm must be powerful enough to enable us to get out of an arbitrary maze. 19 / 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend