Vous êtes sur la page 1sur 8

Assignment

Roll No. I006 Name: SHRISHTI BANSAL


Class: MBA TECH IT 2ND YEAR Batch: A1
Date of Tutorial: Date of Submission: 17/10/2017
Grade:

1. What is dynamic programming? Explain a solution to a problem


using this approach.

Dynamic programming approach is similar to divide and conquer in breaking


down the problem into smaller and yet smaller possible sub-problems. But
unlike, divide and conquer, these sub-problems are not solved independently.
Rather, results of these smaller sub-problems are remembered and used for
similar or overlapping sub-problems.

Dynamic programming is used where we have problems, which can be divided


into similar sub-problems, so that their results can be re-used. Mostly, these
algorithms are used for optimization. Before solving the in-hand sub-problem,
dynamic algorithm will try to examine the results of the previously solved sub-
problems. The solutions of sub-problems are combined in order to achieve the
best solution.

So we can say that

The problem should be able to be divided into smaller overlapping sub-


problem.

An optimum solution can be achieved by using an optimum solution of


smaller sub-problems.

Dynamic algorithms use memorization.


Comparison
In contrast to greedy algorithms, where local optimization is addressed, dynamic
algorithms are motivated for an overall optimization of the problem.

In contrast to divide and conquer algorithms, where solutions are combined to


achieve an overall solution, dynamic algorithms use the output of a smaller sub-
problem and then try to optimize a bigger sub-problem. Dynamic algorithms use
memorization to remember the output of already solved sub-problems.

The following computer problems can be solved using dynamic programming


approach

Fibonacci number series


Knapsack problem
Tower of Hanoi
All pair shortest path by Floyd-Warshall
Shortest path by Dijkstra
Project scheduling
Dynamic programming can be used in both top-down and bottom-up manner.
And of course, most of the times, referring to the previous solution output is
cheaper than recomputing in terms of CPU cycles.

Example

Consider a program to generate Nth Fibonacci number


Fib(n)=Fib(n-1)+Fib(n-2)

Solution 1 using top-down approach without Dynamic Programming


Solution 2 using top-down approach with Memorization (Dynamic
Programming)
Solution 3 Bottom up Dynamic Programming
2. Which is the data structures used to perform recursion? Explain
how it handles recursion.
Stack is the data structure used to perform recursion.

Problems that yield to recursion have the following characteristics:

1. The computation used to solve the problem is identical for every position
in the data set.
2. The result of the initial computation will depend on the result of the
subsequent computations.

#2 is the reason a stack is required. A stack allows you to keep a bookmark of


the state while you go off to calculate downstream. When you get back to the
bookmark, you pop it off the stack and complete the computation.

A simple array sum can be accomplished without a stack because the unit
operation (adding the current value to the total) doesnt depend on the results of
the other computations. But if you are summing up nodes in a tree, any node
value is going to be the sum the of the child values. You wont have enough
information to compute this until you compute the children first, so youll need a
stack to remember to come back to this calculation.

In practice, there are typically two kinds of stacks to use:

1. the call stack (which you sort of get for free)


2. a stack or queue object (which you allocate explicitly)
Most recursion examples use the call stack because it is simpler to write and
read. This is fine for toy problems and even many real-world computations, but
be aware the call stack usually has limited space, is more expensive performance-
wise, and when you run out of space the program is not recoverable. If you
instead use an explicitly declared stack that you allocate on the heap, you have
much more space for recursion and you can recover more gracefully if you run
out of space. You also get the bonus of having all the computation occur inside a
single step in the call stack which can make it easier to debug and easier to do
performance profiling.
3. Explain any 2 applications for the following data structures.

a. Trees
Decision Making: To get the Next Move in games. Computer chess
games build a huge tree (training) which they prune at runtime using
heuristics to reach an optimal move.

Organizing Things: Folders/ files in the Operating system.HTML


Document Object Model (DOM).Company Organisation Structures.PDF
is a tree-based format. It has a root node followed by a catalog node
followed by a pages node which has several child page nodes.

b. Graphs

Social network graphs: Graphs that represent who knows whom, who
communicates with whom, who influences whom or other relationships
in social structures. An example is the twitter graph of who follows
whom. These can be used to determine how information flows, how
topics become hot, how communities develop, or even who might be a
good match for who, or is that whom.
Website analysis -The best known example is the link graph of the
web, where each web page is a vertex, and each hyperlink a directed
edge. Link graphs are used, for example, to analyze relevance of web
pages, the best sources of information, and good link sites.

c. Stacks
Backtracking (game playing, finding paths, exhaustive searching).
Memory management, run-time environment for nested language
features and expression evaluation.

d. Priority queues
Dijkstras Shortest Path Algorithm using priority queue: When the graph
is stored in the form of adjacency list or matrix, priority queue can be
used to extract minimum efficiently when implementing Dijkstras
algorithm.
Artificial Intelligence: A* Search Algorithm : The A* search algorithm
finds the shortest path between two vertices of a weighted graph, trying
out the most promising routes first. The priority queue (also known as
the fringe) is used to keep track of unexplored routes, the one for which
a lower bound on the total path length is smallest is given highest
priority.

e. Hash tables
Message digests: Say that I decide to upload all of my documents, music,
and videos up to Drop box or Google Drive. I'm thrilled that I won't need
to store 100 GB of files locally on my computer anymore! But, I might be
a little worried: how can I be sure that Drop box or Google isn't
tampering with my files? Hashing provides a clever solution. Before I
upload all of my files to Drop box/Drive, I can compute the hash of each
file. Each hash is typically just a few bytes (merely 32 bytes in the case of
the SHA-256 hash function), so storing hashes for even a million files
won't be a problem.

Commitment: Lets say you're playing a game of guess-the-number with


you friend, and you've thought of a random number from 1 to 10^20.
The best way to prevent accusations of "you changed your number!"
would be to write down your number in a sealed envelope and place the
sealed envelope in plain sight. After the game ends, your friend can
open the envelope and verify that you did not cheat. This is an example
of commitment. Hash functions give us a way to perform commitments
digitally. The concept is straightforward: in the guess-the-number
example, you would just hash the number you've thought of and give
the hashed value to your friend. Say your random number was
"123,456"; then the SHA-256 hash value is "f8339a..." (a 64-character
long string). You can give your friend the "f8339a..." value and your
friend won't be able to reverse it unless he/she tries to hash every value
from 1 to 10^20*. But when the game ends, your friend can verify that
"f8339a..." is indeed the hash of 64. You have successfully committed to
your value.

f. Heap
Order statistics: The Heap data structure can be used to efficiently find
the kth smallest (or largest) element in an array.

There are two types of heap "ascending heap" and "descending heap".
In ascending heap root is the smallest one and in descending heap root
is the largest element of the complete or almost complete binary tree.
So, wherever priority thing need to be implemented, ascending or
descending heap can be used. To create Priority queue, heaps are used.

Vous aimerez peut-être aussi