Given a binary tree with an integer, Left & Right pointers, how can one traverse the tree in O(n) time and O(1) extra memory (no stack/queue/recursion)?
This guy gave a solution which is not O(n) total time that encoded the current path as an integer (and thus works on for trees of limited depth).
I am looking for the classical solutio...
I have an image file of a map and a curvy road marked with bold red line (wider than 1 px, there is no other red objects on image). Can someone please suggest steps how to recognize this road as a line and then turn it into a function y=f(x) so I can measure precise distances. I have no idea where to start...
Thanks.
...
I recently wrote a Bayesian spam filter, I used Paul Graham's article Plan for Spam and an implementation of it in C# I found on codeproject as references to create my own filter.
I just noticed that the implementation on CodeProject uses the total number of unique tokens in calculating the probability of a token being spam (e.g. if the...
Consider an array element X is one of leaders of the array if the all the elements following that array element is lesser than or equal to the array element X ...then what is the best algorithm to find all the leaders of the array?
Array may have more leaders.. consider the following array [10 9 8 6 ] then 10,9,8 are leaders of the...
Say I have a
struct SMyStruct
{
int MULT;
int VAL;
};
std::map<std::string, SMyStuct*> _idToMyStructMap;
Now I want to calculate total of all SMyStuct, where total is defined as MULT1 *VAL1 + MULT2 *VAL2 for each elements in the idToMyStructMap.
Seems like accumulate function is a natural choice. Please suggest. thanks
...
I have a DAG storing the relation between certain objects in my application. When this structure is updated by adding a new vertex below an existing one (i. e., implicitly creating a new edge into the new vertex) and then (at any later time) a new edge from there to other vertices, I want to ensure that the graph stays a DAG, i. e. that ...
I have the following classes. For testing purpose, I would like to get all the possible permutations of the class Client. I know that the number can be very large, but this is not my problem for now.
Client: No (int), Name(string), Address(Address object)
Address: Street(string), Country(string), etc.
For a property of type int, I alw...
I am a graph/network enthusiast and this just for my curiosity :)
I am trying to model the StackOverflow community as a graph/network. Assume that the people in the SO community are nodes and that the answers given to any of the question establishes a relationship between these nodes. The relationship can be assumed to be directed(li...
In order to find leap years, why must the year be indivisible by 100 and divisible by 400?
I understand why it must be divisible by 4. Please explain the algorithm.
...
From time to time have I come across manually implemented sort and/or search algorithms, instead of using language implemented algorithms. Most source code I've been looking into is written in Java, C# or PHP - but I'd guess this phenomenon is language agnostic.
Regarding regular data structures such as lists; Why and where do you imple...
I was reading this article today on two different regular expression algorithms.
According to the article old Unix tools like ed, sed, grep, egrep, awk, and lex, all use what's called the Thompson NFA algorithm in their regular expresssions...
However newer tools like Java, Perl, PHP, and Python all use a different algorithm for thei...
Hello there,
Imagine I have a situation where I need to index sentences. Let me explain it a little bit deeper.
For example I have these sententes:
The beautiful sky.
Beautiful sky dream.
Beautiful dream.
As far as I can imagine the index should look something like this:
But also I would like to do search by any of these words...
I need to encode/decode an integer which is up to 9 digits long but most often 7 digits long. I'd like to make it easier to communicate/memorise - it will be communicated by phone, copied & pasted, keyed in from a card/memory/label, hand-written on labels and generally abused!
I'm looking to reduce the number of digits (whilst adding a ...
I'm implementing a CSS Sprites setup in my ASP.NET application based on this article..
http://weblogs.asp.net/zowens/archive/2008/03/05/css-sprite-for-asp-net.aspx
In the section "Get the SPRITE already!"... the author says...
"The only real down side to this is that all your images are lined up vertically. I'm not quite sure that thi...
Suppose I have a sequence of numbers:
{n, n+1, n+2, ... n + m}
Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3,...m} will spit out the original set in a random (or at least pseudo random) order.
For example assume my sequence is {10, 11, 12, 13, 14, 15, 16, 17}
f(1) could ...
I know a bit about database internals. I've actually implemented a small, simple relational database engine before, using ISAM structures on disk and BTree indexes and all that sort of thing. It was fun, and very educational. I know that I'm much more cognizant about carefully designing database schemas and writing queries now that I kno...
For general-purpose sorting, the answer appears to be no, as quick sort, merge sort and heap sort tend to perform better in the average- and worst-case scenarios. However, insertion sort appears to excel at incremental sorting, that is, adding elements to a list one at a time over an extended period of time while keeping the list sorted,...
Firstly, I realise that this is a very similar question to this one: http://stackoverflow.com/questions/345982/which-are-the-good-open-source-libraries-for-collective-intelligence-in-net-java
... but all the answers to that one were Java centric so I am asking again, this time looking more for .Net (idealy C#) ideas.
A little backgroun...
Why are most priority/heap queues implemented as 0 being the highest priority? I'm assuming I'm missing out some key mathematical principle. As I was implementing my own priority queue recently it seemed easier to write the insert function if priority went up with the integer value, but apparently people smarter than me think it should g...
I use my own library for a lot of stuff, and recently I decided to add gradient functionality, but I've encountered a problem that I seem to remember having a while ago also, and this is the matter of my gradient being slightly off near the end. First, the code in question:
gradient = function(l, g)
{
var r = [], s = [], f = g.length -...