我在这里实施 Karger 的随机最小切割算法有什么问题?

问题描述

我对代码的解释如下-

  1. 函数 remove sl 最初删除图中的自循环。
  2. 函数 adjacency_list 以邻接表的形式读取输入文本文件并创建向量映射。映射中的每个键都充当节点,每个向量也是与其相邻的节点列表。

该算法在函数 mincut 中实现。

  1. 当节点数大于 2 时,它先选择一个节点,然后再选择另一个节点以有效地选择一条随机边。
  2. 如果与节点 2 相邻的所有节点不为零或节点 1 本身不为零,则将它们添加到节点 1 的列表中。
  3. node1 列表中 node2 的条目为零,因此也有效地消除了自循环。
  4. 任何其他等于 node2 的条目都被设置为等于 node1,因为该节点现在连接到 node1。
  5. Node2 已从地图中删除。
  6. 在 while 循环之后,第一个节点中的所有非零条目都被计算为最小切割。

我注意到的一个错误是在 mincut 第三步中没有反映在图中。否则我找不到我哪里出错了。

#include <iostream>
#include <vector>
#include <map>
#include <bits/stdc++.h>
#include <fstream>
#include <sstream>

using namespace std;

map <int,vector<int>> adjacency_list (string &file);
map <int,vector <int>> removesl (map <int,vector <int>> &graph);
int mincut (map <int,vector <int>> &graph);

int main ()
{
    string s = "graph.txt";
    int min = 100000000;
    map <int,vector<int>> graph = adjacency_list (s);
    graph = removesl(graph);
    for (int i = 1; i <= 1000; i++)
    {
        int min_cut = mincut (graph);
        if (min_cut < min)
            min = min_cut;
    }
    cout << min;
}


map <int,vector<int>> adjacency_list (string &file)
{
    map <int,vector <int>> graph;
    ifstream ifile (file);
    string line;
    int a,b;
    while(getline (ifile,line))
    {
        istringstream iss (line);
        iss >> a;
        graph [a] = vector<int>();
        while (iss >> b)
        {
            graph [a].push_back(b);
        }
    }
    return graph;
}

map <int,vector <int>> &graph)
{
    for (int i = 1; i <= graph.size(); i++)
    {
        for (int j = 0; j < graph[i].size(); j++)
        {
            if(graph[i][j] == i)
                graph[i][j] = 0;
        }
    }
    return graph;
}

int mincut (map <int,vector <int>> &graph)
{
    while (graph.size() > 2)
    {
        int node1 = (rand() % 200) + 1;
        if (graph.find(node1) == graph.end())
            continue;
        int size = graph [node1].size();
        int index = (rand() % size);
        int node2 = graph [node1][index];
        for (int i = 0; i < graph[node2].size(); i++)
        {
            if (graph[node2][i] != node1 && graph[node2][i] != 0)
                graph[node1].push_back(graph[node2][i]);
        }
        for (int i = 0; i < graph[node1].size(); i++)
        {
            if (graph[node1][i] == node2)
                graph[node1][i] = 0;
        }
        graph.erase(node2);
        for (auto i : graph)
        {
            for (int j = 0; j < i.second.size(); j++)
            {
                if (i.second[j] == node2)
                    i.second[j] = node1;
            }
        }
    }
    auto it = graph.begin();
    int count = 0;
    for (int i = 0; i < it -> second.size(); i++)
    {
        if (it -> second[i] != 0)
            count++;
    }
    return count;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...