graph search
play

Graph Search Lecture 15 October 18, 2018 Chandra Chekuri (UIUC) - PowerPoint PPT Presentation

CS/ECE 374: Algorithms & Models of Computation, Fall 2018 Graph Search Lecture 15 October 18, 2018 Chandra Chekuri (UIUC) CS/ECE 374 1 Fall 2018 1 / 45 Part I Graph Basics Chandra Chekuri (UIUC) CS/ECE 374 2 Fall 2018 2 / 45 Why


  1. CS/ECE 374: Algorithms & Models of Computation, Fall 2018 Graph Search Lecture 15 October 18, 2018 Chandra Chekuri (UIUC) CS/ECE 374 1 Fall 2018 1 / 45

  2. Part I Graph Basics Chandra Chekuri (UIUC) CS/ECE 374 2 Fall 2018 2 / 45

  3. Why Graphs? Graphs help model networks which are ubiquitous: transportation 1 networks (rail, roads, airways), social networks (interpersonal relationships), information networks (web page links), and many problems that don’t even look like graph problems. Fundamental objects in Computer Science, Optimization, 2 Combinatorics Many important and useful optimization problems are graph 3 problems Graph theory: elegant, fun and deep mathematics 4 Chandra Chekuri (UIUC) CS/ECE 374 3 Fall 2018 3 / 45

  4. Graph Definition An undirected (simple) graph G = ( V , E ) is a 2 -tuple: V is a set of vertices (also referred 1 to as nodes/points) E is a set of edges where each edge 2 e ∈ E is a set of the form { u , v } with u , v ∈ V and u � = v . Example In figure, G = ( V , E ) where V = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } and E = {{ 1 , 2 } , { 1 , 3 } , { 2 , 3 } , { 2 , 4 } , { 2 , 5 } , { 3 , 5 } , { 3 , 7 } , { 3 , 8 } , { 4 , 5 } , { 5 , 6 } , { 7 , 8 }} . Chandra Chekuri (UIUC) CS/ECE 374 4 Fall 2018 4 / 45

  5. Example: Modeling Problems as Search State Space Search Many search problems can be modeled as search on a graph. The trick is figuring out what the vertices and edges are. Missionaries and Cannibals Three missionaries, three cannibals, one boat, one river Boat carries two people, must have at least one person Must all get across At no time can cannibals outnumber missionaries How is this a graph search problem? What are the vertices? What are the edges? Chandra Chekuri (UIUC) CS/ECE 374 5 Fall 2018 5 / 45

  6. Example: Missionaries and Cannibals Graph Chandra Chekuri (UIUC) CS/ECE 374 6 Fall 2018 6 / 45

  7. Notation and Convention Notation An edge in an undirected graphs is an unordered pair of nodes and hence it is a set. Conventionally we use ( u , v ) for { u , v } when it is clear from the context that the graph is undirected. u and v are the end points of an edge { u , v } 1 Multi-graphs allow 2 loops which are edges with the same node appearing as both 1 end points multi-edges : different edges between same pairs of nodes 2 In this class we will assume that a graph is a simple graph unless 3 explicitly stated otherwise. Chandra Chekuri (UIUC) CS/ECE 374 7 Fall 2018 7 / 45

  8. Graph Representation I Adjacency Matrix Represent G = ( V , E ) with n vertices and m edges using a n × n adjacency matrix A where A [ i , j ] = A [ j , i ] = 1 if { i , j } ∈ E and A [ i , j ] = A [ j , i ] = 0 1 if { i , j } �∈ E . Advantage: can check if { i , j } ∈ E in O (1) time 2 Disadvantage: needs Ω( n 2 ) space even when m ≪ n 2 3 Chandra Chekuri (UIUC) CS/ECE 374 8 Fall 2018 8 / 45

  9. Graph Representation II Adjacency Lists Represent G = ( V , E ) with n vertices and m edges using adjacency lists: For each u ∈ V , Adj ( u ) = { v | { u , v } ∈ E } , that is 1 neighbors of u . Sometimes Adj ( u ) is the list of edges incident to u . Advantage: space is O ( m + n ) 2 Disadvantage: cannot “easily” determine in O (1) time whether 3 { i , j } ∈ E By sorting each list, one can achieve O (log n ) time 1 By hashing “appropriately”, one can achieve O (1) time 2 Note: In this class we will assume that by default, graphs are represented using plain vanilla (unsorted) adjacency lists. Chandra Chekuri (UIUC) CS/ECE 374 9 Fall 2018 9 / 45

  10. A Concrete Representation Assume vertices are numbered arbitrarily as { 1 , 2 , . . . , n } . Edges are numbered arbitrarily as { 1 , 2 , . . . , m } . Edges stored in an array/list of size m . E [ j ] is j ’th edge with info on end points which are integers in range 1 to n . Array Adj of size n for adjacency lists. Adj [ i ] points to adjacency list of vertex i . Adj [ i ] is a list of edge indices in range 1 to m . Chandra Chekuri (UIUC) CS/ECE 374 10 Fall 2018 10 / 45

  11. A Concrete Representation Array of edges E e j information including end point indices Array of adjacency lists List of edges (indices) that are incident to v i v i Chandra Chekuri (UIUC) CS/ECE 374 11 Fall 2018 11 / 45

  12. A Concrete Representation: Advantages Edges are explicitly represented/numbered. Scanning/processing all edges easy to do. Representation easily supports multigraphs including self-loops. Explicit numbering of vertices and edges allows use of arrays: O (1) -time operations are easy to understand. Can also implement via pointer based lists for certain dynamic graph settings. Chandra Chekuri (UIUC) CS/ECE 374 12 Fall 2018 12 / 45

  13. Connectivity Given a graph G = ( V , E ) : A path is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 1 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 . The length of the path is k − 1 (the number of edges in the path) and the path is from v 1 to v k . Note: a single vertex u is a path of length 0 . Chandra Chekuri (UIUC) CS/ECE 374 13 Fall 2018 13 / 45

  14. Connectivity Given a graph G = ( V , E ) : A path is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 1 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 . The length of the path is k − 1 (the number of edges in the path) and the path is from v 1 to v k . Note: a single vertex u is a path of length 0 . A cycle is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 2 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 and { v 1 , v k } ∈ E . Single vertex not a cycle according to this definition. Caveat: Some times people use the term cycle to also allow vertices to be repeated; we will use the term tour. Chandra Chekuri (UIUC) CS/ECE 374 13 Fall 2018 13 / 45

  15. Connectivity Given a graph G = ( V , E ) : A path is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 1 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 . The length of the path is k − 1 (the number of edges in the path) and the path is from v 1 to v k . Note: a single vertex u is a path of length 0 . A cycle is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 2 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 and { v 1 , v k } ∈ E . Single vertex not a cycle according to this definition. Caveat: Some times people use the term cycle to also allow vertices to be repeated; we will use the term tour. A vertex u is connected to v if there is a path from u to v . 3 Chandra Chekuri (UIUC) CS/ECE 374 13 Fall 2018 13 / 45

  16. Connectivity Given a graph G = ( V , E ) : A path is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 1 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 . The length of the path is k − 1 (the number of edges in the path) and the path is from v 1 to v k . Note: a single vertex u is a path of length 0 . A cycle is a sequence of distinct vertices v 1 , v 2 , . . . , v k such 2 that { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 and { v 1 , v k } ∈ E . Single vertex not a cycle according to this definition. Caveat: Some times people use the term cycle to also allow vertices to be repeated; we will use the term tour. A vertex u is connected to v if there is a path from u to v . 3 The connected component of u , con ( u ) , is the set of all vertices 4 connected to u . Is u ∈ con ( u ) ? Chandra Chekuri (UIUC) CS/ECE 374 13 Fall 2018 13 / 45

  17. Connectivity contd Define a relation C on V × V as uCv if u is connected to v 1 7 9 In undirected graphs, connectivity is 1 a reflexive, symmetric, and transitive 2 3 relation. Connected components are 4 5 8 10 the equivalence classes. 6 Graph is connected if only one 2 connected component. Chandra Chekuri (UIUC) CS/ECE 374 14 Fall 2018 14 / 45

  18. Connectivity Problems Algorithmic Problems Given graph G and nodes u and v , is u connected to v ? 1 Given G and node u , find all nodes that are connected to u . 2 Find all connected components of G . 3 Chandra Chekuri (UIUC) CS/ECE 374 15 Fall 2018 15 / 45

  19. Connectivity Problems Algorithmic Problems Given graph G and nodes u and v , is u connected to v ? 1 Given G and node u , find all nodes that are connected to u . 2 Find all connected components of G . 3 Can be accomplished in O ( m + n ) time using BFS or DFS . BFS and DFS are refinements of a basic search procedure which is good to understand on its own. Chandra Chekuri (UIUC) CS/ECE 374 15 Fall 2018 15 / 45

  20. Basic Graph Search in Undirected Graphs Given G = ( V , E ) and vertex u ∈ V . Let n = | V | . Explore ( G , u ): array Visited [1 .. n ] Set Visited [ i ] = FALSE for 1 ≤ i ≤ n Initialize: List: ToExplore , S Add u to ToExplore and to S , Visited [ u ] = TRUE while ( ToExplore is non-empty) do Remove node x from ToExplore for each edge ( x , y ) in Adj ( x ) do if ( Visited [ y ] == FALSE ) Visited [ y ] = TRUE Add y to ToExplore Add y to S Output S Chandra Chekuri (UIUC) CS/ECE 374 16 Fall 2018 16 / 45

  21. Example 1 7 9 2 3 4 5 8 10 6 Chandra Chekuri (UIUC) CS/ECE 374 17 Fall 2018 17 / 45

  22. Properties of Basic Search Proposition Explore( G , u ) terminates with S = con ( u ) . Chandra Chekuri (UIUC) CS/ECE 374 18 Fall 2018 18 / 45

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