DFS and BFS with adjacency matrix counted in Java - java

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.

Related

How to change from user input of adjacency matrix into hard coding it into the program?

try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
This is what the user input looks like:
Enter the number of vertices
5
Enter the Weighted Matrix for the graph
0 9 6 5 3
0 0 0 0 0
0 2 0 4 0
0 0 0 0 0
0 0 0 0 0
How would I be able to hard code the matrix into the program rather than ask the user to enter the matrix?

Two dimensional Array Java

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

Java Multidimensional Array Outprint

I dont understand, why these numbers are printed out. Shouldn't it just out print 3 2 1? Instead, it prints:
3
0
0
0
2
0
0
0
1
Thank you for your help :)
public static void main(String[] args) {
int i, j, n = 3;
int[][] polje = new int[n][n];
polje[0][0] = 3;
polje[1][1] = 2;
polje[2][2] = 1;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(polje[i][j] + " ");
System.out.println();
}
}
}
you have set likewise,
3 0 0
0 2 0
0 0 1
so obviously you get, 3 0 0 0 2 0 0 0 1
if you want to print only 3 2 1 then made this changes only,
if(i == j){
System.out.print(polje[i][j] + " ");
System.out.println();
}

2^N Combinaisons with Integers (Kernel), how to generate them?

(Merry Christmas btw ^^)
Here is my problem (in JAVA) but it's definitely an algorithmic problem and I don't know how to solve it :/
So here it is, with an example (just for information, all my calculs are in Binary, so 1+1 = 0)
let's name variables:
N : the number of elements in kernel.
M : the length of an element in the kernel.
int[][] Kernel:
....
i : 0 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 (length = M)
i+1 : 1 0 1 0 1 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 1 0 1 0 1 (length = M)
....
N : ....
My goal with theses things, is to generate all the possible combinaison (so 2^N elements)
and I want to generate them.
By generate I mean exactly this :
Result[0] = 0 0 0 0 0 0 0 0 0 0 0 0 0
Result[1] = Kernel[0]
Result[2] = Kernel[1]
....
Result[i] = Kernel[i-1]
Result[N-1] = Kernel[N-2]
Result[N] = Kernel[0] + Kernel[1]
Result[N+1] = Kernel[0] + Kernel[2]
Result[N+i] = Kernel[0] + Kernel[i]
Result[2N-1] = Kernel[0] + Kernel[N-1]
....
Result[I] = Kernel[0] + Kernel[1] + Kernel[2]
Result[I+1] = Kernel[0] + Kernel[1] + Kernel[i]
Result[I+J] = Kernel[0] + Kernel[1] + Kernel[N-1]
....
Result[2^N+1] = Kernel[0] + Kernel[1] + ... + Kernel[i] + ... + Kernel[N-1]
Here is what I already success to do, but it's not complete and I don't know how to generalize the calcul in order to work with any N...
public static int[][] combinaisons(int[][] kernel) {
/* if the kernel is empty, there is no possible combinaison */
if(kernel.length == 0) return kernel;
/* We allocate the good number of space... */
int[][] result = new int[(int) (Math.pow(2, noyau.length)+1)][];
/* Every element in result has the same length as in kernel's elements. */
for(int i = 0; i < resultat.length; i++) {
result[i] = new int[kernel[0].length];
}
/* The first element of result has to be only 0 0 0 0 0 0 0 */
for(int j = 0; j < kernel[0].length; j++) {
result[0][j] = 0;
}
/* We rewrite the element of kernel because they are a part of the solution... */
for(int i = 0; i < kernel.length; i++) {
for(int j = 0; j < kernel[i].length; j++) {
result[i+1][j] = kernel[i][j];
}
}
/*
I managed to do it when it's the sum of only 2 elements,
but it has to be with 3, 4 ... N-1 :/
*/
for(int i = 0; i < kernel.length; i++) {
for(int j = 0; j < kernel[i].length; j++) {
for(int k = i+1; k < kernel.length; k++) {
result[k*kernel.length+i][j] = (kernel[i][j]+kernel[k][j])%2;
}
}
}
return result;
}
Edit:
About an example, let's give this:
N = 2
M = 4
Kernel:
0 1 1 0
1 0 0 1
In result I want:
0 0 0 0
0 1 1 0
1 0 0 1
1 1 1 1 (the sum of the 2 elements in Kernel)
So this is a simple example (quite particularly values, if you want bigger, just ask :) )
Even if the array at the end seems to be VERY HUGE :) that's exactly what I want to generate (don't care about memory, it will for sure be okay)
I am going to use boolean[][] instead of int[][]. 0 means false, 1 means true.
public static boolean[][] combinations(boolean kernel[][]) {
int n = kernel.length;
int m = kernel[0].length;
int p = 1 << n;
boolean[][] temp = new boolean[p][m];
for (int i = 0; i < p; i++)
for (int j = 0; j < n; j++)
if (((1 << j) & i) != 0)
for (int k = 0; k < m; k++)
temp[i][k] ^= kernel[j][k];
return temp;
}

