Question: import java.util. * ; class vertex implements Comparable { int x; int y; int vertexcost; int totalCost; boolean checked; ArrayList adj; public vertex ( int

import java.util.*;
class vertex implements Comparable {
int x;
int y;
int vertexcost;
int totalCost;
boolean checked;
ArrayList adj;
public vertex(int x, int y, int vertexcost){
this.x = x;
this.y = y;
this.vertexcost = vertexcost;
this.checked = false;
this.adj = new ArrayList<>();
this.totalCost = Integer.MAX_VALUE; // Initialize with max value
}
public int compareTo(vertex other){
return this.totalCost - other.totalCost;
}
}
public class GraphBasedAlgorithm {
public int[][] cost;
public static int N;
public static ArrayList all_vertices;
public GraphBasedAlgorithm(int[][] map){
this.cost = map;
N = map.length;
}
public int Graph_Solution(){
all_vertices = new ArrayList<>();
// Populate the vertex list
for (int i =0; i < N; i++){
for (int j =0; j < N; j++){
vertex v = new vertex(i, j, cost[i][j]);
all_vertices.add(v);
}
}
all_vertices.get(0).totalCost = cost[0][0]; // Starting point's cost
build_Adjacency_List(); // Build adjacency list
return minPathSum(); // Find the minimum path sum
}
public static vertex get_vertex(int x, int y){
for (vertex v : all_vertices){
if (v.x == x && v.y == y){
return v;
}
}
return null;
}
public void build_Adjacency_List(){
for (vertex v : all_vertices){
int x = v.x;
int y = v.y;
int newX, newY;
for (int i =0; i <4; i++){
if (i ==0){
newX = x -1;
newY = y;
} else if (i ==1){
newX = x +1;
newY = y;
} else if (i ==2){
newX = x;
newY = y -1;
} else {
newX = x;
newY = y +1;
}
if (newX >=0 && newX < N && newY >=0 && newY < N){
vertex neighbour = get_vertex(newX, newY);
if (neighbour != null){
v.adj.add(neighbour);
}
}
}
}
}
public int minPathSum(){
PriorityQueue pq = new PriorityQueue<>(); // Initialize priority queue
pq.add(all_vertices.get(0)); // Add starting vertex
while (!pq.isEmpty()){
vertex current = pq.poll();
int x = current.x;
int y = current.y;
if (current.checked){
continue;
}
current.checked = true;
if (x == N -1 && y == N -1){
return current.totalCost;
}
for (vertex cur_adj : current.adj){
if (current.totalCost + cur_adj.vertexcost < cur_adj.totalCost && !cur_adj.checked){
cur_adj.totalCost = current.totalCost + cur_adj.vertexcost;
pq.add(cur_adj);
}
}
}
return -1; // If there's no valid path
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the map:");
N = input.nextInt();
int[][] map = new int[N][N];
System.out.println("Enter "+ N * N +" values for the map:");
for (int i =0; i < N; i++){
for (int j =0; j < N; j++){
map[i][j]= input.nextInt();
}
}
GraphBasedAlgorithm gba = new GraphBasedAlgorithm(map);
int result = gba.Graph_Solution();
if (result !=-1){
System.out.println("Minimum path sum: "+ result);
} else {
System.out.println("No valid path found.");
}
input.close();
}
} give me time complexity for each line and 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!