What do i need to know about dynamic programming? - java

Started up solving UVa problems again as a way to pass time (going to the army in 6 weeks). I love writing Java, but end up using C / C++. It's not because IO is faster, no need to box data, more memory or use of unsigned, because its algorithm efficiency that counts.
In short i am slowly constructing how to/article/code base for different categories of efficient algorithms and dp is next.
Quoting Mark Twain: It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so.
I aid assistance in building priority list what are must have efficient algorithms.

This MIT lecture is a good introduction to dynamic programming if you're already familiar with algorithms.

The wikipedia article on Dynamic Programming has a section entitled "Algorithms that use dynamic programming" with many examples.
Here is another good list of practice problems in dynamic programming.
Since you referenced the UVa problem list, you should definitely take a look at Problem 103 - Stacking Boxes. The problem lends itself well to a solution using a Longest Increasing Subsequence algorithm.

Related

How to approach writing algorithm from a complex research paper

I thought of writing a piece of software which does Alpha Compositing. I didn't wanted ready made code off from internet so I tried to find research papers and other sources to understand the mathematical algorithms, and initiated to implement.
But, I got lost very quickly. So my question is,
How should I approach these papers to extract the necessary details from it in order to write algorithm based on it. Any specific set of steps which works well?
Desired answer :
Read ...
Extract ...
Understand ...
Implement ...
Note: This question is not limited to only Alpha Compositing, so more generalised approach will be helpful. I have tagged Java and C++, because thats my desired language to implement the image processing.
What I have done so far?
This is not a homework question but it is of course better to say what I know. I have read wiki of Alpha compositing, and few closely related Image compositing research papers. But, I stuck at the next step to take in order to go from understanding to implementation.
Wikipedia
Technical Memo, Image compositing
I'd recommend reading articles with complex formulas with a pencil and paper. Work through the math involved until you have a good grasp on it. Then, you'll be ready to code.
Start with identifying the steps needed to perform your algorithm on some image data. Include all of the steps from loading the image itself into memory all the way through the complex calculations that you may need to perform. Then structure that list into pseudocode. Once you have that, it should be rather easy to code up.
Write pseudocode. Ideally, the authors of the research papers would have done this, but often they don't. Write pseudocode for some simple language like Matlab or possibly Python, and hack away at writing a working implementation based on the psuedocode.
If you understand some parts of the algorithm but not others, then implement your pseudocode into real code for the parts you understand, and leaving comments for the places you don't.
The section from The Pragmatic Programmer on "Tracer Bullets" basically describes this idea. You want to quickly hack together something that takes your data into some form of an output, and then iterate on the body of the code to get it to slowly resemble the algorithm you're trying to produce.
My answer is necessarily somewhat vague. There's no magic bullet for something like this.
Have you implemented any image processing algorithms? Maybe start with something a little simpler, like desaturation/color intensification, reversal (side to side and upside down), rotating, scaling, and compositing images through a mask.
Once you have those figured out, you will be in a very good position to do an alpha composite.
I agree that academic papers seem to go out of their way to make implementation details muddy and uncertain. I find that large amounts of simplification to what is written is needed to begin to perform a practical implementation. In their haste to be general, writers excessively parameterize every aspect. To build useful, reliable software, it is necessary to start with something simple which actually works so that it can be a framework to add features. To do that, it is necessary to throw away 80–90 percent of the academic generality. Often much can be done with a raft of symbolic constants, but abandoning generality (say for four and five dimensional images) doesn't really lose anything in practice.
My suggestion is to first write the algorithm using Matlab to make sure that you understood all the steps and then try to implement using C++ or java.
To add to the good suggestions above, try to write your pseudocode in simple module (Object oriented style ) so has to have a deep understanding of each part of your code while not loosing the big picture. Writing everything in a procedural way is good a the beginning but as the code grow, it might get become hard to keep up will all you are trying to do.
This example cites one of the seminal works on the topic: Compositing Digital Images by Porter & Duff. The class java.awt.AlphaComposite implements the same rules.

ML technique for classification with probability estimates

