I'm trying to do an ArrayList of 2 dimensional arrays.
It is the same 2 dim array that I'm adding to the ArrayList, but it has different values each time.
The problem is : when I add the array to the list, it's auto-updating the other versions of the array in the list.
I tried to clone/copy the array just before adding it to the List, but it has no effect.
import java.util.*;
public class Test {
static ArrayList<int[][]> list = new ArrayList<int[][]>();
public static void main(String[] args) {
Lister L = new Lister();
}
public static void add(int[][] array) {
list.add(array);
printArray();
}
public static void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println("Element: " + i);
printDim(list.get(i));
}
System.out.println("--------------------------------");
}
public static void printDim(int[][] array) {
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[0].length; y++) {
System.out.print(array[y][x]+" ");
}
System.out.println();
}
System.out.println("-----------");
}
}
class Lister {
Lister() {
int[][] array1 = new int[5][5];
array1[0][4] = 1;
Test.add(array1);
int[][] array2 = array1.clone();
array2[1][2] = 1;
Test.add(array2);
}
}
Output:
Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
---------------------
Element: 0
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
-----------
Element: 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
--------------------
Expected Output:
Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
--------------------
Element: 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
-----------
Element: 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
1 0 0 0 0
---------------------
A 2D array cannot be shallow copied. Since it's an array of arrays, a shallow copy will give you a new outer array holding references to the same inner arrays as the original.
You need to implement a deep copy instead:
int[][] array2 = array1.clone();
for (int i = 0; i < array2.length; i++) {
array2[i] = array1[i].clone();
}
Please not that this only works for primitive arrays. If you have object arrays, you need to copy each object as well (except you're fine with having the same objects referenced).
It's because you're still using the same 'matrix' of type int. This should be fixed by declaring it again after using the first one.
import java.util.*;
class Test {
static ArrayList<int[][]> list = new ArrayList<int[][]>();
public static void main(String[] args) {
int[][] array1 = new int[5][5];
array1[0][4] = 1;
list.add(array1);
array1 = new int[5][5];
array1[1][2] = 1;
list.add(array1);
printArray();
}
public static void printArray() {
for (int i = 0; i < list.size(); i++) {
printDim(list.get(i));
}
}
public static void printDim(int[][] array) {
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[0].length; y++) {
System.out.print(array[y][x]+" ");
}
System.out.println();
}
System.out.println("--------");
}
}
Related
I am in the process of writing a program that works like Minesweeper. This is done in a 10x10 2d-array, and since I'm working with constraints, I can only write this using a 2d-array. However, I am getting a logical error with my count() method. The count() method returns the number of -1s found in the grid surrounding the input position (input position is what I'd make row and column in main(), for example, (5, 5). It also must not check a position outside the bounds of the 2d array. Here is a visual of what the output of count() should look like.
I also have to use count() with setCounts(). setCounts() goes through the entire 2d array, skips any position that is a -1, and calls the count() method, setting the current position to the value the count() method returns.
public int count(int row, int col)
{
int value = 0;
for(int r = -1; r < 2; r++)
{
for(int c = -1; c < 2; c++)
{
if(c == 0 && r == 0)
continue;
int newR = row + c;
int newC = col + c;
if(newR < 0 || newR >= array.length && newC < 0 || newC >= array[0].length)
continue;
if(array[newR][newC] == -1)
value++;
}
}
return value;
}
public void setCounts()
{
for(int r = 0; r < array.length; r++)
{
for(int c = 0; c < array[r].length; c++)
{
if(array[r][c] != -1)
array[r][c] = count(r, c);
String formatted = String.format("%2d", array[r][c]);
System.out.print(formatted + " ");
}
System.out.println();
}
}
The problem is that:
The count() method is incorrectly counting adjacent -1s surrounding any position I put in main()
setCounts() goes out of bounds after printing a random number of rows
I am certain that it has to do with this block of code:
if(newR < 0 || newR >= array.length && newC < 0 || newC >= array[0].length)
continue;
if(array[newR][newC] == -1)
value++;
When I printed newR and newC in the loop after continue, the loop is randomly adding more numbers to a row/column combo with no direct pattern for the entire output when count() was called in setCounts():
00
11
11
00
11
0 01
12
12
01
12
0 02
13
13
02
13
0 03
14
14
03
14
3 04
15
15
04
15
0 -1 -1 -1 -1 09
09
0
-1 00
...
So taking the print statements out, I get this as an output:
0 -1 -1 0 -1 -1 -1 0 0 0
0 0 0 0 0 0 -1 0 0 0
0 -1 0 -1 0 0 0 0 0 0
0 0 -1 0 0 0 0 0 -1 0
-1 0 0 0 -1 0 0 0 0 0
0 0 0 -1 0 0 0 -1 0 0
0 0 0 0 0 0 -1 0 -1 0
0 0 0 0 -1 -1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
there are 0 -1s
-----------
3 3 0 -1 3 3 0 -1 0 0
/* Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
at Grid3.count(Grid3.java:44)
at Grid3.setCounts(Grid3.java:58)
at Grid3.main(Grid3.java:86) */
The first array is the array I make with Grid's constructor. The second array that the program is trying to print is being done when setCounts() is called.
I was thinking that changing it to:
if(newR < 0 || newR >= array.length)
continue;
if(newC < 0 || newC >= array[0].length)
continue;
if(array[newR][newC] == -1)
value++;
would work: and it does, but not logically. It gets rid of the ArrayIndexOutOfBoundsException error, but logically does not work since it doesn't count adjacent -1's right. It also seems to be adding more numbers randomly to any row/column combo. I put the position as (5, 5) in main() and one time I ran the code, it counted 6 -1s but there are only 3 -1s in the position I put:
-1 0 0 0 0 -1 0 -1 0 0
0 0 0 0 -1 -1 0 0 0 0
0 -1 0 0 0 0 0 0 0 0
0 -1 0 -1 -1 0 0 -1 -1 0
0 0 0 -1 -1 -1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 -1 0 0 0
0 -1 0 0 0 -1 0 0 0 0
0 -1 0 0 -1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
there are 6 -1s
And for setCounts(), it printed a full 10x10 2d array but does not count -1s properly either. For the position (0, 0) (in this case, the 3 at the top left corner of the output shown below), that position should actually have a value of 2 since there are only 2 adjacent -1s, but it actually counts 3.
3 0 0 0 0 0 -1 0 0 0
-1 -1 0 3 0 0 0 -1 0 0
-1 3 6 3 -1 0 3 0 3 0
0 3 0 -1 -1 -1 0 -1 0 0
3 0 0 0 -1 3 6 3 3 0
3 -1 0 0 0 3 0 -1 -1 0
-1 -1 3 0 0 0 0 3 3 3
0 6 6 0 3 0 0 0 -1 0
0 0 -1 -1 0 -1 0 0 0 3
0 0 0 3 3 0 3 0 0 0
Here is a full output:
0 0 0 0 0 0 -1 0 0 0
0 -1 0 0 -1 0 0 0 0 0
0 -1 0 0 0 -1 0 0 0 0
0 0 -1 -1 -1 0 0 -1 0 0
0 0 0 0 0 -1 -1 0 -1 0
-1 0 0 -1 0 -1 0 0 0 0
0 0 0 0 0 0 0 0 -1 0
0 -1 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 -1 0 -1 0
0 0 0 0 0 0 0 0 0 0
there are 2 -1's
-----------
3 0 0 3 0 0 -1 0 0 0
3 -1 0 0 -1 0 0 3 0 0
0 -1 6 3 0 -1 3 0 0 0
0 0 -1 -1 -1 3 3 -1 0 0
0 0 3 3 6 -1 -1 0 -1 0
-1 0 0 -1 0 -1 3 6 0 3
3 3 0 0 3 0 3 3 -1 0
0 -1 0 0 0 3 0 3 -1 3
0 0 3 0 0 0 -1 0 -1 3
0 0 0 0 0 0 0 3 0 3
I cannot figure out what I'm doing wrong. I need count() to properly count adjacent -1s given a position. If it doesn't count adjacent -1s properly, then setCounts() will not logically work. What should I change in either or both methods so that it properly and logically works? Here is my code so far.
public class Grid
{
private int [][] array;
private int max;
public Grid(int max)
{
array = new int[10][10];
this.max = max;
setRandom();
}
public void setRandom()
{
int i = 0;
while(i < max)
{
int r = (int)(Math.random() * 9) + 0;
int c = (int)(Math.random() * 9) + 0;
if(array[r][c] != -1)
{
array[r][c] = -1;
i++;
}
}
}
public int count(int row, int col)
{
int value = 0;
for(int r = -1; r < 2; r++)
{
for(int c = -1; c < 2; c++)
{
if(c == 0 && r == 0)
continue;
int newR = row + c;
int newC = col + c;
if(newR < 0 || newR >= array.length && newC < 0 || newC >= array[0].length)
continue;
if(array[newR][newC] == -1)
value++;
}
}
return value;
}
public void setCounts()
{
for(int r = 0; r < array.length; r++)
{
for(int c = 0; c < array[r].length; c++)
{
if(array[r][c] != -1)
array[r][c] = count(r, c);
String formatted = String.format("%2d", array[r][c]);
System.out.print(formatted + " ");
}
System.out.println();
}
}
public void print()
{
for(int r = 0; r < array.length; r++)
{
for(int c = 0; c < array[r].length; c++)
{
System.out.print(array[r][c] + " ");
}
System.out.println();
}
}
public static void main(String[] args) // printing grid
{
Grid first = new Grid(20);
int count = first.count(5, 5);
first.print();
System.out.println("there are " + count + " -1s");
System.out.println("-----------");
first.setCounts();
}
}
It might be easier to not use a 2d-array.
Or at least, you might want to store an object representing each cell rather than just an int. This way, you could implement the logic for figuring out the neighboring cells for a particular cell within the Cell-Class.
Here is an example (using a map instead of an array):
public class Grid {
public final int width, height;
private final Map<Coordinate, Cell> cells;
public Grid(int width, int height) {
this.width = width;
this.height = height;
this.cells = IntStream.range(0, width).boxed()
.flatMap(column ->
IntStream.range(0, height).boxed()
.map(row -> new Coordinate(row, column))
)
.map(Cell::new)
.collect(Collectors.toMap(Cell::getCoordinate, Function.identity()));
}
public Cell get(int row, int col) {
return this.cells.get(new Coordinate(row, col));
}
public class Cell {
private final Coordinate coordinate;
private final boolean isMine;
public Cell(Coordinate coordinate, boolean isMine) {
this.coordinate = coordinate;
this.isMine = isMine;
}
public Cell(Coordinate coordinate) {
this(coordinate, new Random().nextBoolean());
}
public Coordinate getCoordinate() {
return coordinate;
}
public List<Cell> getNeighbours() {
int leftNeighbourColumnIdx = coordinate.column - 1;
int topNeighbourRowIdx = coordinate.row - 1;
return IntStream.range(leftNeighbourColumnIdx, leftNeighbourColumnIdx + 3).boxed()
.flatMap(column -> IntStream.range(topNeighbourRowIdx, topNeighbourRowIdx + 3).boxed().map(row -> new Coordinate(row, column)))
.map(Grid.this.cells::get)
.filter(Objects::nonNull)
.filter(c -> !c.equals(this))
.collect(Collectors.toList());
}
public int countNeighbouringMines() {
return (int) getNeighbours().stream()
.filter(cell -> cell.isMine)
.count();
}
}
public static class Coordinate {
private final int row, column;
public Coordinate(int row, int column) {
this.row = row;
this.column = column;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Coordinate that = (Coordinate) o;
return row == that.row && column == that.column;
}
#Override
public int hashCode() {
return Objects.hash(row, column);
}
}
public static void main(String[] args) {
final var grid = new Grid(10, 10);
for (int row = 0; row < grid.height; row++) {
for (int col = 0; col < grid.width; col++) {
final var cell = grid.get(row, col);
System.out.print(cell.isMine ? "x" : "o");
System.out.print(" ");
}
System.out.println();
}
System.out.printf("mines around (5,5): %d%n", grid.get(5, 5).countNeighbouringMines());
}
}
Edit: same example as above but with a 2d-array instead of a map
public class Grid {
public final int width, height;
private final Cell[][] cells;
public Grid(int width, int height) {
this.width = width;
this.height = height;
cells = new Cell[width][height];
IntStream.range(0, width)
.forEach(column -> IntStream.range(0, height)
.forEach(row -> cells[column][row] = new Cell(column, row))
);
}
public Cell get(int row, int col) {
final var column = col < 0 || col >= this.cells.length ? null : this.cells[col];
return row < 0 || column == null || row >= column.length ? null : column[row];
}
public class Cell {
private final int column, row;
private final boolean isMine;
public Cell(int column, int row, boolean isMine) {
this.column = column;
this.row = row;
this.isMine = isMine;
}
public Cell(int column, int row) {
this(column, row, new Random().nextBoolean());
}
public List<Cell> getNeighbours() {
int leftNeighbourColumnIdx = column - 1;
int topNeighbourRowIdx = row - 1;
return IntStream.range(leftNeighbourColumnIdx, leftNeighbourColumnIdx + 3).boxed()
.flatMap(column -> IntStream.range(topNeighbourRowIdx, topNeighbourRowIdx + 3).boxed()
.map(row -> Grid.this.get(row, column)))
.filter(Objects::nonNull)
.filter(c -> !c.equals(this))
.collect(Collectors.toList());
}
public int countNeighbouringMines() {
return (int) getNeighbours().stream()
.filter(cell -> cell.isMine)
.count();
}
}
public static void main(String[] args) {
final var grid = new Grid(10, 10);
for (int row = 0; row < grid.height; row++) {
for (int col = 0; col < grid.width; col++) {
final var cell = grid.get(row, col);
System.out.print(cell.isMine ? "x" : "o");
System.out.print(" ");
}
System.out.println();
}
System.out.printf("mines around (5,5): %d%n", grid.get(5, 5).countNeighbouringMines());
}
}
The idea is, that the get(int row, int col) method in Grid returns either a Cell object or null if row or cell are invalid -- but it never fails (throws an exception).
Every cell can then use this method to try to get all its neighbours (getNeighbours()).
The cell doesn't need to worry, if the row/column it asks for is valid or not -- this will be handled by get(int row, int col).
It just needs to filter out all null values (those coordinates were invalid) and itself.
With getNeighbours(), you have a list with all the neighboring cells. If you filter out all cells which have no mines - you get a list of neighbours with mines on them -- and then you just need to count them.
Currently trying to build a program that can implement both DFS and BFS in Java by taking in an adjacency matrix from a file and printing out the following info: order that vertices are first encountered, order that vertices become dead ends, number of components, and the tree edges.
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ProjectDriver {
public static int count;
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Integer> dfsDeadEnd = new ArrayList<Integer>();
ArrayList<Integer> dfsVertices = new ArrayList<Integer>();
ArrayList<Integer> bfsVertices = new ArrayList<Integer>();
int dfsComponents = 0;
int bfsComponents = 0;
Scanner scanner = new Scanner(new File("sample1.txt"));
int n = 8;
int[][] edges = new int[n][n];
boolean[] visited = new boolean[n];
count = 0;
for(int i=0; i < n; i++) {
for (int j=0; j < n; j++) {
edges[i][j] = scanner.nextInt();
}
}
for(int i = 0; i < n; i++){
visited[i] = false;
}
int[][] BFStreeEdgeGraph = new int[n][n];
int[][] DFStreeEdgeGraph = new int[n][n];
int[][] crossGraph = new int[n][n];
for(int i = 0; i <= n-1; i++){
for(int j = 0; j <= n-1; j++){
DFStreeEdgeGraph[i][j] = 0;
BFStreeEdgeGraph[i][j] = 0;
crossGraph[i][j] = 0;
}
}
for(int i = 0; i <= n-1; i++){
if(visited[i] == false){
dfs(edges,i,visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(i);
dfsComponents++;
}
}
for(int i = 0; i <= n-1; i++) {
if(visited[i] == false) {
bfs(edges, i, visited, bfsVertices, BFStreeEdgeGraph);
bfsComponents++;
}
}
System.out.println();
System.out.println("DFS: Number of Connected Components: " + dfsComponents);
System.out.print("DFS: Order of First Encountered: ");
for(int i : dfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.print("DFS: Order of First Dead-Ends: ");
for(int i : dfsDeadEnd){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
System.out.println("Tree edges:");
displayGraph(DFStreeEdgeGraph, n);
System.out.println();
System.out.println();
System.out.println("BFS: Number of Connected Components: " + bfsComponents);
System.out.print("BFS: Order of First encountered: ");
for(int i : bfsVertices){
System.out.print((i+1) + " ");
}
System.out.println();
System.out.println();
}
public static void dfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> dfsVertices, ArrayList<Integer> dfsDeadEnd, int[][] DFStreeEdgeGraph) {
visited[vertex] = true;
dfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w <= edges.length-1; w++) {
if(edges[vertex][w] == 1 && !visited[w]) {
DFStreeEdgeGraph[vertex][w] = 1;
dfs(edges, w, visited, dfsVertices, dfsDeadEnd, DFStreeEdgeGraph);
dfsDeadEnd.add(w);
}
}
}
public static void bfs(int[][] edges, int vertex, boolean[] visited, ArrayList<Integer> bfsVertices, int[][] BFStreeEdgeGraph) {
bfsVertices.add(count, vertex);
count = count + 1;
for(int w = 0; w < edges.length; w++) {
if(edges[vertex][w] != 0 && !visited[w]) {
visited[vertex] = true;
}
}
for(int w = bfsVertices.indexOf(vertex) + 1; w < bfsVertices.size(); w++) {
int value = bfsVertices.get(w);
bfs(edges, value, visited, bfsVertices, BFStreeEdgeGraph);
}
}
public static void displayGraph(int[][] graph, int n) {
for(int i = 0; i <= n-1; ++i){
System.out.print(" ");
for(int j = 0; j <= n-1; ++j){
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
And here is the output from running my code:
Input graph:
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
DFS: Number of Connected Components: 1
DFS: Order of First Encountered: 1 2 6 5 7 3 4 8
DFS: Order of First Dead-Ends: 5 6 8 4 3 7 2 1
Tree edges:
0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
BFS: Number of Connected Components: 0
BFS: Order of First encountered:
And here are my issues. For the DFS, my order of first encountered should be 1 2 6 7 4 3 5 8, but this is not what I'm getting. Additionally, my order of dead ends should be 8 7 5 4 1 2 6 3, but this is also off. My tree edges managed to come out correct.
For the BFS, I can't get anything to print, and debugging my DFS and BFS methods hasn't given me the answer yet. I'd be very grateful from some help.
I would like to resolve a problem for my homework. I have to multiply an array by itself using a method in Java that I created. However, my method doesn't work and I don't know why.
public static void initialiser(int mat[][]) {
int i,j;
for (i = 0; i < mat.length; i++) {
for(j = 0; j < mat[0].length; j++) {
mat[i][j] = 0;
}
}
}
public static int[][] produit(int mat1[][], int mat2[][]) {
int i,j,k;
int [][] matProduit = new int[mat1.length][mat2[0].length];
for (i = 0; i < mat1.length; i++) {
for(j = 0; j < mat2[0].length; j++) {
initialiser(matProduit);
for (k = 0; k < mat2.length; k++) {
matProduit[i][j] = mat1[i][k] * mat2[k][j];
}
}
}
return matProduit;
}
This is the result I should get:
0 1 0 0 0 0
0 0 1 0 0 1
0 0 0 0 1 1
0 0 1 0 1 0
0 0 0 0 0 0
0 0 0 0 0 0
This is the result I get:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
As pointed by Assylias in the comments, you are calling initialiser(matProduit) at each iteration, thus resetting your matrix to zero.
I would advise you to try the debug mode for those kind of problems. It is your top tool for your daily coding.
Please find my own answer on another question, adressing the debug, how to do it, activating it and using it at least correctly.
I have a 2d array:
0 0 0 0 0 0
0 0 0 0 0 0
1 1 0 0 0 0
0 0 0 0 1 1
1 0 1 1 0 0
1 0 0 0 1 1
All blocks take up two adjacent cells either (cells have value =1) horizontally or vertically. Now, I drop all blocks and I get a new array:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
1 1 0 0 0 0
1 0 0 0 1 1
1 0 1 1 1 1
Here is my Source Code:
package IOJava;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ReadFile {
public static int rows, cols;
public static int[][] cells;
public static void main(String[] args) throws IOException {
try {
File file = new File("input.txt");
Scanner input = new Scanner(file);
int rows = 0; // i chinh la so hang
int cols = 0;
int[][] newArr = new int[100][100];
ArrayList string = new ArrayList();
while (input.hasNextLine()) {
String line = input.nextLine();
line = line.trim();
String[] arrLine = line.split(" ");
cols = arrLine.length;
for (int i = 0; i < arrLine.length; i++) {
newArr[rows][i] = Integer.parseInt(arrLine[i]);
}
rows++;
}
int[][] newCopyArr = twoDimensionalArrayClone(newArr);
for (int i = 0; i <rows; i++) {
for (int j = 0; j <cols; j++) {
if(i+1<rows && j+1<cols){
if(newCopyArr[i][j]==1 && newCopyArr[i][j+1]==1){
if(newCopyArr[i+1][j]==0 && newCopyArr[i+1][j+1]==0){
newCopyArr[i][j]=0 ; newCopyArr[i][j+1]=0;
newCopyArr[i+1][j]=1 ; newCopyArr[i+1][j+1]=1;
}
}
if(newCopyArr[i][j]==1 && newCopyArr[i+1][j]==1){
if(i+2<rows){
if(newCopyArr[i+2][j]==0){
newCopyArr[i][j]=0;
newCopyArr[i+2][j]=1;
}
}
}
// if(newCopyArr[i][j]==1 && newCopyArr[i+1][j]==1 && newCopyArr[i+1][j]==1){
//
// }
//
}
}
}
//sysout
for (int k = 0; k < rows; k++) {
for (int h = 0; h < cols; h++) {
System.out.print(" " + newCopyArr[k][h]);
}
System.out.println();
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static int[][] twoDimensionalArrayClone(int[][] a) {
int[][] b = new int[a.length][];
for (int i = 0; i < a.length; i++) {
b[i] = a[i].clone();
}
return b;
}
}
But, when I try to execute this code with other 2d array[], it is not right.
Example:
0 0 0 0 0 0
0 1 1 0 0 0
1 1 0 0 0 0
0 0 0 0 1 1
1 0 1 1 0 0
1 0 0 0 1 1
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to implement Dijkstra Algorithm using priority queue given an adjaceny matrix.
I know the problem is probably at where I add vertices to the priority queue but I can't figure out how to fix it!
static int dijkstra(int[][] g, int i, int j) {
// Get the number of vertices in G
int n = g.length;
int counter = 0;
PriorityQueue<Vertex> q = new PriorityQueue<Vertex>(n, new Comparator<Vertex>() {
public int compare(Vertex a, Vertex b) {
Vertex v1 = (Vertex) a;
Vertex v2 = (Vertex) b;
if (v1.getD() > v2.getD()) {
return 1;
} else if (v1.getD() < v2.getD()) {
return -1;
} else {
return 0;
}
}
});
int[] distance = new int[n];
for (int l = 0; l < n; l++) {
distance[l] = 99999;
}
distance[i] = 0;
for (int l = 0; l < n/2; l++) {
for (int m = 0; m < n; m++) {
if (g[l][m] > 1) {
System.out.printf("%d was added \n", g[l][m]);
q.add(new Vertex(l, g[l][m]));
}
}
}
while (!q.isEmpty()) {
int u = 0;
for (int z = 0; z < n; z++) {
if (distance[z] < distance[u]) {
u = z;
}
}
if (distance[u] == 99999) { break; }
q.remove();
for (int l = 0; l < n; l++) {
if (g[u][l] > 1) {
int alt = distance[u] + g[u][l];
if (alt < distance[l]) {
distance[l] = alt;
q.remove();
q.add(new Vertex(u, distance[l]));
}
}
}
}
for (int k = 0; k < n; k++) {
System.out.printf("==>%d", distance[j]);
}
return distance[j];
}
And:
class Vertex {
int v,d;
public Vertex(int num, int dis) {
v = num;
d = dis;
}
public int getV() {
return v;
}
public int getD() {
return d;
}
}
I am testing with the following matrix:
0 0 0 0 0 0 0 38 0 0 0 0 0 0 0 0
0 0 0 0 65 0 64 0 6 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 6 8 0 0 0 62
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 65 0 0 0 0 0 0 0 6 0 0 0 0 55 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 64 0 0 0 0 0 53 0 0 36 0 45 0 0 0
38 0 0 0 0 0 53 0 0 0 0 91 0 29 0 0
0 6 0 0 0 0 0 0 0 0 0 95 55 0 0 0
0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0
0 0 6 0 0 0 36 0 0 0 0 0 0 0 0 0
0 0 8 0 0 0 0 91 95 0 0 0 60 0 0 0
0 0 0 0 0 0 45 0 55 0 0 60 0 0 0 0
0 0 0 0 0 0 0 29 0 0 0 0 0 0 0 0
0 0 0 0 55 0 0 0 0 0 0 0 0 0 0 0
0 0 62 0 0 0 0 0 0 0 0 0 0 0 0 0
And start is 0, end is n - 1. I should be getting 195 but it seems like none of the distances are being changed!
When you're printing the distances you print the array at j all the time while k is the iterator. The distances appear constant but they are changing.
for (int k = 0; k < n; k++) {
System.out.printf("==>%d", distance[k]);
}
Also, in your algorithm you're removing the top vertex twice which isn't plausible. The algorithm should be something like this instead:
while (!q.isEmpty()) {
int u = q.peek().v;
q.remove();
for (int l = 0; l < n; l++) {
if (g[u][l] > 1) {
int alt = distance[u] + g[u][l];
if (alt < distance[l]) {
distance[l] = alt;
q.add(new Vertex(u, distance[l]));
}
}
}
}