How do I use Dynamic Programming to solve Knapsack - dynamic-programming

I'm working on algorithms the classic Knapsack Problem (for MIT 6.002x) and also trying to get my head around Dynamic Programming.
The course doesn't cover DP, but asked me to go look it up. Which I have done but am none the wiser. Am finding it hard to relate to any of the tutorials / videos because they're mostly about solving other algorithms. And because I'm a bit fuzzy on DP to begin with they're not really helping.
Can anybody point me to a place that explains Dynamic Programming and how it is used to solve the Knapsack problem

you can learn basics of dynamic programming and understand how knapsack problem can be solved using link below.
http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/

Related

Sliding tile puzzle in Python 3, where to begin?

I am pretty new to coding and I'm currently stuck on the following practice questions in my textbook.
Write a program that generates an 'eight puzzle'. It should randomly shuffle the puzzle, then allow the user to solve it.
Extend your program so it has a 'solve' option that will solve it using A* search.
The problem is, after hours of browsing the web and YouTube, I only come across tutorials and examples which either assume advanced knowledge of this topic or include no useful annotations which would help me learn it.
I was wondering if anyone could please point me to a resource of some sort that explains how to even approach such a puzzle in Python 3. I have no clue what the best way to learn and start this is.
Thank you in advance for your time and help.

Is it possible to implement Peterson's algorithms for 3 processes and more?

So one of my assistive instructors at school gave us this assignment of implementing Peterson's algorithms for 3 processes along with some other assignments. I thought it would be doable having known how it works for 2 threads, but it wasn't and also so hard to check where it was failing because of the nature of multi-processing. I googled and found there are Filter's algorithm and Bakery algorithm that can work for N-processes but those don't look straight out of Peterson's algorithms. The instructor said he didn't check the validity of his question and seeing how I can't really find relevant code on this vast Google ocean I'm becoming dubious if I'm even doing things that can be done..!
Thank you for any help and forgive if there was wrong grammar, I'm not a native speaker.
The extension to the Peterson's algorithm is Filter algorithm, just go ahead. This can be done.

Suffix tries vs dynamic programming for string algorithms

