128k views
5 votes
4 Counting Simple Paths in a DAG (45 pts) In this problem you will design an algorithm that takes as input a directed acyclic graph G=(V,E) and two vertices s and t, and returns the number of simple paths from s to t in G. For example, the directed acyclic graph below contains exactly four simple paths from vertex p to vertex v : pov, poryv, posryv, and psryv. Notice: your algorithm needs only to count the simple paths, not list them. (a) (15 pts) Design a recursive backtracking (brute-force) algorithm that determines the number of paths from s to t. Write down the pseudocode of your algorithm and prove its correctness, i.e., convince us that it works beyond any doubt. (Hint: using induction.) A solution with no proof will receive 0 points! Make your pseudocode as simple as possible. You will be graded on the elegance of your solution, not just the correctness. (b) (15 pts) Modify your pseudocode in part (a) to use memoization. Write down the modified pseudocode and explain your choice of the memoization table and the indices used for memoization. (Hint: What is a natural way to memoize solutions to subproblems that had already been computed?) (c) (15 pts) Design a bottom-up dynamic programming algorithm that counts the number of simple paths from s to t. Write the pseudocode and analyze its running time. Argue why your choice of the order in which you fill in the values is the correct one. (Hint: Think about the order on which the computation of the paths from each vertex depends.)

User Circey
by
8.3k points

1 Answer

3 votes

Final answer:

The straight-line distance on a two-dimensional path where 9 blocks are walked east and 5 blocks north is calculated using the Pythagorean theorem and is the square root of 106 blocks.

Step-by-step explanation:

To calculate the straight-line distance between two points in a two-dimensional path, we use the Pythagorean theorem.

The theorem states that in a right-angled triangle, the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides.

In this scenario, walking 9 blocks east and then 5 blocks north forms a right-angled triangle with the starting point where the two paths at right angles meet at the starting point.

To find the straight-line distance, which is the hypotenuse of this triangle, we calculate the square root of the sum of the squares of the other two sides:

  1. Let the eastward path be 'a' = 9 blocks.
  2. Let the northward path be 'b' = 5 blocks.
  3. The straight-line distance 'd' can be calculated using 'd = √(a² + b²)'.
  4. Substitute the given values to get 'd = √(9² + 5²)'.
  5. 'd = √(81 + 25)'.
  6. 'd = √106'.
  7. The straight-line distance is 'd = √106' blocks.

Therefore, the straight-line distance from the starting point to the final point is the square root of 106 blocks.

the complete question is:4 Counting Simple Paths in a DAG (45 pts) In this problem you will design an algorithm that takes as input a directed acyclic graph G=(V,E) and two vertices s and t, and returns the number of simple paths from s to t in G. For example, the directed acyclic graph below contains exactly four simple paths from vertex p to vertex v : pov, poryv, posryv, and psryv. Notice: your algorithm needs only to count the simple paths, not list them. (a) (15 pts) Design a recursive backtracking (brute-force) algorithm that determines the number of paths from s to t. Write down the pseudocode of your algorithm and prove its correctness, i.e., convince us that it works beyond any doubt. (Hint: using induction.) A solution with no proof will receive 0 points! Make your pseudocode as simple as possible. You will be graded on the elegance of your solution, not just the correctness. (b) (15 pts) Modify your pseudocode in part (a) to use memoization. Write down the modified pseudocode and explain your choice of the memoization table and the indices used for memoization. (Hint: What is a natural way to memoize solutions to subproblems that had already been computed?) (c) (15 pts) Design a bottom-up dynamic programming algorithm that counts the number of simple paths from s to t. Write the pseudocode and analyze its running time. Argue why your choice of the order in which you fill in the values is the correct one. (Hint: Think about the order on which the computation of the paths from each vertex depends.)

User Nicolas Charvoz
by
8.6k points