740 likes | 747 Views
Learn about different ways to represent graphs and how to perform Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms. This presentation includes examples and explanations of adjacency lists, adjacency matrix, running time analysis, and shortest-path tree.
E N D
Graphs & Paths Presentation : Part II
Graph representation • Given graph G = (V, E). • May be either directed or undirected. • Two common ways to represent for algorithms: • Adjacency lists. • Adjacency matrix. • When expressing the running time of an algorithm, it’s often in terms of both |V| and |E|.
Adjacency lists • Array Adj of |V| lists, one per vertex. • Vertex u’s list has all vertices v such that (u, v) ∈ E. (Works for both directed and undirected graphs.)
Example: • For an undirected graph:
Example • For a directed graph: Same asymptotic space and time.
Breadth-first search • Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V. • Output: d[v] = distance (smallest # of edges) from s to v, for all v ∈ V.
Idea • Idea: Send a wave out from s. • First hits all vertices 1 edge from s. • From there, hits all vertices 2 edges from s. • Etc. • Use FIFO queue Q to maintain wavefront. • v ∈ Q if and only if wave has hit v but has not come out of v yet.
Breadth-First Search r s t u v w x y
0 Breadth-First Search r s t u v w x y Q s 0
1 0 1 Breadth-First Search r s t u v w x y Q r w 1 1
1 2 0 1 Breadth-First Search r s t u v w x y Q w v 1 2
1 2 0 1 2 2 Breadth-First Search r s t u v w x y Q v t x 2 2 2
1 2 0 1 2 2 Breadth-First Search r s t u v w x y Q t x 2 2
1 3 2 0 1 2 2 Breadth-First Search r s t u v w x y Q x u 2 3
1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q u y 3 3
1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q y 3
1 3 2 0 1 2 3 2 Breadth-First Search r s t u v w x y Q
Breadth-First Search • 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
Breadth-First Search • Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. • Time = O(V + E). • O(V) because every vertex enqueued at most once. • O(E) because every vertex dequeued at most once and we examine (u, v) is in E only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.
Depth-first search • Input: G = (V, E), directed or undirected. No source vertex given! • Output: 2 timestamps on each vertex: • d[v] = discovery time • f [v] = finishing time
Depth-first search • Will methodically explore every edge. • Start over from different vertices as necessary. • As soon as we discover a vertex, explore from it. • Unlike BFS, which puts a vertex on a queue so that we explore from it later. • As DFS progresses, every vertex has a color: • WHITE = undiscovered • GRAY = discovered, but not finished (not done exploring from it) • BLACK = finished (have found everything reachable from it) • Discovery and finish times: Unique integers from 1 to 2|V|. • For all v, d[v] < f [v]. • In other words, 1 ≤ d[v] < f [v] ≤ 2|V|.
Depth-First Search • 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] tt +1 for each v adjacent to u do if v is White then Visit(v) color u Black f [u] tt +1 Gray vertices = stack of recursive calls
3 6 8 7 2 4 1 5 Depth-First Search
3 6 8 7 2 1 4 5 Depth-First Search 1
3 6 8 7 1 4 5 Depth-First Search 1 2 2
3 6 8 7 1 5 4 Depth-First Search 1 3 2 2
3 6 8 7 1 5 4 Depth-First Search 1 3 2 2
3 6 8 7 1 5 4 Depth-First Search 1 3 2 2
3 6 8 7 1 5 4 2 Depth-First Search 1 3 2
3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2
3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2
3 6 8 7 1 5 4 2 Depth-First Search 1 4 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 7 1 4 6 3 2
3 6 8 7 1 5 4 2 Depth-First Search 5 7 1 4 6 3 2
5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2
5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2
5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2
5 1 3 6 8 7 4 2 Depth-First Search 5 7 1 4 6 3 8 2