Greedy Algorithms in Python: Optimal Task Assignment

  Рет қаралды 41,200

LucidProgramming

LucidProgramming

Күн бұрын

Пікірлер: 60
@KippiExplainsStuff
@KippiExplainsStuff 5 жыл бұрын
First off, the explanation about ~0 is brilliant! I feel like I've gained a new tool as a programmer. Thanks for that! Secondly - this needs to be said - this solution will not work for an odd number of tasks. I checked to make sure. one task won't be assigned a worker. What I did was to add a check to see if there are odd or even number of tasks. If it's odd, I first assign a worker only the last task (I did the assignments as tuples) and then did the rest. I actually didn't print, rather just returned a list of tuples.If I assigned the last task (i take the last one because bu itself is probably closer to the average of the rest) i popped it before getting to the loop. here is my code: def optimal_task_assignment(tasks): workers = [] tasks = sorted(tasks) if len(tasks) % 2 == 1: workers.append((tasks[-1],)) tasks.pop() for i in range(len(tasks) //2): workers.append((tasks[i], tasks[~i])) return workers
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Hi Amos. Thanks as always for your commentary! I appreciate you improving upon the solution to work for a broader case. Thanks very much for that, and if you feel like improving the code on my Github, feel free to make a pull request, and I can add your code in the repo. In any case, thanks very much for your comment!
@KippiExplainsStuff
@KippiExplainsStuff 5 жыл бұрын
@@LucidProgramming I'd be happy to, in a few days, once I've had my interview :P
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@KippiExplainsStuff Best of luck with your interview, Amos!
@thedumbfounds767
@thedumbfounds767 6 жыл бұрын
great video! This seems to be a quite practical algorithm for many use cases. I am wondering, how would you approach assigning an odd number of tasks to each worker?
@vernongriesel3910
@vernongriesel3910 6 жыл бұрын
For this example each worker needed to do 2 jobs, so an odd amount of jobs was not an option. Let's assume that there were an odd number of jobs, you could assign 1 worker with the biggest job and then continue to do the greedy algorithm OR possible assign 1 worker with 3 of the smallest jobs and then continue the greedy approach. That doesn't scale well, and especially won't work in this example. This approach also assumes that the values don't deviate much from each other either. So while this is a very useful, it is not robust by itself. Nice video btw, really good explanation.
@jeetshah8513
@jeetshah8513 4 жыл бұрын
what is the length of the array is an odd number will we just add a zero in the beginning?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
No, it should work in the odd case.
@mr.banana5718
@mr.banana5718 2 жыл бұрын
Sir, what if the number of tasks is odd ???
@josephoduor5322
@josephoduor5322 5 жыл бұрын
A=[6,3,2,7,5,5] A= sorted (A) for i in range(len(A)//2): print(A[i],A[-i]) I got (2,2)(3,7)(5,6)
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Hi Joseph. Your code is wrong. It should be "~" instead of "-".
@Joni2Back
@Joni2Back 5 жыл бұрын
It is print(A[i], A[~i])
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@Joni2Back Yep. That's what I commented 2 months ago :P. Thanks all the same though!
@Joni2Back
@Joni2Back 5 жыл бұрын
@@LucidProgramming omg sorry didn't see
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@Joni2Back Np :P
@alexisjulian8316
@alexisjulian8316 5 жыл бұрын
It really helps me in Assignment :D My only problem is how to put those arrays into worker 1 - worker 3 so it can be understandable by the reader
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Thanks, glad it helped. Couldn't you just add that to the print statement to solve your problem?
@argorninc
@argorninc 6 жыл бұрын
Thanks. can you help me to modify the code for a case where one worker is suppose to complete the load
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hi Kweku. No problem. If you would like me to make a video and solve for a specific case, you can request that on my Patreon page. Every little bit helps support my channel, and I really appreciate any support that can be provided! Thanks: www.patreon.com/lucidprogramming
@mohamedsadik84
@mohamedsadik84 5 жыл бұрын
great work man Keep them coming
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link (paypal.me/VincentRusso1 ) for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my KZbin page. bit.ly/lp_email
@alalehrazmjoo2850
@alalehrazmjoo2850 5 жыл бұрын
@@LucidProgramming Hi there, the PayPal link is broken! Thanks a lot for the great content, I am going through them while I am getting prepared for interviews.
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@alalehrazmjoo2850 Oh, whoops! Thank you for letting me know about that. I went ahead and fixed it so that it directs to paypal.me/VincentRusso1. Thanks again or letting me know, and best of luck with your interviews!
@mariano3395
@mariano3395 5 жыл бұрын
Dynamic programming problem solving would be appreciated! Thanks
@tareqoweinat5952
@tareqoweinat5952 2 жыл бұрын
intuitive , thank you very much
@vivekanandsahu2631
@vivekanandsahu2631 4 жыл бұрын
This algorithm will not always work! e.g [1,2,2,3,4,8] gives (9,6,5) but the best ans will be (8,6,6)
@EvilsoriHC
@EvilsoriHC 4 жыл бұрын
Hi! I suppose you mean that the 1st worker does the 8-hour task, second does 1+2+3 hour tasks, and the 3rd does 2+4. You got a point and your answer seems to be more optimized for workers. However, You might have misunderstood the problem: each of the three workers has to do EXACTLY two tasks, not three tasks and not only one task. That’s why you can’t get (8,6,6). I hope i explained it well and you will understand :) have a nice day
@vivekanandsahu2631
@vivekanandsahu2631 4 жыл бұрын
Got that.
@sai-du6qg
@sai-du6qg 4 жыл бұрын
Hey help me with this problem. Given an array of Integers , you need to find the maximum we can get . If we select some integer we need to add equal value until end and return the maximum we can get. Some test cases are Arr - [ 4,80,84] Ans - 160 Arr- [ 90, 8,30] Ans - 30 Arr-[ 40,20,60] Ans - 60
@LucidProgramming
@LucidProgramming 4 жыл бұрын
What have you tried so far?
@8koi139
@8koi139 3 жыл бұрын
I only needed 40 seconds to came up whit a solution to my problem lol. I just added an if to force the function to be greedy
@javadkhalilarjmandi3906
@javadkhalilarjmandi3906 6 жыл бұрын
perfect, but I have one problem, how can I assign these pears, each to the certain worker, because worker's performance are different(in my case)
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hi Javad. I'm not sure I understand your question. You want to perform an optimal assignment of paired tasks? Can you elaborate more on the problem so that I can try to help? Cheers, and thanks for watching.
@javadkhalilarjmandi3906
@javadkhalilarjmandi3906 6 жыл бұрын
really thanks for replay, here is my problem, making pear of tasks which you explained very well, but I have three stations (workers)that their performance is not like each other and one is faster than other,so i wanna assign two pears to one station and one other pear to two other stations, we have three pears but i wanna give two of them to just one worker because his performance is faster(4 time faster, in this example) than other stations,thx again:-)
@LucidProgramming
@LucidProgramming 6 жыл бұрын
Hi Javad. There are quite a few variations that one can have on this problem, and the greedy method may not always be the appropriate approach. There are some properties that this problem has that make it particularly amenable to such a strategy. I would suggest you look up "assignment optimization", specifically the Wikipedia page. Try to come up with a formula for your case based on the information you read there. Then, see if you can write a solution. Let me know how your process goes, but I encourage you to actually sit down and think it through. Don't worry about giving the problem your time and attention, it's worth it to learn.
@javadkhalilarjmandi3906
@javadkhalilarjmandi3906 6 жыл бұрын
tnx for your help, sure I will try.I must fix this problem!again tnx
@LucidProgramming
@LucidProgramming 6 жыл бұрын
It's a gradual process. Keep at it. You'll get it. Thanks again for watching.
@amanjhurani6178
@amanjhurani6178 4 жыл бұрын
Which text editor you are using?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
vim. All the information for that is in the description of this video.
@amanjhurani6178
@amanjhurani6178 4 жыл бұрын
@@LucidProgramming Why you stopped making videos? Your videos are so much informative.
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@@amanjhurani6178 Thanks. I want to start up again. My schedule has been a bit rough these days. It's nice to know other people find these videos useful and informative. Thanks for that.
@amanjhurani6178
@amanjhurani6178 4 жыл бұрын
@@LucidProgramming Great, we will wait for something exciting.
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@@amanjhurani6178 Hoping there is something soon. Cheers!
@harrydadson7467
@harrydadson7467 5 жыл бұрын
I'm confused why do we divide the range by 2...please let me know thank you
@LucidProgramming
@LucidProgramming 5 жыл бұрын
I think this is explained in the slide portion of the video, right?
@harrydadson7467
@harrydadson7467 5 жыл бұрын
@@LucidProgramming That was pretty fast thanks but isnt range(len(a)//2) = 3 --> (0,1,2)
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@harrydadson7467 I don't understand what (0,1,2) has to do with it. The division will give you an integer.
@jackb7620
@jackb7620 5 жыл бұрын
because we only need to enter the loop three times (in this scenario), because each time we iterate, we pop the first AND the last item of the array. So if we have [1,2,3,4,5,6], it will look like this: Begin: [1,2,3,4,5,6] 1st iteration completed: [2,3,4,5] 2nd iteration completed [3,4] 3rd iteration completed: [] So now the array is empty, even though we only iterated half the times (3 iterations on an 6-element-array)
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@jackb7620 Thanks for the added clarification, Jakokb!
@louielogn6117
@louielogn6117 4 жыл бұрын
Is the slides available ?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Description has been updated with a link to the slides.
@louielogn6117
@louielogn6117 4 жыл бұрын
@@LucidProgramming ​ Thanks for all hard work u doing in this channel
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@@louielogn6117 Cheers, and thanks for the support.
@devendratapdia11
@devendratapdia11 5 жыл бұрын
Awesome
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Thank you! If you like my content, I've been working on some projects during the past couple of months. If you would like to stay up-to-date, please consider subscribing to my mail list. Also, if you haven't already, please consider subscribing! I really appreciate the support, and if you want to support the channel, I do have a PayPal link (www.paypal.me/VincentRusso1) for donations that go directly to the creation of content on this channel. I hope that the content I provide there will enhance the videos on my KZbin page. bit.ly/lp_email
@isaacyoon3926
@isaacyoon3926 3 жыл бұрын
Khan is this you?
@LucidProgramming
@LucidProgramming 3 жыл бұрын
Haha, Salman Khan? Afraid not :)
Greedy Algorithms Explained
17:48
Tech With Tim
Рет қаралды 114 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 816 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 9 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 62 МЛН
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 7 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 14 МЛН
Sorting Algorithms in Python: Intersection of Two Sorted Arrays
12:27
LucidProgramming
Рет қаралды 9 М.
Set Cover Problem Explained - Algorithms in Python
28:20
NeuralNine
Рет қаралды 2,5 М.
String Processing in Python: Spreadsheet Encoding
12:51
LucidProgramming
Рет қаралды 4,5 М.
3. Greedy Method -  Introduction
12:02
Abdul Bari
Рет қаралды 1,5 МЛН
Big O Notation - with Examples in Python
21:58
Jon Krohn
Рет қаралды 22 М.
Is THIS Python's MOST Underrated Operator? (Walrus Operator)
5:45
you will never ask about pointers again after watching this video
8:03
Binary Search - A Different Perspective | Python Algorithms
8:56
5 Simple Steps for Solving Dynamic Programming Problems
21:27
Reducible
Рет қаралды 1,1 МЛН
Binary Trees in Python: Level-order Traversal
15:50
LucidProgramming
Рет қаралды 35 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 9 МЛН