#CodingExercise
I apologize for cluttering up my posts with coding exercises, I know its distracting but cannot resist, and hence I'm tagging them so you can skip:
Successor in a binary search tree:
Node GetSuccessor(Node root)
{
if (root == null) return null;
if (root.right)
{
// return tree-minimum
Node current = root.right;
while(current && current.left)
current = current.left;
return current;
}
Node parent = root.parent;
while(parent && parent.right == root)
{
root = parent;
parent =parent.parent;
}
return parent;
}
For string matching between a string T of length n and a pattern P of length m, we mentioned Rabin Karp method earlier that brought two improvements over the brute force, one is that we can skip ahead up to a length m on a mismatch and for any spurious hits, a portion could match. We do this with a special hash function that builds up the resulting hash from the hash of the portions.
// after preprocessing
for (int s = 0; s<= n-m; s++)
if ( pattern == t.substring)
// do a full comparision of the substring before calling it a match
if s < n-m
then update the next substring with d(ts - T[s+1]h) + T[s + m + 1])mod q
KnuthMorrisPratt improves the preprocessing with the knowledge of how to shift the substring.
Finding the convex hull of a set Q of points can be solved with one of the following methods:
1 Rotational sweep methods such as
a) Graham's scan - Graham's scan solves the convex hull problem by maintaining a stack S of candidate points. While the angle formed by the next-to-top, top and pi makes a non-left turn, the stack is popped and the candidate point is pushed on the stack. At the end, the stack is left with exactly the points on the hull in the anticlockwise manner.
b) Jarvis' march builds a sequence of vertices which has the smallest polar angle with respect to the previous point. Similarly p2 has the smallest polar angle with respect to p1 and so on. When we reach the top we start all over and separate the left and the right sequence. The advantage of the separate chains is that we need not compute the angles but just compare it.
2) Incremental method : In the incremental method, the points are sorted from left to right, yielding a sequence. At the ith stage of the convex hull of I-1 points, the sequence is updated according to the ith point from the left.
3) Divide and Conquer method:
In this method the set of n points is divided into two subsets and the convex hull is computed recursively. Then the hulls are combined. Each recursive step takes as input the set P of points, the set X and Y each of which contains the points in P sorted monotonically by X and Y axis coordinates respectively. All the inputs are divided in half to form corresponding left and right sequences. The recursion is to make two calls - one to find the closest pair of points in the PL and the other for finding the same in PR. The merge step occurs by finding the closest pair either from one of the recursive calls with distance delta or by combining one from the PL and the other from the PR that reside in the two times delta vertical strip. In each recursive call, we form a sorted subset of a sorted array Y.
len(YL) = len(YR) = 0;
for I = 1 to len (Y)
do if Y[I] belongs to PL:
then len(YL) = len(YL) + 1
YL(len(YL) = y[I]
else
len[YR] = len[YR] + 1
YR[len[YR]] = Y[I]
4) The prune and search method: This finds the upper portion of the convex hull by repeatedly throwing out a constant fraction of the remaining points until only the upper chain remains. Then the process is repeated for the lower chain.
Node GetPredecessor(Node root)
{
if (root == null) return null;
if (root.left)
{
// return tree-minimum
Node current = root.left;
while(current && current.right)
current = current.right;
return current;
}
Node parent = root.parent;
while(parent && parent.left == root)
{
root = parent;
parent =parent.parent;
}
return parent;
}
void PrintFactors(n)
{
// Pollard Rho
int i = 1;
Random r = new Random();
int x = r.Next(0, n-1);
int y = x;
int k = 2;
for (;;)
{
i = i + 1;
x = (x * x - 1) % n;
d = gcd(y - x, n);
if (d != 1 && d != n)
Console.Writeline("{0}", d);
if (i == k)
{
y = x;
k = 2k;
}
}
}
int gcd(int a, int b)
{
// Euclid
if (b == 0)
{
return a;
}
else
{
return gcd(b, a mod b);
}
}
Kerninghan and Pike string matching code:
/* match: search for regexp anywhere in text */
int match(char* regexp, char* text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do {
if (matchhere(regexp, text))
return 1;
}while(*text++ != '/0');
return 0;
}
int matchere(char* regexp, char* text)
{
if (regexp[0] == '/0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '/0')
return *text == '/0';
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
return matchhere(regexp+1, text+1);
return 0;
}
int matchstar(int c, char* regexp, char* text)
{
do {
if (matchhere(regexp, text))
return 1;
} while (*text != '/0' && (*text++ == c || c == '.'));
return 0;
}
List<List<T>> GetPowerSet(List<T> elements)
{
if (elements.Count == 0) return new List<List<T>>(){ new List<T>();};
var cut = elements.GetRange(0,1);
var remaining = elements.Except(cut);
var powerSet = GetPowerSet(remaining) ;
powerSet.Union(Combine(powerset, cut.First()));
return powerSet;
}
List<List<T>> Combine (List<List<T>> powerSet, T item)
{
if (item)
{
foreach (var set in powerSet)
set.Add(item);
}
return powerSet;
}
void Stack::Push(Item item)
{
this.list.InsertAt(0, Item);
}
Item Stack::Pop()
{
this.list.RemoveAt(0);
}
I apologize for cluttering up my posts with coding exercises, I know its distracting but cannot resist, and hence I'm tagging them so you can skip:
Successor in a binary search tree:
Node GetSuccessor(Node root)
{
if (root == null) return null;
if (root.right)
{
// return tree-minimum
Node current = root.right;
while(current && current.left)
current = current.left;
return current;
}
Node parent = root.parent;
while(parent && parent.right == root)
{
root = parent;
parent =parent.parent;
}
return parent;
}
For string matching between a string T of length n and a pattern P of length m, we mentioned Rabin Karp method earlier that brought two improvements over the brute force, one is that we can skip ahead up to a length m on a mismatch and for any spurious hits, a portion could match. We do this with a special hash function that builds up the resulting hash from the hash of the portions.
// after preprocessing
for (int s = 0; s<= n-m; s++)
if ( pattern == t.substring)
// do a full comparision of the substring before calling it a match
if s < n-m
then update the next substring with d(ts - T[s+1]h) + T[s + m + 1])mod q
KnuthMorrisPratt improves the preprocessing with the knowledge of how to shift the substring.
Finding the convex hull of a set Q of points can be solved with one of the following methods:
1 Rotational sweep methods such as
a) Graham's scan - Graham's scan solves the convex hull problem by maintaining a stack S of candidate points. While the angle formed by the next-to-top, top and pi makes a non-left turn, the stack is popped and the candidate point is pushed on the stack. At the end, the stack is left with exactly the points on the hull in the anticlockwise manner.
b) Jarvis' march builds a sequence of vertices which has the smallest polar angle with respect to the previous point. Similarly p2 has the smallest polar angle with respect to p1 and so on. When we reach the top we start all over and separate the left and the right sequence. The advantage of the separate chains is that we need not compute the angles but just compare it.
2) Incremental method : In the incremental method, the points are sorted from left to right, yielding a sequence. At the ith stage of the convex hull of I-1 points, the sequence is updated according to the ith point from the left.
3) Divide and Conquer method:
In this method the set of n points is divided into two subsets and the convex hull is computed recursively. Then the hulls are combined. Each recursive step takes as input the set P of points, the set X and Y each of which contains the points in P sorted monotonically by X and Y axis coordinates respectively. All the inputs are divided in half to form corresponding left and right sequences. The recursion is to make two calls - one to find the closest pair of points in the PL and the other for finding the same in PR. The merge step occurs by finding the closest pair either from one of the recursive calls with distance delta or by combining one from the PL and the other from the PR that reside in the two times delta vertical strip. In each recursive call, we form a sorted subset of a sorted array Y.
len(YL) = len(YR) = 0;
for I = 1 to len (Y)
do if Y[I] belongs to PL:
then len(YL) = len(YL) + 1
YL(len(YL) = y[I]
else
len[YR] = len[YR] + 1
YR[len[YR]] = Y[I]
4) The prune and search method: This finds the upper portion of the convex hull by repeatedly throwing out a constant fraction of the remaining points until only the upper chain remains. Then the process is repeated for the lower chain.
Node GetPredecessor(Node root)
{
if (root == null) return null;
if (root.left)
{
// return tree-minimum
Node current = root.left;
while(current && current.right)
current = current.right;
return current;
}
Node parent = root.parent;
while(parent && parent.left == root)
{
root = parent;
parent =parent.parent;
}
return parent;
}
void PrintFactors(n)
{
// Pollard Rho
int i = 1;
Random r = new Random();
int x = r.Next(0, n-1);
int y = x;
int k = 2;
for (;;)
{
i = i + 1;
x = (x * x - 1) % n;
d = gcd(y - x, n);
if (d != 1 && d != n)
Console.Writeline("{0}", d);
if (i == k)
{
y = x;
k = 2k;
}
}
}
int gcd(int a, int b)
{
// Euclid
if (b == 0)
{
return a;
}
else
{
return gcd(b, a mod b);
}
}
Kerninghan and Pike string matching code:
/* match: search for regexp anywhere in text */
int match(char* regexp, char* text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do {
if (matchhere(regexp, text))
return 1;
}while(*text++ != '/0');
return 0;
}
int matchere(char* regexp, char* text)
{
if (regexp[0] == '/0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '/0')
return *text == '/0';
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
return matchhere(regexp+1, text+1);
return 0;
}
int matchstar(int c, char* regexp, char* text)
{
do {
if (matchhere(regexp, text))
return 1;
} while (*text != '/0' && (*text++ == c || c == '.'));
return 0;
}
List<List<T>> GetPowerSet(List<T> elements)
{
if (elements.Count == 0) return new List<List<T>>(){ new List<T>();};
var cut = elements.GetRange(0,1);
var remaining = elements.Except(cut);
var powerSet = GetPowerSet(remaining) ;
powerSet.Union(Combine(powerset, cut.First()));
return powerSet;
}
List<List<T>> Combine (List<List<T>> powerSet, T item)
{
if (item)
{
foreach (var set in powerSet)
set.Add(item);
}
return powerSet;
}
void Stack::Push(Item item)
{
this.list.InsertAt(0, Item);
}
Item Stack::Pop()
{
this.list.RemoveAt(0);
}
No comments:
Post a Comment