multipath two-phase natural balanced merge - java

I have a problem. I need implement multipath two-phase natural balanced merge using java. I saw merger sort example here http://www.vogella.de/articles/JavaAlgorithmsMergesort/article.html
I understand how it works, but I do not know how implement two-phase merge. One function divide array for indices and the second is to arrange a merger. Also it should be implemented for array and files (instead array used files). Who knows how to do it ?

public class Mergerer {
private int countWay = 0;
private int countIndex = 0;
private Double[] base;
private Double[] sortBase;
private Double[] tempBase;
private Double[][] arrayWay;
private String[] array1;
private String[] array2;
private String[] array3;
private String[] array4;
private String[] array5;
private String[] array6;
List<String[]> listA = new ArrayList<String[]>();
private int countElements = 0;
public Double[] getSortBase() {
return sortBase;
}
public Double[] sort() {
divide();
merger(base);
return sortBase;
}
public int getCountWay() {
return countWay;
}
public Mergerer(Double[] base, int countWay) {
this.countWay = countWay;
this.arrayWay = new Double[countWay][base.length + 1];
tempBase = base;
countElements = base.length;
array1 = new String[base.length * 2];
array2 = new String[base.length * 2];
array3 = new String[base.length * 2];
array4 = new String[base.length * 2];
array5 = new String[base.length * 2];
array6 = new String[base.length * 2];
listA.add(array1);
listA.add(array2);
listA.add(array3);
listA.add(array4);
listA.add(array5);
listA.add(array6);
}
public int divide() {
int[][] posWay = new int[countWay][1];
int beginIndexPos = 0;
int numberWay = 0;
int pos = 0;
for (int i = 1; i < tempBase.length; i++) {
if (tempBase[i] < tempBase[i - 1]) {
for (int j = beginIndexPos; j < i; j++) {
listA.get(numberWay)[posWay[numberWay][0]] = Double.toString(tempBase[j]);
arrayWay[numberWay][posWay[numberWay][0]] = tempBase[j];
posWay[numberWay][0]++;
countIndex++;
}
listA.get(numberWay)[posWay[numberWay][0]] = "'";
posWay[numberWay][0]++;
beginIndexPos = i;
if (numberWay == countWay - 1)
numberWay = 0;
else
numberWay++;
pos = 0;
}
}
for (int i = beginIndexPos; i < tempBase.length; i++) {
listA.get(numberWay)[posWay[numberWay][0]] = Double.toString(tempBase[i]);
arrayWay[numberWay][posWay[numberWay][0]] = tempBase[i];
posWay[numberWay][0]++;
countIndex++;
}
listA.get(numberWay)[posWay[numberWay][0]] = "'";
return countIndex;
}
public void printIndex() {
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (arrayWay[i][j] != null)
System.out.print(arrayWay[i][j]);
}
System.out.println();
}
}
public void printIndexList() {
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (listA.get(i)[j] != null)
System.out.print(listA.get(i)[j]);
}
System.out.println();
}
}
public void merger(Double[] base) {
List<String[]> listB = new ArrayList<String[]>();
for (int i = 0; i < countWay; i++) {
listB.add(new String[base.length * 2]);
}
int numberWay = 0;
int[][] posWay = new int[countWay][1];
double lastAdded = 0;
int countSeries = 0;
int[] mem = new int[countWay];
for (int i = 0; i < countWay; i++) {
mem[i] = 0;
}
while (true) {
double tmp = 999999999;
int way = -1;
for (int i = 0; i < countWay; i++) {
if (listA.get(i)[mem[i]] != null) {
String str = listA.get(i)[mem[i]];
if (str.equals("'") && listA.get(i)[mem[i] + 1] != null) {
mem[i]++;
str = listA.get(i)[mem[i]];
}
if (str != null && !str.equals("'")) {
double step = Double.parseDouble(str);
if (tmp >= step) {
tmp = Double.parseDouble(str);
way = i;
}
}
}
}
if (lastAdded > tmp || way == -1) {
listB.get(numberWay)[posWay[numberWay][0]] = "'";
countSeries++;
if (numberWay == countWay - 1) {
numberWay = 0;
} else
numberWay++;
}
if (way == -1) {
break;
}
mem[way]++;
lastAdded = tmp;
listB.get(numberWay)[posWay[numberWay][0]] = Double.toString(tmp);
posWay[numberWay][0]++;
}
listA = listB;
if (countSeries > 1) {
merger(base);
} else {
sortBase = new Double[countElements];
for (int i = 0; i < countWay; i++) {
for (int j = 0; j < countElements; j++) {
if (listA.get(i)[j] != null)
sortBase[j] = Double.parseDouble(listA.get(i)[j]);
}
}
return;
}
}
Double[] array = new Double[10];
for (int i=10; i>0; i--) {
array[10-i] = i;
}
Mergerer merg = new Mergerer(array, 4);
merg.divide();
merg.merger(array);

