Question

Create the code for GraphAlgorithm.prim() so that the MST main routine can run properly public class...

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);
                G.insertEdge(2, 4, 7);
                G.insertEdge(5, 2, 3);
                G.insertEdge(2, 5, 3);
                G.insertEdge(4, 5, 1);
                G.insertEdge(5, 4, 1);
                
                GraphAlgorithm.displayGraph(G);
                System.out.println();
                MyGraph H = GraphAlgorithm.prim(G);
                GraphAlgorithm.displayGraph(H);
                System.out.println(GraphAlgorithm.totalCost(H));
                System.out.println();
                MyGraph I = GraphAlgorithm.dijkstra(G,0,4);
                GraphAlgorithm.displayGraph(I);
                System.out.println(GraphAlgorithm.totalCost(I));
                System.out.println();
                
                

        }

}

----------------------------------------------------

public class GraphAlgorithm {
        
        public static double totalCost(MyGraph G) {
                double total = 0;
                Edge loc = G.first();
                while (!G.eol()) {
                        total = total + loc.cost;
                        loc = G.next();
                }
                return total;
        }
        
        public static void displayGraph(MyGraph G) {
                Edge loc = G.first();
                while (!G.eol()) {
                        System.out.println(loc.orig +" "+loc.dest+" "+loc.cost);
                        loc= G.next();
                }
        }
        
        public static MyGraph prim(MyGraph G) {
                INSERT CODE HERE
        }

        public static MyGraph cycle(MyGraph G, int source) {
                INSERT CODE HERE
        }
        
        public static MyGraph dijkstra(MyGraph G, int source, int terminal) {
                int n = G.getSize();
                MyGraph result = new MyGraph(n);
                double [] label = new double[n];
                label[source] = 0;
                int[] pred = new int[n];
                pred[source] = source;
                for (int i=0; i= 0 && pred[e.dest]  < 0 && label[e.orig] + e.cost < min) {
                                        best = e;
                                        min = label[e.orig] + e.cost;
                                }
                                e = G.next();
                        }
                        pred[best.dest] = best.orig;
                        label[best.dest] = label[best.orig] + best.cost;
                }
                int node = terminal;
                while (node != pred[node]) {
                        result.insertEdge(pred[node], node, G.getCost(pred[node],node));
                        node = pred[node];
                }
                return result;
        }
}
---------------------------------
public class Edge {
        public Edge(int i, int j, double c) {
                orig = i;
                dest = j;
                cost = c;
        }

        public int orig;
        public int dest;
        public double cost;
        public Edge next = null;
}

--------------------------------------------------------

public class MyGraph {
        public static final int MAXSIZE = 100;
        public static final double BIGM = 10000000;
        
        public MyGraph(int size) {
                n = size;
                nodeStart = new Edge[MAXSIZE];
                for (int i=0; i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public static MyGraph prim(MyGraph G) {

int n= G.getSize();

MyGraph re=new MyGraph(n);

boolean[] in=new boolean[n];

in[0]=true;

for(int i=1; i

{

in[i]=false;

}

for(int step=1;step

{

Edge best = null;

double min=MyGraph.BIGM;

Edge e= G.first();

while(!G.eol()) {

if(in[e.orig]&& !in[e.dest]&& e.cost

{

best=e;

min=e.cost;

}

e=G.next();

}

re.insertEdge(best.orig,best.dest, best.cost);

re.next();

in[best.dest]=true;

}

return re;

}

Add a comment
Know the answer?
Add Answer to:
Create the code for GraphAlgorithm.prim() so that the MST main routine can run properly public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public...

    In the class GraphAlgorithm, insert java code for the method prim public class MyGraph { public static final int MAXSIZE = 100; public static final double BIGM = 10000000; public MyGraph(int size) { n = size; nodeStart = new Edge[MAXSIZE]; for (int i=0; i<n; i++) { nodeStart[i] = null; } } public int getSize() { return n; } public double getCost(int i, int j) { double value = BIGM; Edge e = nodeStart[i]; while (e !=null) { if (e.dest ==...

  • How would I traverse through this graph? Provide example code, please! class Edge {    int src,...

    How would I traverse through this graph? Provide example code, please! class Edge {    int src, dest;    Edge(int src, int dest)    {        this.src = src;        this.dest = dest;    } }; // class to represent a graph object class Graph {    // A list of lists to represent adjacency list    List<List<Integer>> adj = new ArrayList<>();    // Constructor to construct graph    public Graph(List<Edge> edges)    {        // allocate memory for adjacency list        for (int i = 0; i < edges.size(); i++) {            adj.add(i,...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    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);   ...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • What are the errors in this ? public class Mystery { public static void main(String[] args)...

    What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i;    System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); }    }

  • Example Output: STARTING CODE: Assignment5RecursionConsoleDriver.java: import java.io.*; public class Assignment5RecursionConsoleDriver {    public static void main(String[]...

    Example Output: STARTING CODE: Assignment5RecursionConsoleDriver.java: import java.io.*; public class Assignment5RecursionConsoleDriver {    public static void main(String[] args) throws IOException    {        System.out.println("puzzleFormula(1) returns: " + Assignment5Recursion.puzzleFormula(1));        System.out.println("puzzleLoop(1) returns: " + Assignment5Recursion.puzzleLoop(1));        System.out.println("puzzleRecurse(1) returns: " + Assignment5Recursion.puzzleRecurse(1));        System.out.println("\npuzzleFormula(2) returns: " + Assignment5Recursion.puzzleFormula(2));        System.out.println("puzzleLoop(2) returns: " + Assignment5Recursion.puzzleLoop(2));        System.out.println("puzzleRecurse(2) returns: " + Assignment5Recursion.puzzleRecurse(2));        System.out.println("\npuzzleFormula(3) returns: " + Assignment5Recursion.puzzleFormula(3));        System.out.println("puzzleLoop(3) returns: " + Assignment5Recursion.puzzleLoop(3));        System.out.println("puzzleRecurse(3)...

  • How to solve my code for my Deliv C program?

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT