Question: 1 - import java.util. * ; class BruteForce { static int N; static int [ ] [ ] map; static boolean [ ] [ ]

1-import java.util.*;
class BruteForce {
static int N;
static int[][]map;
static boolean[][]visited;
static int minCost =Integer.MAX_VALUE;
static void bruteForce(int row ,int column ,int cost){
if (row ==N-1&& column ==N-1){
minCost =Math.min(minCost,cost);
return;
}
visited[row][column]=true;
int[]dRow ={-1,1,0,0}; //up or down (-1:up ,1:down ,0=same place)
int[]dColumn ={0,0,-1,1}; //right or left (-1:left ,1:right ,0=same place)
for (int i =0; i <4; i++){
int newRow =row +dRow[i],newColumn =column +dColumn[i];
if (newRow >=0&& newRow <N && newColumn >=0&& newColumn <N && !visited[newRow][newColumn]){
bruteForce(newRow,newColumn, cost +map[newRow][newColumn]);
}
}
visited[row][column]=false; //Backtrack
}
public static void main(String[]args){
Scanner input =new Scanner(System.in);
System.out.println("Enter the size of map:");
N =input.nextInt();
map =new int[N][N];
visited =new boolean[N][N];
System.out.println("Enter "+N*N +"value for the map:");
for (int i =0; i <N; i++){
for (int j =0; j <N; j++){
map[i][j]=input.nextInt();
}
}
bruteForce(0,0,map[0][0]);
System.out.println(minCost);
}
}
2-import java.util.Arrays;
import java.util.Scanner;
public class GraphBasedAlgorithm {//bellman ford algorithm
static class Edge {
int u,v,weight;
public Edge(int u,int v,int weight){
this.u =u;
this.v =v;
this.weight =weight;
}
}
//1D
public static int index(int row, int col, int N){
return row *N +col;
}
public static int bellmanFord(int[][]map, int N){
int totalVertices =N *N;
Edge[]edges =new Edge[4*totalVertices]; //4for left, right, up,down
int edgeCount =0;
//
for (int i =0; i <N; i++){
for (int j =0; j <N; j++){
int current =index(i,j,N);
//Add edge for right neighbor
if (j <N -1){
edges[edgeCount++]=new Edge(current,index(i,j +1,N),map[i][j +1]);
}
//Add edge for down neighbor
if (i <N -1){
edges[edgeCount++]=new Edge(current,index(i +1,j,N),map[i +1][j]);
}
//Add edge for left neighbor
if (j >0){
edges[edgeCount++]=new Edge(current,index(i,j -1,N),map[i][j -1]);
}
//Add edge for up neighbor
if (i >0){
edges[edgeCount++]=new Edge(current,index(i -1,j,N),map[i -1][j]);
}
}
}
//Bellman-Ford Initialization
int[]distance =new int[totalVertices];
Arrays.fill(distance,Integer.MAX_VALUE);
distance[0]=map[0][0];
//Relax edges up to N *N -1times
for (int i =0; i <totalVertices -1; i++){
for (int j =0; j <edgeCount; j++){
Edge e =edges[j];
if (distance[e.u]!=Integer.MAX_VALUE && distance[e.u]+e.weight <distance[e.v]){
distance[e.v]=distance[e.u]+e.weight;
}
}
}
return distance[totalVertices -1];
}
public static void main(String[]args){
Scanner input =new Scanner(System.in);
System.out.println("Enter the size of map:");
int N =input.nextInt();
int [][]map=new int[N][N];
System.out.println("Enter "+N*N +"value for the map:");
for (int i =0; i <N; i++){
for (int j =0; j <N; j++){
map[i][j]=input.nextInt();
}
}
System.out.println(bellmanFord(map ,N));
}
} convert to pseudocode

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!