I want to implement a OCR system. I need my program to not make any mistakes on the letters it does choose to recognize. It doesn't matter if it cannot recognize a lot of them (i.e high precision even with a low recall is Okay).
Can someone help me choose a suitable ML algorithm for this. I've been looking around and find some confusing things. For example, I found contradicting statements about SVM. In the scikits learn docs, it was mentioned that we cannot get probability estimates for SVM. Whereas, I found another post that says it is possible to do this in WEKA.
Anyway, I am looking for a machine learning algorithm that best suites this purpose. It would be great if you could suggest a library for the algorithm as well. I prefer Python based solutions, but I am OK to work with Java as well.
It is possible to get probability estimates from SVMs in scikit-learn by simply setting probability=True when constructing the SVC object. The docs only warn that the probability estimates might not be very good.
The quintessential probabilistic classifier is logistic regression, so you might give that a try. Note that LR is a linear model though, unlike SVMs which can learn complicated non-linear decision boundaries by using kernels.
I've seen people using neural networks with good results, but that was already a few years ago. I asked an expert colleague and he said that nowadays people use things like nearest-neighbor classifiers.
I don't know scikit or WEKA, but any half-decent classification package should have at least k-nearest neighbors implemented. Or you can implement it yourself, it's ridiculously easy. Give that one a try: it will probably have lower precision than you want, however you can make a slight modification where instead of taking a simple majority vote (i.e. the most frequent class among the neighbors wins) you require larger consensus among the neighbors to assign a class (for example, at least 50% of neighbors must be of the same class). The larger the consensus you require, the larger your precision will be, at the expense of recall.

Learn backtracking algorithm

I want to learn the backtracking algorithm. Can someone please teach me some of it? I tried learning from some websites, but it didn't work. So can someone please teach me. Thank you!
Though language agnostic, this tutorial is nice and presents several examples that might provide the necessary intuition.
That said, the idea behind backtracking is not difficult to grasp at all. A backtracking algorithm essentially explores all the solution space just like when performing a brute force, except (and this makes it more efficient) it backtracks from a partial solution as soon as it realizes that it is not feasible.
An example
Consider this partial solution for the well known eight queens problem.
The queens in the first four columns have already been positioned, but the the last one is in an invalid square. A brute force solution would continue placing queens for the rest of the columns, oblivious of the fact that regardless of how this partial solution is augmented the result will be invalid.
The backtracking algorithm will be "smarter": it will realize the fourth queen is incorrectly placed and "go back" to considering other squares for it.
Fundamentals Of Computer Algorithms contains a nice chapter on backtracking. But you have not specified how much familiar you are with formal algorithm text and data structures. You may have some problems in reading this book if you are not familiar with basic algorithmic things like complexity analysis or don't know what is a tree. I mean in that case you will need to read the book from the beginning, direct jumping to backtracking chapter will not be much helpful.

Point me in the right direction on NLP datastructures and search algorithm

I've got a school assignment to make a language analyzer that's able to guess the language of an input. The assignment states this has to be done by pre-parsing language defined texts and making statistics about letters used, combinations of letter etc and then making a guess based on this data.
The data structure we're supposed to use is simple multi-dimensional hashtables but I'd like to take this opportunity to learn a bit more about implementing structures etc. What'd I'd like to know is what to read up about. My knowledge of algorithms is very limited but I'm keen on learning if someone could point me in the right direction.
Without any real knowledge and just reading up on different posts I'm currently planing on studying undirected graphs as a datastructure for letter combinations (and somehow storing the statistics within the graph as well) and boyer-moore for the per-word search algorithm.
Am I totally on the wrong track and these would be impossible to implement in this situation or is there something else superior for this problem?
If you can get your hands on a copy of Cormen et al. "Introduction to Algorithms"
http://www.amazon.com/Introduction-Algorithms-Second-Thomas-Cormen/dp/0262032937
It's a very very good book to read up on data structures and algorithms.
Language detection using character trigrams

