Professional Documents
Culture Documents
1. b
2. b
3. b
ESSAY:
1. Greedy algorithms are algorithms that take the locally optimum solution to a problem in a
given process. If in that one step of the process a better solution to the problem is found
a greedy algorithm will choose that better solution instead of finding the optimum solution
overall. The result is usually not optimal but sometimes due to chance it just might be
optimal and in which case could be better than a dynamic algorithm . Greedy algorithms
are usually fast and don’t take up much memory.
2. Brute force algorithms try to find the optimal solution to a problem using whatever
methods they can find without caring for technique or speed. Brute force algorithms
usually specialize towards the problem and can usually only be used in very specific
cases.They try every possible solution to a problem and check whether it is the correct
solution. This is a simple and straightforward approach, but it can be very
time-consuming, especially for problems with a large number of possible solutions. On
the other hand, greedy algorithms are a type of algorithm that makes the locally optimal
choice at each step with the hope of finding a global optimum. They do this by selecting
the option that looks the best at the current moment without considering the
consequences of that choice on future steps. In general, brute force algorithms are more
thorough and may find the optimal solution, but they can be very slow. Greedy
algorithms are faster but may not always find the optimal solution.
Iterate through the lengths of the rod from 1 to 4 and fill in the table as follows:
For each length, consider all possible ways to cut the rod and select the one that gives
the maximum price. The price of the rod of length i can be obtained by adding the price
of the cut rod of length j (for j < i) and the price of the remaining rod of length i-j.
We need to consider all possible ways to cut the rod and select the one that gives the
maximum price. The possible ways to cut the rod of length 4 are:
Cut the rod into two pieces of length 2: This would give a price of $4 (the price of the rod
of length 2) plus $4 (the price of the rod of length 2).
Cut the rod into a piece of length 3 and a piece of length 1: This would give a price of $9
(the price of the rod of length 3) plus $1 (the price of the rod of length 1).
The results:
0 1 2 3 4
0 1 5 8 10
The maximum price for a rod of length 4 is $10, which is the value in the cell at row 2,
column 4.
Therefore, the optimal strategy for cutting and selling a rod of length 4 at the highest
price is to cut it into one piece of length 3 and one piece of length 1.
5. Huffman Tree:
(72)
/ \
(17) B(22)
/ \
A(10) G(18)
/ \ \
F(15) D(18) E(12)
/
C(5)
Huffman Table:
A 00 10 (2 x 10) = 20
B 1 22 22
C 010 5 (3 x 5) = 15
D 011 18 (3 x 18) = 54
E 1100 12 (4 x 12) = 48
F 0100 15 (4 x 15) = 60
G 0101 18 (4 x 18) = 72
6. Memoization is when a dynamic programming algorithm stores results from its previous
calculation in some sort of memory, usually an array or matrix. For example if I am
traversing a graph and I happen to go through a certain weighted edge again and again
the results of me going through that same path would not be calculated again and
instead me progressing forward would be calculated. In simpler terms if went through
hallway 1 with a cost of 4 and again through hallway 2 with a cost of 6 that first
calculation will occur but if the next day I decide to go to hallway 3 from hallway 1 which
connects with hallway 2 the algorithm will remember the cost of moving from hallway 1 to
2 which is 10 and will instead use that value when proceeding with the calculation.
Another example of memoization could be the fibonacci sequence and it’s algorithm. To
compute the nth term of the Fibonacci sequence using a recursive algorithm, you can
write a function that takes an integer n as input and returns the nth term of the Fibonacci
sequence.
Like this:
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
The function has a time complexity of ON^2 and thus is very slow. We can use
memoization to store the results of expensive function calls and return the results when
the same calculations are called again.
Here is an example of the fibonacci function with memoization:
The result is outputted in a much faster time with the same results.