Related

Why am I getting 100% accuracy in my Java MLP program?

I am writing a code in java that uses an MLP algorithm on a digit recognition dataset. I have 2 excel files. One for training and one for testing and they both contain 65 columns and 2810 rows. Below is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
public class Main {
static int hiddenNeurons = 64; // Number of hidden neurons
static int[][] WH = new int[hiddenNeurons][64]; // Weight between the input and the hidden neurons
static int[][] WO = new int[10][hiddenNeurons]; // Weight between the hidden neurons and the output
static double[] outputHidden = new double[hiddenNeurons];
static double[] outputNeuron = new double[10];
static int[] dataSample = new int[64];
static int[] mapTarget = new int[10];
static int[] errorOutput = new int[10];
static int[] errorHidden = new int[hiddenNeurons];
static void initializeWeights(){
Random randomNumber = new Random();
for (int i = 0; i < WH.length; i++) {
for (int j = 0; j < WH[i].length; j++) {
WH[i][j] = randomNumber.nextInt(10);
}
}
for (int i = 0; i < WO.length; i++) {
for (int j = 0; j < WO[i].length; j++) {
WO[i][j] = randomNumber.nextInt(10);
}
}
}
public static void feedForward(){
double sigmoid = 0;
double sumWH = 0;
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
sumWH += dataSample[j] * WH[i][j];
}
sigmoid = 1 / (1 + Math.exp(-sumWH));
outputHidden[i] = sigmoid;
}
for (int i = 0; i < outputNeuron.length; i++) {
double sumWO = 0;
for (int j = 0; j < dataSample.length; j++) {
sumWO += outputHidden[j] * WO[i][j];
}
outputNeuron[i] = sumWO >= 0 ? 1 : 0;
}
}
static boolean testError() {
for (int i = 0; i < outputNeuron.length; i++) {
if (mapTarget[i] != outputNeuron[i]) {
return false;
}
}
return true;
}
static void training(){
double learningRate = 0.1;
for (int i = 0; i < outputNeuron.length; i++) {
errorOutput[i] = (int) (mapTarget[i] - outputNeuron[i]);
}
for (int j = 0; j < hiddenNeurons; j++) {
double errorTemp = 0;
for (int i = 0; i < outputNeuron.length; i++) {
errorTemp += errorOutput[i] * WO[i][j]; //try [j][i]
}
errorHidden[j] = (int) (outputHidden[j] * (1-outputHidden[j]) * errorTemp);
}
for (int i = 0; i < outputNeuron.length; i++) {
for (int j = 0; j < hiddenNeurons; j++) {
WO[i][j] = (int) (WO[i][j] + learningRate * outputHidden[j] * errorOutput[i]);
}
}
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
WH[i][j] = (int) (WH[i][j] + learningRate * dataSample[j] * errorHidden[i]);
}
}
}
static void testing(){
double success = 0;
try(BufferedReader br = new BufferedReader(new FileReader("test.csv"))) {
String line;
int numOfRows = 0;
boolean errorTested = false;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
numOfRows++;
mapTarget[targetOutput] = 1;
feedForward();
errorTested = testError();
if(!errorTested){
success++;
}
}
double accuracy = success / numOfRows;
System.out.println("testing dataset accuracy: " + accuracy * 100);
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
double success = 0;
initializeWeights();
try (BufferedReader br = new BufferedReader(new FileReader("train.csv"))) {
String line;
int numOfRows = 0;
double accuracy = 0;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
mapTarget[targetOutput] = 1;
feedForward();
if (testError()) {
training();
} else {
success++;
}
numOfRows++;
accuracy = success / numOfRows;
}
System.out.println("training dataset: " + accuracy*100);
testing();
}
catch (Exception e){
e.printStackTrace();
}
}
}
This code, when run, gives me this output:
training dataset: 100.0
testing dataset accuracy: 100.0
What is the issue that seems to show me 100% accuracy even though that isn't possible.

