multiplyByConstant method and matrices - matrix

public class Matrix{
public double myArray[][];
public Matrix(double a[][]){
this.myArray=a;
}
public Matrix(int b,Vector...vectors) {
double myArray[][] = new double[vectors.length][];
int row = vectors.length;
int column = vectors.length;
for (int i = 0; i < row; i++) {
myArray[i] = new double[column];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if(b==0)
{
myArray[i][j] = vectors[i].getYourArray()[j];
}
else
{
myArray[j][i] = vectors[i].getYourArray()[j];
}
}
}
}
public Matrix(int a){
double [][] t=new double[a][a];
Matrix z=new Matrix(t);
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if(i==j) z.myArray[i][j]=1;
else z.myArray[i][j]=0;
}
}
this.myArray=z.myArray;
}
public Matrix multiplyByConstant(double m){ // here
}
}
multiplyByConstatnt: Multiplication by a constant: taking a double as a multiplication factor and multiply every element of the matrix with that factor and return a new matrix.
I have also vector and test class,but i don't know how to use this method with matrix

Related

Problem with DFS graph algorithm, wrong loops found

I want to create an algorithm to understand how many closed areas there are in a graph with the relative points, at the moment the problem is that it finds almost all the loops, using a DFS algorithm. However, a problem arises
This is my actual code,momentarily done on processing for instant video feedback:
import java.util.Iterator;
import java.util.LinkedList;
class Graph {
int white = 0, gray = 1, black = 2;
ArrayList<ArrayList<Integer>> path = new ArrayList<ArrayList<Integer>>();
int V;
LinkedList<Integer>[] adj;
LinkedList<Integer>[] cycles;
LinkedList<PVector> points = new LinkedList<PVector>();
int num_cycles = 0;
Graph(int v) {
V = v;
adj = new LinkedList[V];
cycles = new LinkedList[V];
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList();
cycles[i] = new LinkedList();
}
}
void DFSCycleUtil(int source, int parent, int[] colors, int[] parents) {
if (colors[source] == gray) {
System.out.println("Cycle");
path.add(new ArrayList<Integer>());
int curr_parent = parent;
cycles[num_cycles].add(source);
System.out.println(source);
path.get(num_cycles).add(source);
while (curr_parent != source) {
cycles[num_cycles].add(curr_parent);
path.get(num_cycles).add(curr_parent);
System.out.println(curr_parent);
curr_parent = parents[curr_parent];
}
num_cycles++;
return;
} else if (colors[source] == black) {
return;
}
parents[source] = parent;
colors[source] = gray;
Iterator<Integer> i = adj[source].listIterator();
while (i.hasNext()) {
int n = i.next();
if (n != parent) {
DFSCycleUtil(n, source, colors, parents);
}
}
colors[source] = black;
}
void DFSCycle() {
int colors[] = new int[V];
int parents[] = new int[V];
for (int i = 0; i < V; i++) {
colors[i] = white;
}
for (int i = 0; i < V; i++) {
if (colors[i] == white) {
DFSCycleUtil(i, -1, colors, parents);
}
}
}
void addEdge(int u, int v) {
adj[u].add(v);
adj[v].add(u);
}
void addPoint(int x, int y){
points.add(new PVector(x,y));
}
void drawGraph(){
for(int i = 0; i<points.size();i++){
circle(points.get(i).x,points.get(i).y,10);
text(i+1,points.get(i).x,points.get(i).y-5);
Iterator<Integer> in = adj[i+1].listIterator();
print(i + ": \n");
while (in.hasNext()) {
int t = in.next();
line(points.get(i).x,points.get(i).y,points.get(t-1).x,points.get(t-1).y);
}
}
for(int i = 0; i<path.size();i++){
fill(10,10,random(255));
beginShape();
for(int j = 0; j<path.get(i).size(); j++){
vertex(points.get(path.get(i).get(j)-1).x, points.get(path.get(i).get(j)-1).y);
}
endShape(CLOSE);
}
}
}
void setup(){
size(500,500);
Graph g = new Graph(9);
g.addEdge(1,2);
g.addEdge(1,3);
g.addEdge(3,2);
g.addEdge(3,4);
g.addEdge(5,2);
g.addEdge(5,6);
g.addEdge(4,2);
g.addEdge(4,6);
g.addEdge(4,5);
g.addPoint(100,50);
g.addPoint(150,50);
g.addPoint(100,100);
g.addPoint(150,100);
g.addPoint(200,50);
g.addPoint(200,100);
g.DFSCycle();
g.drawGraph();
}
The problem is that it also finds areas that cover other previously found areas, and I don't know how to avoid it.
I would like to know first if there is a better method than the one I am using, and then how I can solve my need.
Thanks in advance
Example images:
This is my actual result:
And this would be my final result:

