具有unordered_map的二元图匹配

时间:2018-12-09 07:44:20

标签: c++

对于bigraph最大匹配算法,我使用2个哈希表(unordered_map)来解决它,但是当我编译代码时,IDE告诉我C2100,我什至不知道我的代码在哪里是假的,它只是在“ xhash”文档,我认为此问题与迭代器有关。无论如何,请帮助我解决这个问题,谢谢!

#include<iostream>
#include<queue>
#include<unordered_map>
#include<vector>
using namespace std;
struct Edge
{
    int to;
    int go;
    int length;
    Edge* next;
    int ID;
};
struct Dot
{
    int data;
    Edge* first;
};
class Web
{
public:
    Web(int num);
    ~Web();
    void Insert(int i, int j, int w);
    void DFS(int s, int* visited,queue<int>&);
    void BFS(int s, int* visited);
    void show()
    {
        for (int i = 0; i<n; i++)
        {
            Edge* p = D[i].first;
            if (p != NULL)
            {
                cout << p->length << " ";
                p = p->next;
            }
        }
    }
    void MostMatch();
private:
    int e;
    int n;
    Dot* D;
    vector<Edge*> E;
};
Web::Web(int num)
{
    e = 0;
    n = num;
    D = new Dot[num];
    for (int i = 0; i<num; i++)
    {
        D[i].first = NULL;
        D[i].data = 0;
    }
}
Web::~Web()
{
    delete[] D;
}
void Web::Insert(int i, int j, int w)
{
    Edge* p = D[i].first;
    int count = 0;
    if (D[i].first == NULL)
    {
        D[i].first = new Edge;
        D[i].first->next = NULL;
        D[i].first->length = w;
        D[i].first->to = j;
        D[i].first->go = i;
        D[i].first->ID = count++;
        E.push_back(D[i].first);
    }
    else
    {
        while (p->next != NULL)
            p = p->next;
        Edge* q = new Edge;
        p->next = q;
        q->next = NULL;
        q->length = w;
        q->go = i;
        q->to = j;
        q->ID = count++;
        E.push_back(q);
    }
    p = D[j].first;
    if (D[j].first == NULL)
    {
        D[j].first = new Edge;
        D[j].first->next = NULL;
        D[j].first->length = w;
        D[j].first->to = i;
        D[j].first->ID = count;
        e++;
    }
    else
    {
        while (p->next != NULL)
            p = p->next;
        Edge* q = new Edge;
        p->next = q;
        q->next = NULL;
        q->length = w;
        q->to = i;
        q->ID = count;
        e++;
    }
}
void Web::MostMatch()
{
    cout << "make sure it is a biggraph, press Q to quit." << endl;
    char ch;
    cin >> ch;
    if (ch == 'Q')
    {}
    else
    {
        unordered_map<int, int> mape; 
        unordered_map<int, int> mapd;
        int count = 0;
        for (int i = 0; i < n; i++) 
        {
            Edge* p;
            if (D[i].first)
            {
                p = D[i].first;
                if (mapd.find(i) == mapd.end())
                {
                    while (p)
                    {
                        if (mapd.find(p->to) == mapd.end())
                        {
                            mapd.insert(i,count++);
                            mapd.insert(p->to, count++);
                            mape.insert(p->ID, i);
                            break;
                        }
                    }
                }
            }
        }
        unordered_map<int, int>::iterator iter = mape.begin();
        while (iter != mape.end())
        {
            Edge* p = D[E[iter->first]->go].first, *q = D[E[iter->first]->to].first;
            while (p)
            {
                if (mapd.find(p->to) == mapd.end())
                {
                    while (q)
                    {
                        if (mapd.find(q->to) == mapd.end())
                        {
                            mapd.insert(p->to, count++);
                            mapd.insert(q->to, count++);
                            mape.insert(p->ID, count++);
                            mape.insert(q->ID, count++);
                            mape.erase(iter->first);
                            break;
                        }
                    }
                    break; 
                }
            }
            iter++;
        }
        for (iter = mape.begin(); iter != mape.end(); iter++)
        {
            cout << "v" << E[iter->first]->go << " to v" << E[iter->first]->to << " ID: " << E[iter->first]->ID << endl;
        }
    }
}
int main()
{
    Web w1(8);
    w1.Insert(0, 3, 1);
    w1.Insert(1, 4, 1);
    w1.Insert(1, 5, 1);
    w1.Insert(2, 5, 1);
    w1.Insert(2, 6, 1);
    w1.Insert(2, 7, 1);
    w1.Insert(3, 7, 1);
    w1.MostMatch();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

  

我什至不知道我的代码在哪里错误

这是最小化代码的好理由:https://stackoverflow.com/help/mcve

如果您的编译器没有告诉您错误消息/行(并且我确实确实会告诉您),请尝试使用另一种错误/行,它们很可能兼容。这个:https://www.onlinegdb.com/online_c++_compiler告诉我那行

mapd.insert(i,count++);

和类似的错误。以下列方式替换它们会编译:

mapd[i] = count++;