Docsity

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

Toggle sidebar
bg1
Lecture 13 Graphs I: BFS 6.006 Fall 2011
Lecture 13: Graphs I: Breadth First Search
Lecture Overview
•Applications of Graph Search
•Graph Representations
•Breadth-First Search
Recall:
Graph G= (V, E )
•V= set of vertices (arbitrary labels)
•E= set of edges i.e. vertex pairs (v, w)
–ordered pair =⇒directed edge of graph
–unordered pair =⇒undirected
ab
cd
a
bc
UNDIRECTED DIRECTED
e.g. V = {a,b,c,d}
E = {{a,b},{a,c},
{b,c},{b,d},
{c,d}}
V = {a,b,c}
E = {(a,c),(b,c),
(c,b),(b,a)}
Figure 1: Example to illustrate graph terminology
Graph Search
“Explore a graph”, e.g.:
•find a path from start vertex sto a desired vertex
•visit all vertices or edges of graph, or only those reachable from s
1
pf3
pf4
pf5

Partial preview of the text

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

Lecture 13: Graphs I: Breadth First Search

Lecture Overview

  • Applications of Graph Search
  • Graph Representations
  • Breadth-First Search

Recall:

Graph G = (V, E)

  • V = set of vertices (arbitrary labels)
  • E = set of edges i.e. vertex pairs (v, w)
    • ordered pair =⇒ directed edge of graph
    • unordered pair =⇒ undirected

a b

c d

a

b c

UNDIRECTED DIRECTED

e.g. V = {a,b,c,d}

E = {{a,b},{a,c},

{b,c},{b,d},

{c,d}}

V = {a,b,c}

E = {(a,c),(b,c),

(c,b),(b,a)}

Figure 1: Example to illustrate graph terminology

Graph Search

“Explore a graph”, e.g.:

  • find a path from start vertex s to a desired vertex
  • visit all vertices or edges of graph, or only those reachable from s

Applications:

There are many.

  • web crawling (how Google finds pages)
  • social networking (Facebook friend finder)
  • network broadcast routing
  • garbage collection
  • model checking (finite state machine)
  • checking mathematical conjectures
  • solving puzzles and games

Pocket Cube:

Consider a 2 × 2 ×2 Rubik’s cube

Configuration Graph:

  • vertex for each possible state
  • edge for each basic move (e.g., 90 degree turn) from one state to another
  • undirected: moves are reversible

Diameter (“God’s Number”) 11 for 2 × 2 × 2, 20 for 3 × 3 × 3, Θ(n^2 / lg n) for n × n × n [Demaine, Demaine, Eisenstat Lubiw Winslow 2011]

... “breadth-

first

tree”

possible first moves reachable in two steps but not one

“hardest configs” solved

Object-oriented Variations:

  • object for each vertex u
  • u.neighbors = list of neighbors i.e. Adj[u]

In other words, this is method for implicit graphs

Incidence Lists:

  • can also make edges objects

e.a e e.b

  • u.edges = list of (outgoing) edges from u.
  • advantage: store edge data without hashing

Breadth-First Search

Explore graph level by level from s

  • level 0 = {s}
  • level i = vertices reachable by path of i edges but not fewer

level

s

level

level

last

level

Figure 3: Illustrating Breadth-First Search

  • build level i > 0 from level i − 1 by trying all outgoing edges, but ignoring vertices from previous levels

Breadth-First-Search Algorithm

BFS (V,Adj,s): See CLRS for queue-based implementation level = { s: 0 } parent = {s : None } i = 1 frontier = [s] # previous level, i − 1 while frontier: next = [ ] # next level, i for u in frontier: for v in Adj [u]: if v not in level: # not yet seen level[v] = i ] = level[u] + 1 parent[v] = u next.append(v) frontier = next i + =

Example

a (^) s d f

z x c v

(^1 0 2 )

2 1 2 3

level 1 level 0

level 2 level 3

frontier 0 = {s} frontier 1 = {a, x} frontier 2 = {z, d, c} frontier 3 = {f, v} (not x, c, d)

Figure 4: Breadth-First Search Frontier

Analysis:

  • vertex V enters next (& then frontier) only once (because level[v] then set) base case: v = s

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.