Algorithm and computing system(Python)
Weight value
2 16
3 19
4 23
5 28
total number of items = 4
capacity of the knapsack = 7
sim(A,O) = 0.1
sim(B,O) = 0.3
sim(C,O) = 0.85
sim(D,O) = 0.8
sim(E,O) = 0.9
sim(F,O) = 0.6
Also, suppose objects A, B and C are in class 1 and
D,E and F are in class 2.
With k=4, what class should O be in, using the kNN algorithm?
With k=2, what class should O be in, using the kNN algorithm?
QUESTION 1:
Dynamic Programming is a class of algorithms in computer programming which solves a problem by dividing a problem into sub-problems and optimum substructure of the problem so that they can be reused.
They are usually faster because each sub-problem is solved only once and is saved for future use. So that if in another solution path the same sub-problem is encountered again it does not solve it completely and instead uses the value stored previously for the sub-problem since it had already been solved once. This saves time and memory thus it is said to work faster.
QUESTION 2:
THE CODE IS:
def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
# Build the table for k[][] in bottom-top/backtracking approach
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i-1] <= w:
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w])
else:
K[i][w] = K[i-1][w]
# return the max value
return K[n][W]
# driver function
def main():
# initialising the value list
val = [16, 19, 23, 28]
# initialising the weightlist
wt = [2, 3, 4, 5]
# initialising the maximum weight of the sack
W = 7
# length of the list of items
length = len(val)
print('The max value of items is:', knapSack(W, wt, val, length))
if __name__ == '__main__':
main()
SCREENSHOT OF CODE TO UNDERSTAND INDENTATION AND OUTPUT:
QUESTION 3:
i) k = 4. Since k = 4, we need to consider it's
nearest 4 neighbours to classify O as either Class 1 or Class
2.
It's 4 nearest neighbours are A(0.1), B(0.3), F(0.6) & D(0.8)
with A being the nearest and D being the 4th nearest point.
Since A & B belong to class 1 and D & F belong to class 2
it's a tie with a vote of 2 - 2. Therefore, O could either be in
class 1 or class 2.
ii) k = 2. Since k = 2 we will consider it's 2 nearest neighbours to classify O. The two closest points are A(0.1) and B(0.3). Since A & B both belong to class 1 O belongs to Class 1 with a vote of 2 - 0.
Algorithm and computing system(Python) What is dynamic programming? Why does it usually work faster? Using the...
a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should read inputs from a file called “data.txt”, and the output will be written to screen, indicating the optimal subset(s). b) For the bottom-up dynamic programming algorithm, prove that its time efficiency is in Θ(nW), its space efficiency is in Θ(nW) and the time needed to find the composition of an optimal subset from a filled dynamic programming table is in O(n). Consider the following...
1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5
3. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (20 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? Item 1 weighs 2 pounds and is worth $9.00 Item 2 weighs 3 pounds and is worth $12.00 Item 3 weighs 5 pounds and is worth $14.00 Item 4...
A) Write the pseudocode for an algorithm using dynamic programming to solve the activity-selection problem based on this recurrence: c[i, j] = 0 if Si; = Ø max {c[i, k] + c[k,j] + 1} if Sij +0 ak eSij B) Analyze the running time (the time complexity) of your algorithm and compare it to the iterative greedy algorithm.
Haloo , i have java program , Java Program , dynamic program Given a knapsack with capacity B∈N and -n- objects with profits p0, ..., p n-1 and weights w0, ..., wn-1. It is also necessary to find a subset I ⊆ {0, ..., n-1} such that the profit of the selected objects is maximized without exceeding the capacity. However, we have another limitation: the number of objects must not exceed a given k ∈ N Example: For the items...
Design a dynamic programming algorithm for the problem. Define the original problem as a function that takes parameters, and return some results. Define the subproblems Write recursive formula that relates a problem's solution to solutions of smaller subproblems. Finally write out pseudocode for the algorithm (using top-down memoization or bottom-up) Suppose a list Р[L..n] gives the daily stock price of a certain company for a duration of n days, we want to find an optimal day di to buy the...
Give pseudocode that performs the traceback to construct an LCS from a filled dynamic programming table without using the “arrows”, in O(n + m) time. 2. Suppose we are given a “chain” of n nodes as shown below. Each node i is “neighbors” with the node to its left and the node to its right (if they exist). An independent set of these nodes is a subset of the nodes such that no two of the chosen nodes are neighbors....
Programming Using Python>>>>>>>>>>>>>>>>>>>>>>>> (Algebra: linear equations) Design a class named LinearEquation for 2x2 a system of linear equations: The class contains: ? The private data fields a, b, c, d, e, and f with get methods. ? A constructor with the arguments for a, b, c, d, e, and f. ? Six get methods for a, b, c, d, e, and f. ? A method named isSolvable() that returns true if is not 0. ? The methods getX() and getY()...
1 (15 pts) Implement recursive, memoized, and dynamic programming Fibonacci and study their performances using different problem instans You can choose to look at the perfor- mance by either timing the functions or counting the basic operations (in code) Provide your results below, and submit your code. Also, describe the pros and cons of your choice of performance metric Note: If you decide to use timing, the standard way to time an algorithm is to run the same problem 100...
9. Referring again to the problem of dynamic programming problem considered in Question 8, what is the cost of matching the two substrings ELEPHAN and EALEP? (a) 3 (b) 4 (c) 5 (d) 6 (e) None of the above 10. Which of the following statements is/are true? (i) Dijkstra's algorithm and the Fast Marching algorithm are identical (ii) Dijkstra's algorithm finds the shortest Euclidean path in space (ii) Dijkstra's algorithm finds the shortest path in a network (a) (i) only...