MATLAB exercise program (Kruskal minimal spanning tree)

Source: Internet
Author: User

There are too many online examples. However, it is necessary for me to practice.

The reason for reading this algorithm is that some papers recently used the minimum spanning tree when looking at color aggregation vectors, so I will get familiar with it.

The Kruskal algorithm is similar to the connected branch algorithm. It feels very similar to the previously implemented connected area mark algorithm.

Steps:

1. For a graph, extract each edge of the graph from small to large for sorting.

2. Add the sorted edge to the New Graph in sequence. If a ring appears in the new graph, the edge is discarded.

3. Repeat Step 2.

The following two figures show how the Kruskal algorithm looks like.

The Code is as follows:

Main. m

Clear all; close all; clc; % introduction to algorithms p349 column G = [0 4 0 0 0 0 0 8 0; 4 0 8 0 0 0 0 0 11 0; 0 8 0 7 0 4 0 2; 0 0 7 0 9 14 0 0 0; 0 0 0 9 0 0 0 0; 0 0 4 14 10 0 2 0; 0 0 0 0 0 2 0 1 6; 8 11 0 0 0 0 1 0 7; 0 0 2 0 0 6 7 0]; [m n] = size (g); E = []; k = 0; % Number of edges for I = 1: m for J = I: n if G (I, j )~ = 0 e = [E; G (I, j) I j]; % extract edge, triple storage K = k + 1; end endendfor I = K: -% sort by edge weight, small row front for j = 1: I-1 if E (J, 1)> E (J +) TMP = E (J, :); E (J, :) = E (J + 1, :); E (J + 1, :) = TMP; end endenda = zeros (m, n ); for I = 1: k a (E (I, 2), E (I, 3) = E (I, 1); A (E (I, 3 ), E (I, 2) = E (I, 1); If Huan (a) % is added to the edge to determine whether the graph contains a (E (I, 2 ), E (I, 3) = 0; A (E (I, 3), E (I, 2) = 0; endend

Huan. m

Function Re = Huan (a) [m n] = size (a); While 1 pre_a = A; for I = 1: m du = 0; % degree of the M element for j = 1: n if a (I, j )~ = 0 du = du + 1; end if du = 1% when the degree of an element is 1, this element is deleted, and its adjacent elements are reduced by a (I, :) = 0; A (:, I) = 0; end if pre_a = A % if there is no element with the degree of 1 in the graph, exit break; end if sum ((:)) = 0 Re = 0; else Re = 1; endend

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.