compX123 1 s2 2024R

Java Python compX123   Assignment 1

s2 2024

This assignment is due on August 18 and should be submitted on Grade-scope. All submitted work must be done individually without consulting someone else’s solutions in accordance with the University’s “Academic Dishonesty and Plagiarism” policies.

Before you read any further, go to the last page of this document and read the Written Assignment Guidelines section.

Problem 1. (10 points)

Consider the following algorithm that given an array A of length n produces an array B where B[i] is the sum of the 16 elements of A following A[i].

1: function algorithm(A)

2: n ← length of A

3: B ← new array of size n − 16

4: for i ∈ [0 : n − 16] do

5: B[i] ← 0

6: for j ∈ [0 : 16] do

7: B[i] ← B[i] + A[i + j + 1]

8: return B

Use O-notation to upperbound the running time of the algorithm.

Problem 2. (25 points)

We want to build a queue for integer elements that in addition to the operations we saw during the lecture, also supports a difference() operation that on a given queue holding the elements Q1, . . . , Qn returns the difference between the sum of the first half of the elements and the sum of the second half of the elements:

All operations should run in O(1) time. Your data structure should take O(n) space, where n is the number of elements currently stored in the data structure. Your task is to:

a) Design a data structure that supports the required operations in the re-quired time and space.

b) Briefly argue the correctness of your data structure and operations.

c) Analyse the running time of your operations and space of your data structure.

Problem 3. (25 points)

In robotics, a common problem is figuring out what type of configuration a set of robots is in using very limited information. Here, we look at a small example of this.

You’re given n robots (each robot is uniquely identified simply using an in-teger between 1 and n) and you’re told that we know that the n robots are on a line (say the x-axis), but we don’t know the order of the robots along this line. Unfortunately, the robots don’t have GPS, so they can’t tell you their coordinate along the line. What you do have is a function adjacent(a,b) which takes two robots (i.e., integers) a and b as input and returns whether robot a and robot b are adjacent. You can assume that a single call to this function runs in O(1) time. You’re asked to design an algorithm that outputs the order of the robots along the line. You can output the robots from left to right, or from right to left. For full marks, your algorithm should run in O(n 2 ) time.

Example:

If we have three robots 1, 2, and 3 and the (unknown) order a compX123 Assignment 1 s2 2024R long the line from left to right is 2, 3, 1, your algorithm should return either 2, 3, 1 (the left to right order) or 1, 3, 2 (the right to left order). Returning any order other than these two is incorrect. In this example adjacent(1,2) would return false, since robot 1 and robot 2 aren’t adjacent on the line. adjacent(1,3) would return true, since robot 1 and robot 3 are adjacent.

Remember to:

a) describe your algorithm in plain English,

b) argue its correctness, and

c) analyze its time complexity.

Written Assignment Guidelines

• Assignments should be typed and submitted as pdf (no pdf containing text as images, no handwriting).

• Start by typing your student ID at the top of the first page of your submis-sion. Do not type your name.

• Submit only your answers to the questions. Do not copy the questions.

• When asked to give a plain English description, describe your algorithm as you would to a friend over the phone, such that you completely and unambiguously describe your algorithm, including all the important (i.e., non-trivial) details. It often helps to give a very short (1-2 sentence) de-scription of the overall idea, then to describe each step in detail. At the end you can also include pseudocode, but this is optional.

• In particular, when designing an algorithm or data structure, it might help you (and us) if you briefly describe your general idea, and after that you might want to develop and elaborate on details. If we don’t see/under-stand your general idea, we cannot give you marks for it.

• Be careful with giving multiple or alternative answers. If you give multiple answers, then we will give you marks only for "your worst answer", as this indicates how well you understood the question.

• Some of the questions are very easy (with the help of the slides or book). You can use the material presented in the lecture or book without proving it. You do not need to write more than necessary (see comment above).

• When giving answers to questions, always prove/explain/motivate your answers.

• When giving an algorithm as an answer, the algorithm does not have to be given as (pseudo-)code.

• If you do give (pseudo-)code, then you still have to explain your code and your ideas in plain English.

• Unless otherwise stated, we always ask about worst-case analysis, worst-case running times, etc.

• As done in the lecture, and as it is typical for an algorithms course, we are interested in the most efficient algorithms and data structures, though slower solutions may receive partial marks.

