After two days, I finally understood how to obtain the connected components of an undirected graph. The algorithm was told by the teacher before. I forgot it, so I can understand it.
An undirected graph is placed in a matrix, such as a 3*3
List [0]
List [1]
List [2]
Loop traversal. If two or two combinations are used, compare the minimum value with the average value, merge a list with the previous one, and delete the list. I cannot tell you clearly. I still need to pay attention to many details when writing.
Don't talk about it. Go to the code
// This program calculates the connected component class triplet {public int X; Public int y; Public double Dist; Public triplet (INT x_v, int y_v, double dist_v) of an undirected graph) {x = x_v; y = y_v; Dist = dist_v ;}}// create a class and use the variables in it. Use it to instantiate public class conncomp {public static double [] [] matrix = {0, 0.2, 0, 0}, {0.2, 0, 0, 0 }, {0, 0, 0, 0.4}, {0, 0, 0.4, 0}; public static int node = 4; public static int [] [] list = new int [node] [node]; public static int deleted = 0; public static int add = 1; public static void main (string [] ARGs) {// todo auto-generated method stub for (INT I = 0; I <node; I ++) {for (Int J = 0; j <node; j ++) {if (I! = J) list [I] [J] = deleted;} list [I] [I] = add; // The initialization list is used to store the vertex of the connected component. At the beginning, they were independent from each other in their respective lists and were not connected to each other. }/* Select the smallest distance between two vertices and merge them into a list. Therefore, a distance function dist () and a function Merge () are required (), after merging, delete the merged list. Clear () is required. Finally, a printing function print_result () is required (). there is also a tag () function. If tag is 1, there is a list value. If tag is 0, it indicates that the list has been deleted. When it is set to 1, it can be added to another list. Here you need to know the position of the record vertex. Set a triple storage. */Triplet temtriplet = new triplet (-1,-1,1000); For (int K = 0; k <node; k ++) {temtriplet. X =-1; temtriplet. y =-1; temtriplet. dist = 1000; // initialize and store the Three-element value of the minimum vertex, which must be initialized in each outermost loop, because each for (INT I = 0; I <node; I ++) for (Int J = I + 1; j <node; j ++) {If (0 <tag_num (list [I]) & 0 <tag_num (list [J]) {double min_dist = dist (list [I], list [J], matrix); If (min_dist <temtriplet. dist & min_dist! = 0.0) {temtriplet. x = I; temtriplet. Y = J; temtriplet. Dist = min_dist ;}} if (temtriplet. X! =-1 & temtriplet. y! =-1 & temtriplet. Dist! = 0.0) {Merge (list [temtriplet. x], list [temtriplet. y]); // clear (list [temtriplet. y]); // Delete after merging} print_result (list); system. out. println ("\ n"); system. out. println ("finished. "); return;} public static int tag_num (INT [] list) // used to check whether the list has been deleted {int list_cnt = 0; For (INT I = 0; I <node; I ++) {If (list [I] = add) list_cnt ++;} return list_cnt;} public static double dist (INT [] list1, int [] list2, double [] [] Matrix) {double sum = 0.0; int CNT = 0; int [] list = new int [node]; for (INT I = 0; I <node; I ++) {list [I] = (list1 [I] = add | list2 [I] = add )? Add: deleted;} For (INT I = 0; I <node; I ++) for (Int J = 0; j <node; j ++) {If (list [I] = add & list [J] = add & I! = J) {If (Matrix [I] [J]! = 0) {sum + = matrix [I] [J]; CNT ++;} else {return 0 ;}}} if (CNT = 0 | sum = 0) {return 0;} return sum/CNT;} public static void merge (INT [] dest_list, int [] src_list) {for (INT I = 0; I <node; I ++) {If (src_list [I] = add) dest_list [I] = add ;}} public static void clear (INT [] list) {for (INT I = 0; I <node; I ++) list [I] = deleted ;} public static void print_result (INT [] [] list) {for (INT I = 0; I <node; I ++) {system. out. print ("[" + I + "]: \ t"); If (tag_num (list [I])> 0) {for (Int J = 0; j <node; j ++) {If (list [I] [J] = add) system. out. print ("" + J + "") ;}} system. out. print ("\ n ");}}}
These things should have been there two years ago. I hope it is not too late.
There is a general algorithm for finding the connected component of an undirected graph. I plan to study it. After all, this is not.