exercise

9.3 Number of paths in DAGs

a)

We prove the existence of a topological sorting by providing a constructive method. Let’s first remove and all the edges connect to them from . Removing nodes and edges in DAG does not create cycles, so the new graph is still a DAG, and therefore has a topological sorting. Let this topological sorting be . Now we construct a new topological sorting for : . We claim this sequence to be a topological sorting:

  • All the relative orders in remains the same.
  • Since is a source, there is no edge pointing to . Then is before all other vertices, so it can be placed at position
  • Since is a sink, there is no edge pointing from . Then is behind all other vertices, so it can be placed at position

This proves the claim.

b)

Take any edge from the --path , we have according to the property of topological order. Therefore, if we apply this property to every consecutive pair in --path, we get

c)
  1. Dimensions: The table is one dimensional and has a size of
  2. Subproblems: the entry dp[i] is the number of possible paths from to
  3. Recursion:
    1. Base Case: dp[1] is
    2. Normal Case: the entry dp[i] is the sum of dp[j] where node point to node
  • Justification:
  1. We assume to are correctly calculated.
  2. Every path to must go through one of the nodes that point to .
  3. To calculate the number of possible paths to , we can add up all the paths that leads to each nodes that point to
  4. Since is in topological order, all the nodes we need to calculate must have been calculated.
  5. Calculation order: iterate through the topological order.
countPaths(n, Adj):
 dp[1] = 1
 for i = 1 to n:
 for each j in Adj[i]: 
 dp[j] = dp[j] + dp[i] 
 return dp[n]
  1. Extracting the solution: take the value in dp[n]
  2. Running time:
d)

Yes it is still possible we just need to change the initialization process the transition process is same as before, and we get the answer from

9.4 Bipartite graphs, Eulerian graphs and painting rooms

b)

We prove the claim by contraposition Assume has odd number of edges. Assume is bipartite. If there exists a closed walk in it must alternates between two sets and . To return back to the starting node, the walk must have even number of steps, meaning every closed walk has even number of edges. If is Eulerian, the closed Eulerian walk must have odd number of edges. This contradicts to the assumption we made.

is Eulerian and has an even number of vertices means there exists a closed Eulerian walk that contains even number of vertices. To connected this cycle we need exactly same number of edges to the vertices. Since the Eulerian walk travels each edge in exactly once, the number of edges in must also be even.

c)

No, it is not possible We model the floor plan as a graph, where each room is a node and a door between two rooms means there is an edge between these two rooms. The task is to check if this graph is bipartite (room is red means this node is in set , purple means in set ). Using Theorem 1, a graph is bipartite, only if it does not contains any cycle of odd length. However the sequence (Best friend’s room Bar Hallway Kitchen Your room Best friend’s room) forms a cycle of length 5. Therefore this graph is not bipartite, which means such a coloring plan does not exists.