Computer Science analysis of algorithms assignment
i need code for all the questions
3 years ago
30
hw1.zip
hw1.zip
hw1/input1.txt
/* Remove this for your test. 8 // the total number of data points 1 3 // (x, y) of the first data point 2 4 // (x, y) of the second data point */ 8 1 3 2 4 5 5 4 6 7 9 11 23 3 7 5 6
__MACOSX/hw1/._input1.txt
hw1/output2.txt
Brute-Force: TIME <idx1, idx2> = dist Divide-and-Conquer: TIME <idx1, idx2> = dist
__MACOSX/hw1/._output2.txt
hw1/output3.txt
Brute-Force: TIME <idx1, idx2> = dist Divide-and-Conquer: TIME <idx1, idx2> = dist
__MACOSX/hw1/._output3.txt
hw1/input2.txt
12 1 3 4 5 6 3 2 7 5 8 1 9 23 4 12 3 54 2 23 8 9 10 21 23
__MACOSX/hw1/._input2.txt
hw1/output1.txt
/* remove this Brute-Force: execution time <index-of-point-a, index-of-point-b> = distance Divide-and-Conquer: execution time <index-of-point-a, index-of-point-b> = distance index starts from 1. /* Brute-Force: TIME <3, 8> = 1 Divide-and-Conquer: TIME <3, 8> = 1
__MACOSX/hw1/._output1.txt
hw1/CS 5200 Homework-1.pdf
CS 5200 Homework -1 (Deadline: 09-17-2023 11:59pm)
Instructor: Huiyuan Yang Q1. Complexity analysis [11%]
1) Consider the following algorithm (assume n >1)
int any-equal (int n, int A[ ][ ]) { Index i, j, k, m; for (i=1; i<=n; i++) for (j=1; j<=n; j++) for (k=1; k<=n; k++) for (m=1; m<=n; m++) if (A[i][j] == A[k][m] && !(i==k && j==m)) return 1; return 0; }
§ Show the best case; what is the best-case time complexity. [3%] § Show the worst case; what is the worst-case time complexity. [3%] § Try to improve the efficiency of the algorithm. [5%]
Q2. Growth of functions [15%]
1) Using the definition of O(.) and Ω(.), show that: [5%] 10𝑛 + 8 ∈ 𝑂(𝑛!), 𝑏𝑢𝑡 10𝑛 + 8 ∉ Ω(𝑛!)
2) Using the definition of Θ(. ) To show that: [5%] 2𝑛" + 2𝑛! + Θ(𝑛) ∈ Θ(𝑛")
3) Show by using limits that: [5%] I. 4!#$% ∈ Θ(16#)
II. 𝑛& − 𝑛! + 2𝑛 ∈ 𝜔(𝑛) Q3. Master theorem [9%]
Find the asymptotic bound of the recurrence equations T(n) using master theorem when applies:
I. 𝑇(𝑛) = 9𝑇(𝑛/3) + 𝑛! + 4 II. 𝑇(𝑛) = 6𝑇(𝑛/2) + 𝑛! − 2
III. 𝑇(𝑛) = 4𝑇(𝑛/2) + 𝑛" + 7
Q4. Divide-and-Conquer [25%]
1) Solve the following recurrence equation using recursion tree. [7%] 𝑇(𝑛) = T(n/2) + T(n/4) + T(n/8) + n
2) We have a problem of size n, and two solutions to solve this problem: [8%]
a. Solution-I: runs 𝑛! times to solve this problem directly (non-recursive algorithm). b. Solution-II: is a recursive algorithm, which takes 𝑛𝑙𝑜𝑔𝑛 times to split problem into
two sub-problems with equal size of 𝑛/2, and 𝑙𝑔𝑛 times to combine the results together.
Show whether the solution-I or solution-II is more efficient.
3) Consider the following algorithm, which return a pair with minimum and maximum in the list of A.[10%]
Min_Max (A, lt, rt) { if (rt – lt ≤ 1) Return (min(A[lt], A[rt]), max(A[lt], A[rt]));
(min1, max1) = Min_Max(A, lt, ⌊(lt + rt)/2⌋); (min2, max2) = Min_Max(A, ⌊(lt + rt)/2⌋ + 1, rt); Return (min(min1, min2), max(max1, max2)); }
§ Write the recurrence equation for the code above. [3%] § Solve the recurrence equation. [2%] § Show by example why the algorithm works. (hint: you can show each step when
call the Min_Max() function with a list of size 6, etc). [5%] Q5. Programming [35%] Implement the algorithm for finding the closest pair of points in two-dimension plane using the Divide-and-Conquer strategy.
§ Compile without errors (by calling make) [5%] § Use the brute-force approach to solve the problem and print out the execution time. [5%] § Use the Divide-and-Conquer strategy to solve the problem and print out the execution
time. [25%] Bonus: [Extra 20%] Design an algorithm: Given an integer array of 2𝑛 integers.
(1) Partition the list into two arrays of length 𝑛, such that the difference between the sums of the two sub-arrays is maximized; show the time complexity. [5%]
(2) Partition the list into two arrays of length 𝑛, such that the difference between the sums of the two sub-arrays is minimized; show the time complexity. [15%]
Submission requirements: [5%] Put the theory part and code into a folder with the name: [YourStudentID-YourName-HW1].zip
§ Following all the submission requirements [5%] § Theory part:
o You solution should be clear, concise but also contain enough details. o Only PDF format is allowed. o Please make sure the scanned PDF file is high-resolution and easily readable if
you wrote your solution on paper. § Programming part:
o A sanity check to test the correctness of your algorithm. o Your program will read an input file and write an output file. o Your program will be called like this: prompt>./hw1 ./input.txt. ./output.txt o Your code should be complied by calling the makefile.
__MACOSX/hw1/._CS 5200 Homework-1.pdf
hw1/input3.txt
100 1708 4089 2958 1117 8790 3068 5952 6402 2317 4467 6928 9093 7873 7420 574 3958 7961 9252 6733 1431 4112 1461 6662 2888 380 6149 257 2324 8911 3984 1456 6972 4425 766 8089 9567 3834 4042 2321 2504 4861 5601 1597 2734 3021 2171 6692 7334 7775 9778 8765 1888 7591 1780 1128 4324 4281 7737 3000 9544 8073 4456 6516 8850 5222 958 8418 5409 1352 739 7913 2565 6341 9510 1651 5714 8033 4695 3049 5809 825 8166 4049 8417 9946 5177 9093 4227 9267 2093 124 7340 2902 6640 2543 4476 3950 961 9885 1654 8052 4150 4219 745 3660 2222 6460 8046 6918 9509 3855 7743 4027 7904 2512 3974 9433 1605 4553 5052 51 4677 2393 9305 7670 4936 3781 7972 2249 19 9627 6653 4169 198 7399 4182 2421 211 2228 5691 6072 2435 9786 99 6691 2299 4073 2476 256 4979 7529 6659 9656 6274 5964 7326 7562 6098 1651 9811 2469 1278 2816 2990 7828 215 3524 6601 6778 2104 2292 2850 4539 8431 2950 1230 7082 3375 3707 7338 8354 1236 350 4363 3862 6314 8041 1424 8764 9692 7587 1233 7322 403 576 5151 619 4100 1752 7397
__MACOSX/hw1/._input3.txt
hw1/makefile
all: hw1 hw1:main.c gcc main.c -o hw1 -lm clean: rm hw1