KruskaI algorithm of minimum spanning tree, which uses union search set to judge whether there is ring or not

Posted by angelssin on Sun, 14 Jun 2020 06:42:41 +0200

The concept of minimum spanning tree:

  • One edge of a connected graph is a tree
  • The minimum spanning tree is the weight sum of the edges in all spanning trees
  • Often used in the optimization of constructive problems such as network construction

Algorithm implementation idea:

  1. First, the weighted edges are sorted in ascending order.
  2. Take out the edge from the smallest to the largest. When taking it, judge whether the two nodes connected by the edge are in the same set. If the two nodes are not in the same set, give up the edge and start the judgment of the next edge. (this is to avoid rings).
  3. At the beginning, each node is an independent set. When an edge is taken out, two sets corresponding to two vertices corresponding to this edge are combined into a set. Adding the second step of judgment can ensure that the extracted edge will not form a ring; because the edges of the same set are all on the same line, if an edge is added to two nodes of an edge, a ring will appear.
  4. When the extracted edge is equal to the number of nodes - 1, it ends; at this time, all the extracted edges constitute the minimum spanning tree.

Parallel search set

  • Above, we use the method of whether or not we are in the same set to determine whether or not there is a ring; in fact, we use the parallel search set algorithm; we can use a tree to represent a set, and use the root node of the tree as the symbol of the set.

  • At the beginning, all nodes are an independent tree. When merging two nodes, you can select one node as the root node of the tree (that is, let one node point to the other). When merging two trees, you can point the root node of one tree to the other tree. When you need to find the collection of a node, you can find the root node all the way up through the node, and then return the root node to determine the collection of the node.

  • The main function of concurrent query set is to merge the set and find the set where the node is located.

The parallel search set can be realized by a one-dimensional array. The algorithm steps are as follows:

  1. Create a one-dimensional array of size n, n is the number of nodes, array subscript represents the node number, array element represents the parent node of the corresponding node.
  2. Initializing the array to - 1, - 1 means that the node has no parent, that is, the node is a root node.
  3. Merge the corresponding set of two nodes, that is, merge the two trees. First find out the root nodes of two sub trees, and then point one of the root nodes to the other.

The code is as follows:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class minimum spanning tree  {
	
	static int[] parent = new int[5];//Store the parent of each node
	
	public static void main(String[] args) {
		//Initialization diagram
		List<Edge> l = new ArrayList<Edge>();
		l.add(new Edge('C','D',1));
		l.add(new Edge('C','A',1));
		l.add(new Edge('C','E',8));
		l.add(new Edge('A','B',3));
		l.add(new Edge('D','E',3));
		l.add(new Edge('B','C',5));
		l.add(new Edge('B','E',6));
		l.add(new Edge('B','D',7));
		l.add(new Edge('A','D',2));
		l.add(new Edge('A','E',9));
		Collections.sort(l);  //Sort edges
		
		Arrays.fill(parent, -1);  //Initialize array to - 1
		
		Set<String> s = new HashSet<String>(); //Used to store selected edges
		int count = 0;  //Used to count the number of extracted edges
		
		//Add four edges in turn
		for(int i=0; count<5-1; i++){
			Edge temp = l.get(i);
			if(!union(temp.start,temp.end)){
				continue;
			}
			String str = (char)(temp.start+'A') + "--" +(char)(temp.end+'A'); 
			s.add(str);
			count++;
		}
		
		//Print results
		for(String str:s){
			System.out.println(str);
		}
		
	}
	//Find the collection of nodes (find root node)
	private static int findRoot(int t){
		int res = t;
		while(parent[res]!=-1){
			res = parent[res];
		}
		return res;
	}
	
	//Merge the tree (Collection) where the two nodes are located. If the merge fails, false will be returned. If the merge succeeds, true will be returned
	//You can also check whether two nodes are in the same set
	private static boolean union(int x, int y){
		int xroot = findRoot(x);
		int yroot = findRoot(y);
		if(xroot == yroot){   //Two nodes in the same tree
 			return false;
		}
		parent[xroot] = yroot; //Point the root of x to the root of y
		return true;
	}
}

class Edge implements Comparable<Edge>{
	int start;  //Start of edge
	int end;    //End of edge
	int distance; //Weight of edge
	
	public Edge(char start, char end, int distance) {
		super();
		this.start = (int)(start-'A');  //Convert character to int
		this.end = (int)(end-'A');
		this.distance = distance;
	}

	@Override
	public int compareTo(Edge o) {  //Return a positive number to indicate this will come next
		if(this.distance>o.distance){
			return 1;
		}
		if(this.distance<o.distance){
			return -1;
		}
		return 0;
	}
}

Topics: Java network