Algorithm – Recursion

Algorithm – Recursion

It may be useful to write a separate wrapper routine to do initialization for a complex recursive routine.
Factorial routine that returns all of its intermediate results
int[] allFactorials(int n) { /* Wrapper routine */
int[] results = new int[n == 0 ? 1 : n];
doAllFactorials(n, results, 0);
return results;
}
int doAllFactorials(int n, int[] results, int level) {
if (n > 1) { /* Recursive case */
results[level] = n * doAllFactorials(n - 1, results, level + 1);
return results[level];
} else { /* Base case */
results[level] = 1;
return 1;
}
}
Permutations of a String
Implement a routine that prints all possible orderings of the characters in a string.
Treat each character in the input string as a distinct character, even if it is repeated.
To find all permutations starting at position n, successively place all allowable letters in position n, and for each new letter in position n find all permutations starting at position n + 1 (the recursive case). When n is greater than the number of characters in the input string, a permutation has been completed; print it and return to changing letters at positions less than n (the base case).
If you're past the last position
Print the string
Return
Otherwise
For each letter in the input string
If it's marked as used, skip to the next letter
Else place the letter in the current position
Mark the letter as used
Permute remaining letters starting at current position + 1
Mark the letter as unused
public static void permute(String str) {
int length = str.length();
boolean[] used = new boolean[length];
StringBuffer sb = new StringBuffer();
char[] in = str.toCharArray();
doPermute(in, sb, used, length, 0);
}
private static void doPermute(char[] in, StringBuffer sb, boolean[] used, int length,
int level) {
if (level == length) {
System.out.println(sb.toString());
return;
}
for (int i = 0; i < length; ++i) {
if (used[i])
continue;
sb.append(in[i]);
used[i] = true;
doPermute(in, sb, used, length, level + 1);
used[i] = false;
sb.setLength(sb.length() - 1);
}
}
Combinations of a String
These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of their characters are the same combination.
For each letter from input start position to end of input string
Select the letter into the current position in output string
Print letters in output string
If the current letter isn't the last in the input string
Generate remaining combinations starting at next position with iteration starting
at next letter beyond the letter just selected
public static void combine( String str ){
int length = str.length();
char[] instr = str.toCharArray();
StringBuilder outstr = new StringBuilder();
doCombine( instr, outstr, length, 0, 0 );
}
private static void doCombine( char[] instr, StringBuilder outstr, int length,
int level, int start ){
for( int i = start; i < length; i++ ){
outstr.append( instr[i] );
System.out.println(outstr);
if( i < length - 1 ){
doCombine( instr, outstr, length, level + 1, i + 1 );
}
outstr.setLength(outstr.length() - 1);
}
}
Telephone Words
Write a routine that takes a seven-digit telephone number and prints out all of the possible "words" or combinations of letters that can represent the given number.
You can use the helper routine char getCharKey( int telephoneKey, int place )
If the current digit is past the last digit
Print the word because you're at the end
Else
For each of the three digits that can represent the current digit, going from
low to high
Have the letter represent the current digit
Move to next digit and recurse
If the current digit is a 0 or a 1, return
public static final int PHONE_NUMBER_LENGTH = 7;
public static void printTelephoneWords(int[] phoneNum) {
char[] result = new char[PHONE_NUMBER_LENGTH];
doPrintTelephoneWords(phoneNum, 0, result);
}
private static void doPrintTelephoneWords(int[] phoneNum, int curDigit, char[] result) {
if (curDigit == PHONE_NUMBER_LENGTH) {
System.out.println(String.valueOf(result));
return;
}
for (int i = 1; i <= 3; i++) {
result[curDigit] = getCharKey(phoneNum[curDigit], i);
doPrintTelephoneWords(phoneNum, curDigit + 1, result);
if (phoneNum[curDigit] == 0 || phoneNum[curDigit] == 1)
return;
}
}
Recursive Version:
Create the first word character by character
Loop infinitely:
Print out the word
Increment the last letter and carry the change
If the first letter has reset, you're done
private static final int PHONE_NUMBER_LENGTH = 7;
public static void printTelephoneWords(int phoneNum[]) {
char[] result = new char[PHONE_NUMBER_LENGTH];
int i;
/*
* Initialize the result (in our example, put GWP1WAR in result).
*/
for (i = 0; i < PHONE_NUMBER_LENGTH; i++)
result[i] = getCharKey(phoneNum[i], 1);
/* Main loop begins */
while (true) {
for (i = 0; i < PHONE_NUMBER_LENGTH; ++i) {
System.out.print(result[i]);
}
System.out.print('\n');
/*
* Start at the end and try to increment from right to left.
*/
for (i = PHONE_NUMBER_LENGTH - 1; i >= -1; i--) {
/*
* You're done because you tried to carry the leftmost digit.
*/
if (i == -1)
return;
/* Otherwise, we're not done and must continue. */
/*
* You want to start with this condition so that you can deal
* with the special cases, 0 and 1 right away.
*/
if (getCharKey(phoneNum[i], 3) == result[i] || phoneNum[i] == 0
|| phoneNum[i] == 1) {
result[i] = getCharKey(phoneNum[i], 1);
/* No break, so loop continues to next digit */
} else if (getCharKey(phoneNum[i], 1) == result[i]) {
result[i] = getCharKey(phoneNum[i], 2);
break;
} else if (getCharKey(phoneNum[i], 2) == result[i]) {
result[i] = getCharKey(phoneNum[i], 3);
break;
}
}
}
}
Print number in base m
public final class PrintInt {
private static final String DIGIT_TABLE = "0123456789abcdef";
private static final int MAX_BASE = DIGIT_TABLE.length();
// Print n in any base, recursively
// Precondition: n >= 0, 2 <= base <= MAX_BASE
private static void printIntRec(long n, int base) {
if (n >= base)
printIntRec(n / base, base);
System.out.print(DIGIT_TABLE.charAt((int) (n % base)));
}
// Driver routine
public static void printInt(long n, int base) {
if (base <= 1 || base > MAX_BASE)
System.err.println("Cannot print in base " + base);
else {
if (n < 0) {
n = -n;
System.out.print("-");
}
printIntRec(n, base);
}
}
}