Is a Java book enough or should I have to learn algorithms first? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am new to Java and referring Head-First book.
Do I have to learn Algorithms to be able to make programs in Java?
Should I learn Algorithms first or only Java books like Effective Java, Java puzzlers etc will be enough?
I want to be a successful enterprise developer. Then what algorithms and data structures I should be well versed with? What books would you recommend me?
To be a successful Java developer, do I need to know all the advanced algorithms such as those given in CLRS?
PS: I had C and C++ in my past semesters, I got good marks in them but that was kinda mugging. I know basics of programming. I am not a newbie. Thing is that I don't have sound knowledge in any language also I want to be a developer not an algorithmist.
If you want to become a successfull developer you'll need to learn a lot of stuff. A main programming language (java) and CS basics (e.g. algorithms) are just two of them. Others are: a communication skills, testing (I'd say TDD but I don't want to start a fight), handling of databases, web stuff, OOD and many more. Also a lot of stuff that you might not use directly will still help a lot by broadening your horizon (functional programming, advanced CS concepts)
There is no fixed order in which to learn that stuff. Since learning works best when you are motivated, just pick what you are interested in most or what is helping you most. Learn a little here, then learn a little there. Keep your eyes open and practice a lot and you will be fine.
In your current situation I'd recommend to pick up an Algorithms book and implementing the algorithms there in java. It teach you java and algorithms.
Also read "Clean Code", "The Pragmatic Programmer" and the SOLID http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod principles
My advice - learn a subset of Java, then learn some basic algorithms, then figure out where you want to go from there.
You don't need to know all of Java to experiment with implementing algorithms. You will need to be able to wrap a few methods (often only one) in a single class (and even that only because Java insists everything's a class). You'll need conditional and looping constructs and arrays. For data structures, you'll probably need to understand Java references, though you may be able to fake pointers using integers as array subscripts.
The point is that algorithms are important, but on a relatively small scale. Data structures typically need a few algorithms to make them work. But you don't need an understanding of larger-scale issues such as object-oriented design to experiment with algorithms.
EDIT
From comments, I see that you're already well into the "where to go from there" stage.
Don't forget algorithms (or discrete math) because you don't care about analysis, though. A broad (not necessarily in depth) understanding is a useful resource - they are great for problem-solving methods.
For example, I was recently confused in a dependency-ordering issue about how to deal with cycles. I'd forgotten about "strongly connected components" in digraphs. Once I was reminded, the problem became trivial - no point trying to order within a strongly connected component, but those components form an acyclic digraph. From there, the answer is just a topological sort away.
Knowing about topological sorts makes the last step trivial. Having forgotten about strongly connected components cost me a fair bit of time. Understanding how Tarjans strongly connected components algorithm works... Wikipedia and a few minutes with pen and paper are enough, once you know what to look for.
Actually, I should confess - "I was reminded" means I looked up an old Dr. Dobbs article on topological sorting that used the same approach.
I recommend you begin learning with Head First. functionx.com is also a good site. Most Java books describe algorithms as part of the book, so don't worry about algorithms. First of all learn Java.
Let me give you a easy way to find good books. Always look for higher rated books in amazon.com and then download them from wowebook.be.
I recommend "Data structures and algorithms in Java" by Michael T. Goodrich and Roberto Tamassia.
"Think Like a Programmer" are good algorithm-related books.
I'm with Steve314 for the most part but wanted to specify some things.
Learn enough Java (or whatever else) to be dangerous. Then the main data structures (lists, stacks, maps, trees, etc), which does include some algorithms for traversing them. Do the rest of your algorithms work after that. Here are some specific goals, if you want them, that more or less mirror the order of Data Structures & Algorithms in Java.
Understand Java and OO on a basic level. Specifically inheritance and polymorphism
(as Java define them) and how to use generics.
Have a basic understanding of Big-O
(how it's defined and why you can
drop lower order terms).
Be able to use, write, and trace recursive functions.
Code singly- and doubly-linked lists. They should implement Iterable and support adding and removing elements.
Use them to implement a stack and a queue.
A couple of things to know about once you do dive into a book like Algorithms:
MIT OCW has videos lectures up from the class that Algorithms was written for, given by one of its authors.
The purpose of a book like Algorithms is not just to show you specific algorithms, but to teach you how to analyze their running times. You need a small amount of discrete math for that, basically sums and recurrence relations. Look on here for big-O questions to see what I mean.
Speaking of videos, I think this is a way better place to start: Programming Abstractions (Stanford, Julie Zelenski). I'm very happy that those exist.
(As it happens I am in the middle of implementing a map with a binary search tree.)
#Chankey: According to me you should take both java and algorithms hand in hand. First learn some basics of Java programming language like what are classes, data types, functions etc. Then learn some basic algorithms of sorting and searching. And, now apply your java knowledge to implement these algorithms.
Cheers!!
Mukul Gupta

Categories

Resources