How to print numbers in a triangle pattern?

Below is the pattern I would like to print in the console:
1
2 0 2
3 0 0 0 3
And below is the code I have right now. Each number should have 5 spaces between each other.
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int[] row = new int[0];
for(int i=0 ; i < n ; i++){
row = nextRow(row);
for(int j=0;j < n-i;j++){
//Padding For Triangle
System.out.print(" ");
}
//Output the values
for(int j=0 ; j < row.length ; j++){
System.out.print(row[j]+" ");
}
//Start New Line
System.out.println();
}
}
/*Find Values Of Next Row*/
public static int[] nextRow(int row[]){
int nextRow[] = new int [row.length+1];
nextRow[0] = row.length+1;
nextRow[nextRow.length-1] =row.length+1;
for(int i=1 ; i < nextRow.length-1 ; i++){
nextRow[i] = 0;
}
return nextRow;
}
}
Can anyone help me with this?
Here is some modified code. Its not complete and still something need to update your code to get desired result. That part I am leaving it for you. I also left hint in code so that you could modify.
1.
// Padding For Triangle
System.out.print(" "); // 6 white space
2.
System.out.print(row[k]);
System.out.print(" "); // 5 white space
3.
/* Find Values Of Next Row */
#SuppressWarnings("null")
public static int[] nextRow(int row[]) {
int nextRow[] = null;
if (row.length == 0) {
nextRow = new int[1];
nextRow[0] = 1;
} else {
// count++; // Hint
nextRow = new int[row.length + 2];
for (int i = 0; i < nextRow.length; i++) {
if ((i == 0 || i == nextRow.length - 1)) {
nextRow[i] = nextRow.length - 2;
// nextRow[i] = count;
} else {
nextRow[i] = 0;
}
}
}
return nextRow;
}
If you have any questions, just ask. Good Luck.
You first have to identify the pattern that the output expects, which in your case is:
Row0, columns 1
Row1, columns 3
Row2, columns 5
Row3, columns 7
which is, rowNum*2+1
Based, on this, I modified your code and here is the working solution:
import java.util.Scanner;
public class Triangle{
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int[] row = null;
for(int i=0 ; i < n ; i++){
row = nextRow(i);
for(int j=0;j < n-i;j++){
//Padding For Triangle
System.out.print(" ");
}
//Output the values
for(int j=0 ; j < row.length ; j++){
System.out.print(row[j]+" ");
}
//Start New Line
System.out.println();
}
}
/*Find Values Of Next Row*/
public static int[] nextRow(int rowNum){
int nextRow[] = new int [rowNum*2+1];
nextRow[0] = rowNum+1;//-rowNum/2;
nextRow[nextRow.length-1] = nextRow[0];
for(int i=1 ; i < nextRow.length-1 ; i++){
nextRow[i] = 0;
}
return nextRow;
}
}
And here is some output:
2:
1
2 0 2
5:
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 0 0 5
10:
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 0 0 5
6 0 0 0 0 0 0 0 0 0 6
7 0 0 0 0 0 0 0 0 0 0 0 7
8 0 0 0 0 0 0 0 0 0 0 0 0 0 8
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9
10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
i saw your code, i think that we can do this, follow is my modified code:
public class Triangle {
public static void main(String args[]) {
System.out.println("Input the number of lines you want to print.");
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int [] row = new int[0];
for (int i = 0; i < n; i++) {
row = nextRow(row);
for (int j = 0; j < n - i; j++) {
// Padding For Triangle
System.out.print(" ");
}
// Output the values
for (int j = 0; j < row.length; j++) {
System.out.print(row[j] + " ");
}
// Start New Line
System.out.println();
}
}
/* set space between each other. */
public static String printSpace(int n) {
String result = "";
for (int i = 0; i < n; i++) {
result += " ";// 5 space
}
return result;
}
/* Find Values Of Next Row */
public static int [] nextRow(int row[]) {
int nextRow[] = new int[row.length + 1];
nextRow[0] = row.length + 1;
nextRow[nextRow.length - 1] = row.length + 1;
for (int i = 1; i < nextRow.length - 1; i++) {
nextRow[i] = 0;
}
return nextRow;
}
}
may be this is answer what you want.
Look up the arithmetic sequence for printing out the zeros, the rest should be trivial.

Categories

Resources