Skip to content

Commit bd05fdb

Browse files
author
Michael Ho
committed
Completed divide and conquer session 5
1 parent 37b506c commit bd05fdb

File tree

7 files changed

+165
-111
lines changed

7 files changed

+165
-111
lines changed

.idea/workspace.xml

+55-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
12
## Algorithms in Java
23
#### [Introduction to Graduate Algorithms - Udacity](https://classroom.udacity.com/courses/ud401)
34

45
[![Generic badge](https://img.shields.io/badge/jdk-1.8-blue.svg)](https://shields.io/)
56
[![Generic badge](https://img.shields.io/badge/junit-5.4.0-orange.svg)](https://shields.io/)
67
[![Generic badge](https://img.shields.io/badge/junit--platform--commons-1.4.0-green.svg)](https://shields.io/)
78

8-
### Difficulty table
9+
### Difficulty Legends
10+
|Difficulty| Easy | Medium | Hard | Very Hard |
11+
|--|:--:|:--:|:--:|:--:|
12+
| Icon | 🍰 | 🌰 | 🌰🌰 | 🌰🌰🌰 |
13+
14+
15+
### Difficulty Table
916
Dynamic Programming | [Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DynamicProgramming/DP1.java) | [Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DynamicProgramming/DP2.java) | [Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DynamicProgramming/DP3.java) | --- | ---
1017
:------------- | :------------: | :-------------: | :-------------: | :-------------: | :-------------:
1118
Difficulty | 🌰 🌰 | 🌰 🌰 | 🌰 🌰 🌰 | --- | ---
1219
**Randomized Algorithms** | **[Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/RandomizedAlgorithms/RA1.java)** | **[Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/RandomizedAlgorithms/RA2.java)** | **[Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/RandomizedAlgorithms/RA3.java)** | **---** | **---**
1320
Difficulty | 🍰 | 🌰 🌰 | 🌰 | --- | ---
1421
**Divide and Conquer** | **[Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC1.java)** | **[Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC2.java)** | **[Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC3.java)** | **[Lesson 4](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC4.java)** | **[Lesson 5](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC5.java)**
15-
Difficulty | 🍰 | 🌰 | 🍰 | 🌰 🌰 🌰| ---
22+
Difficulty | 🍰 | 🌰 | 🍰 | 🌰 🌰 🌰 | 🌰 🌰 🌰
23+
**Graph Algorithms** | **[Lesson 1](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC1.java)** | **[Lesson 2](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC2.java)** | **[Lesson 3](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC3.java)** | **[Lesson 4](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC4.java)** | **---**
24+
Difficulty | --- | --- | --- | --- | ---
1625

1726
#### [Dynamic Programming](https://docs.google.com/document/d/1vziO8Enan327BmAeR9yAxNgySxRCF01qZlb6c-i627E/edit?usp=sharing)
1827
##### Lesson 1 - [source code](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DynamicProgramming/DP1.java)
@@ -59,6 +68,9 @@ Difficulty | 🍰 | 🌰 | 🍰 | 🌰 🌰 🌰| ---
5968
1. Polynomial Multiplication
6069
1. Fast Fourier Transform - FFT
6170

71+
##### Lesson 5 - [source code](https://github.com/twho/Algorithms-Java/blob/master/src/com/michaelho/DivideAndConquer/DC5.java)
72+
1. Polynomial Multiplication using FFT
73+
6274
### Credits:
6375
- [Udacity Course - Introducation to Graduate Algorithms](https://classroom.udacity.com/courses/ud401)
6476
- [Geeks for Geeks](https://www.geeksforgeeks.org/)

src/com/michaelho/DivideAndConquer/DC4.java

+4-53
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* The DC4 class explores a set of divide and conquer algorithms such as polynomials multiplication using
7-
* naive method and Fast Fourier Transform.
7+
* naive method, and Fast Fourier Transform Algorithm.
88
*
99
* @author Michael Ho
1010
* @since 2015-01-12
@@ -59,61 +59,12 @@ int[] multiplication(int[] A, int[] B) {
5959

6060
class FastFourierTransform {
6161

62-
double[] multiply(double[] p, double[] q, int n, Complex[] omega, Complex[] omegaInv) {
63-
// Generate complex objects with 2 times the size
64-
Complex[] pInteger = new Complex[n];
65-
Complex[] qInteger = new Complex[n];
66-
67-
// Copy the coefficents into the real part of complex number and pad zeros to remaining terms.
68-
for (int i = 0; i < n/2; i++) {
69-
pInteger[i] = new Complex(p[i], 0);
70-
qInteger[i] = new Complex(q[i], 0);
71-
}
72-
73-
for (int i = n/2; i < n; i++) {
74-
pInteger[i] = new Complex(0, 0);
75-
qInteger[i] = new Complex(0, 0);
76-
}
77-
78-
int pow = 1;
79-
80-
// Apply the FFT to the two factors
81-
Complex[] solp = fastFourierTransform(pInteger, omega, n, pow);
82-
Complex[] solq = fastFourierTransform(qInteger, omega, n, pow);
83-
84-
// Multiply the results pointwise recursive
85-
Complex[] finalSol = new Complex[n];
86-
for (int i = 0; i < n; i++)
87-
finalSol[i] = solp[i].multiply(solq[i]);
88-
89-
// Apply the FFT to the pointwise product
90-
Complex[] poly = fastFourierTransform(finalSol, omegaInv, n, pow);
91-
92-
double[] result = new double[n-1];
93-
for (int i=0; i < n-1; i++)
94-
result[i] = poly[i].getReal() / n;
95-
96-
return result;
97-
}
98-
99-
Complex[] getOmega(int n, boolean inverse) {
100-
Complex[] omega = new Complex[n];
101-
return omegaComputation(omega, n, inverse);
102-
}
103-
104-
private Complex[] omegaComputation(Complex[] x, int length, boolean inverse) {
105-
for(int i=0;i<length;i++){
106-
x[i] = new Complex(Math.cos((2*i*Math.PI)/length), (inverse ? -1 : 1)*Math.sin((2*i*Math.PI)/length));
107-
}
108-
return x;
109-
}
110-
11162
/**
11263
* The function that calculates Fast Fourier Transform.
11364
*
11465
* @param a The complex numbers.
11566
* */
116-
private Complex[] fastFourierTransform(Complex[] a, Complex[] omega, int length, int pow) {
67+
Complex[] FFT(Complex[] a, Complex[] omega, int length, int pow) {
11768

11869
if (length == 1)
11970
return a;
@@ -129,8 +80,8 @@ private Complex[] fastFourierTransform(Complex[] a, Complex[] omega, int length,
12980
aOdd[k/2] = a[k];
13081
}
13182

132-
Complex[] solutionEven = fastFourierTransform(aEven, omega, length/2, pow*2);
133-
Complex[] solutionOdd = fastFourierTransform(aOdd, omega, length/2, pow*2);
83+
Complex[] solutionEven = FFT(aEven, omega, length/2, pow*2);
84+
Complex[] solutionOdd = FFT(aOdd, omega, length/2, pow*2);
13485

13586
Complex[] polySol = new Complex[length];
13687
for (int i = 0; i < length; i++)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.michaelho.DivideAndConquer;
2+
3+
import com.michaelho.DataObjects.Complex;
4+
5+
/**
6+
* The DC5 class explores a set of divide and conquer algorithms such as polynomials multiplication using
7+
* Fast Fourier Transform Algorithm.
8+
*
9+
* @author Michael Ho
10+
* @since 2015-01-12
11+
* */
12+
public class DC5 {
13+
DC4 dc4 = new DC4();
14+
15+
/**
16+
* Calculate the multiplication of 2 polynomials.
17+
*
18+
* @param p The coefficients of the first polynomials in double.
19+
* @param q The coefficients of the second polynomials in double.
20+
* @param omega The omega w in the FFT formula.
21+
* @param omegaInv The omega inverse w^-1 in the FFT formula.
22+
*
23+
* @return The result of the multiplication.
24+
* */
25+
double[] multiply(double[] p, double[] q, int n, Complex[] omega, Complex[] omegaInv) {
26+
// Generate complex objects with 2 times the size
27+
Complex[] pInteger = new Complex[n];
28+
Complex[] qInteger = new Complex[n];
29+
30+
// Copy the coefficents into the real part of complex number and pad zeros to remaining terms.
31+
for (int i = 0; i < n/2; i++) {
32+
pInteger[i] = new Complex(p[i], 0);
33+
qInteger[i] = new Complex(q[i], 0);
34+
}
35+
36+
for (int i = n/2; i < n; i++) {
37+
pInteger[i] = new Complex(0, 0);
38+
qInteger[i] = new Complex(0, 0);
39+
}
40+
41+
int pow = 1;
42+
43+
// Apply the FFT to the two factors
44+
Complex[] solp = dc4.fft.FFT(pInteger, omega, n, pow);
45+
Complex[] solq = dc4.fft.FFT(qInteger, omega, n, pow);
46+
47+
// Multiply the results point-wise recursive
48+
Complex[] finalSol = new Complex[n];
49+
for (int i = 0; i < n; i++)
50+
finalSol[i] = solp[i].multiply(solq[i]);
51+
52+
// Apply the FFT to the point-wise product
53+
Complex[] poly = dc4.fft.FFT(finalSol, omegaInv, n, pow);
54+
55+
double[] result = new double[n-1];
56+
for (int i=0; i < n-1; i++)
57+
result[i] = poly[i].getReal() / n;
58+
59+
return result;
60+
}
61+
62+
/**
63+
* Calculate the omega w value.
64+
*
65+
* @param n The length of the coefficient array.
66+
* @param inverse Set true if needs to calculate inverse values.
67+
*
68+
* @return The coefficients of the omega w in the formula.
69+
* */
70+
Complex[] getOmega(int n, boolean inverse) {
71+
Complex[] omega = new Complex[n];
72+
for(int i=0;i<n;i++){
73+
omega[i] = new Complex(Math.cos((2*i*Math.PI)/n), (inverse ? -1 : 1)*Math.sin((2*i*Math.PI)/n));
74+
}
75+
return omega;
76+
}
77+
}

0 commit comments

Comments
 (0)