微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我需要删除重复的顶点邻接列表

如何解决我需要删除重复的顶点邻接列表

文件 test.txt

  • 6(这是顶点数)

  • 1 4 (Еhe 第一个是顶点的编号,第二个是正在构造边的顶点的编号。

  • 1 6

  • 2 1

  • 2 3

  • 2 4

  • 2 5

  • 3 2

  • 3 5

  • 4 1

  • 4 2

  • 4 5

  • 5 2

  • 5 3

  • 5 4

  • 6 1

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <list>
    
    using namespace std;
    
    class edge
    {
    private:
    int node2id,nodeid;
    public:
    edge(int id,int id2)
    {
        node2id = id2;
        nodeid = id;
    
    }
    
    int getnodeid()
    {
        return nodeid;
    }
    int getnodeid2()
    {
        return node2id;
    }
    
    };
    
    int main()
    { 
    int totalnode,node1,node2;
    ifstream input("test.txt");
    input >> totalnode;
    vector<list<edge>>adjList(totalnode);
    while (input >> node1 >> node2)
    {
        adjList[node1 - 1].push_back(edge(node2,node1));
    }
    
    int c = 1;
    vector<list<edge>>::iterator i;
    for (i = adjList.begin(); i != adjList.end(); i++)
    {
        cout << c << " -- ";
        list<edge>  li = *i;
        list<edge>::iterator iter;
        for (iter = li.begin(); iter != li.end(); iter++)
        {
            cout << "[" << (*iter).getnodeid() << "] ";
        }
        cout << endl;
        c++;
    }
    
    while (input >> node1 >> node2)
    {
        adjList[node1 - 1].push_back(edge(node1,node1));
    }
    
    cout << "\n";
    

在这部分中,创建了新顶点,但我不知道如何删除相同的顶点。

vector<list<edge>>::iterator j;

for (j = adjList.begin(); j != adjList.end(); j++)
{


    //cout << c << " -- ";
    list<edge>  li = *j;
    list<edge>::iterator iter;
    for (iter = li.begin(); iter != li.end(); iter++)
    {

        cout << c <<" -- " << "[" << (*iter).getnodeid2() << "] " << "[" << (*iter).getnodeid() << "]\n";
        c++;

    }


}
return 0;
}

输出

将创建邻接表


1 -- [2] [4] [6]
2 -- [1] [3] [4] [5]
3 -- [2] [5]
4 -- [1] [2] [5]
5 -- [2] [3] [4]
6 -- [1]

这些顶点已创建,但例如,顶点 7 和 10(等等)具有相同的连接。并且您需要确保没有相同的顶点。

7 -- [1] [2]
8 -- [1] [4]
9 -- [1] [6]
10 -- [2] [1]
11 -- [2] [3]
12 -- [2] [4]
13 -- [2] [5]
14 -- [3] [2]
15 -- [3] [5]
16 -- [4] [1]
17 -- [4] [2]
18 -- [4] [5]
19 -- [5] [2]
20 -- [5] [3]
21 -- [5] [4]
22 -- [6] [1]

 

解决方法

根据您目前的情况,您似乎并不真正需要 edge 类。所有信息都可以存储在 int 中。


为了解决您想要的问题,您可以首先将所有边存储在 std::vector<std::set<int> 中,例如:

while (input >> node1 >> node2)
{
    adjList[node1 - 1].insert(node2);
}

通过使用集合,所有边都以数字升序存储。所以,而不是:

10 -- [2] [1]

你会:

10 -- [1] [2]

现在您可以将向量解析为 std::map,例如:

std::map<std::set<int>,int> tempMap;

for(auto i = 0; i < totalnode; ++i)
{
    s.try_emplace(adjList[i],i);
}

这将自动摆脱已经有另一个节点具有相同相邻节点集的节点(如您在问题中所述)。

不过,我只是将其称为 tempMap,因为目前,它们是按相邻位置排序的,而不是按名称排序。

所以你可以做的是:

std::map<int,std::set<int>> adjMap;

for(auto& [key,value] : tempMap)
{
    adjMap.emplace(value,key);
}

现在你可以像这样遍历整个地图:

for(auto& [node,edges] : adjMap
{
    std::cout << node << " -- ";
    for(auto& edge: edges)
    {
        std::cout << "[" << edge << "]";
    }
    std::cout << "\n";
}

旁注,我注意到您编写了如下代码:

vector<list<edge>>::iterator i;
for (i = adjList.begin(); i != adjList.end(); i++) { // your loop }

相反,您可以使用:

for(auto it = adjList.begin(); it != adjList.end(); ++it) { // your loop }
    ^^^^

它会自动为 i 分配适合该容器的迭代器类型。

甚至在很多情况下:

for(auto& item : adjList) { // your loop }

您可以在循环中使用 item 的方式与前面示例中的 *i 相同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?