Docsity

Graph Search and Representations in Algorithms: Lecture 12 from MIT 6.006 Course, Study notes of Computer Programming

Partial preview of the text

Download Graph Search and Representations in Algorithms: Lecture 12 from MIT 6.006 Course and more Study notes Computer Programming in PDF only on Docsity!

MIT OpenCourseWare http://ocw.mit.edu

6.006 Introduction to Algorithms

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

Lecture 12: Searching I: Graph Search and Representations

Lecture Overview: Search 1 of 3

  • Graph Search
  • Applications
  • Graph Representations
  • Introduction to breadth-first and depth-first search

Readings

CLRS 22.1-22.3, B.

Graph Search

Explore a graph e.g., find a path from start vertices to a desired vertex 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

  • can factor out 24-fold symmetry of cube: fix one cubelet 811 .3 =⇒ 7!. 37 = 11 , 022 , 480
  • in fact, graph has 3 connected components of equal size =⇒ only need to search in one =⇒ 7!. 36 = 3, 674 , 160

“Geography” of configuration graph

... “breadth- first tree” possible first moves reachable in two steps but not one Figure 4: Breadth-First Tree � reachable configurations distance 90 ◦^ turns 90 ◦^ & 180 ◦^ turns 0 1 1 1 6 9 2 27 54 3 120 321 4 534 1, 5 2,256 9, 6 8,969 50, 7 33,058 227, 8 114,149 870, 9 360,508 1,887, 10 930,588 623, 11 1,350,852 2,644 ←diameter 12 782, 13 90, 14 276 ←diameter 3,674,160 3,674, Wikipedia Pocket Cube Cf. 3 × 3 × 3 Rubik’s cube: ≈ 1. 4 trillion states; diameter is unknown! ≤ 26

e.a e e.b

Figure 6: Edge Representation Representing Graphs: contd. The above representations are good for for sparse graphs where | E |� (| V |)^2. This translates to a space requirement = Θ(V + E) (Don’t bother with |. | ’s inside O/Θ). Adjacency Matrix:

  • assume V = { 1 , 2 ,... , |v|} (number vertices)
  • A = (aij ) = |V | × |V | matrix where i = row and j = column, and 1 if (i, j) � E aij = φ otherwise See Figure 7.
  • good for dense graphs where | E |≈ (| V |)^2
  • space requirement = Θ(V 2 )
  • cool properties like A^2 gives length-2 paths and Google PageRank ≈ A∞
  • but we’ll rarely use it Google couldn’t; V |≈ 20 billion = (| V )^2 ≈ 4. 1020 [50,000 petabytes]

a b c A =

0 0 1 1 0 1 0 1 0 1 2 3 1 2 3 Figure 7: Matrix Representation

Implicit Graphs: Adj(u) is a function or u.neighbors/edges is a method =⇒ “no space” (just what you need now)

High level overview of next two lectures:

Breadth-first search Levels like “geography”

... frontier s Figure 8: Illustrating Breadth-First Search

  • frontier = current level
  • initially {s}
  • repeatedly advance frontier to next level, careful not to go backwards to previous level
  • actually find shortest paths i.e. fewest possible edges Depth-first search This is like exploring a maze.
  • e.g.: (left-hand rule) - See Figure 9
  • follow path until you get stuck
  • backtrack along breadcrumbs until you reach an unexplored edge