24 Review Graph
24 Review Graph
1. See §24.1.
2. See §24.2.
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<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:
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 graph1.getIndex("Seattle")? 0
What is graph1.getDegree(5)? 5
11. Omitted
14. There is a possibility that a vertex may be pushed into the stack more than once. Can you
give such an example?
17. Omitted
Atlanta, Miami, Houston, Dallas, Kansas City, New York, Los Angeles, San Francisco,
Denver, Chicago, Boston, Seattle.
If you change it to
the node will be changed in the flipACell function, but will not be changed after the
function exits.