2.22k likes | 2.25k Views
Graph Search Algorithms. Graph. a. b. Node ~ city or computer. Edge ~ road or network. c. Undirected or Directed. A surprisingly large number of computational problems can be expressed as graph problems. A directed graph G = ( V, E ), where V = {1,2,3,4,5,6} and
E N D
Graph a b Node ~ city or computer Edge ~ road or network c Undirected or Directed A surprisingly large number of computational problems can be expressed as graph problems. COSC 3101B, PROF. J. ELDER
A directed graph G = (V, E), where V = {1,2,3,4,5,6} and • E = {(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)}. • The edge (2,2) is a self-loop. (b) An undirected graph G = (V,E), where V = {1,2,3,4,5,6} and E = {(1,2), (1,5), (2,5), (3,6)}. The vertex 4 is isolated. (c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}. Directed and Undirected Graphs COSC 3101B, PROF. J. ELDER
Trees Tree Forest Graph with Cycle COSC 3101B, PROF. J. ELDER
Running Time of Graph Algorithms • Running time often a function of both |V| and |E|. • For convenience, drop the | . | in asymptotic notation, e.g. O(V+E). COSC 3101B, PROF. J. ELDER
Representations: Undirected Graphs Adjacency Matrix Adjacency List COSC 3101B, PROF. J. ELDER
Representations: Directed Graphs Adjacency Matrix Adjacency List COSC 3101B, PROF. J. ELDER
Breadth-First Search • Goal: To recover the shortest paths from a source node s to all other reachable nodes v in a graph. • The length of each path and the paths themselves are returned. • Note: This problem is harder for general graphs than trees because of cycles! ? s COSC 3101B, PROF. J. ELDER
Breadth-First Search • Idea: send out search ‘wave’ from s. • Keep track of progress by colouring vertices: • Undiscovered vertices are coloured black • Just discovered vertices (on the wavefront) are coloured red. • Previously discovered vertices (behind wavefront) are coloured grey. COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue s b a e d g c f j i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s b a s d=0 e d g c f j i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=0 a e d=1 d d g g c f b j i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a a d=1 d e d g g b c f j i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=1 d e d g g b c f c d=2 j f d=2 i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g g b c f c d=2 j f d=2 m i e h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g b c f c d=2 j f d=2 m i e h j m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=1 e d g c f c d=2 j f d=2 m i e h j m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 c e d f m g e c f j j d=2 i h m k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d f m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d m g e c f j j h d=3 d=2 i i h m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g e c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g c f j j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=2 e d g c f j h d=3 d=2 i i h l m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=3 h i e d l g c f j d=2 i h m d=3 k l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=3 i e d l g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=3 e d l g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=3 e d g d=4 k c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=4 k e d g c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER
BFS FoundNot HandledQueue d=0 s d=1 b a d=4 e d=5 d g c f j d=2 i h m d=3 k d=4 l COSC 3101B, PROF. J. ELDER
Each vertex assigned finite d value at most once. d values assigned are monotonically increasing over time. Q contains vertices with d values {i, …, i, i+1, …, i+1} BLACK RED BLACK RED GRAY Breadth-First Search Algorithm COSC 3101B, PROF. J. ELDER
Correctness v & there is an edge from u to v Basic Steps: s d u The shortest path to uhas length d There is a path to v with length d+1. COSC 3101B, PROF. J. ELDER
Correctness • Vertices are discovered in order of their distance from the source vertex s. • When we discover v, how do we know there is not a shorter path to v? • Because if there was, we would already have discovered it! s v d u COSC 3101B, PROF. J. ELDER
Correctness Two-step proof: COSC 3101B, PROF. J. ELDER
End of Lecture 13 October 19, 2006
BLACK RED BLACK RED GRAY COSC 3101B, PROF. J. ELDER
s v u COSC 3101B, PROF. J. ELDER
BLACK RED w GRAY State when u is dequeued: s v u COSC 3101B, PROF. J. ELDER
BLACK RED BLACK RED GRAY Progress? • On every step at least one vertex is processed (turns gray). COSC 3101B, PROF. J. ELDER
BLACK RED BLACK RED GRAY Running Time COSC 3101B, PROF. J. ELDER
Recovering the Shortest Path For each node v, store predecessor of v in (v). s v u (v) (v) = u. Predecessor of v is COSC 3101B, PROF. J. ELDER
Recovering the Shortest Path COSC 3101B, PROF. J. ELDER
Colours are actually not required COSC 3101B, PROF. J. ELDER
Depth First Search (DFS) • Idea: • Continue searching “deeper” into the graph, until we get stuck. • If all the edges leaving v have been explored we “backtrack” to the vertex from which v was discovered. • Does not recover shortest paths, but can be useful for extracting other properties of graph, e.g., • Topological sorts • Detection of cycles • Extraction of strongly connected components COSC 3101B, PROF. J. ELDER
Depth-First Search • Explore every edge, starting from different vertices if necessary. • As soon as vertex discovered, explore from it. • Keep track of progress by colouring vertices: • Black: undiscovered vertices • Red: discovered, but not finished (still exploring from it) • Gray: finished (found everything reachable from it). COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> f d / / / / / / / / / / / / / / DFS s b a e d g c f j i h m k l COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> 1/ / / / / / / / / / / / / / DFS s b a e d g c f j i s,0 h m k l COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> 1/ / 2/ / / / / / / / / / / / DFS s b a e d g c f j i a,0 s,1 h m k l COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / / / / / DFS s b a e d g c f j c,0 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / 4/ / / / DFS s b a e d g c f h,0 j c,1 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER
FoundNot HandledStack <node,# edges> 1/ / 2/ / / 3/ / / / / 4/ / 5/ / DFS s b a e d g c k,0 f h,1 j c,1 i a,1 s,1 h m k l COSC 3101B, PROF. J. ELDER