. Problems Let’s play a game. Or, more precisely, design and analyse some algorithms that might be used in a simple two-dimensional (2D) game. Figure 1: A tiny example game scenario. Enemy...

1 answer below »
this assignment is based on the algorithm and complexity . in which few question we have to make algorithm and in others question we have to make the a paragraph according to the question . its 30% weightage . make sure there should be no plargism in my assignment .


. Problems Let’s play a game. Or, more precisely, design and analyse some algorithms that might be used in a simple two-dimensional (2D) game. Figure 1: A tiny example game scenario. Enemy (AI-controlled) players are shown in blue. The xed location of the human player is marked by the red cross. Consider a game played on an two-dimensional grid, whose Cartesian coordinates range from ( M; M) : : : (M; M). Figure 1 depicts a game board for which M = 4. The game contains a xed number N of enemy players, who are AI-controlled. Each player (including the human player and each enemy AI) is located at some arbitrary but xed position (x; y) on the board, i.e. for which M x M and M y M. The human player can perform certain attacks, which damage all enemy players within a certain range of the human player. To avoid the need to calculate with expensive multiplication and square root operations, we will approximate the range by a square situated about the human player. 1 Speci cally, given some xed bound b, for which 0 b M, if the human player is located at position (px; py) then all enemy players that lie within or on the borders of the box whose bottom-left corner is (px b; py b) and whose top-right corner is (px + b; py + b) are a ected by the attack. Figure 1 depicts an example box for the bound b = 1:5. 1. [1 mark] Implement an (1) function to determine whether an enemy at position (x; y) is a ected by the player’s attack, given the player’s location (px; py): function IsAffected((px; py); (x; y); b) . . . 2. [2 marks] Suppose (for now) that the positions of the enemy players are stored in an unsorted array A[0] : : : A[N 1]. The following function uses the divide and conquer approach to mark all enemy players a ected by a player attack. Marking is implemented by the Mark function whose details are not important. (Note: the division below is integer division, i.e. it rounds down to the nearest integer.) function MarkAffected((px; py); A; b) MarkAffectedDC((px; py); A; 0; N 1; b) function MarkAffectedDC((px; py); A; lo; hi; b) . assume lo hi if lo = hi then if IsAffected((px; py); A[lo]; b) then Mark(A[lo]) else mid lo + (hi lo)=2 MarkAffectedDC((px; py); A; lo; mid; b) MarkAffectedDC((px; py); A; mid + 1; hi; b) Explain the purpose of the outermost \if /else" test. In particular, suppose we removed the line \if lo = hi" and the \else" line. In no more than a paragraph explain how this would a ect the algorithm. 3. [2 marks] Consider the following recurrence relations. Which one best describes the worst-base complexity of the MarkAffectedDC function, whose input size is n = hi lo + 1 and whose basic operations are calling IsAffected and Mark? Justify your answer in no more than a paragraph of text. T(1)=1 T(1)=2 T(1)=0 T(1)=2 T (n) = 2T (n=2) T (n) = 2T (n=2) + 2 T (n) = 2T (n=2) T (n) = 2T (n=2) 4. [2 marks] Use telescoping (aka back substitution) to derive a closed form for your chosen recurrence relation and thereby prove that the worst case complexity of the MarkAffectedDC algorithm is in (n). 5. [2 marks] Can we do better than this? Recall that the human player is at some xed location (px; py). Your task is to work out how you would sort the array A so that those enemy AIs that need to be marked can be identi ed in log(N) time. Speci cally, complete the following comparison function that would be used while sorting the ar-ray A. Here, (x1; y1) and (x2; y2) are two points from the array A. The function should return true if it considers the rst point to be less than or equal to the second, and should return false otherwise. Your function can use the player’s coordinate (px; py) as global variables, i.e. you are allowed to refer to px and py in your function. 2 function LessOrEqualTo((x1; y1); (x2; y2)) . . . 6. [3 marks] Now, supposing the array has been sorted using your comparison function, implement an algorithm whose worst case complexity is in (log(N)) that determines which array elements should be marked. Your function should take the bound b as an argument, and may also take the player’s coorinates (px; py). 7. [2 marks] How many elements will need to be marked in the worst case and what, therefore, would be the worst-case complexity of an algorithm that, given an array A sorted according to your comparison function, implemented the behaviour of MarkAffected above? Express your answer in Big-Theta notation. 8. [2 marks] The worst case is quite pessimistic. On average, we might expect that enemy AIs are uniformly distributed throughout the game board. Let d be the expected number of enemy AIs contained in or on the borders of a box of bound b (i.e. whose width and height are each 2b) on the board. For simplicity, we limit our attention to boxes that are contained entirely within the game baord. Consider an algorithm that, given an array A sorted according to your comparison function, imple-mented the behaviour of MarkAffected above by rst using your (log(N)) function to determine the elements that need to be marked (of which we expect there to be d), and then marking those d elements. We might characterise its expected complexity as: log(N) + d Derive a formula for d in terms of b, M and N. 9. [2 marks] As a point of comparison, what is the expected complexity of the divide-and-conquer algorithm above in this average case, expressed in Big-Theta notation? Justify your answer in no more than a paragraph of text. 10. [2 marks] Now compare the best case complexity of the divide-and-conquer algorithm and the one that uses a pre-sorted array A described above. Express the best-case complexity of each in Big-Theta notation as a function of N. Justify both in no more than a paragraph of text. 11. [4 marks] Suppose now that the enemy AIs can send each other messages. However, two AIs, whose positions are (x1; y1) and (x2; y2) respectively, can directly communicate only if the line (x1; y1) | (x2; y2) that connects them does not pass through the rectangle (including its borders) surrounding the human player whose bottom left corner is (px b; py b) and whose top-right corner is (px + b; py + b), where (px; py) is the human player’s position. Implement a function that determines whether two enemy AIs can directly communicate. Include a brief description (no more than a single paragraph) of how your function works, i.e. the rationale behind its design. function CanDirectlyCommunicate((x1; y1); (x2; y2); (px; py); b) . . . 12. [6 marks] Imagine that your CanDirectlyCommunicate function has been used to construct a graph whose nodes are the enemy AIs, such that there exists an edge between node n1 and n2 if and only if those two AIs can directly communicate. This graph is stored as an adjacency matrix M. 3 Here the nodes ni are simply the indices of enemy AIs in the array A, so each of them is simply an integer in the range 0 ni N 1. Therefore the adjacency matrix M is simply a two-dimensional array, M[0][0] : : : M[N 1][N 1]. Implement an O(N2) algorithm that, given two enemy AIs n1 and n2 determines whether there ex-ists a path by which they might communicate via other enemy AIs. If a path exists, your algorithm should return the shortest path by which communication is possible. Otherwise, your algorithm should return the empty path. Your algorithm also takes as its input the adjacency matrix repre-sentation of the graph described above. You should represent a path as a linked list of enemy AI indices ni. The empty path is therefore the empty linked list without any nodes.
Answered Same DayAug 30, 2021

