JAVA: (29.1) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is another well-known algorithm for finding a minimum spanning tree. The algorithm repeatedly finds a minimum- weight edge and adds it to the tree if it does not cause a cycle. The process ends when all vertices are in the tree. Design and implement an algorithm for finding an MST using Kruskal’s algorithm.
STEPS
here is snippet of code :
while(index<vertices-1){
Edge edge = pq.remove();
//check if adding this edge creates a cycle
int x_set = find(parent, edge.source);
int y_set = find(parent, edge.dest);
if(x_set==y_set){
//ignore, will create cycle
}else {
//add it to our final result
mst.add(edge);
until spanning tree has vertices-1(v-1: v is vertices in graph), repeat the step 2.
java code:
|
import java.util.ArrayList; class KrushkalMST { public Edge(int source, int dest, int weight) { static class Graph { Graph(int vertices) { public void addEgde(int source, int dest, int weight) { //add all the edges to priority queue, //sort the edges on
weights //create a parent [] //makeset ArrayList<Edge> mst = new ArrayList<>(); //process vertices - 1 edges if(x_set==y_set){ public void makeSet(int [] parent){ public int find(int [] parent, int vertex){ public void union(int [] parent, int x, int y){ public void printGraph(ArrayList<Edge> edgeList){ |
output:

take example:
given graph:

#1: there is no cycle formed by including edge 1-2, so we will include it:

#2: similarly there is no cycle is formed by edge 1-3, we will include this edge also:

#3, #4: similarly we will include edge 3-4 and 0-2.

#5: edge 0-1 will form a cycle if we include it, so ignore it.
#6: similarly edge 2-3 will form cycle so ignore this edge as well.

# 7: No cycle is formed when including edge 4-5, so we will include it.

Finally we have:

Minimum Spanning Tree(MST) Total cost: 3+1+1+2+6= 13
JAVA: (29.1) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is...
For minimum spanning tree (MST) construction, Kruskal’s algorithm selects an edge. a) with maximum number of vertices connected to it b) with minimum weight so that cost of MST is always minimum c) that does not introduce a cycle d) none of the above
Algorithm Question: The following questions are on minimum spanning tree. (a) Suppose we have an undirected graph with weights that can be either positive or negative. Do Prim’s and Kruskal’s algorithim produce a MST for such a graph? Explain. (b) Prove that for any weighted undirected graph such that the weights are distinct (no two edges have the same weight), the minimal spanning tree is unique.
3) Find the minimum spanning tree using a) Using Kruskal b) Prim’s algorithm,
Given the following weighted graph G. use Prim's algorithm to determine the Minimum-Cost Spanning Tree (MCST) with node 1 as the "root". List the vertices in the order in which the algorithm adds them to the solution, along with the edge and its weight used to make the selection, one per line. Each line should look like this: add vertex y: edge = (x,y), weight = 5 When the algorithm ends there are, generally, edges left in the heap. List...
You are given an undirected graph G with weighted edges and a minimum spanning tree T of G. Design an algorithm to update the minimum spanning tree when the weight of a single edge is increased. The input to your algorithm should be the edge e and its new weight: your algorithm should modify T so that it is still a MST. Analyze the running time of your algorithm and prove its correctness.
You are given an undirected graph G with weighted edges and a minimum spanning tree T of G. Design an algorithm to update the minimum spanning tree when the weight of a single edge is decreased. The input to your algorithm should be the edge e and its new weight; your algorithm should modify T so that it is still a MST. Analyze the running time of your algorithm and prove its correctness.
Using Kruskal’s Algorithm find the minimum spanning tree of the Graph below. Requirements… Show each step but using a priority queue. Where we show each step of the priority queue list. Assume that vertices of an MST are initially viewed as one element sets, and edges are arranged in a priority queue according to their weights. Then, we remove edges from the priority queue in order of increasing weights and check if the vertices incident to that edge is already...
Given the graph above, use Kruska’s algorithm and Prim’s
algorithm to find the minimum spanning tree. Break ties using
alphabetical order (e.g., if edges have the same cost, pick (A, D)
over (A, G) and pick (A, H) over (C, F). Show the order of the
edges added by each algorithm.
Give an algorithm to find a maximum spanning tree. Is this harder than finding a minimum spanning tree.
Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not). Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means...