In java, I just use a variable of 'A' to replace 'A' in one line , but get a different answer

When I'm implementing the SOR approach with java, I find this problem:
when I do this:
Matrix mm = ((D.sub(L)).inv());
B = mm.dot(b);
The value of B is right. However, when I use:
B = ((D.sub(L)).inv()).dot(b);
I get a quite different value of B which is wrong.
I fell this may be a reference problem, but I can't get the exact answer.
This is the SOR method:
public static Matrix[] SOR(Matrix m, Matrix b, double omiga) {
Matrix D = getD(m);
Matrix L = getL(m);
Matrix U = getU(m);
Matrix M = null;
Matrix B = null;
try {
Matrix mm = ((D.sub(L.pro(omiga))).inv());
M = mm.dot((D.pro(1 - omiga)).add(U.pro(omiga)));
// This is right:
B = (mm.dot(b)).pro(omiga);
// This is wrong:
// B = (((D.sub(L.pro(omiga))).inv()).dot(b)).pro(omiga);
} catch (MatrixProcessException e) {
System.out.println("3 Matrix process problem!");
}
Matrix[] matrices = {M, B};
return matrices;
}
And this is the Matrix class created by me (methods in it have all been checked and have no problem):
public class Matrix {
private int row;
private int col;
private int rowNum;
private int colNum;
private double[][] m;
public Matrix(int rowNum, int colNum) {
this.rowNum = rowNum;
this.colNum = colNum;
m = new double[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
m[i][j] = 0;
}
}
}
public Matrix(double[][] m) {
rowNum = m.length;
colNum = m[0].length;
this.m = m;
}
public Matrix(double[] v) {
rowNum = v.length;
colNum = 1;
double[][] m = new double[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
m[i][0] = v[i];
}
this.m = m;
}
public Matrix pro(double lambda) {
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
m[i][j] *= lambda;
}
}
return this;
}
public Matrix add(Matrix mm) throws MatrixProcessException {
if (mm.getRowNum() != rowNum || mm.getColNum() != colNum) {
throw new MatrixProcessException();
}
double[][] m1 = m;
double[][] m2 = mm.getM();
double[][] addM = new double[m.length][m[0].length];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
addM[i][j] = m1[i][j] + m2[i][j];
}
}
return new Matrix(addM);
}
public Matrix sub(Matrix mm) throws MatrixProcessException {
if (mm.getRowNum() != rowNum || mm.getColNum() != colNum) {
throw new MatrixProcessException();
}
return this.add(mm.pro(-1));
}
}
public double[][] getM() {
return m;
}
public void setM(double[][] m) {
this.m = m;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public int getColNum() {
return colNum;
}
public void setColNum(int colNum) {
this.colNum = colNum;
}
public Matrix dot(Matrix M) throws MatrixProcessException {
double[][] m1 = m;
double[][] m2 = M.getM();
if (this.colNum != M.rowNum) {
System.out.println("sss");
throw new MatrixProcessException();
}
double[][] ansMatrix = new double[this.rowNum][M.colNum];
for (int i = 0; i < this.rowNum; i++) {
for (int j = 0; j < M.colNum; j++) {
double num = 0;
for (int k = 0; k < this.colNum; k++) {
num += m1[i][k] * m2[k][j];
}
ansMatrix[i][j] = num;
}
}
return new Matrix(ansMatrix);
}
public Matrix inv() throws MatrixProcessException {
if (rowNum != colNum) {
throw new MatrixProcessException();
}
Matrix mm = new Matrix(rowNum, 2 * colNum);
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < 2 * colNum; j++) {
if (j < rowNum) {
mm.set(i, j, m[i][j]);
}
else {
if (j - rowNum == i) {
mm.set(i, j, 1);
}
}
}
}
Collections.calculateUpperTriangle(mm);
Collections.calculateSimplest(mm);
for (int i = 0; i < rowNum; i++) {
mm.proR(i, 1.0 / mm.get(i, i));
}
Matrix M = new Matrix(rowNum, colNum);
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < rowNum; j++) {
M.set(i, j, mm.get(i, j + rowNum));
}
}
return M;
}
public void showMatrix() {
System.out.println("\nMatrix:");
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public double[] toVector() {
double[] v = new double[rowNum];
if (colNum == 1) {
for (int i = 0; i < rowNum; i++) {
v[i] = m[i][0];
}
}
else {
System.out.println("This matrix is not a vector");
}
return v;
}
}
May anyone help me to get the reason of the problem?
Thx!

