Question

JAVA: (29.1) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

STEPS

  1. first we need to sort the edges in ascending order of weights
    • in code i used priority queue to sort the edges
    • here is snippet of code used:
      • for (int i = 0; i <allEdges.size() ; i++) {
        pq.add(allEdges.get(i));
        }
      • allEdges is the arrayList of Edge type which containing all nodes
      • Edge is another class implemented.
  2. select the edge of least weight and check if it forms a cycle in the tree including selected edge: if yes=> ignore it, if NO=> add it to spanning tree.
    • for above step i used while loop going through vertices to find if this edge creates cycle.
    • 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);

  3. until spanning tree has vertices-1(v-1: v is vertices in graph), repeat the step 2.

java code:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

class KrushkalMST {
static class Edge {
int source;
int dest;
int weight;

public Edge(int source, int dest, int weight) {
this.source = source;
this.dest = dest;
this.weight = weight;
}
}

static class Graph {
int vertices;
ArrayList<Edge> allEdges = new ArrayList<>();

Graph(int vertices) {
this.vertices = vertices;
}

public void addEgde(int source, int dest, int weight) {
Edge edge = new Edge(source, dest, weight);
allEdges.add(edge); //add to total edges
}
  
public void kruskalMST(){
PriorityQueue<Edge> pq = new PriorityQueue<>(allEdges.size(), Comparator.comparingInt(o -> o.weight));

//add all the edges to priority queue, //sort the edges on weights
for (int i = 0; i <allEdges.size() ; i++) {
pq.add(allEdges.get(i));
}

//create a parent []
int [] parent = new int[vertices];

//makeset
makeSet(parent);

ArrayList<Edge> mst = new ArrayList<>();

//process vertices - 1 edges
int index = 0;
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);
index++;
union(parent,x_set,y_set);
}
}
//print MST
System.out.println("Minimum Spanning Tree: ");
printGraph(mst);
}

public void makeSet(int [] parent){
//Make set- creating a new element with a parent pointer to itself.
for (int i = 0; i <vertices ; i++) {
parent[i] = i;
}
}

public int find(int [] parent, int vertex){
//chain of parent pointers from x upwards through the tree
// until an element is reached whose parent is itself
if(parent[vertex]!=vertex)
return find(parent, parent[vertex]);;
return vertex;
}

public void union(int [] parent, int x, int y){
int x_set_parent = find(parent, x);
int y_set_parent = find(parent, y);
//make x as parent of y
parent[y_set_parent] = x_set_parent;
}

public void printGraph(ArrayList<Edge> edgeList){
for (int i = 0; i <edgeList.size() ; i++) {
Edge edge = edgeList.get(i);
System.out.println("Edge-" + i + " source: " + edge.source +
" destination: " + edge.dest +
" weight: " + edge.weight);
}
}
}
public static void main(String[] args) {
int vertices = 6;
Graph graph = new Graph(vertices);
graph.addEgde(0, 1, 4);
graph.addEgde(0, 2, 3);
graph.addEgde(1, 2, 1);
graph.addEgde(1, 3, 2);
graph.addEgde(2, 3, 4);
graph.addEgde(3, 4, 2);
graph.addEgde(4, 5, 6);
graph.kruskalMST();
}
}

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

Add a comment
Know the answer?
Add Answer to:
JAVA: (29.1) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is...
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
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