It seems that many difficult string algorithms can be solved both using suffix tries(trees) and Dynamic Programming.
But I am not sure which approach is best to use and when.
Additionally which approach is better to master on the specific area of algorithms and have it in your arsenal in the area of job interviews? I assume it would be the one that would be used more frequently by a programmer in any task or something like that?
This is more of which algorithmic technique is more useful to master as most frequent to use in your job than simply comparing asymptotic notations
Think of a problem requiring the Lexicographically nth substring of a given string : A suffix array is just what you need...and it is easy to learn the bare essentials for solving most problems involving suffix arrays..
On the other hand DP is an algorithmic technique..MASTER IT and you will be able to solve a HUGE number of problems..not only strings.
For an interview though i will take DP anyday...for interviewers, a DP problem lets them make it knotty that is almost impossible to solve without DP (within given constraints) but the solution would mean that you give them a basic recursion and how DP helps you solve it.If it were a suffix-array-only-problem that would mean that they are assessing you over a single data structure( easy once learned) rather than an more general technique which requires mastery.
PS: I had put off learning DP until recently when i got fed up trying to solve problems (that require DP ) using any advanced data structures and would invariably fail ( Case in point : UVA 1394 -- simple problem now that i know how to solve it using DP but instead went on to study segment trees and achieved a O(nlgn) whereas DP gave me O(n). So final advice : if one hasn't studied DP drop everything else and go for it.
honestly, for job interview, no suffix tree is needed. that's too difficult and beyond the scope. however, DP is widely used in interviews for some famous companies like google and facebook.
suffix tree has limitation for solving problems compared with DP. usually it is used to solve string related problems. but DP can solve many different areas.

Resources for graph structure?

I have a problem related to graph.
I am not a computer science grad hence needed a some quick intro on what is graph and were can i read about graph and how to solve graph related problem in c++ or in general.
The boost graph library may be a starting point and give you some code for solving your graph related problems.
Please see Graph problems in the Stony Brook Algorithm Repository,
and a cute lecture by Xavier Llora.
I would start by studying a few specific algorithms. Dijkstra's algorithm and the graph closure algorithm are good places to start. Also most introductory computer science (eg. Data Structures) texts have a section on graphs. I used this book, mostly after I was already pretty comfortable with most of the material though. It takes a pretty formal approach, so if your maths is strong you might like it.
The community might be able to give you better pointers if you mentioned something specific that you're trying to solve (if there is such a thing).
This is a very cool tool for representing graphs

canonical problems list

Does anyone known of a a good reference for canonical CS problems?
I'm thinking of things like "the sorting problem", "the bin packing problem", "the travailing salesman problem" and what not.
edit: websites preferred
You can probably find the best in an algorithms textbook like Introduction to Algorithms. Though I've never read that particular book, it's quite renowned for being thorough and would probably contain most of the problems you're likely to encounter.
"Computers and Intractability: A guide to the theory of NP-Completeness" by Garey and Johnson is a great reference for this sort of thing, although the "solved" problems (in P) are obviously not given much attention in the book.
I'm not aware of any good on-line resources, but Karp's seminal paper Reducibility among Combinatorial Problems (1972) on reductions and complexity is probably the "canonical" reference for Hard Problems.
Have you looked at Wikipedia's Category:Computational problems and Category:NP Complete Problems pages? It's probably not complete, but they look like good starting points. Wikipedia seems to do pretty well in CS topics.
I don't think you'll find the answers to all those problems in only one book. I've never seen any decent, comprehensive website on algorithms, so I'd recommend you to stick to the books. That said, you can always get some introductory material on canonical algorithm texts (there are always three I usually recommend: CLRS, Manber, Aho, Hopcroft and Ullman (this one is a bit out of date in some key topics, but it's so formal and well-written that it's a must-read). All of them contain important combinatorial problems that are, in some sense, canonical problems in computer science. After learning some fundamentals in graph theory you'll be able to move to Network Flows and Linear Programming. These comprise a set of techniques that will ultimately solve most problems you'll encounter (linear programming with the variables restricted to integer values is NP-hard). Network flows deals with problems defined on graphs (with weighted/capacitated edges) with very interesting applications in fields that seemingly have no relationship to graph theory whatsoever. THE textbook on this is Ahuja, Magnanti and Orlin's. Linear programming is some kind of superset of network flows, and deals with optimizing a linear function on variables subject to restrictions in the form of a linear system of equations. A book that emphasizes the relationship to network flows is Bazaraa's. Then you can move on to integer programming, a very valuable tool that presents many natural techniques for modelling problems like bin packing, task scheduling, the knapsack problem, and so on. A good reference would be L. Wolsey's book.
You definitely want to look at NIST's Dictionary of Algorithms and Data Structures. It's got the traveling salesman problem, the Byzantine generals problem, the dining philosophers' problem, the knapsack problem (= your "bin packing problem", I think), the cutting stock problem, the eight queens problem, the knight's tour problem, the busy beaver problem, the halting problem, etc. etc.
It doesn't have the firing squad synchronization problem (I'm surprised about that omission) or the Jeep problem (more logistics than computer science).
Interestingly enough there's a blog on codinghorror.com which talks about some of these in puzzle form. (I can't remember whether I've read Smullyan's book cited in the blog, but he is a good compiler of puzzles & philosophical musings. Martin Gardner and Douglas Hofstadter and H.E. Dudeney are others.)
Also maybe check out the Stony Brook Algorithm Repository.
(Or look up "combinatorial problems" on google, or search for "problem" in Wolfram Mathworld or look at Hilbert's problems, but in all these links many of them are more pure-mathematics than computer science.)
#rcreswick those sound like good references but fall a bit shy of what I'm thinking of. (However, for all I know, it's the best there is)
I'm going to not mark anything as accepted in hopes people might find a better reference.
Meanwhile, I'm going to list a few problems here, fell free to add more
The sorting problem Find an order for a set that is monotonic in a given way
The bin packing problem partition a set into a minimum number of sets where each subset is "smaller" than some limit
The travailing salesman problem Find a Hamiltonian cycle in a weighted graph with the minimum total weight

Resources