Recent

Author Topic: GREEDY ALGORITHM in Pascal  (Read 15360 times)

joydivision

  • Newbie
  • Posts: 6
GREEDY ALGORITHM in Pascal
« on: April 15, 2014, 04:46:41 pm »
Hi guys,I've been having some problems with this one since I'm only a beginner.

There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Each boxs has a weight given in kilograms and the value per each kg of the item from that box.
The thief cannot carry more than m (variable) kilograms.
He doesn't have to take the whole box.He can as well take a smaller amount of fruits or vegetables from it.
What is the biggest profit that the thief can make in a given situation?

n- number of the boxes
x- weight
y- price of the items in the box per kg
m- number of kgs he is able to take out

I'm not really sure about the assign-s and the code.
Thanks in advance!



Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: GREEDY ALGORITHM in Pascal
« Reply #1 on: April 15, 2014, 05:18:15 pm »
I would select the box with the highest y and take as much as he can (i.e. less then m).
Then select box with the second highest y and take as much as he can (m minus what he already have).
And so on.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: GREEDY ALGORITHM in Pascal
« Reply #2 on: April 15, 2014, 05:25:28 pm »
And the biggest profit would be the sum of (y of a box) * (how many kilograms was taken from that box).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GREEDY ALGORITHM in Pascal
« Reply #3 on: April 15, 2014, 07:09:53 pm »
The biggest hint is that you're learning greedy algorithm. So, simply take the best value at each step would yield the best result.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: GREEDY ALGORITHM in Pascal
« Reply #4 on: April 15, 2014, 11:56:20 pm »
Its always nice to have informational post on discussion boards which helps individuals to get all the relevant info which he/she may be looking for.
It sounds like a good idea. Do you have anything you want to add here?  :)

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: GREEDY ALGORITHM in Pascal
« Reply #5 on: April 16, 2014, 11:33:04 am »
There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Are you asking us to help a thief?   :) :D ;D

Sorry, just couldn't resist!  ::)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: GREEDY ALGORITHM in Pascal
« Reply #6 on: April 16, 2014, 12:50:24 pm »
There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Are you asking us to help a thief?   :) :D ;D

Sorry, just couldn't resist!  ::)

Bad news, Avra.
I think Xe's calling you one.

joydivision

  • Newbie
  • Posts: 6
Re: GREEDY ALGORITHM in Pascal
« Reply #7 on: April 16, 2014, 03:43:26 pm »
I would select the box with the highest y and take as much as he can (i.e. less then m).
Then select box with the second highest y and take as much as he can (m minus what he already have).
And so on.

Thank you so much!
Could you please write the code for it?   %)
That is the problem I'm having. :(
I understand it but I cannot really put it into code.


Thanks again!

joydivision

  • Newbie
  • Posts: 6
Re: GREEDY ALGORITHM in Pascal
« Reply #8 on: April 16, 2014, 03:45:04 pm »
There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Are you asking us to help a thief?   :) :D ;D

Sorry, just couldn't resist!  ::)

I thought the task was rather weird when I got it,too.  ::)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: GREEDY ALGORITHM in Pascal
« Reply #9 on: April 16, 2014, 03:51:30 pm »
There is a lack of information, such as the number of items on each box.

Basically you need to sort the items by Y (price) and get them from the top until the weight is M.
« Last Edit: April 16, 2014, 04:39:11 pm by typo »

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: GREEDY ALGORITHM in Pascal
« Reply #10 on: April 16, 2014, 07:59:39 pm »
There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Are you asking us to help a thief?   :) :D ;D

Sorry, just couldn't resist!  ::)

I thought the task was rather weird when I got it,too.  ::)

Better run on back to corporate, JoyDivision.




marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: GREEDY ALGORITHM in Pascal
« Reply #11 on: April 16, 2014, 08:24:08 pm »
Hi guys,I've been having some problems with this one since I'm only a beginner.

There's a thief in the grocery store with a multiple boxes of fruits and vegetables.
Each boxs has a weight given in kilograms and the value per each kg of the item from that box.
The thief cannot carry more than m (variable) kilograms.
He doesn't have to take the whole box.He can as well take a smaller amount of fruits or vegetables from it.
What is the biggest profit that the thief can make in a given situation?

Isn't that the NP complete knapsack problem?

Ian Curtis.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GREEDY ALGORITHM in Pascal
« Reply #12 on: April 17, 2014, 02:27:20 am »
Quote
Isn't that the NP complete knapsack problem?
I don't think so, since only biggest profit is asked. It's a 0-1 knapsack. Just forget the box, it's a confuser.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: GREEDY ALGORITHM in Pascal
« Reply #13 on: April 17, 2014, 04:07:40 am »
I think that "price of the items per Kg" makes the two problems be different. Maybe the "Greedy Approximation Algorithm". I don't know any implementation in Pascal.
« Last Edit: April 17, 2014, 04:21:42 am by typo »

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: GREEDY ALGORITHM in Pascal
« Reply #14 on: April 17, 2014, 06:28:45 am »
If he understands scaling and is programming in LAZARUS, you might fit in the palm of his hand, JoyDivision.

http://upload.wikimedia.org/wikipedia/commons/9/91/Orion_constelation_PP3_map_PL.jpg

 

TinyPortal © 2005-2018