Assignment Four Solutions

Maximum Number of Edges

If a graph has n vertices, what is the maximum number of edges that this graph can have?
(assume that there can be at most one edge between any pair of vertices)

Solution: There is at most one edge between each pair of vertices, so, the question is, how many pairs are there? There are n(n-1)/2 pairs. Or, think about it this way: each vertex can be adjacent to at most n-1 other vertices. Therefore, the adjacency list of each vertex contains at most n-1 entries. Each edge is represented in the adjacency lists of each of its end vertices. Therefore, there are at most n(n-1)/2 edges.

Graph Application

In order to graduate, a student must take and pass all required courses. To complicate matters, each course has a list of prerequisites, i.e. a list of courses that must be taken and passed before the student may register for the course. For example, the list of required computer science courses might be CS100, CS103, CS200, CS250, CS260, CS300, CS303, CS360, CS370, CS400, and CS405. Prerequisites for these courses might be something like CS100 (pre: none), CS103 (pre: none), CS200 (pre: CS100), CS250 (pre: CS100, CS103), CS260 (pre: CS200, CS250), CS300 (pre: CS103, CS200), CS303 (pre: CS260, CS103), CS360 (pre: CS300, CS250), CS370 (pre: CS300), CS400 (pre: CS200, CS300), CS405 (pre: CS260, CS360, CS400). Is it possible to graduate, given these requirements? If not then explain why not; otherwise, give a prerequisite to one of the courses that makes it impossible to graduate. Transform this example into a graph. In graph theoretic terms, write what it means for it to be possible to graduate and write what it means for it to be impossible to graduate.

Solution: It is possible to graduate. Simply complete the courses in the following order: CS100, CS103, CS200, CS250, CS260, CS300, CS303, CS360, CS370, CS400 and finally CS405.

To make it impossible, we could make CS405 a prerequisite for CS260.

Here is the directed graph:

It is possible to graduate if and only if there are no directed cycles. Notice that the graph above has cycles but they aren't directed.

Adjacency Matrices and Lists

Given the following adjacency list representation of a graph, draw the corresponding graph and give the corresponding adjacency matrix. List the order in which the vertices would be visited in a DFS traversal starting at vertex m. Do the same for a BFS traversal (use the descriptions of these algorithms given in class or in the lecture notes).
mx,q,r
nq,u,o
on,r,s,p,v
po,s,z
qm,t,n
rm,u,y,o,s
sr,o,p
tq,u
ut,n,r
vx,y,o,w
wv,z
xm,v
yr,v
zp,w

Solution: Here is a drawing of the graph:

Adjacency Matrix:
m n o p q r s t u v w x y z
mF F F F T T F F F F F T F F
nF F T F T F F F T F F F F F
oF T F T F T T F F T F F F F
pF F T F F F T F F F F F F T
qT T F F F F F T F F F F F F
rT F T F F F T F T F F F T F
sF F T T F T F F F F F F F F
tF F F F T F F F T F F F F F
uF T F F F T F T F F F F F F
vF F T F F F F F F F T T T F
wF F F F F F F F F T F F F T
xT F F F F F F F F T F F F F
yF F F F F T F F F T F F F F
zF F F T F F F F F F T F F F

DFS traversal: m, x, v, y, r, u, t, q, n, o, s, p, z, w
BFS traversal: m, x, q, r, v, t, n, u, y, o, s, w, p, z

On average, how many steps would it take to determine whether or not two vertices are adjacent in a graph with 13,342 vertices and 79,243 edges. The answer depends on how we represent the graph so give two answers.

Solution:
Adjacency Matrix: 1
Adjacency List: 1 + (79,243 / 13,342)
Adjacency list representations are just like hashtables that use chaining (see lectures notes). To determine if an element is in a hashtable we calculate its position in the table, O(1) steps, and then search the list at that position; likewise, to determine if two vertices are adjacent in a adjacency list representation, we first find the position of one vertex in the table, 1 step, and then search the adjacency list at that position. For hashtables the average number of steps required to determine if an element is in the table is 1 + load factor/2. Recall that the load factor is just the number of elements in the hashtable divided by the size of the table. The "load factor" in this example is (2 * 79,243 / 13,342) because the table has size 13,342 and the number of elements in the table is 2*79,243 (remember, each edge is represented in the adjacency lists of each of its end vertices).

Pruning Graphs

Turn the graph from the previous question into a tree by removing edges. List the edges that you removed.

Solution: There are many solutions to this problem so we give only one here. The removed edges are drawn as dashed line segments.

Turn the graph from the previous question into three trees by removing edges. List the edges that you removed.

Solution: In addition to the edges removed to turn the graph into a tree, we also removed two more to create three trees. These two edges are drawn as dashed line segments.

Given only the number v of vertices and the number e of edges in a graph:

  1. Is it possible to predict the number of edges that must be removed to turn the graph into a tree if the graph is connected?
    Answer: Yes, by the theorem stating six equivalent ways to define a tree , every tree has v-1 edges. Therefore, to transform a connected graph into a tree we must remove exactly e - (v - 1) edges.
  2. What if the graph is not connected?
    Answer: No. A tree is by definition connected. Removing edges from an unconnected graph will never make the graph connected. Therefore, it can never be a tree.
  3. Is it possible to predict the number of edges that must be removed to turn the graph into t trees if the graph is connected?
    Answer: Yes. We first remove e - (v - 1) edges to obtain a tree. If we then remove one more edge, we disconnect our tree into two trees. Removing another edge results in three trees, and so on. Thus, we must remove e - (v - 1) + (t - 1) edges to create t trees.

K-ary Trees

Give an example to show that the following statement is false: if a tree is rooted at a leaf and the maximum degree of a vertex in the tree is k then the tree is a (k-1)-ary tree.

Solution: The following tree, consisting of only one edge, is the only example:


The maximum degree of a vertex in the tree is k=1. If we root the tree at either vertex the resulting tree is 1-ary, not 0-ary.

Missionaries and Cannibals Variations

How many solutions are there to the missionaries and cannibals problem when there are two missionaries, two cannibals and the boat can hold at most two people at a time.

Solution: Following is a labelled list of all valid states:

State 0  State 1  State 2  State 3    
  2 0      2 0      2 0      1 1           
  2 0      1 1      0 2      1 1     
  B -      B -      B -      B -    

State 4  State 5  State 6      
  0 2      0 2      0 2                  
  2 0      1 1      0 2            
  B -      B -      B -            

State 7  State 8  State 9  State 10    
  2 0      2 0      2 0      1 1           
  2 0      1 1      0 2      1 1     
  - B      - B      - B      - B            

State 11 State 12 State 13     
  0 2      0 2      0 2                  
  2 0      1 1      0 2            
  - B      - B      - B                 
The initial state is labelled "0" and the goal state "13". The graph corresponding to this state space looks like the following:
8   9        3    2
 \ / \      / \  /
  0   1 - 12   13
 / \ /      \ /  \
11  10       4    5

6  7 
Thus, there are an infinite number solutions to the problem, however, there are only four optimal solutions.

Dijkstra's Algorithm

The result of appying Dijkstra's algorithm on the weighted graph starting at vertex A:

The edges of the spanning tree are red and the distances from A are given in red beside each vertex. The vertices were added to the spanning tree in the following order: ABCDIHGEOKJPRFTMLWUXNVSQYZ. Thus, the shortest path from A to Z is ABDIGKPTMWVYZ.