21.11.2014 Views

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

45 for (int k = 0; k < 9; k++)<br />

46 {<br />

47 vector node = getNode(u);<br />

48 if (node[k] == 'H')<br />

49 {<br />

50 int v = getFlippedNode(node, k);<br />

51 int numberOfFlips = getNumberOfFlips(u, v);<br />

52 // Add edge (v, u) for a legal move from node u to node v<br />

53 // with weight numberOfFlips<br />

54 edges.push_back(<strong>Weighted</strong>Edge(v, u, numberOfFlips));<br />

55 }<br />

56 }<br />

57 }<br />

58<br />

59 return edges;<br />

60 }<br />

61<br />

62 int <strong>Weighted</strong>NineTailModel::getNumberOfFlips(int u, int v)<br />

63 {<br />

64 vector node1 = getNode(u);<br />

65 vector node2 = getNode(v);<br />

66<br />

67 int count = 0; // Count the number of different cells<br />

68 for (unsigned i = 0; i < node1.size(); i++)<br />

69 if (node1[i] != node2[i]) count++;<br />

70<br />

71 return count;<br />

72 }<br />

73<br />

74 int <strong>Weighted</strong>NineTailModel::getNumberOfFlips(int u)<br />

75 {<br />

76 return static_cast(tree)->getCost(u);<br />

77 }<br />

78 #endif<br />

<strong>Weighted</strong>NineTailModel extends NineTailModel to build a <strong>Weighted</strong>Graph to model the<br />

weighted nine tail problem (line 9). For each node u, the getEdges() function finds a flipped node v<br />

<strong>and</strong> assigns the number of flips as the weight for edge (u, v) (line 50). The getNumberOfFlips(int<br />

u, int v) function returns the number of flips from node u to node v (lines 62–72). The number of<br />

flips is the number of the different cells between the two nodes (line 69).<br />

The <strong>Weighted</strong>NineTailModel constructs a <strong>Weighted</strong>Graph (line 33) <strong>and</strong> obtains a<br />

ShortestPathTree rooted at the target node 511 (line 36). It then assigns it to tree* (line 36).<br />

tree, of the Tree* type, is a protected data field defined NineTailModel. The functions defined in<br />

NineTailModel use the tree property. Note that ShortestPathTree is a child class of Tree.<br />

42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!