Docsity

DEPTH FIRST SEARCH IN DSA, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
Lecture 14 Graphs II: DFS 6.006 Fall 2011
Lecture 14: Graphs II: Depth-First Search
Lecture Overview
•Depth-First Search
•Edge Classification
•Cycle Testing
•Topological Sort
Recall:
•graph search: explore a graph
e.g., find a path from start vertex sto a desired vertex
•adjacency lists: array Adj of |V|linked lists
–for each vertex u∈V, Adj[u] stores u’s neighbors, i.e., {v∈V|(u, v)∈E}
(just outgoing edges if directed)
For example:
a
bc
a
b
c
c
c
b
a
Adj
Figure 1: Adjacency Lists
Breadth-first Search (BFS):
Explore level-by-level from s— find shortest paths
1
pf3
pf4
pf5

Partial preview of the text

Download DEPTH FIRST SEARCH IN DSA and more Slides Data Structures and Algorithms in PDF only on Docsity!

Lecture 14: Graphs II: Depth-First Search

Lecture Overview

  • Depth-First Search
  • Edge Classification
  • Cycle Testing
  • Topological Sort

Recall:

  • graph search: explore a graph e.g., find a path from start vertex s to a desired vertex
  • adjacency lists: array Adj of |V | linked lists
    • for each vertex u ∈ V , Adj[u] stores u’s neighbors, i.e., {v ∈ V | (u, v) ∈ E} (just outgoing edges if directed)

For example:

a

b c

a

b

c

c

c

b

a

Adj

Figure 1: Adjacency Lists

Breadth-first Search (BFS):

Explore level-by-level from s — find shortest paths

Depth-First Search (DFS)

This is like exploring a maze.

s

Figure 2: Depth-First Search Frontier

Depth First Search Algorithm

  • follow path until you get stuck
  • backtrack along breadcrumbs until reach unexplored neighbor
  • recursively explore
  • careful not to repeat a vertex

parent = {s: None} DFS-visit (V, Adj, s): for v in Adj [s]: if v not in parent: parent [v] = s DFS-visit (V, Adj, v) DFS (V, Adj) parent = { } for s in V: if s not in parent: parent [s] = None DFS-visit (V, Adj, s)

search from start vertex s (only see stuff reachable from s)

explore entire graph

(could do same to extend BFS)

start v finish v

Figure 3: Depth-First Search Algorithm

Cycle Detection

Graph G has a cycle ⇔ DFS has a back edge

Proof

tree edges is a cycle

back edge: to tree ancestor

(<=)

(=>) consider first visit to cycle:

FIRST!

v (^2)

v 3

vk

v 1 v 0

  • before visit to vi finishes, will visit vi+1 (& finish): will consider edge (vi, vi+1) =⇒ visit vi+1 now or already did
  • =⇒ before visit to v 0 finishes, will visit vk (& didn’t before)
  • =⇒ before visit to vk (or v 0 ) finishes, will see (vk, v 0 ) as back edge

Job scheduling

Given Directed Acylic Graph (DAG), where vertices represent tasks & edges represent dependencies, order tasks without violating dependencies

G

A

H

B C^ F

D E

I

Figure 6: Dependence Graph: DFS Finishing Times

Source:

Source = vertex with no incoming edges = schedulable at beginning (A,G,I)

Attempt:

BFS from each source:

  • from A finds A, BH, C, F
  • from D finds D, BE, CF ← slow... and wrong!
  • from G finds G, H
  • from I finds I

Topological Sort

DFS-Visit(v)

... Reverse of DFS finishing times (time at which DFS-Visit(v) finishes)

  order.append(v)  ^ order.reverse()

MIT OpenCourseWare http://ocw.mit.edu

6.00 6 Introduction to Algorithms

Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.