當前位置:網站首頁>亞線性的近似最小支撐樹

亞線性的近似最小支撐樹

2022-01-28 07:52:05 叫我老伯

問題

在這裏插入圖片描述
自己給出ϵ的值並帶入計算

算法過程

在這裏插入圖片描述

代碼

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <queue>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define QueueSize 1000

class Env
{
    
public:
    vector<vector<int>> graph;
    Env()
    {
    
        graph = getGraph("rand2_2.csv"); //rand1_1.csv rand1_2.csv rand1_3.csv rand2_1.csv rand2_2.csv 共5個測試用例
    }

    vector<vector<int>> getGraph(string file_path)
    {
    
        vector<vector<int>> user_arr;
        ifstream fp(file_path);
        string line;
        while (getline(fp, line))
        {
    
            vector<int> data_line;
            string number;
            istringstream readstr(line);
            for (int j = 0; j < 33; j++)
            {
    
                getline(readstr, number, ',');
                data_line.push_back(atoi(number.c_str()));
            }
            user_arr.push_back(data_line);
        }
        return user_arr;
    }
};

// 所有的代碼都需要寫在 Algo 類內,自行定義其他需要的變量或函數
class Algo
{
    
public:
    Algo()
    {
    
        cout << "algo ok" << endl;
    }

    typedef struct
    {
    
        int front;
        int rear;
        int count;
        int data[QueueSize];
    } Queue;

    void InitQueue(Queue *M)
    {
    
        M->front = M->rear = 0;
        M->count = 0;
    }

    int QueueEmpty(Queue *M) 
    {
    
        if (M->count == 0)
            return 1;
        else
            return 0;
    }

    void EnQueue(Queue *M, int x) 
    {
    
        M->count++;
        M->data[M->rear] = x;
        M->rear = (M->rear + 1) % QueueSize;
    }

    int DeQueue(Queue *M) 
    {
    
        int temp;
        temp = M->data[M->front];
        M->count--;
        M->front = (M->front + 1) % QueueSize;
        return temp;
    }
    double cnum(vector<vector<int>> graph)
    {
    
        Queue M;
        InitQueue(&M);
        double a = 0.1;
        double b[1000];
        double n;
        n = 2 / a;
        srand((unsigned)time(NULL));
        for (int k = 1; k < 61; k++) //取樣60個數
        {
    
            InitQueue(&M);
            int visited[33];
            for (int m = 0; m < 33; m++)
                visited[m] = 0;
            int i = rand() % 33;
            visited[i] = 1;
            EnQueue(&M, i);
            double t = 1;
            while (!QueueEmpty(&M))
            {
    
                i = DeQueue(&M);
                for (int j = 0; j < 33; j++)
                {
    
                    if (double(j) <= double(n))
                    {
    
                        if (graph[i][j] > 0 && visited[j] == 0)
                        {
    
                            t = double(t + 1);
                            visited[j] = 1;
                            EnQueue(&M, j);
                        }
                    }
                }
            }

            if (t != 0)
            {
    
                b[k] = double(1 / t);
            }
            else
            {
    
                b[k] = 0;
            }
        }
        double sum;
        for (int k = 1; k < 61; k++)
        {
    
            sum = b[k] + sum;
        }
        sum = sum / 60 * 33;
        cout << "連通分量:" << sum << endl;
        return sum;
    }
    int run(Env env)
    {
    
        //在此寫入你的代碼 env.graph 為目標矩陣
        int w = 0;
        for (int i = 1; i < 33; i++)
        {
    
            for (int j = 0; j < 33; j++)
            {
    
                if (w < env.graph[i][j])
                {
    
                    w = env.graph[i][j];
                }
            }
        }
        cout << "最大權重:" << w << endl;
        double sums, W;
        double n = 33;
        for (int n = 0; n < w-1; n++)
        {
    
            for (int i = 0; i < 33; i++)
            {
    
                for (int j = 0; j < 33; j++)
                {
    
                    if (env.graph[i][j] = w - n)
                    {
    
                        env.graph[i][j] == 0;
                    }
                }
            }
            sums = double(sums + cnum(env.graph));
        }
        cout <<"連通分量總和"<< sums << endl;
        W = double(n - w + sums);
        cout << "最優解:" << W << endl;

        return 0; //修改為返回正確的結果
    }
};

int main()
{
    
    Algo algo;
    Env env;
    time_t start = clock();
    algo.run(env);
    time_t end = clock();
    cout << "程序耗時: " << double(end - start) / CLOCKS_PER_SEC << " ms" << endl;
    cout << "占用內存:" << sizeof(algo) << " byte" << endl;
    return 0;
}

結果

在這裏插入圖片描述

版權聲明
本文為[叫我老伯]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201280752052695.html

隨機推薦