Download presentation
Presentation is loading. Please wait.
1
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
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
3
Depth-First Search 5 6 1 3 8 7 4 2
4
Depth-First Search 1 5 6 1 3 8 7 4 2
5
Depth-First Search 1 5 6 1 3 8 2 7 4 2
6
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
7
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
8
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
9
Depth-First Search 1 5 6 1 3 8 3 2 7 4 2
10
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
11
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
12
Depth-First Search 1 5 6 1 4 3 8 3 2 7 4 2
13
Depth-First Search 1 5 5 6 1 4 3 8 3 7 4 2 2
14
Depth-First Search 1 5 5 6 1 4 3 8 3 7 4 2 2
15
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
16
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
17
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
18
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
19
Depth-First Search 1 5 5 6 1 4 6 3 8 3 4 2 7 2
20
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 7 2
21
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 7 2
22
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
23
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
24
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
25
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
26
Depth-First Search 1 5 5 7 6 1 4 6 3 8 3 4 2 8 7 2
27
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
28
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
29
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
30
Breadth-First Search r s t u v w x y
31
Breadth-First Search r s t u v w x y Q s
32
Breadth-First Search r s t u 1 1 v w x y Q r w 1 1
33
Breadth-First Search r s t u 1 2 1 v w x y Q w v 1 2
34
Breadth-First Search r s t u 1 2 2 1 2 v w x y Q v t x 2 2 2
35
Breadth-First Search r s t u 1 2 2 1 2 v w x y Q t x 2 2
36
Breadth-First Search r s t u 1 2 3 2 1 2 v w x y Q x u 2 3
37
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q u y 3 3
38
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q y 3
39
Breadth-First Search r s t u 1 2 3 2 1 2 3 v w x y Q
40
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.