0% found this document useful (0 votes)
29 views5 pages

CS721 q9 Spring 2024 HW - 2

The document describes three homework problems - (1) solving a two city scheduling problem using a greedy algorithm, (2) improving a dynamic programming solution to count the number of ways to divide numbers into two equal sums, and (3) finding the minimum number of months to complete all courses given prerequisite relationships between courses and time to complete each course.

Uploaded by

mdevichakradhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

CS721 q9 Spring 2024 HW - 2

The document describes three homework problems - (1) solving a two city scheduling problem using a greedy algorithm, (2) improving a dynamic programming solution to count the number of ways to divide numbers into two equal sums, and (3) finding the minimum number of months to complete all courses given prerequisite relationships between courses and time to complete each course.

Uploaded by

mdevichakradhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2

CS 721: Algorithms

April 18, 2024

Name:

WSU ID:

1
Assignment 2 2

1. [Homework Question] Greedy Algorithms


Deliverables: Please submit a pdf file containing the solution.
Name your file textitWSUID hw greedy algo.pdf
a. [20 points] Your task is to write a function in Python that solves the problem using an optimal
greedy strategy.
b. [60 points] Provide the proof of optimality for your choice of ordering by applying exchange
arguments.
c. [20 points] Provide the time complexity and space complexity of your approach.

Problem Statement: A company is planning to interview 2n people. Given the array costs where
costs[i] = [aCosti, bCosti ], the cost of flying the ith person to city a is aCosti , and the cost of flying the
ith person to city b is bCosti .
Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.
Example 1:
Input: costs = [[10,20],[30,200],[400,50],[30,20]]
Output: 110
Explanation:
• The first person goes to city A for a cost of 10.
• The second person goes to city A for a cost of 30.
• The third person goes to city B for a cost of 50.
• The fourth person goes to city B for a cost of 20.
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
Example 2:
Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
Output: 1859
Example 3:
Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
Output: 3086

Link: https://leetcode.com/problems/two-city-scheduling/description/
Assignment 2 3

2. [Extra Credit] Dynamic Programming


Deliverables: Please submit a pdf file containing the solution.
Name your file WSUID extracred dp.pdf
a. [60 points] Your task is to write a function in Python that outperforms the provided recf v0 0()
function.
You are required to use the top-down approach without any loops. Your solution should outperform
the provided recf v0 0() function, which is also recursive and loop-free. Explain the recursive
formulation of your solution.
Additionally, compare the execution time of your function with the provided recf v0 0() function
for a few values of n (e.g., 100, 200, 300, 400, 500) and discuss your observations.
b. [30 points] Draw the recursive call graph for the value n=7. Ensure that the graph is neatly drawn,
and redundant nodes in the graph are merged into a single node.
c. [10 points] Provide the time complexity and space complexity of your approach.

Problem Statement: Your task is to count the number of ways numbers 1, 2, . . . , n can be divided
into two sets of equal sum. For example, if n = 7, there are four solutions:

{1, 3, 4, 6} and {2, 5, 7}


{1, 2, 5, 6} and {3, 4, 7}
{1, 2, 4, 7} and {3, 5, 6}
{1, 6, 7} and {2, 3, 4, 5}

Input
The only input line contains an integer n.
Output
Print the answer modulo 109 + 7.
Constraints
1 ≤ n ≤ 500
Example
Input:

Output:

Link: https://cses.fi/problemset/task/1093
Assignment 2 4

3. [Missed Quiz] Graph Algorithms


Deliverables: Please submit a pdf file containing the solution.
Name your file WSUID missed graphtopo.pdf
a. [80 points] Your task is to write a function in Python that solves the problem.
b. [20 points] Explain the choice of your algorithm.

Problem Statement: You are given an integer n, which indicates that there are n courses labeled from
1 to n. You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej ]
denotes that course prevCoursej has to be completed before course nextCoursej (prerequisite relation-
ship). Furthermore, you are given a 0-indexed integer array time where time[i] denotes how many months
it takes to complete the (i + 1)th course.
You must find the minimum number of months needed to complete all the courses following these rules:
• You may start taking a course at any time if the prerequisites are met.
• Any number of courses can be taken at the same time.
Return the minimum number of months needed to complete all the courses.
Note: The test cases are generated such that it is possible to complete every course (i.e., the graph is
a directed acyclic graph).
Example 1:
Input: n = 3, relations = [[1,3],[2,3]], time = [3,2,5]
Output: 8

Explanation: The figure above represents the given graph and the time required to complete each
course. We start course 1 and course 2 simultaneously at month 0. Course 1 takes 3 months and course
2 takes 2 months to complete respectively. Thus, the earliest time we can start course 3 is at month 3,
and the total time required is 3 + 5 = 8 months.
Example 2:
Input: n = 5, relations = [[1,5],[2,5],[3,5],[3,4],[4,5]], time = [1,2,3,4,5]
Output: 12
Assignment 2 5

Explanation: The figure above represents the given graph and the time required to complete each
course. You can start courses 1, 2, and 3 at month 0. You can complete them after 1, 2, and 3 months
respectively. Course 4 can be taken only after course 3 is completed, i.e., after 3 months. It is completed
after 3 + 4 = 7 months. Course 5 can be taken only after courses 1, 2, 3, and 4 have been completed,
i.e., after max(1, 2, 3, 7) = 7 months. Thus, the minimum time needed to complete all the courses is
7 + 5 = 12 months.
The given graph is a directed acyclic graph.

Link: https://leetcode.com/problems/parallel-courses-iii/description/

Appendix
Reading materials:
• Dynamic Programming: https://leetcode.com/discuss/general-discussion/458695/Dynamic-Programming-
Patterns
• Greedy Exchange Arguments: https://codeforces.com/blog/entry/63533

Definition for function: recf v0 0(n, id=2, set1sum=0, set2sum=0)

1 from f u n c t o o l s i mpo rt l r u c a c h e
2 im po rt time
3
4 #l r u c a c h e v e r s i o n
5 @ l r u c a c h e ( m a x s i z e=None )
6 d e f r e c f v 0 0 ( n , i d =2 , set1sum =0 , set2sum =0) :
7 #Bases
8 i f ( id > n) :
9 set2sum = set2sum + 1
10 i f ( set1sum == set2sum ) :
11 return 1
12 else :
13 return 0
14
15 #p i c k f o r s e t 1
16 s 1 = r e c f v 0 0 ( n , i d +1 , set1sum+id , set2sum )
17
18 #p i c k f o r s e t 2
19 s 2 = r e c f v 0 0 ( n , i d +1 , set1sum , set2sum+i d )
20
21 r e t = i n t ( ( s 1+s 2 ) % ( 1 e9 +7) )
22 #r e t = s 1+s 2
23 return ret
24
25 n = i n t ( i n p u t ( ’ Enter t h e v a l u e ( n ) : ’))
26
27 # Measure t h e time t a k e n by t h e f u n c t i o n
28 s t a r t = time . time ( )
29 print ( int ( recf v0 0 (n) ) )
30 end = time . time ( )
31 p r i n t ( ’ Time t a k e n by r e c f v 0 0 f u n c t i o n f o r n = ’ , n , ’ : ’ , end − s t a r t , ’ seconds ’ )

You might also like