algorithm

given two bits in a set of four, find position of two other bits

hello I am working on a simple combinatorics part, and found that I need to recover position of two bits given position of other two bits in 4-bits srring. for example, (0,1) maps to (2,3), (0,2) to (1,3), etc. for a total of six combinations. My solution is to test bits using four nested ternary operators: ab is a four bit string, wi...

LRU cache design

Least Recently Used (LRU) Cache is to discard the least recently used items first How do you design and implement such a cache class? The design requirements are as follows: 1) find the item as fast as we can 2) Once a cache misses and a cache is full, we need to replace the least recently used item as fast as possible. How to analyze a...

What is a typical algorithm for finding a string within a string?

I recently had an interview question that went something like this: Given a large string (haystack), find a substring (needle)? I was a little stumped to come up with a decent solution. What is the best way to approach this that doesn't have a poor time complexity? ...

Breadth First Search and Depth First Search

Can anybody give a link for a simple explanation on BFS and DFS with its implementation? ...

GO TO and DON'T GO TO ! proof of this:

is this proofed: every algorithm A designed using go to or something like, is equivalence to another algorithm B that does not use go to. in other words: every algorithm designed using go to, can be designed without using go to. how to proof? ...

A Relation based page rank algorithm for semantic web search engine

what is the work flow of page rank algorithm? ...

Determining The Coordinates Of A Point Based On Its Known Difference From Three Other Points

I have the coordinates of three points on a plane. Let's call them X1,Y1, X2,Y2, X3 Y3. I need to calculate X4,Y4 but all I know is: X1,Y1 is 350 units in distance from X4,Y4 X2,Y2 is 200 units in distance from X4,Y4 X3,Y3 is 50 units in distance from X4,Y4 I Know The Exact Values For X1,Y1, X2,Y2, and X3,Y3 How can I determine the ...

Convert arbitrary size of byte[] to BigInteger[] and then safely convert back to exactly the same byte[], any clues?

I believe conversion exactly to BigInteger[] would be optimal in my case. Anyone had done or found this written in Java and willing to share? So imagine I have arbitrary size byte[] = {0xff,0x3e,0x12,0x45,0x1d,0x11,0x2a,0x80,0x81,0x45,0x1d,0x11,0x2a,0x80,0x81} How do I convert it to array of BigInteger's and then be able to recover it b...

Check if any item in a list matches any item in another list

A coleague asked me to write a one-liner to replace the following method: public static bool IsResourceAvailableToUser(IEnumerable<string> resourceRoles, IEnumerable<string> userRoles) { foreach (var userRole in userRoles) foreach (var resourceRole in resourceRoles) if (resourceRole == userRole) r...

Version Spaces algorithm examples

I'm asked to learn a concept using the Version Spaces (VS) algorithm that is able to predict life expectancy given a set of samples and data. The class examples we saw are kind of trivial compared to the assignment we got now. In particular I'm unsure about how to decide on the granularity of the concept hierarchies given that the data i...

How to find validity of a string of parentheses, curly brackets and square brackets?

I recently came in contact with this interesting problem. You are given a string containing just the characters '(', ')', '{', '}', '[' and ']', for example, "[{()}]", you need to write a function which will check validity of such an input string, function may be like this: bool isValid(char* s); these brackets have to close in the co...

Algorithm to aggregate values from child tree nodes

I have objects in a tree structure, I would like to aggregate status information from children nodes and update parent node with aggregated status. Lets say node A has children B1, B2, and B1 has C1, C2, C3 as children. Each of the nodes have a status attribute. Now if C1, C2, C3 are all complete then I would like to mark B1 as complete...

How can I calculate what date Good Friday falls on given a year?

Does anyone have a good algorithm to calculate what date Good Friday falls on given the year as an input? Preferably in C#. ...

Average of two strings in alphabetical/lexicographical order

Suppose you take the strings 'a' and 'z' and list all the strings that come between them in alphabetical order: ['a','b','c' ... 'x','y','z']. Take the midpoint of this list and you find 'm'. So this is kind of like taking an average of those two strings. You could extend it to strings with more than one character, for example the midpo...

What is the most efficient way to determine if a directed graph is singly connected?

I am working on an assignment where one of the problems asks to derive an algorithm to check if a directed graph G=(V,E) is singly connected (there is at most one simple path from u to v for all distinct vertices u,v of V. Of course you can brute force check it, which is what I'm doing right now, but I want to know if there's a more eff...

C: Why some string.h algorithms work slower than simple hand-made ones?

For example, strlen() takes about 3*O(n). Why 3? I've wrote a really simple strlen-like function - it works much faster. int string_len(char s[],int &l) { for(l=0;s[l];l++); return l; } Well, some of my friends says, nearly all of string.h algorithms are slower than they should be. ...

What is the name of this geometrical function?

In a two dimentional integer space, you have two points, A and B. This function returns an enumeration of the points in the quadrilateral subset bounded by A and B. A = {1,1} B = {2,3} Fn(A,B) = {{1,1},{1,2},{1,3},{2,1},{2,2},{2,3}} I can implement it in a few lines of LINQ. private void UnknownFunction(Point to, Point from, List<Poi...

comparison of sorting algorithms

When do we prefer a) bucket sort, and b) radix sort over comparison sorts like bubble sort insertion sort selection sort merge sort quick sort? ...

Bit Reversal using bitwise

Hi all I am trying to do bit reversal in a byte. I use the code below static int BitReversal(int n) { int u0 = 0x55555555; // 01010101010101010101010101010101 int u1 = 0x33333333; // 00110011001100110011001100110011 int u2 = 0x0F0F0F0F; // 00001111000011110000111100001111 int u3 = 0x00FF00FF; // 00000000111111110000000...

Count Occurence of Needle String in Haystack String, most optimally?

The Problem is simple Find "ABC" in "ABCDSGDABCSAGAABCCCCAAABAABC" without using String.split("ABC") Here is the solution I propose, I'm looking for any solutions that might be better than this one. public static void main(String[] args) { String haystack = "ABCDSGDABCSAGAABCCCCAAABAABC"; String needle = "ABC"; char [] needl = needl...