public class BinarySearchRecursive
{
public static final int NOT_FOUND = -1;
public static <AnyType extends Comparable<? super AnyType>> int binarySearch( AnyType [ ] a, AnyType x )
{
return binarySearch( a, x, 0, a.length -1 );
}
// Hidden recursive routine.
private static <AnyType extends Comparable<? super AnyType>>
int binarySearch( AnyType [ ] a, AnyType x, int low, int high )
{
if( low > high )
return NOT_FOUND;
int mid = ( low + high ) / 2;
if( a[ mid ].compareTo( x ) < 0 )
return binarySearch( a, x, mid + 1, high );
else if( a[ mid ].compareTo( x ) > 0 )
return binarySearch( a, x, low, mid - 1 );
else
return mid;
}
}

public class BinarySearch
{
public static final int NOT_FOUND = -1;
public static <AnyType extends Comparable<? super AnyType>>
int binarySearch( AnyType [ ] a, AnyType x )
{
int low = 0;
int high = a.length - 1;
int mid;
while( low <= high )
{
mid = ( low + high ) / 2;
if( a[ mid ].compareTo( x ) < 0 )
low = mid + 1;
else if( a[ mid ].compareTo( x ) > 0 )
high = mid - 1;
else
return mid;
}
return NOT_FOUND; // NOT_FOUND = -1
}
}

Draw Ruler
public class Ruler extends Frame
{
private static final int theSize = 511;
public void paint( Graphics g )
{
drawRuler( g, 10, theSize - 1 + 10, 8 );
}
private void drawRuler( Graphics g, int left, int right, int level)
{
if( level < 1 )
return;
int mid = ( left + right ) / 2;
/*************
***** Uncomment this section to see the drawing in slow motion
try { Thread.sleep( 100 ); }
catch( InterruptedException e ) { }
*************/
g.drawLine( mid, 80, mid, 80 - level * 5 );
drawRuler( g, left, mid - 1, level- 1 );
drawRuler( g, mid + 1, right, level - 1 );
}
// Simple test program
// For simplicity, must terminate from console
public static void main( String [ ] args )
{
Frame f = new Ruler( );
f.setSize( theSize + 20, 110 );
f.setVisible( true );
}
}

Modulo operation
A ≡ B (mod n) if A and B have the same remainder when divided by n, we say that A is equivalent to B, modulo n.
Equivalently, A ≡ B (mod n) if and only if n is A divisor of B - A.
if A ≡ B (mod n), then:
1. A+C ≡ B+C (mod n)
2. AD ≡ BD (mod n)
3. for any positive number P, A^P ≡ B^P (mod n)


X= 3333^5555 (mod 10) = (3333 (mod 10))^5555 (mod 10) = 3^5555 (mod 10)
∵3^4 (mod 10) = 1
∴ X= 3^5555 (mod 10) = (3^4^1388 * 3^3 )(mod 10) = 3^3 (mod 10) = 7

Compute X^N (mod P)
/**
* Return x^n (mod p)
* Assumes x, n >= 0, p > 0, x < p, 0^0 = 1
* Overflow may occur if p > 31 bits.
*/
public static long power( long x, long n, long p )
{
if( n == 0 )
return 1;
long tmp = power( ( x * x ) % p, n / 2, p );
if( n % 2 != 0 )
tmp = ( tmp * x ) % p;
return tmp;
}

Greatest Common Divisor
public static long gcd(long m, long n) {
if (m < n) {
long t = m;
m = n;
n = t;
}
long r = m % n;
if (r == 0) {
return n;
} else {
return gcd(n, r);
}
}

LCD - Lowest common denominator
public static long lcd(long m, long n) {
return m*n/gcd(m,n);
}


Resources:
Data Structures and Problem Solving Using Java
Programming Interviews Exposed: Secrets to Landing Your Next Job, 2nd Edition
Post a Comment

Labels

Java (159) Lucene-Solr (110) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (36) Eclipse (34) Code Example (31) Linux (24) JavaScript (23) Spring (22) Windows (22) Web Development (20) Nutch2 (18) Tools (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) J2EE (13) Network (13) Troubleshooting (12) PowerShell (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Http Client (8) Maven (8) Problem Solving (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) ANT (6) Coding Skills (6) Database (6) Scala (6) Shell (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) Lesson Learned (5) Programmer Skills (5) Tips (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) System Design (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts