Can someone please help with this task for my current code?
Task 3 (50 pts (Extra Credit)). Implement a function to organize the shortest path for each node as a string. For example, if a node 4’s shortest path is 0→2→1→4, you can generate a string variable s=“0→2→1→4”. Modify the displaydistance() function to show the shortest distance and the shortest path for each node.
– Hint 1:You definitely need to do operation for the π variable in this task. Feel free to add any class member data based on your need.
Here is my current code:
public class Graph3 {
int n;
int[][] A;
int[] d; //shortest distance
/**
* @param args
*/
public Graph3 () {
}
public Graph3 (int _n, int[][] _A) {
n = _n;
A = _A;
d = new int[n];
}
public void initialize_single_source(int s) {
for (int i = 0; i < n;
i++)
d[i] =
Integer.MAX_VALUE;
d[s] = 0;
}
public void relax (int u, int v) {
if (d[v] > (d[u] +
A[u][v]))
d[v] = d[u] +
A[u][v];
}
public boolean bellman_ford (int s) {
//fill in your program
initialize_single_source(s);
int vertices = A.length;
for (int i=1; i<=vertices-1; i++) {
for (int u=0; u<=vertices-1; u++) {
for (int v=0; v<=vertices-1; v++) {
if (A[u][v] != 0) {
relax (u, v);
}
}
}
}
for (int u=0; u<=vertices-1;u++) {
for (int v=0; v<=vertices-1; v++) {
if (A[u][v] != 0) {
if (d[v]>d[u]+A[u][v]) {
return false;
}
}
}
}
return true;
}
public void dijkstra (int s) {
//fill in your program
initialize_single_source(0);
int vertices = A.length;
int visit[] = new int[vertices];
for (int i=0; i<vertices; i++) {
visit[i] = 0;
}
for (int count=0; count<vertices-1; count++) {
int min = Integer.MAX_VALUE, u=0;
for (int v=0; v<vertices; v++) {
if (visit[v] == 0 && d[v] <= min) {
min = d[v];
u=v;
}
}
visit[u] = 1;
for (int v=0; v<vertices; v++) {
if (visit[v]==0 && A[u][v]>0 && d[u] !=
Integer.MAX_VALUE && d[u]+A[u][v]<d[v]) {
d[v] = d[u] + A[u][v];
}
}
}
}
public void display_distance () {
for (int i = 0; i < n; i++)
{
System.out.println(i + ": " + d[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
int n = 5;
int[][] A = {
{0, 6, 0, 7, 0},
{0, 0, 5, 8, -4},
{0, -2, 0, 0, 0},
{0, 0, -3, 0, 9},
{2, 0, 7, 0, 0}
};
Graph3 g1 = new Graph3(n, A);
g1.bellman_ford(0);
g1.display_distance();
System.out.println("-----------------------");
int[][] B = {
{0, 10, 0, 5, 0},
{0, 0, 1, 2, 0},
{0, 0, 0, 0, 4},
{0, 3, 9, 0, 2},
{7, 0, 6, 0, 0}
};
Graph3 g2 = new Graph3(n, B);
g2.dijkstra(0);
g2.display_distance();
}
}
Can someone please help with this task for my current
code?
Task 3 (50 pts (Extra Credit)). Implement a function to organize
the shortest path for each node as a string. For example, if a node
4’s shortest path is 0→2→1→4, you can generate a string variable
s=“0→2→1→4”. Modify the displaydistance() function to show the
shortest distance and the shortest path for each node.
– Hint 1:You definitely need to do operation for the π variable in
this task. Feel free to add any class member data based on your
need.
Here is my current code:
public class Graph3 {
int n;
int[][] A;
char s="";
int[] d; //shortest distance
/**
* @param args
*/
public Graph3 () {
}
public Graph3 (int _n, int[][] _A) {
n = _n;
A = _A;
d = new int[n];
}
public void initialize_single_source(int s) {
for (int i = 0; i < n; i++)
d[i] = Integer.MAX_VALUE;
d[s] = 0;
}
public void relax (int u, int v) {
if (d[v] > (d[u] + A[u][v]))
d[v] = d[u] + A[u][v];
}
public boolean bellman_ford (int s) {
//fill in your program
int[] prev = new int[vertices];
initialize_single_source(s);
int vertices = A.length;
for (int i=1; i<=vertices-1; i++) {
for (int u=0; u<=vertices-1; u++) {
for (int v=0; v<=vertices-1; v++) {
if (A[u][v] != 0) {
relax (u, v);
}
}
}
}
for (int u=0; u<=vertices-1;u++) {
for (int v=0; v<=vertices-1; v++) {
if (A[u][v] != 0) {
if (d[v]>d[u]+A[u][v]) {
return false;
}
}
}
}
return true;
}
public void dijkstra (int s) {
//fill in your program
initialize_single_source(0);
int vertices = A.length;
int visit[] = new int[vertices];
for (int i=0; i<vertices; i++) {
visit[i] = 0;
}
for (int count=0; count<vertices-1; count++) {
int min = Integer.MAX_VALUE, u=0;
for (int v=0; v<vertices; v++) {
if (visit[v] == 0 && d[v] <= min) {
min = d[v];
u=v;
}
}
visit[u] = 1;
for (int v=0; v<vertices; v++) {
if (visit[v]==0 && A[u][v]>0 && d[u] !=
Integer.MAX_VALUE && d[u]+A[u][v]<d[v]) {
d[v] = d[u] + A[u][v];
}
}
}
}
public void display_distance () {
for (int i = 0; i < n; i++) {
System.out.println(i + ": " + d[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 5;
int[][] A = {
{0, 6, 0, 7, 0},
{0, 0, 5, 8, -4},
{0, -2, 0, 0, 0},
{0, 0, -3, 0, 9},
{2, 0, 7, 0, 0}
};
Graph3 g1 = new Graph3(n, A);
g1.bellman_ford(0);
g1.display_distance();
System.out.println("-----------------------");
int[][] B = {
{0, 10, 0, 5, 0},
{0, 0, 1, 2, 0},
{0, 0, 0, 0, 4},
{0, 3, 9, 0, 2},
{7, 0, 6, 0, 0}
};
Graph3 g2 = new Graph3(n, B);
g2.dijkstra(0);
g2.display_distance();
}
}
Can someone please help with this task for my current code? Task 3 (50 pts (Extra...
10) Shortest Paths (10 marks) Some pseudocode for the shortest path problem is given below. When DIJKSTRA (G, w,s) is called, G is a given graph, w contains the weights for edges in G, and s is a starting vertex DIJKSTRA (G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) 1: RELAX (u, v, w) 1: if dlv] > dlu (u, v) then 2d[v] <- d[u] +w(u, v) 3 4: end if 4: while Q φ do 5: uExTRACT-MIN Q) for each vertex v...
ProgramIntro.pngProgramIntro2.pngProgramIntro1.pngProgram1.pngProgram2.pngGraph Class:import java.util.ArrayList;//Graph is a class whose objects represent graphs. public class Graph { ArrayList<Node> nodeList; ArrayList<Edge> edgeList; public Graph() { nodeList = new ArrayList<Node>(); edgeList = new ArrayList<Edge>(); } public ArrayList<Node> getNodeList() { return nodeList; } public ArrayList<Edge> getEdgeList() { return edgeList; } public void addNode(Node n) { nodeList.add(n); } public void addEdge(Edge e) { edgeList.add(e); } public String...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
Hello can someone help me in my code I left it as comment task #0, task#1, task#2a and task#2b the things that I am missing I'm using java domain class public class Pizza { private String pizzaCustomerName; private int pizzaSize; // 10, 12, 14, or 16 inches in diameter private char handThinDeep; // 'H' or 'T' or 'D' for hand tossed, thin crust, or deep dish, respecitively private boolean cheeseTopping; private boolean pepperoniTopping; private boolean sausageTopping; private boolean onionTopping; private boolean...
Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) { int i,j; bool s[NUM]; for(i=1; i<=n; i++) { dist[i] = c[v][i]; s[i] = false; if (dist[i]>maxint) prev[i] = 0; else prev[i] = v; } dist[v] = 0; s[v] = true; for(i=1; i<n; i++) { int tmp = maxint; int u = v; for(j=1; j<=n; j++) if(!(s[j]) && (dist[j]<tmp)) { u = j; tmp = dist[j]; ...
Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...
//Graph Class: import java.util.ArrayList; //Graph is a class whose objects represent graphs. public class Graph { ArrayList<Node> nodeList; ArrayList<Edge> edgeList; public Graph() { nodeList = new ArrayList<Node>(); edgeList = new ArrayList<Edge>(); } public ArrayList<Node> getNodeList() { return nodeList; } public ArrayList<Edge> getEdgeList() { return edgeList; } public void addNode(Node n) { nodeList.add(n); } public void addEdge(Edge e) { edgeList.add(e); } public String toString() { String s = "Graph g.\n"; if (nodeList.size() > 0) { for (Node n : nodeList) { // Print node info String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n"; s = s.concat(t); } s = s.concat("\n"); } return s; } } // Node Class: import java.util.ArrayList; // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph. public class Node { String name; String val; // The value of the Node String abbrev; // The abbreviation for the Node ArrayList<Edge> outgoingEdges; ArrayList<Edge> incomingEdges; String color; //Create the color of the TYPE Node List int start; //Create the Starting Time int end; //Create the Ending Time public Node( String theAbbrev ) { setAbbrev( theAbbrev ); val = null; name = null; outgoingEdges = new ArrayList<Edge>(); incomingEdges = new ArrayList<Edge>(); } public String getAbbrev() { return abbrev; } public String getName() { return name; } public String getVal() { return val; } public ArrayList<Edge> getOutgoingEdges() { return outgoingEdges; } public ArrayList<Edge> getIncomingEdges() { return incomingEdges;...
Convert into pseudo-code for below code =============================== class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); ScoresCircularDoubleLL score=new ScoresCircularDoubleLL(); while(true) { System.out.println("1--->Enter a number\n-1--->exit"); System.out.print("Enter your choice:"); int choice=s.nextInt(); if(choice!=-1) { System.out.print("Enter the score:"); int number=s.nextInt(); GameEntry entry=new GameEntry(number); ...
Create the code for GraphAlgorithm.prim() so that the MST main routine can run properly public class MSTmain { public static void main(String[] args) { MyGraph G = new MyGraph(6); G.insertEdge(0, 2, 5); G.insertEdge(2, 0, 5); G.insertEdge(1, 0, 1); G.insertEdge(0, 1, 1); G.insertEdge(0, 5, 8); G.insertEdge(5, 0, 8); G.insertEdge(1, 2, 7); G.insertEdge(2, 1, 7); G.insertEdge(1, 3, 5); G.insertEdge(3, 1, 5); G.insertEdge(2, 3, 1); G.insertEdge(3, 2, 1); G.insertEdge(1, 5, 9); G.insertEdge(5, 1, 9); G.insertEdge(3, 4, 3); G.insertEdge(4, 3, 3); G.insertEdge(4, 2, 7);...
Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList; import java.util.Map; import java.util.HashMap; public class DumbDefaultList<T> extends AbstractList<T> { Map<Integer,T> map; public DumbDefaultList() { map = new HashMap<Integer,T>(); } public int size() { return Integer.MAX_VALUE; } public T get(int i) { return map.get(i); } public T set(int i, T x) { return map.put(i, x); } public void add(int i, T x) { Map<Integer, T> map2 = new HashMap<Integer,T>(); for (Integer k : map.keySet())...