Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I found this primMST java class source code online for the prim algorithm. It was accompanied by another class called BinaryMinHeap, which is used in

I found this primMST java class source code online for the prim algorithm. It was accompanied by another class called BinaryMinHeap, which is used in the primMST class. I copied and pasted the primMST code in NetBeans, and it gave me error labels in the text editor for three classes that were used in the primMST class. They are class Edge, Graph, and Vertex.

There was another error for class List, but I fixed that by using the import statement import java.util.*; Is there an import statement for the class called "Edge", "Graph", and "Vertex"? I've been searching google but I couldn't find anything. Below is the source code for primMST class.

package mst;

import java.util.*;

/** * * @author */ public class PrimMST { /** * Main method of Prim's algorithm. */ public List> primMST(Graph graph){

//binary heap + map data structure BinaryMinHeap> minHeap = new BinaryMinHeap<>();

//map of vertex to edge which gave minimum weight to this vertex. Map,Edge> vertexToEdge = new HashMap<>();

//stores final result List> result = new ArrayList<>();

//insert all vertices with infinite value initially. for(Vertex v : graph.getAllVertex()){ minHeap.add(Integer.MAX_VALUE, v); }

//start from any random vertex Vertex startVertex = graph.getAllVertex().iterator().next();

//for the start vertex decrease the value in heap + map to 0 minHeap.decrease(startVertex, 0);

//iterate till heap + map has elements in it while(!minHeap.empty()){ //extract min value vertex from heap + map Vertex current = minHeap.extractMin();

//get the corresponding edge for this vertex if present and add it to final result. //This edge wont be present for first vertex. Edge spanningTreeEdge = vertexToEdge.get(current); if(spanningTreeEdge != null) { result.add(spanningTreeEdge); }

//iterate through all the adjacent vertices for(Edge edge : current.getEdges()){ Vertex adjacent = getVertexForEdge(current, edge); //check if adjacent vertex exist in heap + map and weight attached with this vertex is greater than this edge weight if(minHeap.containsData(adjacent) && minHeap.getWeight(adjacent) > edge.getWeight()){ //decrease the value of adjacent vertex to this edge weight. minHeap.decrease(adjacent, edge.getWeight()); //add vertex->edge mapping in the graph. vertexToEdge.put(adjacent, edge); } } } return result; } // end of primMST

private Vertex getVertexForEdge(Vertex v, Edge e){ return e.getVertex1().equals(v) ? e.getVertex2() : e.getVertex1(); } } // end of PrimMST class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

5. Give examples of variations in contextual rules.

Answered: 1 week ago