Finding minimum weight and number of edges traversed in a graph

I have to find the minimum distance between two vertices and no. of edges traversed in finding that minimum distance in a adirected weighted graph. I did write a code on using Dijkstras algo. But my test cases are failing. What am i missing here.?
Ideal input: given any two vertices A,B
Output: min distance between two nodes, no. of edges traversed
If the node is unreachable then output should be -1, -1.
//This is a java program to find the shortest path between source vertex and destination vertex
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class Dijkstras_Shortest_Path
{
private int distances[];
private int Numnodes[];
private int numnodes;
private Set<Integer> settled;
private Set<Integer> unsettled;
private int number_of_nodes;
private int adjacencyMatrix[][];
public Dijkstras_Shortest_Path(int number_of_nodes)
{
this.number_of_nodes = number_of_nodes;
distances = new int[number_of_nodes + 1];
Numnodes=new int[number_of_nodes+1];
settled = new HashSet<Integer>();
unsettled = new HashSet<Integer>();
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
}
public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
int evaluationNode;
for (int i = 1; i <= number_of_nodes; i++)
for (int j = 1; j <= number_of_nodes; j++)
adjacencyMatrix[i][j] = adjacency_matrix[i][j];
for (int i = 1; i <= number_of_nodes; i++)
{
distances[i] = Integer.MAX_VALUE;
}
unsettled.add(source);
distances[source] = 0;
while (!unsettled.isEmpty())
{
evaluationNode = getNodeWithMinimumDistanceFromUnsettled();
unsettled.remove(evaluationNode);
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
}
}
private int getNodeWithMinimumDistanceFromUnsettled()
{
int min;
int node = 0;
Iterator<Integer> iterator = unsettled.iterator();
node = iterator.next();
min = distances[node];
for (int i = 1; i <= distances.length; i++)
{
if (unsettled.contains(i))
{
if (distances[i] <= min)
{
min = distances[i];
node = i;
}
}
}
return node;
}
private void evaluateNeighbours(int evaluationNode)
{
int edgeDistance = -1;
int newDistance = -1;
int newNodes=1;
for (int destinationNode = 1; destinationNode <= number_of_nodes;
destinationNode++)
{
if (!settled.contains(destinationNode))
{
if (adjacencyMatrix[evaluationNode][destinationNode] !=
Integer.MAX_VALUE)
{
edgeDistance = adjacencyMatrix[evaluationNode]
[destinationNode];
newDistance = distances[evaluationNode] + edgeDistance;
newNodes=Numnodes[evaluationNode]+1;
if (newDistance < distances[destinationNode])
{
distances[destinationNode] = newDistance;
Numnodes[destinationNode]=newNodes;
}
unsettled.add(destinationNode);
}
}
}
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int number_of_vertices;
int number_of_edges;
int source = 0, destination = 0;
Scanner scan = new Scanner(System.in);
try
{
number_of_vertices = scan.nextInt();
number_of_edges=scan.nextInt();
if (number_of_vertices<1||number_of_vertices>10000)
{
System.out.println("Number of vertices out of boundary");
}
if (number_of_edges<1||number_of_edges>10000)
{
System.out.println("Number of edges out of boundary");
}
adjacency_matrix = new int[number_of_vertices + 1]
[number_of_vertices + 1];
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = 0;
}
}
for(int i=1;i<=number_of_edges;i++)
{
int node1=scan.nextInt();
int node2=scan.nextInt();
adjacency_matrix[node1][node2]=scan.nextInt();
}
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
source = scan.nextInt();
destination = scan.nextInt();
Dijkstras_Shortest_Path dijkstrasAlgorithm = new
Dijkstras_Shortest_Path(
number_of_vertices);
dijkstrasAlgorithm.dijkstra_algorithm(adjacency_matrix, source);
for (int i = 1; i <= dijkstrasAlgorithm.distances.length - 1; i++)
{
if (i == destination)
System.out.println(dijkstrasAlgorithm.distances[i] +" "+
dijkstrasAlgorithm.Numnodes[i]);
}
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}

