Question: 1 - import java.util. * ; class BruteForce { static int N; static int [ ] [ ] map; static boolean [ ] [ ]
import java.util.;
class BruteForce
static int N;
static intmap;
static booleanvisited;
static int minCost IntegerMAXVALUE;
static void bruteForceint row int column int cost
if row N&& column N
minCost MathminminCostcost;
return;
visitedrowcolumntrue;
intdRow ; up or down :up :down same place
intdColumn ; right or left :left :right same place
for int i ; i ; i
int newRow row dRowinewColumn column dColumni;
if newRow && newRow N && newColumn && newColumn N && visitednewRownewColumn
bruteForcenewRownewColumn cost mapnewRownewColumn;
visitedrowcolumnfalse; Backtrack
public static void mainStringargs
Scanner input new ScannerSystemin;
System.out.printlnEnter the size of map:";
N inputnextInt;
map new intNN;
visited new booleanNN;
System.out.printlnEnter NN value for the map:";
for int i ; i N; i
for int j ; j N; j
mapijinputnextInt;
bruteForcemap;
System.out.printlnminCost;
import java.util.Arrays;
import java.util.Scanner;
public class GraphBasedAlgorithm bellman ford algorithm
static class Edge
int uvweight;
public Edgeint uint vint weight
this.u u;
this.v v;
this.weight weight;
D
public static int indexint row, int col, int N
return row N col;
public static int bellmanFordintmap int N
int totalVertices N N;
Edgeedges new EdgetotalVertices; for left, right, updown
int edgeCount ;
for int i ; i N; i
for int j ; j N; j
int current indexijN;
Add edge for right neighbor
if j N
edgesedgeCountnew Edgecurrentindexij Nmapij ;
Add edge for down neighbor
if i N
edgesedgeCountnew Edgecurrentindexi jNmapi j;
Add edge for left neighbor
if j
edgesedgeCountnew Edgecurrentindexij Nmapij ;
Add edge for up neighbor
if i
edgesedgeCountnew Edgecurrentindexi jNmapi j;
BellmanFord Initialization
intdistance new inttotalVertices;
Arrays.filldistanceIntegerMAXVALUE;
distancemap;
Relax edges up to N N times
for int i ; i totalVertices ; i
for int j ; j edgeCount; j
Edge e edgesj;
if distanceeuIntegerMAXVALUE && distanceeueweight distanceev
distanceevdistanceeueweight;
return distancetotalVertices ;
public static void mainStringargs
Scanner input new ScannerSystemin;
System.out.printlnEnter the size of map:";
int N inputnextInt;
int mapnew intNN;
System.out.printlnEnter NN value for the map:";
for int i ; i N; i
for int j ; j N; j
mapijinputnextInt;
System.out.printlnbellmanFordmap N;
convert to 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
