Are you a programming enthusiast looking to stay ahead of the curve in 2023? With the ever-evolving tech landscape, keeping up with the Best Programming Language to learn can be a daunting task. Fear not, as we have compiled a list of the top 10 Programming Languages that you should consider learning in 2023. Python: This versatile language continues to dominate in 2023, with its ease of use, readability, and a vast library of modules. JavaScript: As web development grows increasingly popular, JavaScript remains a crucial player, with its ability to create dynamic and interactive web pages. Java: This language has stood the test of time and remains a popular choice for enterprise software development. C++: A staple in the gaming and systems development industries, C++ offers exceptional performance and memory management. Swift: Apple's preferred language for iOS app development, Swift continues to grow in popularity with its simplicity and reliability. R: As data science and machin...
Fractional Knapsack Problem | DAA
- Get link
- X
- Other Apps
Fractional Knapsack Problem
A thief has a bag or knapsack that can contain the maximum weight W of his loot. There are n items and the weight of an ith item is wi and it worth vi. Any amount of item can be put into the bag i.e. xi fraction of item can be collected, where 0<=xi<=1. Here the objective is to collect the items that maximize the total profit earned.
Here we arrange the items by ratio vi/wi.
Algorithm
GreedyFracKnapsack(W, n)
{
for (i = 1; i <= n; i++)
{
x[i] = 0.0;
}
tempW = W;
for (i = 1; i <= n; i++)
{
if (w[i] > tempW)
then break;
x[i] = 1.0;
tempW -= w[i];
}
if (i <= n)
x[i] = tempW / w[i];
}
We can see that the above algorithm just contains a single loop i.e. no nested loops the running time
for the above algorithm is O(n). However our requirement is that v[1 ... n] and w[1 ... n] are sorted,
so we can use the sorting method to sort it in O(n log n) time such that the complexity of the algorithm above including sorting becomes O(n log n).
Example: Consider five items along with their respective weights and values,
I = {I1, I2, I3, I4, I5}
w = {5, 10, 20, 30, 40}
v = {30, 20, 100, 90, 160}
The knapsack has a capacity of W=60, then find optimal profit earned by using a fractional knapsack.
Solution:
Step 1: Initially
|
Items |
Wi |
Vi |
|
I1 |
5 |
30 |
|
I2 |
10 |
20 |
|
I3 |
20 |
100 |
|
I4 |
30 |
90 |
|
I5 |
40 |
160 |
Step 2: Calculate vi/wi as,
|
Items |
Wi |
Vi |
Pi=vi/wi |
|
I1 |
5 |
30 |
6.0 |
|
I2 |
10 |
20 |
2.0 |
|
I3 |
20 |
100 |
5.0 |
|
I4 |
30 |
90 |
3.0 |
|
I5 |
40 |
160 |
4.0 |
Step 3: Arranging the items with decreasing order of Pi as,
|
Items |
Wi |
Vi |
Pi=vi/wi |
|
I1 |
5 |
30 |
6.0 |
|
I3 |
20 |
100 |
5.0 |
|
I5 |
40 |
160 |
4.0 |
|
I4 |
30 |
90 |
3.0 |
|
I2 |
10 |
20 |
2.0 |
Now filling the knapsack according to decreasing value of Pi
Maximum value= v1+v2+new(v3) = 30+100+140=270
- Get link
- X
- Other Apps
Popular posts from this blog
Eliminating Epsilon transition (ε-Transitions) | Conversion of Epsilon-NFA to DFA | Conversion of Epsilon-NFA to NFA | Theory Of Computation (TOC)
Theory Of Computation (TOC) | Eliminating Epsilon Transition ( ε -Transitions) : Given any Epsilon NFA (ε-NFA) , we can find a DFA D that accepts the same language as E. The construction is close to the subset construction, as the states of D are subsets of the states of E. The only difference is that dealing with ε-transitions, which can be done by using ε-closure.
Top 10 Programming Language to learn in 2023
Are you a programming enthusiast looking to stay ahead of the curve in 2023? With the ever-evolving tech landscape, keeping up with the Best Programming Language to learn can be a daunting task. Fear not, as we have compiled a list of the top 10 Programming Languages that you should consider learning in 2023. Python: This versatile language continues to dominate in 2023, with its ease of use, readability, and a vast library of modules. JavaScript: As web development grows increasingly popular, JavaScript remains a crucial player, with its ability to create dynamic and interactive web pages. Java: This language has stood the test of time and remains a popular choice for enterprise software development. C++: A staple in the gaming and systems development industries, C++ offers exceptional performance and memory management. Swift: Apple's preferred language for iOS app development, Swift continues to grow in popularity with its simplicity and reliability. R: As data science and machin...
Operator Overloading Types and Rules | C++ Programming
Operator Overloading It is the mechanism of giving special meanings to an operator. By overloading operators, we can give additional meanings to operators like +, *, -, <=, >=, etc. which by default are supposed to work only on standard data types like ints, and floats. In general, a = b + c; works only with basic types like ‘int’ and ‘float’, and attempting to apply it when a, b and c are objects of a user-defined class will cause complaints from the compiler. But, using overloading, we can make this statement legal even when a, b and c are user-defined types (objects). Even though the semantics of an operator can be expressed, we cannot change its syntax. When an operator is overloaded, its original meaning is not lost. The grammar rules defined by C++ that govern its use such as the number of operands, precedence, and associatively of the operator remain the same for overloaded operators. We can overload (give additional meanings to) all the C++ operators except the f...


Comments
Post a Comment
Subscribe Us and Thanks for visiting blog.