2/23/2016 Explicit vs Implicit Graphs • Explicit Graph: – All vertices are identified individually and represented separately. CSE373: Data Structures and Algorithms – All edges are identified individually and represented separately. Implicit Graphs • Implicit Graph: – Only a subset, possibly only one, of the vertices is given an explicit representation. (The others are implied.) – Only a subset, and possibly zero, of the edges is given an Steve Tanimoto explicit representation. Winter 2016 – A set of “operators” is provided that can be used to construct “new” edges and vertices. Winter 2016 CSE 373: Data Structures & Algorithms 2 Example Explicit Graph Example Implicit Graph G = (V, E), V = {Seattle, Chicago, Boston}, G = (V, E), V = {v 0 , v 1 , …, v 9 }, E = {(Seattle, Chicago), (Chicago, Boston), (Boston, Seattle)} v 0 = {0} Chicago Operators: { 0 , 1 } Seattle 0 precondition: i < 9 Boston transition: f 0 (v i ) = f 0 ({i}) = {i+1} v j = 0 ({i}) is a (possibly) new vertex (v i , v j ) is a (possibly) new edge 1 precondition: i < 7 transition: f 1 (v i ) = f 1 ({i}) = {i+3} v j = 1 ({i}) is a (possibly) new vertex (v i , v j ) is a (possibly) new edge Winter 2016 CSE 373: Data Structures & Algorithms 3 Winter 2016 CSE 373: Data Structures & Algorithms 4 Some Implicit Graphs Can Be Made Explicit The Towers of Hanoi (Puzzle) • G = (V, E) • V = {{0}, …, {9}} • E = {({0} , {1}), ({1} , {2}), ({2} , {3}), … , ({8} , {9}), ({0} , {3}), ({1} , {4}), ({2} , {5}), … , ({6} , {9}) } 10 + 7 = 17 edges v 0 v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 v 9 Winter 2016 CSE 373: Data Structures & Algorithms 5 Winter 2016 CSE 373: Data Structures & Algorithms 6 1
2/23/2016 Example: TOH* Graph TOH Graph – properties G = (V, E) v 0 = [[4321],[ ], [ ]] G n = TOH graph for an n-disk TOH puzzle. v k = [p 0 , p 1 , p 2 ]; p m = [d m,0 , d m,1 , …, d m,n m ] G n = (V n , E n ) i, j precondition : p i != [] and What is | V n | ? if p j != [] then d i,ni < d j,nj i, j transition : p i : [ ,d i,ni ], p j : [ ] p i : [ ], p j : [ ,d i,ni ] What is | E n | ? The precondition is that peg i must have at least one disk, and if peg j has any disks, Diameter of G n ? the top (last) disk on peg i must be smaller than the last disk on peg j. Longest distance between two vertices of the The transition is that the top disk on peg i is removed from peg i and put on the top graph. of the pile on peg j. * Towers of Hanoi Winter 2016 CSE 373: Data Structures & Algorithms 7 Winter 2016 CSE 373: Data Structures & Algorithms 8 TOH Graph – properties TOH Graph – properties G n = TOH graph for an n-disk TOH puzzle. n=1 G n = TOH graph for an n-disk TOH puzzle. n=1, n=2 G n = (V n , E n ) G n = (V n , E n ) What is | V n | ? What is | V n | ? 3 3 9 What is | E n | ? What is | E n | ? 3 3 12 Diameter of G n ? Diameter of G n ? 1 1 3 Longest distance between two vertices of the Longest distance between two vertices of the graph. graph. Winter 2016 CSE 373: Data Structures & Algorithms 9 Winter 2016 CSE 373: Data Structures & Algorithms 10 TOH Graph – properties TOH Graph – properties G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3 G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3, n=4 G n = (V n , E n ) G n = (V n , E n ) What is | V n | ? What is | V n | ? 3 9 27 3 9 27 81 What is | E n | ? What is | E n | ? 3 12 39 3 12 39 120 Diameter of G n ? Diameter of G n ? 1 3 7 1 3 7 15 Longest distance between two vertices of the Longest distance between two vertices of the graph. graph. Winter 2016 CSE 373: Data Structures & Algorithms 11 Winter 2016 CSE 373: Data Structures & Algorithms 12 2
2/23/2016 TOH Graph – properties TOH Graph – properties G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3, n=4 G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3, n=4 G n = (V n , E n ) G n = (V n , E n ) What is | V n | ? What is | V n | ? 3 9 27 81 3 9 27 81 | V n | = 3 | V n-1 | | V n | = 3 | V n-1 | What is | E n | ? What is | E n | ? 3 12 39 120 3 12 39 120 | E n | = 3 | V n | + 3 Diameter of G n ? Diameter of G n ? 1 3 7 15 1 3 7 15 Longest distance between two vertices of the Longest distance between two vertices of the graph. graph. Winter 2016 CSE 373: Data Structures & Algorithms 13 Winter 2016 CSE 373: Data Structures & Algorithms 14 TOH Graph – properties TOH Graph – properties G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3, n=4 G n = TOH graph for an n-disk TOH puzzle. n=1, n=2, n=3, n=4 G n = (V n , E n ) G n = (V n , E n ) What is | V n | ? What is | V n | ? 3 9 27 81 3 9 27 81 | V n | = 3 | V n-1 | | V n | = 3 | V n-1 | What is | E n | ? What is | E n | ? 3 12 39 120 3 12 39 120 | E n | = 3 | V n | + 3 | E n | = 3 | V n | + 3 Diameter of G n ? Diameter of G n ? 1 3 7 15 1 3 7 15 D(G n ) = 2 D(G n-1 ) + 1 D(G n ) = 2 D(G n-1 ) + 1 Longest distance between two vertices of the Longest distance between two vertices of the graph. graph. Winter 2016 CSE 373: Data Structures & Algorithms 15 Winter 2016 CSE 373: Data Structures & Algorithms 16 n_disks = 4 n_disks = 3 With heuristic distances to landmarks. Solution path shown (the golden path) Winter 2016 17 CSE 373: Data Structures & Algorithms Winter 2016 18 CSE 373: Data Structures & Algorithms 3
2/23/2016 Creating Graph Layouts Heuristic distance d h (v 0 , v 1 ) • Each vertex should be assigned a location on the “page” that Natural method: weight larger disks more heavily. Then add up the helps reflect the structure of the graph. absolute values of differences. n - 1 d h (v 0 , v 1 ) = 2 k | q k (v 0 , v 1 ) | • One method is based on “landmark vertices.” Formulate a distance function d(v 0 , v 1 ). Identify 3 or more “landmark” states in Sigma: k = 0 {L 1 , L 2 , L 3 , . . . , L n } and assign them (x,y) locations. For vertex v, use d to find barycentric coordinates Where q k (v 0 , v 1 ) = 1 if disk k is on different pegs in v 0 and v 1 , and 0 (b 1 , b 2 , . . . , b n ) in terms of L 1 , . . . , L n , and plot v. otherwise. Disk 0 is the smallest disk. (n is the number of disks.) Winter 2016 19 CSE 373: Data Structures & Algorithms Winter 2016 20 CSE 373: Data Structures & Algorithms TOH Graph Laid Out with d m and barycentric Layout depends on distance function coordinates. Let d m (v 0 , v 1 ) = minimum number of moves needed to reach v 0 from v 1 . Note that the golden path is now a straight Then the solution path can be a straight line. line. Winter 2016 21 CSE 373: Data Structures & Algorithms Winter 2016 22 CSE 373: Data Structures & Algorithms Summary Graphs can be represented explicitly, implicitly, or with a combination of methods. Some graphs come in families that share properties. e.g., TOH graph G 4 {G n such that n > 0} Graph layout can reveal structural information. Problems and puzzles can be represented by graphs. Path-finding methods can be used to solve problems. Winter 2016 23 CSE 373: Data Structures & Algorithms 4
Recommend
More recommend