graph search
play

Graph Search Part I Lecture 15 Thursday, October 19, 2017 Graph - PowerPoint PPT Presentation

Algorithms & Models of Computation CS/ECE 374, Fall 2017 Graph Search Part I Lecture 15 Thursday, October 19, 2017 Graph Basics Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 50 Sariel Har-Peled (UIUC) CS374 2 Fall 2017 2 / 50


  1. Algorithms & Models of Computation CS/ECE 374, Fall 2017 Graph Search Part I Lecture 15 Thursday, October 19, 2017 Graph Basics Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 50 Sariel Har-Peled (UIUC) CS374 2 Fall 2017 2 / 50 Why Graphs? Graph Definition Graphs help model networks which are ubiquitous: transportation 1 networks (rail, roads, airways), social networks (interpersonal An undirected (simple) graph relationships), information networks (web page links), and many G = ( V , E ) is a 2 -tuple: problems that don’t even look like graph problems. V is a set of vertices (also referred 1 Fundamental objects in Computer Science, Optimization, 2 to as nodes/points) Combinatorics E is a set of edges where each edge 2 Many important and useful optimization problems are graph 3 e ∈ E is a set of the form { u , v } problems with u , v ∈ V and u � = v . Graph theory: elegant, fun and deep mathematics 4 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 }} . Sariel Har-Peled (UIUC) CS374 3 Fall 2017 3 / 50 Sariel Har-Peled (UIUC) CS374 4 Fall 2017 4 / 50

  2. Example: Modeling Problems as Search Example: Missionaries and Cannibals Graph 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? Sariel Har-Peled (UIUC) CS374 5 Fall 2017 5 / 50 Sariel Har-Peled (UIUC) CS374 6 Fall 2017 6 / 50 Notation and Convention Graph Representation I Notation Adjacency Matrix An edge in an undirected graphs is an unordered pair of nodes and Represent G = ( V , E ) with n vertices and m edges using a n × n hence it is a set. Conventionally we use ( u , v ) for { u , v } when it is adjacency matrix A where clear from the context that the graph is undirected. A [ i , j ] = A [ j , i ] = 1 if { i , j } ∈ E and A [ i , j ] = A [ j , i ] = 0 1 if { i , j } �∈ E . u and v are the end points of an edge { u , v } 1 Advantage: can check if { i , j } ∈ E in O (1) time 2 Multi-graphs allow 2 Disadvantage: needs Ω( n 2 ) space even when m ≪ n 2 3 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. Sariel Har-Peled (UIUC) CS374 7 Fall 2017 7 / 50 Sariel Har-Peled (UIUC) CS374 8 Fall 2017 8 / 50

  3. Graph Representation II A Concrete Representation Adjacency Lists Assume vertices are numbered arbitrarily as { 1 , 2 , . . . , n } . Edges are numbered arbitrarily as { 1 , 2 , . . . , m } . Represent G = ( V , E ) with n vertices and m edges using adjacency lists: 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 . 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 Array Adj of size n for adjacency lists. Adj [ i ] points to to u . adjacency list of vertex i . Adj [ i ] is a list of edge indices in range 1 to m . 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. Sariel Har-Peled (UIUC) CS374 9 Fall 2017 9 / 50 Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 50 A Concrete Representation A Concrete Representation: Advantages Edges are explicitly represented/numbered. Scanning/processing Array of edges E e j all edges easy to do. Representation easily supports multigraphs including self-loops. information including end point indices Explicit numbering of vertices and edges allows use of arrays: O (1) -time operations are easy to understand. Array of adjacency lists Can also implement via pointer based lists for certain dynamic graph settings. List of edges (indices) that are incident to v i v i Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 50 Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 50

  4. Connectivity Given a graph G = ( V , E ) : path: sequence of distinct vertices v 1 , v 2 , . . . , v k such that 1 { v i , v i +1 } ∈ E for 1 ≤ i ≤ k − 1 . The length of the path is Part II 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 . Connectivity cycle: sequence of distinct vertices v 1 , v 2 , . . . , v k such that 2 { 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 ) ? Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 50 Sariel Har-Peled (UIUC) CS374 14 Fall 2017 14 / 50 Connectivity contd Connectivity Problems Define a relation C on V × V as uCv if Algorithmic Problems u is connected to v 1 9 7 Given graph G and nodes u and v , is u connected to v ? In undirected graphs, connectivity is 1 1 2 3 a reflexive, symmetric, and transitive Given G and node u , find all nodes that are connected to u . 2 relation. Connected components are 4 5 10 Find all connected components of G . 8 3 the equivalence classes. 6 Graph is connected if only one 2 Can be accomplished in O ( m + n ) time using BFS or DFS . connected component. BFS and DFS are refinements of a basic search procedure which is good to understand on its own. Sariel Har-Peled (UIUC) CS374 15 Fall 2017 15 / 50 Sariel Har-Peled (UIUC) CS374 16 Fall 2017 16 / 50

  5. Basic Graph Search in Undirected Graphs Example 9 1 7 Given G = ( V , E ) and vertex u ∈ V . Let n = | V | . 2 3 Explore ( G , u ): Visited [1 . . n ] ← FALSE 4 5 10 8 // ToExplore , S : Lists 6 Add u to ToExplore and to S Visited [ u ] ← TRUE while ( ToExplore is non-empty) do Remove node x from ToExplore for each edge xy in Adj ( x ) do if ( Visited [ y ] = FALSE ) Visited [ y ] ← TRUE Add y to ToExplore Add y to S Output S Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 50 Sariel Har-Peled (UIUC) CS374 18 Fall 2017 18 / 50 Properties of Basic Search Properties of Basic Search Proposition Proposition Explore( G , u ) terminates with S = con ( u ) . Explore( G , u ) terminates in O ( m + n ) time. Proof: easy exercise Proof Sketch. Once Visited [ i ] is set to TRUE it never changes. Hence a BFS and DFS are special case of BasicSearch. node is added only once to ToExplore . Thus algorithm Breadth First Search ( BFS ): use queue data structure to 1 terminates in at most n iterations of while loop. implementing the list ToExplore By induction on iterations, can show v ∈ S ⇒ v ∈ con ( u ) Depth First Search ( DFS ): use stack data structure to 2 Since each node v ∈ S was in ToExplore and was explored, no implement the list ToExplore edges in G leave S . Hence no node in V − S is in con ( u ) . Thus S = con ( u ) at termination. Sariel Har-Peled (UIUC) CS374 19 Fall 2017 19 / 50 Sariel Har-Peled (UIUC) CS374 20 Fall 2017 20 / 50

  6. Search Tree Finding all connected components One can create a natural search tree T rooted at u during search. Exercise: Modify Basic Search to find all connected components of a given graph G in O ( m + n ) time. Explore ( G , u ): array Visited [1 .. n ] Initialize: Visited [ i ] ← FALSE for i = 1 , . . . , n List: ToExplore , S Add u to ToExplore and to S , Visited [ u ] ← TRUE Make tree T with root as u 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 Add y to T with x as its parent Output S T is a spanning tree of con ( u ) rooted at u Sariel Har-Peled (UIUC) CS374 21 Fall 2017 21 / 50 Sariel Har-Peled (UIUC) CS374 22 Fall 2017 22 / 50 Directed Graphs Definition A directed graph G = ( V , E ) B A C Part III consists of E F D set of vertices/nodes V 1 and Directed Graphs and Decomposition G H a set of edges/arcs 2 E ⊆ V × V . An edge is an ordered pair of vertices. ( u , v ) different from ( v , u ) . Sariel Har-Peled (UIUC) CS374 23 Fall 2017 23 / 50 Sariel Har-Peled (UIUC) CS374 24 Fall 2017 24 / 50

Recommend


More recommend