Show the output of the following code:
public class Test {
public static void main(String[] args) {
WeightedGraph<Character> graph = new WeightedGraph<>();
graph.addVertex(‘U’);
graph.addVertex(‘V’);
graph.addVertex(‘X’);
int indexForU = graph.getIndex(‘U’);
int indexForV = graph.getIndex(‘V’);
int indexForX = graph.getlndex(’X’);
System.out.println(“indexForU is ” + indexForU);
System.out.println(“indexForV is ” + indexForV);
System.out.println(“indexForX is ” + indexForV);
graph.addEdge(indexForU, indexForV, 3.5);
graph.addEdge(indexForV, indexForU, 3.5);
graph.addEdge(indexForU, indexForX, 2.1);
graph.addEdge(indexForX, indexForU, 2.1);
graph.addEdge(indexForV, indexForX, 3.1);
graph.addEdge(indexForX, indexForV, 3.1);
WeightedGraph<Character>.MST mst
= graph.getMinimumSpanningTree();
graph.printWeightedEdges();
System.out.println(mst.getTotalWeight{));
mst.printTree();
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 29 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects
Starting Out with Python (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Computer Science: An Overview (12th Edition)
Artificial Intelligence: A Modern Approach
- We were given the following code to solve a Pentomino puzzle, several methods might need to be added or editted to solve the problem. The program should take in an input of 3 4 5 6 for 3x20 4x15 5x12 6x10 grids. Then output the number of correct solutions for them (respectively its 2, 368, 1010, and 2339) import java.util.ArrayList;import java.util.LinkedList;import java.util.List; public class DLX { class DataNode { DataNode L, R, U, D; ColumnNode C; public DataNode() { L = R = U = D = this; } public DataNode(ColumnNode c) { this(); C = c; } DataNode linkDown(DataNode node) { node.D = this.D; node.D.U = node; node.U = this; this.D = node; return node; } DataNode linkRight(DataNode node) { node.R = this.R; node.R.L = node; node.L = this; this.R = node; return node;…arrow_forwardIn java, Implement a method that removes all duplicates. Keep the first instance of the element but remove all subsequent duplicate elements.public void removeDuplicates() see the picture for the right output. Given files: Demo6.java public class Demo6 { public static void main(String[] args) { MyLinkedList<String> list = new MyLinkedList<>(); TestBench.addToList(list); TestBench.addToList(list); TestBench.addToList(list); TestBench.addToList(list); for (int x = 0; x < list.size(); x += 1 + x/3) { System.out.println("Removing element at index: " + x); list.remove(x); } System.out.println("Result"); System.out.println(list); System.out.println("Shuffling"); list.shuffle(7777); System.out.println(list); System.out.println("Removing duplicates (keeping first instance)"); list.removeDuplicates(); System.out.println(list); }} TestBench.java import java.util.AbstractList; public class TestBench { public static…arrow_forwardI need help with my Optimizing the intermediate code to improve the efficiency of the generated machine code. I need to improve the effixiency of the generated machine code import java.util.*; public class SimpleCalculator { private final String input; private int position; private boolean hasDivByZero = false; private Map<String, String> symbolTable; private List<String> instructions; public SimpleCalculator(String input) { this.input = input; this.position = 0; this.symbolTable = new HashMap<>(); this.instructions = new ArrayList<>(); } public static void main(String[] args) { SimpleCalculator calculator = new SimpleCalculator("3 + 5 * (2 - 1)"); calculator.parseExpression(); if (calculator.hasDivByZero) { System.out.println("Error: division by zero"); } else { System.out.println("Result: " + calculator.instructions); } } public void parseExpression() { String t1 = parseTerm(); while (true) { String op = null; if (consume('+')) { op = "+"; } else if…arrow_forward
- Add comments to the code and display outputInt main(void)//Input array containing edges of the graph//(X,Y,W)tuple represents an edge from 'x' to y' having weight w'struct Edge edges I= {0,1,4.).(1,2,4).(2,0,4),(2,1,4)int n=size of (edges)/size of (edges|0]);struct Graph *graph=createGraph(edges,n);return 0; static int minsum=100000;static voidgetMinPathSum(Dictionary<int,ArrayList>graph,bool[visited,ArrayLinecessary,int source,int dest,int currsum)if(src==dest) bool flag=true;for each(int i in necessary)if(!visitedi])flag=false;break: if (flag )minsum=Math. Min(minsum,currsum);return;elselvisited[src]=true;for(auto node:graph[src]) if (Ivisited[node.first])visited [node.first])=true;getMinPathSum(graph, visited,necessary,node.first,dest,currsum+node.second);visited [node.first]=false;arrow_forwardPlease help debug this: Debugging Exercise 4-3, Farrell, Joyce, Java Programming, 9th edition, Cengage Learning public class DebugBox { private int width; private int length; private int height; private FixDebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, int height) { width = width; length = length; height = height; } public void showData() { System.out.println("Width: " + width + " Length: " + length + " Height: " + height); } public double getVolume() { double vol = length - width - height; return vol; } }arrow_forwardIdentify errors from the following program and correct them. import java.swing.*; import java.awt.*; public class Cake extends Panel { public final double CREAM_CHEESE = 0.50 public final double BUTTER = 0.25; public final double PEACH_JELLY = 0.75; public final double BLUEBERRY_JAM = 0.75; private JCheckBox creamCheese; private JCheckBox butter; private JCheckBox peachJelly; private JCheckBox blueberryJam; public Cake() { setLayout new GridLayout(4, 1)); creamCheese = new JCheckBox("Cream cheese"); butter = new JCheckBox("Butter"); peachJelly = new JCheckBox("Peach jelly"); blueberryJam = new JCheckBox("Blueberry jam"); setBorder(BorderFactory.createTitledBorder("Cake Decorations")); add(creamCheese); add(butter); add(peachJelly); add(blueberryJam); } public double getDecorationCosts() { double decorationCosts = 0; if (creamCheese.isSelected()) decorationCosts += CREAM_CHEESE; if (butter.isSelected()) decorationCosts += BUTTER; if (peachJelly.isSelected())…arrow_forward
- Here is the code to create a graph in Python: G2 = nx.gnp_random_graph(10, 0.1, seed=1234)nx.draw(G2, with_labels=True)arrow_forwardI need a test harness for the following code: LinkedListImpl.java package linkedList; public abstract class LinkedListImpl implements LinkedList { private ListItem firstItem; public LinkedListImpl() { this.firstItem = null; } @Override public Boolean isItemInList(String thisItem) { ListItem currentItem = firstItem; while (currentItem != null) { if (currentItem.data.equals(thisItem)) { return true; } currentItem = currentItem.next; } return false; } @Override public Boolean addItem(String thisItem) { if (isItemInList(thisItem)) { return false; } else { ListItem newItem = new ListItem(thisItem); if (firstItem == null) { firstItem = newItem; } else { ListItem currentItem = firstItem; while (currentItem.next != null) { currentItem = currentItem.next; } currentItem.next = newItem; } return true; } } @Override public Integer itemCount() { Integer count = 0; ListItem currentItem = firstItem; while (currentItem != null) { count++; currentItem = currentItem.next; } return count; }…arrow_forwardTRANSLATE TO PYTHON import java.util.*; public class queen { finalprivateint [][] map = newint[8][8]; finalprivateint [][] testMap = newint[8][8]; privateintheuristic = 0; privateintqueenLocs = 0; privateintrestarts = 0; privateintmoves = 0; privateintneighbors = 8; publicqueen( ){ //initializes the map for(inti = 0; i < 8; i++){ for(intj = 0; j < 8; j++){ map[i][j] = 0; } } } publicvoidrandomizeMap( ){ //randomizes the map Randomrand = newRandom( ); intnum; while(queenLocs < 8){ for(inti = 0; i < 8; i++){ map[rand.nextInt(7)][i] = 1; queenLocs++; } } heuristic = heuristic(map); } //***************************Heuristic****************************// publicbooleanfindRowEx(int [][] test, inta){ //determines row conflicts booleanexFound = false; intcount = 0; for(inti = 0; i < 8; i++){ if(test[i][a] == 1){ count++; } } if(count > 1){ exFound = true; } returnexFound; } publicbooleanfindColEx(int [][] test, intj){ //determines column conflicts booleanexFound =…arrow_forward
- Java Code: Below is Parser.java and there are errors. getType() and getStart() is undefined for the type Optional<Token> and there is an error in addNode(). Make sure to get rid of all the errors in the code. Attached is images of the errors. Parser.java import java.text.ParseException;import java.util.LinkedList;import java.util.List;import java.util.Optional; import javax.swing.ActionMap; public class Parser { private TokenHandler tokenHandler; private LinkedList<Token> tokens; public Parser(LinkedList<Token> tokens) { this.tokenHandler = new TokenHandler(tokens); this.tokens = tokens; } public boolean AcceptSeparators() { boolean foundSeparator = false; while (tokenHandler.MoreTokens()) { Optional<Token> currentToken = tokenHandler.getCurrentToken(); if (currentToken.getType() == Token.TokenType.NEWLINE || currentToken.getType() == Token.TokenType.SEMICOLON) {…arrow_forwardyou get setup to work with graphs.Create a Graph class to store nodes and edges or download a Graph librarysuch as JUNG. Use it to implement Breadth First Search and Depth First SearchFollow the video from class if you need a reference.arrow_forwardJAVA Implement the equals method of the Arrow class. Two arrows are equal when they have the same starting point and direction. public class Arrow{ private Point start; private String direction; /** Constructs an arrow. @param x the x-position @param y the y-position @param direction a compass direction N E S W NE NW SE SW */ public Arrow(int x, int y, String direction) { start = new Point(); start.move(x, y); this.direction = direction; } /** Checks whether this arrow is equal to another. @param otherObject another arrow @return true if this arrow and otherObject have the same position and direction. */ public boolean equals(Object otherObject) { /* code goes here */ }}arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education