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 vertexint x int y int vertexcost
this.x x;
this.y y;
this.vertexcost vertexcost;
this.checked false;
this.adj new ArrayList;
this.totalCost Integer.MAXVALUE; Initialize with max value
public int compareTovertex other
return this.totalCost other.totalCost;
public class GraphBasedAlgorithm
public int cost;
public static int N;
public static ArrayList allvertices;
public GraphBasedAlgorithmint map
this.cost map;
N map.length;
public int GraphSolution
allvertices new ArrayList;
Populate the vertex list
for int i ; i N; i
for int j ; j N; j
vertex v new vertexi j costij;
allvertices.addv;
allvertices.gettotalCost cost; Starting point's cost
buildAdjacencyList; Build adjacency list
return minPathSum; Find the minimum path sum
public static vertex getvertexint x int y
for vertex v : allvertices
if vx x && vy y
return v;
return null;
public void buildAdjacencyList
for vertex v : allvertices
int x vx;
int y vy;
int newX, newY;
for int i ; i ; i
if i
newX x ;
newY y;
else if i
newX x ;
newY y;
else if i
newX x;
newY y ;
else
newX x;
newY y ;
if newX && newX N && newY && newY N
vertex neighbour getvertexnewX newY;
if neighbour null
vadj.addneighbour;
public int minPathSum
PriorityQueue pq new PriorityQueue; Initialize priority queue
pqaddallvertices.get; Add starting vertex
while pqisEmpty
vertex current pqpoll;
int x current.x;
int y current.y;
if currentchecked
continue;
current.checked true;
if x N && y N
return current.totalCost;
for vertex curadj : current.adj
if currenttotalCost curadj.vertexcost curadj.totalCost && curadj.checked
curadj.totalCost current.totalCost curadj.vertexcost;
pqaddcuradj;
return ; If there's no valid path
public static void mainString args
Scanner input new ScannerSystemin;
System.out.printlnEnter the size of the map:";
N input.nextInt;
int map new intNN;
System.out.printlnEnter N N values for the map:";
for int i ; i N; i
for int j ; j N; j
mapij input.nextInt;
GraphBasedAlgorithm gba new GraphBasedAlgorithmmap;
int result gba.GraphSolution;
if result
System.out.printlnMinimum path sum: result;
else
System.out.printlnNo 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
