Question

In: Computer Science

Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...

Please write code in java ASAP and add comments too, will be really appreciated. Thanks

CSCI203/CSCI803

This assignment involves extension to the single source-single destination shortest path problem.

The Program Your program should:

1. Read the name of a text file from the console. (Not the command line)

2. Read an undirected graph from the file.

3. Find the shortest path between the start and goal vertices specified in the file.

4. Print out the vertices on the path, in order from start to goal.

5. Print out the length of this path.

6. Find the second shortest path between the start and goal vertices specified in the file.

7. Print out the vertices on the path, in order from start to goal.

8. Print out the length of this path.

The data files are constructed as follows:

 Two integers: nVertices and nEdges, the number of vertices and edges in the graph.

 nVertices triples consisting of the label and the x‐ and y‐coordinates of each vertex. (An int followed by two doubles)

 nEdges triples consisting of the labels of the start and end vertices of each edge, along with its weight.

Note: the weight associated with an edge will be greater than or equal to the Euclidean distance between its start and end vertices as determined by their coordinates. (Two ints followed by a double)

 Two labels, the indicating the start and goal vertices for which the paths are required. (Two ints) A possible solution to the second shortest path problem is as follows:

For each edge ei on the shortest path:

Find the shortest path on (V, E – {ei}).

The shortest of these is the second shortest path.

Programs must compile and run under gcc (C programs), g++ (C++ programs), java or python3.

Programs which do not compile and run will receive no marks. Programs should be appropriately documented with comments.

All coding must be your own work. Standard libraries of data structures and algorithms such as STL or its Java equivalent may not be used, nor may code be sourced from textbooks, the internet, etc.

You may use dynamic memory allocation to create the data structures you need at the start of program execution.

This is the only dynamic allocation that is permitted. Do not use classes. Java programmers may use classes that contain only public data as a substitute for structs in C and C++.

Solutions

Expert Solution

code:

import java.util.*;
import java.io.*;
public class Main {
    public static void main(String args[])throws Exception  {
     File file = new File("test.txt");
     BufferedReader br = new BufferedReader(new FileReader(file));
     String s; 
     int arr[]=new int[120];
     int count=0;
     int i=0;
     while ((s = br.readLine()) != null){
     if(count>0&&count<121){
     String str[]=s.split(" ");
     arr[i]=Integer.parseInt(str[1])-Integer.parseInt(str[2]);
     if(arr[i]<0){
     arr[i]*=(-1);}
     i++;
     }
     count++;
     }
     int temp;
     for(int j=0;j<arr.length;j++){
         System.out.println(arr[j]);
     }
     for(int j=0;j<arr.length;j++){
         System.out.println(arr[j]);
         for(int k=j+1;k<arr.length;k++){
         if(arr[j]>arr[k]){
             temp=arr[j];
             arr[j]=arr[k];
             arr[k]=temp;
         }
        }
     }
     System.out.println("Shortest path: "+arr[0]);
     System.out.println("Second Shortest path: "+arr[1]);
    }
}

Related Solutions

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Please use Java language with comments! Thanks! Write a program that will display multiple dots move...
Please use Java language with comments! Thanks! Write a program that will display multiple dots move across the Frame and randomly change direction when they hit the edge of the screen. Do this by creating a Dot class and making each dot an object of that class. You may reuse code written in class for this part of the assignment. Create a subclass of the Dot class called PlayerDot that is controlled by the player through keyboard input (arrow keys)...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an ARM assembly program to calculate the value of the following function: f(y) = 3y^2 – 2y + 10 when y = 3. My Code below, that needs comments added: FunSolve.s LDR R6,=y LDR R1,[R6] MOV R2,#5 MOV R3,#6 MUL R4,R1,R1 MUL R4,R4,R2 MUL R5,R1,R3 SUB R4,R4,R5 ADD R4,R4,#8 st B st y DCD 3
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT