Chapter 24 Graph Applications
1. See §24.1.
2. See §24.2.
3. A complete graph of n vertices has 10 edges. A tree always has 4 edges.
4. A complete graph of n vertices has n(n-1)/2 edges. A tree always has n-1 edges.
5. You can represent vertices in a graph using a vector. To represent edges using an edge
array, declare a two dimensional array. To represent edges using edge objects, create an
object for representing each edge. To represent edges using adjacency matrix, create a
square matrix. Each entry in the matrix indicates whether there is an edge between two
vertices. To represent edges using adjacency lists, create a vector. Each element is the
vector is another vector that contains all edges adjacent to a vertex.
6.
Edge array:
int edges[][2] =
{
{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5},
{1, 0}, {1, 2}, {1, 3}, {1, 4},
{2, 0}, {2, 1}, {2, 3}, {2, 4},
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5},
{4, 0}, {4, 1}, {4, 2}, {4, 3},
{5, 0}, {5, 3}
}
Vector of edge objects:
vector<Edge> list;
list.push_back(Edge(0, 1));
list.push_back(Edge(0, 2));
list.push_back(Edge(0, 3));
list.push_back(Edge(0, 4));
list.push_back(Edge(0, 5));
…
…
…
Adjacency matrix:
int adjacencyMatrix[6][6] =
{
{0, 1, 1, 1, 1, 1}, // node 0
{1, 0, 1, 1, 1, 0}, // node 1
{1, 1, 0, 1, 1, 0}, // node 2
{1, 1, 1, 0, 1, 1}, // node 3
{1, 1, 1, 1, 0, 0}, // node 4
{1, 0, 0, 1, 0, 0} // node 5
};
Adjacency list:
vector< vector<int> > list;
list[0] = vector<int>(6);
list[1] = vector<int>(6);
list[2] = vector<int>(6);
list[3] = vector<int>(6);
list[4] = vector<int>(6);
list[5] = vector<int>(6);
list[0].push_back(1); list[0].push_back(2); list[0].push_back(3); list[0].push_back(4); list[0].push_back(5);
list[1].push_back(0); list[1].push_back(2); list[1].push_back(3); list[1].push_back(4);
list[2].push_back(0); list[2].push_back(1); list[2].push_back(3); list[2].push_back(4);
list[3].push_back(0); list[3].push_back(1); list[3].push_back(2); list[3].push_back(4); list[3].push_back(5);
list[4].push_back(0); list[4].push_back(1); list[4].push_back(2); list[4].push_back(3);
list[5].push_back(0); list[4].push_back(3);
7. The Edge class defines an edge with two vertices. The Tree class defines a tree with a root
and parent to identify the parent for each vertex. The Graph class uses the Edge class and
Tree class to define graph operations.
8. What is [Link]("Seattle")? 0
What is [Link](5)? 5
What is [Link](4)? Kansas City
9. The getParent(int index) function.
10. See the text.
11. Omitted
12. A possible DFS starting from Atlanta for Figure 24.1 is
Atlanta, Houston, Dallas, Los Angeles, San Francisco, Seattle, Denver, Kansas City,
Chicago, New York, Boston, Denver, Miami
13. An instance of Tree.
14. There is a possibility that a vertex may be pushed into the stack more than once. Can you
give such an example?
15. An instance of Tree.
16. See the text
17. Omitted
18. A possible BFS starting from Atlanta for Figure 24.1 is
Atlanta, Miami, Houston, Dallas, Kansas City, New York, Los Angeles, San Francisco,
Denver, Chicago, Boston, Seattle.
19. You may prove it using induction on the path length.
20. See the text
21. See the text
22. getIndex("HTHTTTHHH".toCharArray()) returns 184.
getNode(46) returns HHHTHTTTH
23. No. Because the node may be changed.
24. The flipACell function in NineTailModel.h is defined as follows:
void flipACell(vector<char> & node, int row, int column);
If you change it to
void flipACell(vector<char> node, int row, int column);
the node will be changed in the flipACell function, but will not be changed after the
function exits.