Algorithm for traversing all lines in the n×n grid?

Use a two-dimensional array to represent a nxn grid.
var grid = new int[n,n];
Note that there are two more diagonal lines.
If i will solve this problem. I will make so.
Create extension method for Int[] (So, you can create your own class. But it's another way. I want to show light waight solution)
public static class IntAsMatrixExtensions {
public const int MatrixColumsCount = 3;
public static int At(this int[] matrix, int i, int j)
{
return matrix[i * MatrixColumsCount + j];
}
public static int[] Create()
{
var grid = new int[MatrixColumsCount*MatrixColumsCount] {
1,2,3,
4,5,6,
7,8,9
};
return grid;
}
}
Then first you should print matrix:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
for(int j = 0; j < IntAsMatrixExtensions.MatrixColumsCount; j++)
{
Console.Write(grid.At(i, j));
}
Console.WriteLine();
}
Then print transponated matrix:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
for(int j = 0; j < IntAsMatrixExtensions.MatrixColumsCount; j++)
{
Console.Write(grid.At(j, i)); //!!! i and j is swithed
}
Console.WriteLine();
}
Then print diag:
//Print diag
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
Console.Write(grid.At(i, i)); //!!! i and j is swithed
}
Then print inverse diag:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
Console.Write(grid.At(i, IntAsMatrixExtensions.MatrixColumsCount - i - 1)); //!!! i and j is swithed
}
Here is example on fiddle https://dotnetfiddle.net/pyX31r

I was trying to build a merge sort but ending up with an error

The method mergeSort(int[]) is undefined for the type merge. This is the error that I am facing. Can anyone please point out my fault? Might be I'm making a syntax error, please point out the mistake im making.
public class merge {
public static mergeSort(int[] a) {
int n = a.length;
if (n < 2) {
return(a[]);
}
int mid = n / 2;
int left[] = new int[mid];
int left[] = new int[n - mid];
for (int i = 0; i < mid; i++) {
left[i] = a[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = a[i];
}
mergeSort(left[]);
mergeSort(right[]);
mmerge(left[], right[], A);
}
public static void mmerge(int[] l, int[] r, int[] array) {
int len1 = l.length();
int len2 = r.length();
int i = 0;
int j = 0;
int k = 0;
while (i < len1 && j < len2) {
if (l[i] <= r[j]) {
array[k]=l[i];
k++;
i++;
} else {
array[k] = r[j];
k++;
j++;
}
}
while (i < len1) {
array[k] = l[i];
k++;
i++;
}
while (j < len2) {
array[k] = r[j];
k++;
j++;
}
}
public static void main(String args[]) {
int[] arr = { 4, 6, 2, 9, 1, 7, 3 };
mergeSort(arr);
for (int p = 0; p < arr.length; p++) {
System.out.print(arr[p]+" ");
}
}
}
Try this
public class merge {
public static void mergeSort(int[] a) {
int n=a.length;
if(n<2) {
return;
}
int mid=n/2;
int left[]=new int[mid];
int right[]=new int[n-mid];
for (int i=0;i<mid;i++) {
left[i]=a[i];
}
for (int i=mid;i<n;i++) {
right[i-mid]=a[i];
}
mergeSort(left);
mergeSort(right);
mmerge(left,right,a);
}
public static void mmerge(int[] l,int[] r,int[] array) {
int len1=l.length;
int len2=r.length;
int i=0;
int j=0;
int k=0;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
array[k]=l[i];
k++;
i++;
}else {
array[k]=r[j];
k++;
j++;
}
}
while(i<len1) {
array[k]=l[i];
k++;
i++;
}
while(j<len2) {
array[k]=r[j];
k++;
j++;
}
}
public static void main(String args[]) {
int[] arr={4,6,2,9,1,7,3};
mergeSort(arr);
for(int p=0;p<arr.length;p++) {
System.out.print(arr[p]+" ");
}
}
}

Sorting algorithm is skipping the last element in my array