Rabin Karp algorithm in java

I am interested in implementing the Rabin-Karp algorithm to search for sub strings. Here is the algorithm i used: algorithm.
The code I made for the same is as below.
But output is blank. It says build successfully but gives no output...
package rabinkarp;
public class RabinKarp {
final static int q = 101;
final static int d = 256;
public static void RabinKarpMatcher(String T,String p,int q,int d)
{
int n = T.length();
int M = p.length();
int H = 1,i,j;
for (i = 0; i < M-1; i++)
H = (H*d)%q;
int P = 0;
int t = 0;
for(i = 0;i < M; i++)
{
P = (d*P+p.charAt(i))%q;
t = (d*t+T.charAt(i))%q;
}
for(i = 0; i < n-M; i++)
{
if(P==t)
{
for(j = 0; j < M; j++)
{
if(T.charAt(i+j)!=p.charAt(j))
{
break;
}
}
if(j==M)
{
System.out.println("Pattern found at " + i+1);
}
}
if(i < n-M)
{
t = (d*(t-T.charAt(i)*H)+T.charAt(i+M))%q;
if(i <0)
{
t = t+q;
}
}
}
}
public static void main(String[] args) {
String text = new String("ababaaabhahhhha");
String pat = new String("ha");
RabinKarpMatcher(text,pat,q,d);
}
}

Assigning A Random value 3D Array to an Array of class

I have a class with three fields:
public class CCTest {
public double f;
public double[][][] x;
public double counter;
}
I am trying to assign a random number to it. I have the method below for random data generation:
public static double[][][] getRandomX(int x, int y, int z) {
double[][][] result = new double[x][y][z];
Random r = new Random();
for (int i = 0; i < z; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < x; k++) {
result[k][j][i] = r.nextDouble();
}
}
}
// System.out.println(Arrays.deepToString(result));
return result;
}
As for the issue. I have for example an array with 5 CCTest-objects:
CCTest[] cls = new CCTest[5];
How can I assign a random number to each of the 5 CCTest-objects?
I tried this:
for (int i = 0; i < Size =5; i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].X= getRandomX(this.c, this.D, this.Size);
}
The result should have following structure:
X(:,:,1) =
0.8909 0.5472
0.9593 0.1386
X(:,:,2) =
0.1493 0.8407
0.2575 0.2543
But the code did not produce it. Could anyone guide me to a solution, please?
The problem is that you haven't made any CCTest-instances.
So after you make the CCTest[] cls = new CCTest[5]; the five CCTest-objects are null. You should create them if they don't exist yet:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < (Size = 5); i++) {
// We create a new CCTest-instance if it doesn't exist yet:
if(cls[i] == null){
cls[i] = new CCTest();
}
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}
Alternatively, you could create them first, and then do the for-loop to assign the random doubles:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < cls.length; i++) {
cls[i] = new CCTest();
}
for (int i = 0; i < (Size = 5); i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}

Categories

Resources