Professional Documents
Culture Documents
com
RECYCLE
(RECURSION)
one
2nd
RECURSION
- Recursionis finding the result of an operation depending on the results
of one or two previous operations.
RECYCLE
-A problem has a simple non-recursive solution. Other cases
of the problem can be reduced to a stopping case
recursively.
P1
P2 ......................... pn
.........................................................................................................................................
Situation
Basis
P P P P ..........
P P P P P P P P
5
/* For (n>=2), divide P into P1, P2, ..Pn */ /* Solve problems recursively */
RECYCLE
- The factorial function is a classic example of recursion:
- n! =1*2*3*…*(n–1)*n
- Todefine the factorial operation recursively, it is necessary to define it as
the factorial of small numbers.
- n! =n*(n–1)!
- Stop status 0 ! = is taken as 1.
- At each call, the n value is decreased by one to reach the stop state.
- Recursive definition:
- n = 1, if n = 0
- f(n) =
- n * f( n – 1 ) , if n > 0
7
RECYCLE
- When the code below runs, it calculates the factorial value
of n.
- int recursiveFactorial(int n)
-{
- if ( n == 1 ) return( 1 );
- else return ( n * recursiveFactorial( n - 1 ));
-}
RECYCLE
- Recursion tracking
- One box for each
recursion call
- An arrow from every summoner to the
summoned
RECYCLE
NS
10
RECYCLE
- Generally, iterative functions are more efficient in terms of
time and space.
- Iterative algorithm uses loop structure.
- The recursion algorithm uses the branching algorithm.
RECYCLE
- Recursive - iterative
-} - return callValue;
- }
12
FactorialExample.java
import java.io.*;
class FactorialExample
{ static int number;
public static void main(String args[]) throws IOException
{
System.out.print("Please give number:"); System.out.flush();
number=getInt(); int result = factorial(number);
System.out.println(number+"!="+result);
}
public static int factorial(int n) public static String getString() throws IOException
{ {
if(n==0) return 1; InputStreamReader isr = new InputStreamReader(System.in);
else return(n*factorial(n-1)); BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
}
return s;
}
public static int getInt() throws IOException
{ String s = getString();
return Integer.parseInt(s);
}
}
13
Sum of Numbers Up to N
- Suppose our problem is the sum of numbers from 1 to
n.
return 1
15
Runtime of Sum(n)
/* Merge */
return searchSum + n;
} /* done-searchTotal */
16
an Calculate Expression-Pow(a,n)
/* calculate a^n */
- Divide, rule & join
double Ust(double a, int n){
operations can be done
double searchResult;
with one expression.
/* Base case */
if (n == 0) return 1; else if (n == 1)
return a; /* Calculate a^n */
double Ust(double a, int n){
/* searchResult = a^(n-1) */ /* Base case */
searchResult = top(a, n-1); if (n == 0) return 1; else if (n == 1)
return a;
/* Merge */
return searchResult*a; return Upper(a, n-1)*a;
} /* done-Ust */ } /* done-Ust */
17
Top(3,4) return 81
/* Calculate a^n */
double Ust(double a, int n){ return 3*Ut(3,3) =81
/* Base case */
if (n == 0) return 1; else if (n == 1) return 27
Top(3,3)
return a;
return 3*Ut(3,2) =27
return a * Upper(a, n-1);
} /* done-Ust */ return 9
Top(3,2)
Public ... main(...){ return 3*Ut(3,1) =9
double x;
Runtime of Ust(a, n)
/* Calculate a^n */
double Ust(double a, int n){
/* base case */
if (n == 0) return 1; else if (n == 1)
return a;
Runtime of Pow(x, n)
Power(2, 8)
- 2nd8=2*2*2*2*2*2*2*2
- 2nd8=24*2nd4
- and 24=22nd*2nd2nd
- and 22nd=2one*2ndone
times.
22
Runtime of Power(x, n)
24
Recursion or Iteration?
- If you prefer the iterative solution, the complexity of your
algorithm Front) It is possible.
- If you use this method in the real world, you will be fired.
- If you use recursion with the dynamic programming design
method, the complexity of your algorithm is O(log)2ndn)
happens.
- You can even get a promotion for it.
25
Fibonacci Numbers
Fibonacci Numbers
- The definition of Fibonacci numbers is recursive.
- For example, let's try to find the 40th fibonacci value.
F(40)
F(39) F(38)
Fibonacci Numbers
}
28
Towers of Hanoi
- Given: three needles
Tail Recursion
- In the recursion method, this occurs if the recursive call is the last step.
The iteration call is made at the end of the method.
- int recFact(int n)
-{
- if (n<=1) return 1;
- else return n * recFact(n-1);
-}
- void tail() {
- …..
- …..
- tail(); }
32
NonTail Recursion
- In a recursive method, this occurs if the recursive call is not the
last step. Other operations are done after the iteration call
(printing etc) or it could be binary recursion.
- int nontail(int n)
-{
- if (n > 0) {
- …….
- nontail(n-1);
- printf(n);
- …….
- nontail(n-1); }
-}
33
Indirect Recursion
- The iteration call is made from inside another function.
- void A(int n)
-{
- if (n <= 0) return 1;
- n- -;
- B(n);
-}
- void B(int n)
-{
- if (n <= 0) return 1;
- n- -;
- Moment);
-}
34
Nested Recursion
SUMMARY
Homework
- 1- Make the program that finds the standard deviation (?) for n x
values with iteration and recursive.
- xm=(1/n)
-? =standard deviation
-V = variance value
- xm = mean value
37
Homework
A(m,n)=
Hashing
38
39
- This obtained integer is used as the index of the data held in the form
of a string, allowing us to access the data at once.
HASHING
- Hash Functions
- Selecting Digits
- Folding (shift folding, boundary folding)
-
- Mid-Square-Mid-Square
- Extraction
- Radix Transformation- Base Transformation
HASHING
- With the hash function, the searched key element can be accessed
directly. The hash function calculates the index where a key
information is located in the table.
- For a table with N=15, h(x) = x 15% (partition with - > mode).
If,
- x 25 129 35 2501 47 36 10 9 5
h(x) 11 2 6
- The positions of the keys in the table are as follows:
01 2nd 3 4 5 6 7 8 9 10 11 12 13 14
-- 47 - - 35 36 - - 129 25 2501 - --
43
7 Mary 28200
key 8
9
44
Hash functions
- Hash functions return the index order in which a key is found in the table.
- should assign only one key for each position in the table.
45
Hash functions
Hash functions
(Selecting Digits)
- The key value is squared and the middle part of the result is
selected to find the position value in the table.
- Example: Let key = 3121. The hash function finds
the result as follows;
- 31212nd = 9740641
- hash(3121) = 406
- The square of the key can be represented as binary.
- 31212nd = 100101001010000101100001
- hash(3121) = 0101000010 = 322
50
- x= 65
- h(x) =65 mod 15 = 5
- Conflict occurs if more than one record
arrives at the same position.
53
Hash functions
Conflict elimination (Chaining)
- Records arriving at the same position are
Hash functions
Conflict elimination (Chaining)
- Sample:
Hash functions
Conflict elimination (Linear Probing)
- The second record coming to the same
- Adding: It is done by
finding an empty space.
Hash functions
Conflict elimination (Linear Probing)
- Example: h(x) = x mod 13
- Enter the values 18, 41, 22, 44, 59, 32, 31, 73 in the given order.
0 1 2 3 4 5 6 7 8 9 10 11 12
41 18 44 59 32 22 31 735 6 7 8 9 10 11
012 3 4 12
57
Hash functions
Conflict elimination (Linear Probing)
- Advantages / disadvantages of the Linear Probing method
- The time required for delete and search operations increases as the number of
Hash functions
Conflict elimination (Quadratic Probing)
- The second record that comes to the same position is placed
Hash functions
Conflict elimination (Quadratic Probing)
Hash functions
Conflict elimination (Quadratic Probing)
Hash functions
Conflict elimination (Quadratic Probing)
- h(14,2)=(14+22nd)%15=18%15=3
62
Hash functions
Conflict elimination (Quadratic Probing)
- h(99.2)=(9+22nd)%15=13%15=13
63
Hash functions
Conflict elimination (Quadratic Probing)
Hash functions
Conflict elimination (Quadratic Probing)
- Advantages / disadvantages of the Quadratic Probing method
- It distributes the key values more smoothly than the linear probing
method.
- If the table size is not taken into consideration while adding a new element, there is
a risk of working forever.
Hash functions
Conflict removal (Double Hashing)
-A second hash function is used for the second record that comes to the same
position.
Hash functions
Conflict removal (Double Hashing)
- Example: Adding the value 65
- hashone(x)= x 15%
- hash2nd(x)=11 − ( x 11% )
- hash(65) = hashone(65)
- hash(65.0) =5 full
- hash(65.1) = (hashone(5) + 1 * hash2nd(65))15%
- hash(65.1) =(5 +(11-10))%15=6
- hash(65.2) = (hashone(5) + 2 * hash2nd(65))15%
- hash(65.2) =(5 +2)%15=7
67
Hash functions
Conflict removal (Double Hashing)
- Advantages / disadvantages of the Double Hashing method
- It distributes the key values more smoothly than the linear probing
method and groups do not form.
Hash functions
- Performance
to do
number of probing:
1/(1-α)
α:table fill rate
α=n/m
n: element entered
number
m: table size
69
Homework
Chaining java kd
- public class LinkedHashEntry
- { private int key;
- private int value;
- private LinkedHashEntry next;
- LinkedHashEntry (int key, int value)
- { this.key = key;
- this.value = value;
- this.next = null;
- }
- public int getValue() { return value; }
- public void setValue(int value) { this.value = value; }
- public int getKey() { return key; }
- public LinkedHashEntry getNext() { return next; }
- public void setNext(LinkedHashEntry next) { this.next = next; }
71
Chaining java kd
- public class HashMap
- { private static int TABLE_SIZE = 128;
- LinkedHashEntry[] table;
- HashMap()
- { table = new LinkedHashEntry[TABLE_SIZE];
- for (int i = 0; i < TABLE_SIZE; i++) table[i] = null;
- }
- public int get(int key) {
- int hash = (key % TABLE_SIZE); if (table[hash] ==
- null) return -1; else { LinkedHashEntry entry =
- table[hash];
- while (entry != null && entry.getKey() != key) entry = entry.getNext();
- if (entry == null) return -1;
- else return entry.getValue();
- }
- }
-
72
Chaining java kd
Chaining java kd
- public void remove(int key)
- { int hash = (key % TABLE_SIZE);
- if (table[hash] != null) {
- LinkedHashEntry prevEntry = null;
- LinkedHashEntry entry = table[hash];
- while (entry.getNext() != null && entry.getKey() != key)
- { prevEntry = entry; entry = entry.getNext(); }
- if (entry.getKey() == key) {
- if (prevEntry == null) else table[hash] = entry.getNext();
- prevEntry.setNext(entry.getNext());
- }
- }
- }
- }
- }