ch 3 - 10/6/10 Paths in graphs The classic 15-puzzle...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
10/6/10 1 Paths in graphs The classic 15-puzzle Graph G = (V,E) V = {configurations of puzzle} E: edges between neighboring configurations c a g e b f j d i h a b c d e f h i explore(G,a): Finds a path from a to i. But this isn’t the shortest possible path! Distances in graphs Distance between two nodes = length of shortest path between them c a g e b f j d i h a b c d e f h i distance 0 distance 1 distance 2 distance 3 Physical model: Vertex – ping-pong ball Edge – piece of string dist(a,e) = ? dist(d,g) = ? Suppose we want to compute distances from some starting node s to all other nodes in G. Strategy: layer-by-layer first, nodes at distance 0 then, nodes at distance 1 then, nodes at distance 2, etc. Breadth-first search Suppose we have seen all nodes at distance · d. How to get the next layer? Solution: A node is at distance d+1 if: it is adjacent to some node at distance d it hasn’t been seen yet procedure bfs(G,s) input: graph G = (V,E); node s in V output: for each node u, dist[u] is set to its distance from s for u in V: dist[u] = 1 dist[s] = 0
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/04/2010 for the course CSE 101 taught by Professor Staff during the Spring '08 term at UCSD.

Page1 / 2

ch 3 - 10/6/10 Paths in graphs The classic 15-puzzle...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online