Answer To: . Problems Let’s play a game. Or, more precisely, design and analyse some algorithms that might be...

Sudipta answered on Sep 02 2021
171 Votes
Answer 1:
function IsAffected((px; py); (x; y); b)
    if (x >= (px - b) and x<=(px + b) ) and (y >= (py - b) and y<=(py + b)) then
        Mark affected
    else
        Mark not affected
Answer 2:
In function MarkAffectedDC if we remove the outermos
t if else test then it wont perform the binary search of each point wether it is marked or not. The if statement checks if hi=lo which means there is only one element in the array A so it passes that to IsAffected function. And if there is more than 1 element in the array then it uses binary search or divide and conquer as the array is sorted.
Answer 3:
Consider the array A as A={P1,P2,P3,P4,P5} where Pi=(px,py) ie co-ordinates of the ith enemy
Initially lo=1, hi=5 for the given array
So basically we are dividing the array into two parts one A[lo,...mid] and other A[ mid+1,....,hi]
for the base case i.e, when lo=hi, we perform constant time operation. Say this operation is done in 'c' time.
So, if T(n) is the total time taken for input size=n then, T(n)=2*T(n/2) + c because we do constant time work say 'c'
Using Master's theorem we can say that the time complexity is O(log n)
For input size n=hi-lo+1
For the above example n=5-1+1=5
Since IsAffected() and MarkAffected() works in constant time so it can be said that T(1)=2 and for T(n)=2T(n/2)+2
Answer 4:
The recurrence relation is T(n)=2T(n/2) + c, where k= Θ (1)
T(n)= 2T(n/2) + Θ (1)
= 2T(n/2) + c
= 2{2T(n/4) + c) + c
= 4T(n/4) +3c
= ..........
= n.T(1)+(n-1)c
= n.k+(n-1)c
= 2nc-c
= O(n)
Another way to solve this is
For simplicity, lets assume that the O(1) term hides some
constant c, so the recurrence relation is really
T(n) = 2T(n/2) + c
Lets also assume for simplicity that T(1) = c.
Let us consider that
T(n) <= an + b
For some constants a and b. We prove this inductively.
For n = 1, we need a and b to be such that
c <= a + b
For the inductive step, we want
T(n) <= 2T(n/2) + c
Substituting our guess gives
T(n) <= 2(an / 2 + b) + c
= an + 2b + c
Notice that if 2b + c = b, the left-hand side of this expression
would be an + b, the upper bound we need. Thus we need to
choose a and b such that
c <= a + b
2b + c = c
One possible choice that works here is a = 2c and b = -c, giving
that T(n) <= 2cn - c = O(n).
Answer 5:
We will sort the array according to the distance of an enemy(x1,y1) from the player(px,py). After this we can...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here