Minimum Spanning Tree (prim, kruskal)

Problem Description
= (V, E) in, (u, v) represents a linking edge vertex u to vertex v in the graph G in a given free, and W (u, v) representation of this edge weight, if T is present as and E is the sub-set of acyclic graph, so that W (T) minimum, this minimum spanning tree T of G.

Here Insert Picture Description

Here Insert Picture Description

(A) Kruskal algorithm:
on the side sort, be greedy minimal side from the start, each considered as a collection point, if not the same side at both ends of a collection among the two sets merge. Until the rest of a collection is the minimum spanning tree.

Here Insert Picture Description
(B) Prim's algorithm:
Pick a point for the beginning of the set, find the nearest point from this collection to the collection.

Here Insert Picture Description
Design
[Core Pseudocode]
Minimum Spanning Tree:
(a) the Kruskal algorithm:
establishing an array of structures A, save his side and two end points; n-
enter;
array A was brought to large order; logN
create arrays each B is set equal to themselves and, as an element of each set;
traversing the side a, if the operation is not bilateral in the same set, or combined two sets, the output side; n-
(ii) Prim's algorithm:
establishing a two-dimensional FIG array a is stored;
enter;
established B array set all bits to 0; n
traversing the first side of the connection point, and the minimum mark this point in the array and the point B 1 is set to 1, the output side; <n
cycles {
the If (B are all 1) BREAK;
through all points labeled 1, 0 point marked recently connected with them, which is set to 1, the output side;
}; <n-n-* * (n-1-)

Analysis
In few cases kruskal side should be higher than the efficiency of prim

Source
[GitHub Source Address]
No Git;

kruskal:

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

struct edge
{
    int a, b, len;
};
int cmp(const void *a, const void *b)
{
    int c = (*(edge *)a).len, d = (*(edge *)b).len;
    return  c-d;
}
  int B[10];
int find(int a){
    return B[a] == a ? a : B[a]=find(B[a]);
}

int main()
{
    ifstream in;
    in.open("input.txt");
    int m, n;
    in >> m >> n;
  
    struct edge A[15];

    //初始化数组
    for (int i = 0; i < m; i++)
    {
        B[i] = i;
    }

    //存图
    for (int i = 0; i < n; i++)
    {
        char a, b;
        int c;
        in >> a >> b >> c;
        a -= 'a';
        b -= 'a';
        A[i].a = a;
        A[i].b = b;
        A[i].len = c;
    }
    qsort(A, n, sizeof(A[0]), cmp);


    for (int i = 0; i < n; i++)
    {
        if (find(A[i].a)!=find(A[i].b)){
            B[find(A[i].b)] = find(A[i].a);
            cout << (char)(A[i].b+'a') << "--" << (char)(A[i].a+'a') << "   " << A[i].len << endl;

        }
        
    }
    in.close();
}

prim:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    ifstream in;
    in.open("input.txt");
    int A[10][10], m, n, B[10];
    in >> m >> n;
    //初始化数组
    for (int i = 0; i < m; i++)
    {
        B[i] = 0;
        for (int j = 0; j <= i; j++)
        {
            A[i][j] = A[j][i] = INT32_MAX;
        }
    }
    //存图
    for (int i = 0; i < n; i++)
    {
        char a, b;
        int c;
        in >> a >> b >> c;
        a -= 'a';
        b -= 'a';
        A[a][b] = A[b][a] = c;
    }

    //处理第一个点
    int date = INT32_MAX, flag = 0;
    for (int i = 0; i < m; i++)
    {
        if (A[0][i] < date)
        {
            flag = i;
            date = A[0][i];
        }
    }
    cout << 'a' << "--" << (char)(flag + 'a') << "   "<<date << endl;
    B[0] = B[flag] = 1;

    while (1)
    {
        date = INT32_MAX, flag = 0;
        int flag2 = 0;
        int break_date = 0;
        for (int i = 0; i < m; i++)
        {
            if (B[i] == 0)
                break_date = 1;
            else
            {
                for (int j = 0; j < m; j++)
                {
                    if (!B[j])
                    {
                        if (A[i][j] < date)
                        {
                            flag = i;
                            flag2 = j;
                            date = A[i][j];
                        }
                    }
                }
            }
        }
        if (!break_date)
            break;
        B[flag2] = 1;
        cout << (char)(flag + 'a') << "--" << (char)(flag2 + 'a') <<"   "<< date << endl;
    }

    in.close();
}
Released six original articles · won praise 6 · views 93

Guess you like

Origin blog.csdn.net/qq_45525352/article/details/104644830