From 161791d4a5759878f53e4ef2e397502d621a8e81 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Wed, 25 Jul 2018 17:07:34 +0200 Subject: [PATCH 01/14] fixed bug in method gcd(int, int) --- Others/GCD.java | 52 ++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/Others/GCD.java b/Others/GCD.java index 08da3805e9d8..58a2b5eef5aa 100644 --- a/Others/GCD.java +++ b/Others/GCD.java @@ -2,29 +2,37 @@ //This is Euclid's algorithm which is used to find the greatest common denominator //Overide function name gcd -public class GCD{ - - public static int gcd(int num1, int num2) { - - int gcdValue = num1 % num2; - while (gcdValue != 0) { - num2 = gcdValue; - gcdValue = num2 % gcdValue; +public class GCD { + + public static int gcd(int num1, int num2) { + + if (num1 == 0) + return num2; + + while (num2 != 0) { + if (num1 > num2) + num1 -= num2; + else + num2 -= num1; } - return num2; + + return num1; } - public static int gcd(int[] number) { - int result = number[0]; - for(int i = 1; i < number.length; i++) - //call gcd function (input two value) - result = gcd(result, number[i]); - - return result; - } - - public static void main(String[] args) { - int[] myIntArray = {4,16,32}; - //call gcd function (input array) - System.out.println(gcd(myIntArray)); + + public static int gcd(int[] number) { + int result = number[0]; + for (int i = 1; i < number.length; i++) + // call gcd function (input two value) + result = gcd(result, number[i]); + + return result; + } + + public static void main(String[] args) { + int[] myIntArray = { 4, 16, 32 }; + + // call gcd function (input array) + System.out.println(gcd(myIntArray)); // => 4 + System.out.printf("gcd(40,24)=%d gcd(24,40)=%d\n", gcd(40, 24), gcd(24, 40)); // => 8 } } From 871793bed18823a3aa93a8e86a4100c19946895a Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Wed, 25 Jul 2018 09:16:16 -0700 Subject: [PATCH 02/14] Deleted conflict file for hahsmap --- DataStructures/HashMap/HashMap.java | 283 ---------------------------- 1 file changed, 283 deletions(-) delete mode 100644 DataStructures/HashMap/HashMap.java diff --git a/DataStructures/HashMap/HashMap.java b/DataStructures/HashMap/HashMap.java deleted file mode 100644 index 1cce6260e52c..000000000000 --- a/DataStructures/HashMap/HashMap.java +++ /dev/null @@ -1,283 +0,0 @@ -<<<<<<< HEAD:Data Structures/HashMap/HashMap.java - - -import java.util.ArrayList; -import java.util.LinkedList; - -public class HashMap { - public class hmnodes{ //HashMap nodes - K key; - V value; - } - - private int size=0; //size of hashmap - private LinkedList buckets[]; //array of addresses of list - - public HashMap(){ - buckets=new LinkedList[4]; //initially create bucket of any size - for(int i=0;i<4;i++) - buckets[i]=new LinkedList<>(); - } - - public void put(K key,V value) throws Exception{ - int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index - int fountAt=find(bi,key); //check if key already exists or not - if(fountAt==-1){ - hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert - temp.key=key; - temp.value=value; - buckets[bi].addLast(temp); - this.size++; - }else{ - buckets[bi].get(fountAt).value=value;//if already exist modify the value - } - - double lambda = (this.size*1.0)/this.buckets.length; - if(lambda>2.0){ - rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0 - } - - return; - } - - - public V get(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - return buckets[bi].get(fountAt).value; - } - } - - public V remove(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - this.size--; - return buckets[bi].remove(fountAt).value; - } - } - - public boolean containskey(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return false; - }else{ - return true; - } - } - - public int size(){ - return this.size; - } - - - public boolean isempty(){ - return this.size==0; - } - - public ArrayList keyset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i valueset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i"+temp.value+"]"); - } - System.out.println(); - } - } - - public int find(int bi,K key) throws Exception{ - for(int i=0;i ob[]= buckets; - buckets=new LinkedList[ob.length*2]; - for(int i=0;i(); - - size = 0; - for(int i=0;i { - public class hmnodes{ //HashMap nodes - K key; - V value; - } - - private int size=0; //size of hashmap - private LinkedList buckets[]; //array of addresses of list - - public HashMap(){ - buckets=new LinkedList[4]; //initially create bucket of any size - for(int i=0;i<4;i++) - buckets[i]=new LinkedList<>(); - } - - public void put(K key,V value) throws Exception{ - int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index - int fountAt=find(bi,key); //check if key already exists or not - if(fountAt==-1){ - hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert - temp.key=key; - temp.value=value; - buckets[bi].addLast(temp); - this.size++; - }else{ - buckets[bi].get(fountAt).value=value;//if already exist modify the value - } - - double lambda = (this.size*1.0)/this.buckets.length; - if(lambda>2.0){ - rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0 - } - - return; - } - - - public V get(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - return buckets[bi].get(fountAt).value; - } - } - - public V remove(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return null; - }else{ - this.size--; - return buckets[bi].remove(fountAt).value; - } - } - - public boolean containskey(K key) throws Exception{ - int bi=bucketIndex(key); - int fountAt=find(bi,key); - if(fountAt==-1){ - return false; - }else{ - return true; - } - } - - public int size(){ - return this.size; - } - - - public boolean isempty(){ - return this.size==0; - } - - public ArrayList keyset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i valueset() throws Exception{ - ArrayList arr=new ArrayList<>(); - for(int i=0;i"+temp.value+"]"); - } - System.out.println(); - } - } - - public int find(int bi,K key) throws Exception{ - for(int i=0;i ob[]= buckets; - buckets=new LinkedList[ob.length*2]; - for(int i=0;i(); - - size = 0; - for(int i=0;i>>>>>> 7e3a8c55c865471a33f6932a022a1059c5243fc3:data_structures/HashMap/HashMap.java From 69279375d40bcfc315071ee7b97a926ca105ad43 Mon Sep 17 00:00:00 2001 From: Keshav Date: Mon, 30 Jul 2018 11:16:40 +0800 Subject: [PATCH 03/14] Add method for minimum number of coins required for given amount --- Dynamic Programming/CoinChange.java | 39 +++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/Dynamic Programming/CoinChange.java b/Dynamic Programming/CoinChange.java index f4cda7203b7c..e9d3689d9952 100644 --- a/Dynamic Programming/CoinChange.java +++ b/Dynamic Programming/CoinChange.java @@ -10,9 +10,11 @@ public class CoinChange { public static void main(String[] args) { int amount = 12; - int[] coins = {1, 2, 5}; + int[] coins = {2, 4, 5}; System.out.println("Number of combinations of getting change for " + amount + " is: " + change(coins, amount)); + System.out.println("Minimum number of coins required for amount :" + amount + " is: " + minimumCoins(coins, amount)); + } /** @@ -29,7 +31,7 @@ public static int change(int[] coins, int amount) { for (int coin : coins) { for (int i=coin; i Date: Wed, 1 Aug 2018 14:55:26 +0100 Subject: [PATCH 04/14] Remove Tracis CI badge from README.md Removing the Travis CI badge as it points to a dead URL. As this repository has [very few tests](https://github.com/TheAlgorithms/Java/search?q=Test&unscoped_q=Test), maybe a CI build might be a bit too much. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ce2cdec5e76a..2bf743208910 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# The Algorithms - Java [![Build Status](https://travis-ci.org/TheAlgorithms/Java.svg)](https://travis-ci.org/TheAlgorithms/Java) +# The Algorithms - Java ### All algorithms implemented in Java (for education) From 199220db7c291cba01d80f9b6a648575a75e1c48 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Thu, 23 Aug 2018 14:16:56 -0700 Subject: [PATCH 05/14] Deleted irrelevant file --- myfile.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 myfile.txt diff --git a/myfile.txt b/myfile.txt deleted file mode 100644 index d1b1c0a7ad80..000000000000 --- a/myfile.txt +++ /dev/null @@ -1 +0,0 @@ -����~ \ No newline at end of file From 0a373b5c101ee0cad5912ecbdfab389a7b02f2fa Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Thu, 23 Aug 2018 14:20:01 -0700 Subject: [PATCH 06/14] Delete ClosestPair.java --- ClosestPair/ClosestPair.java | 211 ----------------------------------- 1 file changed, 211 deletions(-) delete mode 100644 ClosestPair/ClosestPair.java diff --git a/ClosestPair/ClosestPair.java b/ClosestPair/ClosestPair.java deleted file mode 100644 index 760ef7fd0c5d..000000000000 --- a/ClosestPair/ClosestPair.java +++ /dev/null @@ -1,211 +0,0 @@ -import java.io.*; -import java.util.*; - -public class ClosestPair { - static int count = 0;// array length - static int secondCount = 0;// array length - static Location array[] = new Location[10000]; - static Location point1 = null; // Minimum point coordinate - static Location point2 = null; // Minimum point coordinate - static double minNum = Double.MAX_VALUE;// Minimum point length - - private static class Location { // Location class - double x = 0, y = 0; - - public Location(double x, double y) { //Save x, y coordinates - this.x = x; - this.y = y; - } - } - - public static int xPartition(Location[] a, int first, int last) { // x-axis Quick Sorting - Location pivot = a[last]; // pivot - int pIndex = last; - int i = first - 1; - Location temp; // Temporarily store the value for position transformation - for (int j = first; j <= last - 1; j++) { - if (a[j].x <= pivot.x) { // Less than or less than pivot - i++; - temp = a[i]; // array[i] <-> array[j] - a[i] = a[j]; - a[j] = temp; - } - } - i++; - temp = a[i];// array[pivot] <-> array[i] - a[i] = a[pIndex]; - a[pIndex] = temp; - return i;// pivot index - } - public static int yPartition(Location[] a, int first, int last) { //y-axis Quick Sorting - Location pivot = a[last]; // pivot - int pIndex = last; - int i = first - 1; - Location temp; // Temporarily store the value for position transformation - for (int j = first; j <= last - 1; j++) { - if (a[j].y <= pivot.y) { // Less than or less than pivot - i++; - temp = a[i]; // array[i] <-> array[j] - a[i] = a[j]; - a[j] = temp; - } - } - i++; - temp = a[i];// array[pivot] <-> array[i] - a[i] = a[pIndex]; - a[pIndex] = temp; - return i;// pivot index - } - - public static void xQuickSort(Location[] a, int first, int last) { //x-axis Quick Sorting - if (first < last) { - int q = xPartition(a, first, last); // pivot - xQuickSort(a, first, q - 1); // Left - xQuickSort(a, q + 1, last); // Right - } - } - - public static void yQuickSort(Location[] a, int first, int last) { //y-axis Quick Sorting - if (first < last) { - int q = yPartition(a, first, last); // pivot - yQuickSort(a, first, q - 1); // Left - yQuickSort(a, q + 1, last); // Right - } - } - - public static double closestPair(Location[] a, int indexNum, int first, int last) {// closestPair - Location divideArray[] = new Location[indexNum]; // array stored before divide - System.arraycopy(a, 0, divideArray, 0, indexNum); // Copy from previous array - - int totalNum = indexNum; // number of coordinates in the divideArray array - int divideX = indexNum / 2; // Intermediate value for divide - Location leftArray[] = new Location[divideX]; //divide - left array - Location rightArray[] = new Location[totalNum - divideX]; //divide - right array - - if (indexNum <= 3) { // If the number of coordinates is 3 or less - return bruteForce(divideArray); - } - System.arraycopy(divideArray, 0, leftArray, 0, divideX); //divide - left array - System.arraycopy(divideArray, divideX, rightArray, 0, totalNum - divideX); //divide - right array - - double minLeftArea = 0; //Minimum length of left array - double minRightArea = 0; //Minimum length of right array - double minValue = 0; //Minimum lengt - - minLeftArea = closestPair(leftArray, divideX, 0, divideX - 1); // recursive closestPair - minRightArea = closestPair(rightArray, totalNum - divideX, divideX, totalNum - divideX - 1); - minValue = Math.min(minLeftArea, minRightArea);// window size (= minimum length) - - // Create window - for (int i = 0; i < totalNum; i++) { // Set the size for creating a window and creating a new array for the coordinates in the window - double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); - if (xGap < minValue) { - secondCount++; // size of the array - } else { - if (divideArray[i].x > divideArray[divideX].x) { - break; - } - } - } - Location firstWindow[] = new Location[secondCount]; // new array for coordinates in window - int k = 0; - for (int i = 0; i < totalNum; i++) { - double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); - if (xGap < minValue) { // if it's inside a window - firstWindow[k] = divideArray[i]; // put in an array - k++; - } else { - if (divideArray[i].x > divideArray[divideX].x) { - break; - } - } - } - yQuickSort(firstWindow, 0, secondCount - 1);// Sort by y coordinates - / * Coordinates in Window * / - double length = 0; - for (int i = 0; i < secondCount - 1; i++) { // size comparison within window - for (int j = (i + 1); j < secondCount; j++) { - double xGap = Math.abs(firstWindow[i].x - firstWindow[j].x); - double yGap = Math.abs(firstWindow[i].y - firstWindow[j].y); - if (yGap < minValue) { - length = (double) Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); - if (length < minValue) { // If the measured distance is less than the current minimum distance - minValue = length;// Change minimum distance to current distance - if (length < minNum) { // Conditional statement for registering final coordinate - minNum = length; - point1 = firstWindow[i]; - point2 = firstWindow[j]; - } - } - } - else - break; - } - } - secondCount = 0; - return minValue; - } - - public static double bruteForce(Location[] array) { // When the number of coordinates is less than 3 - double minValue = Double.MAX_VALUE; // minimum distance - double length = 0; - double xGap = 0, yGap = 0; // Difference between x, y coordinates - if (array.length == 2) { // When there are two coordinates - xGap = (array[0].x - array[1].x); // Difference between x coordinates - yGap = (array[0].y - array[1].y); // Difference between y coordinates - length = (double) Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); // distance between coordinates - if (length < minNum) { // Conditional statement for registering final coordinate - minNum = length; - point1 = array[0]; - point2 = array[1]; - } - return length; - } else if (array.length == 3) { // When there are 3 coordinates - for (int i = 0; i < array.length - 1; i++) { - for (int j = (i + 1); j < array.length; j++) { - xGap = (array[i].x - array[j].x); // Difference between x coordinates - yGap = (array[i].y - array[j].y); // Difference between y coordinates - length = (double) Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); // distance between coordinates - if (length < minValue) { // If the measured distance is less than the current minimum distance - minValue = length; // Change minimum distance to current distance - if (length < minNum) { // Conditional statement for registering final coordinate - minNum = length; - point1 = array[i]; - point2 = array[j]; - } - } - } - } - return minValue; - } - return minValue; - } - - public static void main(String[] args) throws IOException { - // TODO Auto-generated method stub - StringTokenizer token; - - BufferedReader in = new BufferedReader(new FileReader("closest_data.txt")); - //Input data consists of one x-coordinate and one y-coordinate - String ch; - - System.out.println("Input data"); - while ((ch = in.readLine()) != null) { - token = new StringTokenizer(ch, " "); - - array[count] = new Location(Double.parseDouble(token.nextToken()), Double.parseDouble(token.nextToken())); // put in an array - count++; // the number of coordinates actually in the array - System.out.println("x: "+array[count - 1].x + ", y: " + array[count - 1].y); - } - - xQuickSort(array, 0, count - 1); // Sorting by x value - - double result; // minimum distance - result = closestPair(array, count, 0, count - 1); // ClosestPair start - System.out.println("Output Data");// minimum distance coordinates and distance output - System.out.println("(" + point1.x + ", " + point1.y + ")"); - System.out.println("(" + point2.x + ", " + point2.y + ")"); - System.out.println("Minimum Distance : " + result); - - } -} From 5eaf920106343f4311b4c0f58063d8f634701372 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Thu, 23 Aug 2018 14:20:09 -0700 Subject: [PATCH 07/14] Delete closest_data.txt --- ClosestPair/closest_data.txt | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 ClosestPair/closest_data.txt diff --git a/ClosestPair/closest_data.txt b/ClosestPair/closest_data.txt deleted file mode 100644 index 8ebef63f4a66..000000000000 --- a/ClosestPair/closest_data.txt +++ /dev/null @@ -1,12 +0,0 @@ -2 3 -2 16 -3 9 -6 3 -7 7 -9 12 -10 11 -15 2 -15 19 -16 11 -17 13 -19 4 From a1cbd6542fdedb968a9b4b1759b29bd3169ce8e5 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Fri, 24 Aug 2018 10:37:35 -0700 Subject: [PATCH 08/14] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2bf743208910..c11c573e3d53 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # The Algorithms - Java +## A [Development](https://github.com/TheAlgorithms/Java/tree/Development) branch is made for this repo where we are trying to migrate the existing project to a Java project structure. You can switch to [Development](https://github.com/TheAlgorithms/Java/tree/Development) branch for contributions. Please refer [this issue](https://github.com/TheAlgorithms/Java/issues/474) for more info. + ### All algorithms implemented in Java (for education) These are for demonstration purposes only. There are many implementations of sorts in the Java standard library that are much better for performance reasons. From 890b9df9e5c96d7872689dcc38282f7f30d59a97 Mon Sep 17 00:00:00 2001 From: JayH2018 <42029911+JayH2018@users.noreply.github.com> Date: Mon, 27 Aug 2018 15:54:52 +0800 Subject: [PATCH 09/14] className is error Bring fileName into correspondence with className which is marked as public --- DataStructures/Trees/GenericTree.Java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataStructures/Trees/GenericTree.Java b/DataStructures/Trees/GenericTree.Java index 16ab5fb53b1e..cc592e04082e 100644 --- a/DataStructures/Trees/GenericTree.Java +++ b/DataStructures/Trees/GenericTree.Java @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; -public class treeclass { +public class GenericTree { private class Node { int data; ArrayList child = new ArrayList<>(); @@ -22,7 +22,7 @@ public class treeclass { I have done this, while calling from main one have to give minimum parameters. */ - public treeclass() { //Constructor + public GenericTree() { //Constructor Scanner scn = new Scanner(System.in); root = create_treeG(null, 0, scn); } From 28deddd5d78064274b76df56126c9a72163ad361 Mon Sep 17 00:00:00 2001 From: JayH2018 <42029911+JayH2018@users.noreply.github.com> Date: Tue, 28 Aug 2018 14:36:56 +0800 Subject: [PATCH 10/14] source coding is a little complex --- DataStructures/Trees/LevelOrderTraversal.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/DataStructures/Trees/LevelOrderTraversal.java b/DataStructures/Trees/LevelOrderTraversal.java index 8cb304f18c8f..e20753638212 100644 --- a/DataStructures/Trees/LevelOrderTraversal.java +++ b/DataStructures/Trees/LevelOrderTraversal.java @@ -37,14 +37,10 @@ int height(Node root) return 0; else { - /* compute height of each subtree */ - int lheight = height(root.left); - int rheight = height(root.right); - - /* use the larger one */ - if (lheight > rheight) - return(lheight+1); - else return(rheight+1); + /** + * return the larger one; + */ + return Math.max(height(root.left),height(root.right)) + 1; } } @@ -75,4 +71,4 @@ public static void main(String args[]) System.out.println("Level order traversal of binary tree is "); tree.printLevelOrder(); } -} \ No newline at end of file +} From 2dc86b6a04eaa57dfbea7bac3e80b5d9995b4545 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Tue, 28 Aug 2018 06:25:55 -0700 Subject: [PATCH 11/14] Update LevelOrderTraversal.java --- DataStructures/Trees/LevelOrderTraversal.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStructures/Trees/LevelOrderTraversal.java b/DataStructures/Trees/LevelOrderTraversal.java index e20753638212..1f657d92be97 100644 --- a/DataStructures/Trees/LevelOrderTraversal.java +++ b/DataStructures/Trees/LevelOrderTraversal.java @@ -38,7 +38,7 @@ int height(Node root) else { /** - * return the larger one; + * Return the height of larger subtree */ return Math.max(height(root.left),height(root.right)) + 1; } From f713a5ff9c5fbf2a2ee2b08c9de75a43b800ada7 Mon Sep 17 00:00:00 2001 From: DDullahan Date: Thu, 30 Aug 2018 16:47:17 +0800 Subject: [PATCH 12/14] Added MatrixFastPower.java --- DataStructures/Matrix/MatrixFastPower.java | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 DataStructures/Matrix/MatrixFastPower.java diff --git a/DataStructures/Matrix/MatrixFastPower.java b/DataStructures/Matrix/MatrixFastPower.java new file mode 100644 index 000000000000..c2bdcf7cb47d --- /dev/null +++ b/DataStructures/Matrix/MatrixFastPower.java @@ -0,0 +1,139 @@ +import java.util.*; + +/** + * + * Java implementation of Matrix fast power + * It can calculate the high power of constant Matrix with O( log(K) ) + * where K is the power of the Matrix + * + * In order to do that, Matrix must be square Matrix ( columns equals rows) + * + * Notice : large power of Matrix may cause overflow + * + * + * other Matrix basic operator is based on @author Kyler Smith, 2017 + * + * @author DDullahan, 2018 + * + */ + +class Matrix { + private int[][] data; + + /** + * Constructor for the matrix takes in a 2D array + * + * @param pData + */ + public Matrix(int[][] pData) { + + /** Make a deep copy of the data */ + if(pData.length != 0) { + int[][] newData = new int[pData.length][pData[0].length]; + + for(int i = 0; i < pData.length; i++) + for(int j = 0; j < pData[0].length; j++) + newData[i][j] = pData[i][j]; + + this.data = newData; + } else { + this.data = null; + } + } + + /** + * Returns the element specified by the given location + * + * @param x : x cooridinate + * @param y : y cooridinate + * @return int : value at location + */ + public int getElement(int x, int y) { + return data[x][y]; + } + + /** + * Returns the number of rows in the Matrix + * + * @return rows + */ + public int getRows() { + if(this.data == null) + return 0; + + return data.length; + } + + /** + * Returns the number of rows in the Matrix + * + * @return columns + */ + public int getColumns() { + if(this.data == null) + return 0; + + return data[0].length; + } + + /** + * Multiplies this matrix with another matrix. + * + * @param other : Matrix to be multiplied with + * @return product + */ + public Matrix multiply(Matrix other) throws RuntimeException { + + int[][] newData = new int[this.data.length][other.getColumns()]; + + if(this.getColumns() != other.getRows()) + throw new RuntimeException("The two matrices cannot be multiplied."); + + int sum; + + for (int i = 0; i < this.getRows(); ++i) + for(int j = 0; j < other.getColumns(); ++j) { + sum = 0; + + for(int k = 0; k < this.getColumns(); ++k) { + sum += this.data[i][k] * other.getElement(k, j); + } + + newData[i][j] = sum; + } + + return new Matrix(newData); + } + + /** + * Matrix Fast Power + * + * @param k : power of Matrix + * @return product + */ + public Matrix MatrixFastPower(int k) throws RuntimeException { + + if(this.getColumns() != this.getRows()) + throw new RuntimeException("Matrix is not square Matrix."); + + int[][] newData = new int[this.getColumns()][this.getRows()]; + + for(int i = 0; i < this.getColumns(); i++) + newData[i][i] = 1; + + Matrix newMatrix = new Matrix(newData), + coMatrix = new Matrix(this.data); + + while(k != 0) { + + if((k & 1) != 0) + newMatrix = newMatrix.multiply(coMatrix); + + k >>= 1; + coMatrix = coMatrix.multiply(coMatrix); + + } + + return newMatrix; + } +} From 55c179bf19ddcb1c3fcf5e1d6ec56ff6a1788309 Mon Sep 17 00:00:00 2001 From: DDullahan Date: Thu, 30 Aug 2018 21:52:20 +0800 Subject: [PATCH 13/14] Added MatrixFastPower.java with changes --- DataStructures/Matrix/MatrixFastPower.java | 112 +++++++++++++++------ 1 file changed, 82 insertions(+), 30 deletions(-) diff --git a/DataStructures/Matrix/MatrixFastPower.java b/DataStructures/Matrix/MatrixFastPower.java index c2bdcf7cb47d..19f8528a36ec 100644 --- a/DataStructures/Matrix/MatrixFastPower.java +++ b/DataStructures/Matrix/MatrixFastPower.java @@ -1,5 +1,3 @@ -import java.util.*; - /** * * Java implementation of Matrix fast power @@ -14,11 +12,67 @@ * other Matrix basic operator is based on @author Kyler Smith, 2017 * * @author DDullahan, 2018 - * + * */ +class MatrixFastPower { + + /** + * Matrix Fast Power + * + * @param matrix : square Matrix + * @param k : power of Matrix + * @return product + */ + public static Matrix FastPower(Matrix matrix, int k) throws RuntimeException { + + if(matrix.getColumns() != matrix.getRows()) + throw new RuntimeException("Matrix is not square Matrix."); + + int[][] newData = new int[matrix.getColumns()][matrix.getRows()]; + + for(int i = 0; i < matrix.getColumns(); i++) + newData[i][i] = 1; + + Matrix newMatrix = new Matrix(newData), + coMatrix = new Matrix(matrix.data); + + while(k != 0) { + + if((k & 1) != 0) + newMatrix = newMatrix.multiply(coMatrix); + + k >>= 1; + coMatrix = coMatrix.multiply(coMatrix); + + } + + return newMatrix; + } + + public static void main(String[] argv) { + + int[][] data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + Matrix matrix = new Matrix(data); + + System.out.println("original matrix : "); + System.out.println(matrix.toString()); + + matrix = MatrixFastPower.FastPower(matrix, 5); + + System.out.println("after power : "); + System.out.println(matrix.toString()); + + matrix = MatrixFastPower.FastPower(matrix, 1000000); + + System.out.println("notice, large power may cause overflow : "); + System.out.print(matrix.toString()); + System.out.println("you can use mod to fix that :-) "); + + } +} class Matrix { - private int[][] data; + public int[][] data; /** * Constructor for the matrix takes in a 2D array @@ -106,34 +160,32 @@ public Matrix multiply(Matrix other) throws RuntimeException { } /** - * Matrix Fast Power - * - * @param k : power of Matrix - * @return product - */ - public Matrix MatrixFastPower(int k) throws RuntimeException { - - if(this.getColumns() != this.getRows()) - throw new RuntimeException("Matrix is not square Matrix."); - - int[][] newData = new int[this.getColumns()][this.getRows()]; - - for(int i = 0; i < this.getColumns(); i++) - newData[i][i] = 1; - - Matrix newMatrix = new Matrix(newData), - coMatrix = new Matrix(this.data); - - while(k != 0) { - - if((k & 1) != 0) - newMatrix = newMatrix.multiply(coMatrix); - - k >>= 1; - coMatrix = coMatrix.multiply(coMatrix); + * Returns the Matrix as a String in the following format + * + * [ a b c ] ... + * [ x y z ] ... + * [ i j k ] ... + * ... + * + * @return Matrix as String + * TODO: Work formatting for different digit sizes + */ + public String toString() { + String str = ""; + + for(int i = 0; i < this.data.length; i++) { + str += "[ "; + + for(int j = 0; j < this.data[0].length; j++) { + str += data[i][j]; + str += " "; + } + str += "]"; + str += "\n"; } - return newMatrix; + return str; } + } From 88f815545832d68ba443a10f3cc0dd8b69f3334f Mon Sep 17 00:00:00 2001 From: Marisa Afuera Date: Thu, 30 Aug 2018 19:36:00 +0200 Subject: [PATCH 14/14] Refactorized ClosestPair.java in order to be compliant with java sun rules commit divideconquer\ClosesPair.java Refactorized ClosestPair.java. Finding nearest cartesian points. Refactorized ClosestPair.java. Finding nearest cartesian points. --- divideconquer/ClosestPair.java | 348 +++++++++++++++++++++++++++++++++ 1 file changed, 348 insertions(+) create mode 100644 divideconquer/ClosestPair.java diff --git a/divideconquer/ClosestPair.java b/divideconquer/ClosestPair.java new file mode 100644 index 000000000000..93a5d164dd88 --- /dev/null +++ b/divideconquer/ClosestPair.java @@ -0,0 +1,348 @@ +package divideconquer; + +/** + +* For a set of points in a coordinates system (10000 maximum), +* ClosestPair class calculates the two closest points. + +* @author: anonymous +* @author: Marisa Afuera +*/ + + public final class ClosestPair { + + + /** Number of points */ + int numberPoints = 0; + /** Input data, maximum 10000. */ + private Location[] array; + /** Minimum point coordinate. */ + Location point1 = null; + /** Minimum point coordinate. */ + Location point2 = null; + /** Minimum point length. */ + private static double minNum = Double.MAX_VALUE; + /** secondCount */ + private static int secondCount = 0; + + /** + * Constructor. + */ + ClosestPair(int points) { + numberPoints = points; + array = new Location[numberPoints]; + } + + /** + Location class is an auxiliary type to keep points coordinates. + */ + + public static class Location { + + double x = 0; + double y = 0; + + /** + * @param xpar (IN Parameter) x coordinate
+ * @param ypar (IN Parameter) y coordinate
+ */ + + Location(final double xpar, final double ypar) { //Save x, y coordinates + this.x = xpar; + this.y = ypar; + } + + } + + public Location[] createLocation(int numberValues) { + return new Location[numberValues]; + + } + + public Location buildLocation(double x, double y){ + return new Location(x,y); + } + + + /** xPartition function: arrange x-axis. + * @param a (IN Parameter) array of points
+ * @param first (IN Parameter) first point
+ * @param last (IN Parameter) last point
+ * @return pivot index + */ + + public int xPartition( + final Location[] a, final int first, final int last) { + + Location pivot = a[last]; // pivot + int pIndex = last; + int i = first - 1; + Location temp; // Temporarily store value for position transformation + for (int j = first; j <= last - 1; j++) { + if (a[j].x <= pivot.x) { // Less than or less than pivot + i++; + temp = a[i]; // array[i] <-> array[j] + a[i] = a[j]; + a[j] = temp; + } + } + i++; + temp = a[i]; // array[pivot] <-> array[i] + a[i] = a[pIndex]; + a[pIndex] = temp; + return i; // pivot index + } + + /** yPartition function: arrange y-axis. + * @param a (IN Parameter) array of points
+ * @param first (IN Parameter) first point
+ * @param last (IN Parameter) last point
+ * @return pivot index + */ + + public int yPartition( + final Location[] a, final int first, final int last) { + + Location pivot = a[last]; // pivot + int pIndex = last; + int i = first - 1; + Location temp; // Temporarily store value for position transformation + for (int j = first; j <= last - 1; j++) { + if (a[j].y <= pivot.y) { // Less than or less than pivot + i++; + temp = a[i]; // array[i] <-> array[j] + a[i] = a[j]; + a[j] = temp; + } + } + i++; + temp = a[i]; // array[pivot] <-> array[i] + a[i] = a[pIndex]; + a[pIndex] = temp; + return i; // pivot index + } + + /** xQuickSort function: //x-axis Quick Sorting. + * @param a (IN Parameter) array of points
+ * @param first (IN Parameter) first point
+ * @param last (IN Parameter) last point
+ */ + + public void xQuickSort( + final Location[] a, final int first, final int last) { + + if (first < last) { + int q = xPartition(a, first, last); // pivot + xQuickSort(a, first, q - 1); // Left + xQuickSort(a, q + 1, last); // Right + } + } + + /** yQuickSort function: //y-axis Quick Sorting. + * @param a (IN Parameter) array of points
+ * @param first (IN Parameter) first point
+ * @param last (IN Parameter) last point
+ */ + + public void yQuickSort( + final Location[] a, final int first, final int last) { + + if (first < last) { + int q = yPartition(a, first, last); // pivot + yQuickSort(a, first, q - 1); // Left + yQuickSort(a, q + 1, last); // Right + } + } + + /** closestPair function: find closest pair. + * @param a (IN Parameter) array stored before divide
+ * @param indexNum (IN Parameter) number coordinates divideArray
+ * @return minimum distance
+ */ + + public double closestPair(final Location[] a, final int indexNum) { + + Location[] divideArray = new Location[indexNum]; + System.arraycopy(a, 0, divideArray, 0, indexNum); // Copy previous array + int totalNum = indexNum; // number of coordinates in the divideArray + int divideX = indexNum / 2; // Intermediate value for divide + Location[] leftArray = new Location[divideX]; //divide - left array + //divide-right array + Location[] rightArray = new Location[totalNum - divideX]; + if (indexNum <= 3) { // If the number of coordinates is 3 or less + return bruteForce(divideArray); + } + //divide-left array + System.arraycopy(divideArray, 0, leftArray, 0, divideX); + //divide-right array + System.arraycopy( + divideArray, divideX, rightArray, 0, totalNum - divideX); + + double minLeftArea = 0; //Minimum length of left array + double minRightArea = 0; //Minimum length of right array + double minValue = 0; //Minimum lengt + + minLeftArea = closestPair(leftArray, divideX); // recursive closestPair + minRightArea = closestPair(rightArray, totalNum - divideX); + // window size (= minimum length) + minValue = Math.min(minLeftArea, minRightArea); + + // Create window. Set the size for creating a window + // and creating a new array for the coordinates in the window + for (int i = 0; i < totalNum; i++) { + double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); + if (xGap < minValue) { + secondCount++; // size of the array + } else { + if (divideArray[i].x > divideArray[divideX].x) { + break; + } + } + } + // new array for coordinates in window + Location[] firstWindow = new Location[secondCount]; + int k = 0; + for (int i = 0; i < totalNum; i++) { + double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); + if (xGap < minValue) { // if it's inside a window + firstWindow[k] = divideArray[i]; // put in an array + k++; + } else { + if (divideArray[i].x > divideArray[divideX].x) { + break; + } + } + } + yQuickSort(firstWindow, 0, secondCount - 1); // Sort by y coordinates + /* Coordinates in Window */ + double length = 0; + // size comparison within window + for (int i = 0; i < secondCount - 1; i++) { + for (int j = (i + 1); j < secondCount; j++) { + double xGap = Math.abs(firstWindow[i].x - firstWindow[j].x); + double yGap = Math.abs(firstWindow[i].y - firstWindow[j].y); + if (yGap < minValue) { + length = Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); + // If measured distance is less than current min distance + if (length < minValue) { + // Change minimum distance to current distance + minValue = length; + // Conditional for registering final coordinate + if (length < minNum) { + minNum = length; + point1 = firstWindow[i]; + point2 = firstWindow[j]; + } + } + } + else { + break; + } + } + } + secondCount = 0; + return minValue; + } + + /** bruteForce function: When the number of coordinates is less than 3. + * @param arrayParam (IN Parameter) array stored before divide
+ * @return
+ */ + + public double bruteForce(final Location[] arrayParam) { + + double minValue = Double.MAX_VALUE; // minimum distance + double length = 0; + double xGap = 0; // Difference between x coordinates + double yGap = 0; // Difference between y coordinates + double result = 0; + + if (arrayParam.length == 2) { + // Difference between x coordinates + xGap = (arrayParam[0].x - arrayParam[1].x); + // Difference between y coordinates + yGap = (arrayParam[0].y - arrayParam[1].y); + // distance between coordinates + length = Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); + // Conditional statement for registering final coordinate + if (length < minNum) { + minNum = length; + + } + point1 = arrayParam[0]; + point2 = arrayParam[1]; + result = length; + } + if (arrayParam.length == 3) { + for (int i = 0; i < arrayParam.length - 1; i++) { + for (int j = (i + 1); j < arrayParam.length; j++) { + // Difference between x coordinates + xGap = (arrayParam[i].x - arrayParam[j].x); + // Difference between y coordinates + yGap = (arrayParam[i].y - arrayParam[j].y); + // distance between coordinates + length = + Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); + // If measured distance is less than current min distance + if (length < minValue) { + // Change minimum distance to current distance + minValue = length; + if (length < minNum) { + // Registering final coordinate + minNum = length; + point1 = arrayParam[i]; + point2 = arrayParam[j]; + } + } + } + } + result = minValue; + + } + return result; // If only one point returns 0. + } + + /** main function: execute class. + * @param args (IN Parameter)
+ * @throws IOException If an input or output + * exception occurred + */ + + public static void main(final String[] args) { + + //Input data consists of one x-coordinate and one y-coordinate + + ClosestPair cp = new ClosestPair(12); + cp.array[0]=cp.buildLocation(2,3); + cp.array[1]=cp.buildLocation(2,16); + cp.array[2]=cp.buildLocation(3,9); + cp.array[3]=cp.buildLocation(6,3); + cp.array[4]=cp.buildLocation(7,7); + cp.array[5]=cp.buildLocation(19,4); + cp.array[6]=cp.buildLocation(10,11); + cp.array[7]=cp.buildLocation(15,2); + cp.array[8]=cp.buildLocation(15,19); + cp.array[9]=cp.buildLocation(16,11); + cp.array[10]=cp.buildLocation(17,13); + cp.array[11]=cp.buildLocation(9,12); + + System.out.println("Input data"); + System.out.println("Number of points: "+ cp.array.length); + for (int i=0;i