• If you use further resources (books, scientific papers, the internet,...) to formulate your answers, then add references to your sources and explain it in your own words. Only citing a source doesn’t show your understanding and will thus get you very few (if any) marks. Copying from any source without reference is considered plagiarism         

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: void fft(struct compx *xin,int n) 函数的功能是对输入的复数数组进行快速傅里叶变换(FFT)。 傅里叶变换是一种数学变换,可以将信号从时域转换到频域,用于分析信号的频率成分。FFT是一种高效的算法,可以加快傅里叶变换的计算速度。 该函数的输入参数为一个指向复数结构体的指针xin和一个整数n,表示输入数组的长度。复数结构体compx定义了一个复数的实部和虚部。 函数的实现过程如下: 1. 如果n等于1,即输入数组的长度为1,则不需要做任何计算,直接返回。 2. 定义一个临时复数结构体数组,长度为n。 3. 将输入数组按照位逆序重新排列,得到新的数组,存放在临时数组中。 4. 定义一个复数变量w,其实部为1,虚部为0。 5. 对输入数组长度进行二分,依次进行迭代操作,分别得到当前划分的长度k和旋转因子Wnk。 a. 划分长度k从2开始,每次乘以2,直到k小于等于n。 b. 旋转因子Wnk是一个复数,可以通过Euler公式计算:Wnk = cos(2π/n) + jsin(2π/n),其中j为虚数单位。 6. 对划分长度k进行迭代操作,依次对同一划分的不同位置进行计算。 a. 对于划分长度k,计算步长step为n/k。 b. 从0到n-1,以步长step进行迭代,依次获取当前划分的不同位置。 c. 定义一个复数变量旋转因子W,初始值为1,用于不同位置之间的旋转。 d. 对于当前划分的每个位置,计算出它对应的旋转因子Wnk,并进行计算和交换操作。 7. 重复步骤6,直到划分长度k等于n。 8. 将计算结果从临时数组中复制回输入数组。 以上就是fft(struct compx *xin,int n)函数的功能和实现过程的简要说明。通过该函数,可以对输入的复数组进行快速傅里叶变换,得到信号的频域表示。 ### 回答2: void fft(struct compx *xin,int n) 函数的功能是对输入的复数数组进行快速傅里叶变换。 快速傅里叶变换(FFT)是一种高效算法,用于计算离散傅里叶变换(DFT)。DFT将时域信号转换为频域信号,可以用于信号处理、图像处理、通信等领域。 该函数的输入参数为一个复数结构体指针xin和一个整数n。复数结构体中通常包含两个成员,一个是实部,一个是虚部,分别用来表示一个复数的实数部分和虚数部分。 在函数体内部,需要根据输入的复数数组进行FFT计算。具体的计算步骤如下: 1. 首先将输入的复数数组按照特定规则重新排序,以便后续计算能够高效进行。 2. 利用两个循环依次计算各个频率分量的幅度和相位。这个过程中使用了蝶形算子,可以大大减少计算量。 3. 将计算得到的频域信号存储到输出的复数数组中。 4. 返回结果,完成快速傅里叶变换。 需要注意的是,该函数只实现了快速傅里叶变换的计算过程,没有进行后续的逆变换或其他操作。如果需要逆变换或其他进一步处理,可以根据具体的需求进行扩展。 总之,该函数通过在输入的复数数组上进行特定计算,实现了快速傅里叶变换的功能,将时域信号转换为频域信号,为信号处理和相关领域的应用提供了基础计算能力。 ### 回答3: 快速傅里叶变换(FFT)是一种用于将离散时间信号转换为频域信号的算法。给定一个由复数构成的数组xin和数组长度n,函数fft将对xin进行FFT变换。 函数的输入参数为一个指向compx结构体的指针xin,它表示输入的复数组。compx结构体包含两个成员变量,一个是实部成员变量xreal,另一个是虚部成员变量ximag。 函数的第二个输入参数n表示数组的长度,即需要进行FFT变换的数据点的数量。 函数的功能是对输入的复数组进行快速傅里叶变换。快速傅里叶变换是一种高效的算法,它可以在O(nlogn)的时间复杂度内完成计算。该算法将复杂度较高的傅里叶变换过程分解为多个较为简单的计算步骤,从而加快了计算速度。 在函数体内部,会通过递归的方式将输入数组分成两部分,并对分解后的数组进行递归调用。递归的终止条件是数组长度n等于1的情况,即每个数组只包含一个数据点时,无需再进行分解。 在递归调用过程中,会根据当前数组长度n计算出频域中的频率分量,然后通过运算得到该频率分量对应的复数结果。最后,将分解后的结果合并为最终的FFT结果。 函数的返回类型为void,表示不返回任何结果,而是直接在输入的数组中进行原地修改。因此,函数调用后,输入数组xin中的数据将被修改为对应的FFT变换结果。 通过调用该函数,我们可以方便地将一个复数数组转换为频域信号,从而可以进行频域上的各种分析和处理操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值