Sunglass Website Design Free Download HTML CSS JavaScript

Glasses – Sunglass Website Design Free Download HTML CSS JavaScript – Goggles E-commerce Online Shopping Website Template

Bitmask Dynamic Programming

Hope you can now solve lis,knapsack,coin-change problem very easily, you will not have problem printing DP’s solution. Now we will look at a slightly different DP called bitmask DP. Although the name sounds intimidating, the solution is often the first thing that comes to mind when reading Bitmask DP problem.

But before reading this chapter you need to learn how to work with beats, like turning on/off the beat of a specific position. That’s why you can watch this wonderful tutorial, read the whole thing very well because it will be useful for you in many places. I am not writing about bit operations in this tutorial as it will become irrelevant.

We define 3 functions at the beginning.

int Set(int N,int pos){return N=N | (1<<pos);}

int reset(int N,int pos){return N= N & ~(1<<pos);}

bool check(int N,int pos){return (bool)(N & (1<<pos));}

The set function sets the pos th position bit of N number to 1, the reset function sets it to 0 and the check function returns what is in the pos th bit. Any bitmask DP problem will need 3 functions, especially set and check.

We begin with a problem. Suppose you have to buy n items from n stores. You need a0,a1,a2,…,a(n-1) money to buy things. Your city is very strange, when you go to another shop to buy something, the shopkeeper sees the things you bought before and increases the price of his shop!! How much the price will increase will depend on which store you visited earlier. Let n=2, then you will be given a matrix like this:

  10 10

  90 10

now,

  If (i==j) then matrix[i][j]=matrix[i][i]=actual price of i’th item.

  If (i!=j) then matrix[i][j]=Additional cost of i’th item if j’th item is purchased first.

If you buy the 0th thing in the beginning then the price will be 10 rupees, then if you buy the 1st thing it will cost 10 + 90 rupees, because matrix[1][0] = If you buy the 1st thing before the 0th thing, the additional cost added = 90 and the 1st thing Original price of item = 10, then total cost is 10 + (10 + 90) = 110. But if you buy item number 1 first then the total cost is 10 + (10 + 10) = 30 rupees.

Glasses – Sunglass Website Design Free Download HTML CSS JavaScript – Goggles E-commerce Online Shopping Website Template

You understand that your job is to minimize costs. The maximum value of n is 15.

As the value of n is very small, the problem can be easily solved with bitmask DP. Our first task in DP is to determine the state. What information can be disclosed about our status at any time during this purchase? “What items have been purchased so far” is enough information, right? If we know this, we can know how much additional cost will be added when we buy another thing, we will buy the thing that will reduce the total cost. I think that now the question is how to keep the state?

One way is n

Keeping n states for t things like this function(a0,a1,a1,….an-1), but if the value of n changes how do you change the number of parameters? And if you work with 15 parameters, the code will become terribly complicated.

2nd way is bitmask. An integer has 32 bits. We will take advantage of that. If the number 1 bit is 1 we have taken the item number 1, if it is 0 then we have not. Bit 3 is 1

If we take the item number 3, if 0 we don’t have it. If bits 1 and 3 are both 1, we get 2 items.

Initially our state will be 0 or “000000” in binary. That means we haven’t bought anything yet. State will be 3 when 0th and 1st items are purchased

or “000011” and this is our base case for n=2. For n=4 the base case is 15 or “0001111”. No need to worry about leading zero, it is given for ease of understanding. If you think a little you will understand that if mask=(2^n)-1 it will be the base case because then the first n bits in the binary will be 1, then we will return zero because there is nothing left to buy.

int dp[(1 << 15) + 2];

int call(int mask)

{

  if (mask == (1 << n) – 1)

      return 0;

  if (dp[mask] != -1)

      return dp[mask];

  //Rest of the calculation

}

Having understood the base case, our next task is to try out the items that were not purchased. If the ith position of mask contains 0 then the ith item is yet to be purchased.

int dp[(1 << 15) + 2];

int call(int mask)

{

  if (mask == (1 << n) – 1)

      return 0;

  if (dp[mask] != -1)

      return dp[mask];

  //Rest of the calculation

  int ans = 1 << 28; //Infinite, a large value

  for (int i = 0; i < n; i++) {

      if (check(mask, i) == 0) {

          //Rest of the code

      }

  }

  return dp[mask] = ans;

}

We loop until n to find out which ones are left to take. Now the actual price of the i-th item is price=w[i][i]. w[i][j] will be added to this price if i!=j and item j has already been purchased. By checking the jth bit of the mask we can tell whether the jth item has been purchased or not.

Glasses – Sunglass Website Design Free Download HTML CSS JavaScript – Goggles E-commerce Online Shopping Website Template

int dp[(1 << 14) + 2];

int call(int mask)

{

  if (mask == (1 << n) – 1)

      return 0;

  if (dp[mask] != -1)

      return dp[mask];

  int ans = 1 << 28;

  for (int i = 0; i < n; i++) {

      if (check(mask, i) == 0) {

          int price = w[i][i];

          for (int j = 0; j < n; j++)

              if (i != j and check(mask, j) != 0)

                  price += w[i][j];

          int ret = price + call(Set(mask, i));

          ans = min(ans, ret);

      }

  }

  return dp[mask] = ans;

}

With j’s loop we get the total cost. Now what is the next state if i buys the i th item? Only the i-th bit of the mask should be set to 1. We call(Set(mask,i)) thus buying the ith item and moving to the next state. In this way, I bought every item and returned the one that had the lowest price. Job done! Complete code:

int w[20][20];

int n;

int dp[(1 << 15) + 2];

int call(int mask)

{

  if (mask == (1 << n) – 1)

      return 0;

  if (dp[mask] != -1)

      return dp[mask];

  int mn = 1 << 28;

  for (int i = 0; i < n; i++) {

      if (check(mask, i) == 0) {

          int price = w[i][i];

          for (int j = 0; j < n; j++) {

              if (i != j and check(mask, j) != 0) {

                  price += w[i][j];

              }

          }

          int ret = price + call(Set(mask, i));

          mn = min(mn, ret);

      }

  }

  return dp[mask] = mn;

}

int main()

{

  mem(dp, -1);

  cin >> n;

  for (int i = 0; i < n; i++) {

      for (int j = 0; j < n; j++) {

          scanf(“%d”, &w[i][j]);

      }

  }

  int ret = call(0);

  printf(“%d\n”, ret);

  return 0;

}

Glasses – Sunglass Website Design Free Download HTML CSS JavaScript – Goggles E-commerce Online Shopping Website Template

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel

All Links in Below:






Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.

Join Our YouTube Channel & Subscribe with Bell Icon for New Video:

Join Our Official Facebook Page For the Latest updates All Code Projects are Free:

Visit our service page to get premium services.

Free Website Create – HTML CSS, PHP, JavaScript Programming Projects For Free

Follow Us

Thank You,

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel




FreeWebsiteCreate.net tries to provide HTML, CSS, SCSS, JavaScript, React, Android Studio, Java, PHP, Laravel, Python, Django, C#(C Sharp), and ASP.net-related projects 100% free. We try to make learning easier. Free Website Create always tries to give free projects to new learners. Free projects and source code will help to learn quickly.

They can save time and learn more. In this post, we share a free portfolio project website code with HTML and CSS. This free code portfolio contains a single landing page with a responsive design. In this post, we get a free best carpenter and craftsman service website designed by FreeWebsiteCreate with HTML, CSS, Bootstrap, and JavaScript.

To get a free website project code,

Keep bookmarking our website, Save categories links, Follow Social Media, Follow the youtube channel Join Facebook groups.

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Leave a Comment