algorithm

How to use find algorithm with a vector of pointers to objects in c++?

I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem: class A { public: A(string a):_a(a) {} bool operator==(const A& p) { return p._a == _a; } private: string _a; }; vector<A*> va; va.push_back(new A("one")); va.push_back(new A("two")); va.push_b...

Counting, reversed bit pattern

I am trying to find an algorithm to count from 0 to 2n-1 but their bit pattern reversed. I care about only n LSB of a word. As you may have guessed I failed. For n=3: 000 -> 0 100 -> 4 010 -> 2 110 -> 6 001 -> 1 101 -> 5 011 -> 3 111 -> 7 You get the idea. Answers in pseudo-code is great. Code fragments in any language are welcome,...

Painless 'Analysis of Algorithms' Training?

I had a painful experience with the "Analysis of Algorithms" classes back in college but have recently found a need for it in the real world. -- Anyway, I'm looking for a simple-yet-effective crash course. Any ideas? Related Sidenote: It sure would be nice if there were a "Cartoon Guide to Algorithm Analysis", taught by Dilbert. UPD...

Russian Peasant Multiplication

Here is my short implementation of Russian Peasant Multiplication. How can it be improved? Restrictions : only works when a>0,b>0 for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1); ...

Help me write a binary search for boundary values (extracting sub lists)

Let's say I have an array of lots of values (C++ syntax, sorry): vector<double> x(100000); This array is sorted such that x[n] > x[n-1]. I would like a function to retrieve an array of all values in the range [a, b] (that's inclusive). Some interface like: void subarray(const double a, const double b, vector<double> &sub) { ... ...

Best algorithm for detecting cycles in a directed graph

What is the most effiecent alogrithm for detecting all cycles within a directed graph? or I have a directed graph representing a schedule of jobs that need to be exectuted, a job being a node and a dependency being an edge. I need to detect the error case of a cycle within this graph leading to cyclic dependencies. ...

C++: Best algorithm to check if a vector is sorted

What would be the best way to check that a std::vector is sorted ? Is there something faster than a loop checking that v[i]<=v[i+1] ? Is it faster/cleaner with iterators ? Or is it actually better to just call sort every time (though the "v is already sorted" case is quite common) ? We can safely assume the vector only contains PODs, us...

Best sorting algorithms for C# / .NET in different scenarios

What are the best algorithms for sorting data in C#? Is there one sorting algorithm that can handle 80% of sorts well? Please give code examples if applicable. ...

Drawing a Topographical Map

I've been working on a visualization project for 2-dimensional continuous data. It's the kind of thing you could use to study elevation data or temperature patterns on a 2D map. At its core, it's really a way of flattening 3-dimensions into two-dimensions-plus-color. In my particular field of study, I'm not actually working with geograph...

Is writing code out still considered an algorithmic representation?

I just lost 50% of my answer on a test because I wrote the code out instead of an algorithm on my midterm, bumping me from an A to a C. Is writing code out still considered an algorithmic representation? Wikipedia: Algorithm Representation (since programming style is pretty much consensus-based) EDIT: Ok, so let me make a few points cl...

Interdisciplinary Algorithm Construction with Non-developers

Yeah, I know the title is a mouthful... What I mean is to say is how do you communicate with a subject matter expert who needs a theory coded and tested? For example, weather simulation is a collaboration between meteorologists, computer scientists, and software engineers. The computer scientists and software engineers generally speak...

Have you used a traveling salesman algorithm to solve a problem?

I studied TSP in college in the context of NP Completeness. I have never actually had a situation where it would apply to a practical problem. A little bit of research shows that it has been used to pick the cheapest path to move a drill around, that is making holes in circuit boards. That is pretty much all I could find. Are you ...

How do I calculate the closest power of 2 or 10 a number is?

What is the most efficient way to cacluate the closest power of a 2 or 10 to another number? e.g. 3.5 would return 4 for power of 2 and 1 for power of 10 123 would return 128 for power of 2 and 100 for power of 10 0.24 would return 0.25 for power of 2 and 0.1 for power of 10 I'm just looking for the algorithm and don't mind the langu...

Spell checking city names?

I figure this problem is easier than just a regular spell checker since the list of U.S cities is small compared to all known English words. Anyhow, here's the problem: I have text files with full of city names; some of which are spelled correctly and some which aren't. What kind of algorithm can I use to correct all the misspellings ...

Fastest way of finding object in collection with coordinates near point

I have collection of objects. Each object represents a coordinate range (ie, a block). What I want is to find the object near another coordinate in a given direction. Is there a way to do this without traversing the whole collection all the time? ...

How can I negate a functor in C++ (STL)?

I have some function to find a value: struct FindPredicate { FindPredicate(const SomeType& t) : _t(t) { } bool operator()(SomeType& t) { return t == _t; } private: const SomeType& _t; }; bool ContainsValue(std::vector<SomeType>& v, SomeType& valueToFind) { return find_if(v.begin(), v.end(), FindPredicate...

What is the best low-tech protocol to simulate drawing names out of a hat and ensure secrecy?

Each year at Thanksgiving, my family has drawn names out of a hat to determine who they'll be a "Secret Santa" for the Christmas gift exchange. It's important to our family culture that no one else in the family knows who each other got in order to keep it interesting. The only rule to the selection is that you can't pick your spouse. If...

C++ string diff (a la Python's difflib)

I'm trying to diff two strings to determine whether or not they solely vary in one numerical subset of the string structure; for example, varies_in_single_number_field('foo7bar', 'foo123bar') # Returns True, because 7 != 123, and there's only one varying # number region between the two strings. In Python I can use the difflib to accom...

Recommended face detection tools/SDK/etc.

I'm looking for a quick way to detect faces in pictures (stored as JPG or any other popular image format). Code in any popular programming language will do (Python, C#, Java, Matlab, etc.). I'm also willing to implement an algorithm by myself, as long as it is proven to be a good working one. Alternatively, if there are known freeware (...

Finding a pattern in a set

What algorithms could i use to determine common characters in a set of strings? To make the example simple, I only care about 2+ characters in a row and if it shows up in 2 or more of the sample. For instance: 0000abcde0000 0000abcd00000 000abc0000000 00abc000de000 I'd like to know: 00 was used in 1,2,3,4 000 was used in 1,2,3,...