Graph Representation (23.1/22.1) HW: problem 23.3, p.496 G=(V, E) -graph: V = V(G) - vertices; E = E(G) - edges (connecting pairs of vertices) 1 2 3 4 5 Adjacency-list Adjacency-matrix 1 3 4 2 3 3 5 4 5 5 2
Depth-First Search (23.3/22.3) Methodically explore all vertices and edges All vertices are White, t = 0 For each vertex u do if u is White then Visit(u) Procedure Visit(u) color u Gray; d[u] t t +1 for each v adjacent to u do if v is White then Visit(v) color u Black f [u] t t +1 Gray vertices = stack of recursive calls
Depth-First Search 5 6 1 3 8 7 4 2
Depth-First Search 1 5 6 1 3 8 7 4 2
Depth-First Search 1 5 6 1 3 8 2 7 4 2
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
Depth-First Search 1 5 5 6 1 4 3 8 3 7 4 2 2
Depth-First Search 1 5 5 6 1 4 3 8 3 7 4 2 2
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
Depth-First Search (23.3/22.3) Runtime of DFS = O(V+E) once per vertex once per edge Kinds of edges: tree edge (gray to gray) back edge (gray to gray) forward edge (gray to black) cross edge (gray to black) G is undirected only tree and back Undirected G is acyclic no back edges If a graph is acyclic can be found in O(V) time
Topological Sort (23.4/22.4) DAG = directed acyclic graph has levels or depth cannot return up Topological Sort(G) call DFS(G) to compute f[v] sort according to finishing times Directed graph G is acyclic no back edges
Breadth-First Search (23.2/22.2) BFS discovers all vertices at distance k before any vertices at distance k+1. Initialization assigns to all vertices: w(v)= color all white Color s gray, w(s)=0, enqueue s in Q for the head u of Q for each v adjacent to u, d[v]=d[u]+1 enqueue v in Q dequeue Q color u black
Breadth-First Search r s t u v w x y
Breadth-First Search r s t u v w x y Q s
Breadth-First Search r s t u 1 1 v w x y Q r w 1 1
Breadth-First Search r s t u 1 2 1 v w x y Q w v 1 2
Breadth-First Search r s t u 1 2 2 1 2 v w x y Q v t x 2 2 2
Breadth-First Search r s t u 1 2 2 1 2 v w x y Q t x 2 2
Breadth-First Search r s t u 1 2 3 2 1 2 v w x y Q x u 2 3
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q u y 3 3
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q y 3
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q
Breadth-First Search (23.2/22.2) Run-time = O(V+E) Shortest-path tree the final weight is the minimum distance keep predecessors and get the shortest path all BFS shortest paths make a tree