I have a simple algorithm to order numbers in an array, all of the elements become ordered except for the last one. I have tried changing the bounds of my loops to fix this, but it just creates an infinite loop instead.
while (pointer < arrayLength){
int min = findMinFrom(pointer);
for (int i = pointer; i < arrayLength; i ++){
if (A[i] == min){
swap(i, pointer);
pointer ++;
}
compNewS ++;
}
}
You see what's the problem? Your pointer will be updated only if A[i] == min if not then it will keep looping. Put your pointer++ out of that condition.
This can be done with only two loops but here is an adjusted version of your code:
public class Numbers {
private static int [] A ;
public static void main(String [] args) {
int [] array = {3,2,1,4,5,6,7,8,9,7};
A = array;
newSort(array, array.length);
for(int i = 0; i < A.length;i++)
System.out.println(A[i]);
}
public static void newSort(int[] array, int arrayLength){
int pointer = 0;
int p = 0;
while(p < array.length) {
int min = findMinFrom(p,array);
int temp = array[p];
array[p] = min;
array[min] = temp;
p++;
}
}
public static int findMinFrom(int p, int[] array){
int min = p;
for (int i = p; i < array.length; i ++){
if (A[i] < array[p]){
min =i;
}
}
return min;
}
}

MergeSort gives StackOverflow error

this is the code for the mergeSort,this gives an stackoverflow error in line 53 and 54(mergeSort(l,m); and mergeSort(m,h);)
Any help will be regarded so valuable,please help me out,i am clueless,Thank you.
package codejam;
public class vector {
static int[] a;
static int[] b;
public static void main(String[] args) {
int[] a1 = {12,33,2,1};
int[] b1 = {12,333,11,1};
mergeSort(0,a1.length);
a1=b1;
mergeSort(0,b1.length);
for (int i = 0; i < a1.length; i++) {
System.out.println(a[i]);
}
}
public static void merge(int l,int m,int h) {
int n1=m-l+1;
int n2 = h-m+1;
int[] left = new int[n1];
int[] right = new int[n2];
int k=l;
for (int i = 0; i < n1 ; i++) {
left[i] = a[k];
k++;
}
for (int i = 0; i < n2; i++) {
right[i] = a[k];
k++;
}
left[n1] = 100000000;
right[n1] = 10000000;
int i=0,j=0;
for ( k =l ; k < h; k++) {
if(left[i]>=right[j])
{
a[k] = right[j];
j++;
}
else
{
a[k] = left[i];
i++;
}
}
}
public static void mergeSort(int l,int h) {
int m =(l+h)/2;
if(l<h)
{
mergeSort(l,m);
mergeSort(m,h);
merge(l,m,h);;
}
}
}
Following is the recursive iterations table of the mergeSort function with argument l=0 and h=4
when the value of l is 0 and value of h is 1 , expression calculate m value which turn out to be 0 but we are checking condition with h which is still 1 so 0<1 become true , recursive calls of this mergeSort function forms a pattern , this pattern doesn't let the function to terminate , stack runs out of memory , cause stackoverflow error.
import java.lang.*;
import java.util.Random;
public class MergeSort {
public static int[] merge_sort(int[] arr, int low, int high ) {
if (low < high) {
int middle = low + (high-low)/2;
merge_sort(arr,low, middle);
merge_sort(arr,middle+1, high);
arr = merge (arr,low,middle, high);
}
return arr;
}
public static int[] merge(int[] arr, int low, int middle, int high) {
int[] helper = new int[arr.length];
for (int i = 0; i <=high; i++){
helper[i] = arr[i];
}
int i = low;
int j = middle+1;
int k = low;
while ( i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
arr[k++] = helper[i++];
} else {
arr[k++] = helper[j++];
}
}
while ( i <= middle){
arr[k++] = helper[i++];
}
while ( j <= high){
arr[k++] = helper[j++];
}
return arr;
}
public static void printArray(int[] B) {
for (int i = 0; i < B.length ; i++) {
System.out.print(B[i] + " ");
}
System.out.println("");
}
public static int[] populateA(int[] B) {
for (int i = 0; i < B.length; i++) {
Random rand = new Random();
B[i] = rand.nextInt(20);
}
return B;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int A[] = new int[10];
A = populateA(A);
System.out.println("Before sorting");
printArray(A);
A = merge_sort(A,0, A.length -1);
System.out.println("Sorted Array");
printArray(A);
}
}

Resources