简体   繁体   English

在删除图中连接到源顶点的边时获得额外的边

[英]getting extra edge when removing the edges connected to the source vertex in a graph

last week i posted a code to calculate the shortest path in a graph using Dijkastra algorithm but it was very long and nobody was interesting in reading it completely so i deleted it, and now i am trying to simplify the code by going part by part, the code isn't complete yet, and i will cut part of the code here to focus on the first problem that i am facing so far. 上周,我发布了一个代码,该代码使用Dijkastra算法计算图形中的最短路径,但是它很长,没有人对完全阅读它感兴趣,因此我删除了它,现在我试图通过逐部分简化代码,该代码尚未完成,我将在此处剪切部分代码,以关注到目前为止我面临的第一个问题。

briefly i have a class Graph it is going to be constructed by two other classes a vector of elements are instances of a class Edge , and another vector of elements of class Vertex , every vertex has an id , and every edge has two vertices and weight . 简要地说,我有一个Graph类,它将由另外两个类构成,一个元素的向量是Edge类的实例,另一个Vertex类的元素向量,每个顶点都有一个id,每个边都有两个顶点和权重。

class Graph has a method its name is shortest takes two vertices as arguments the first one for the source of the graph and the second is for the destination. Graph类有一个方法,其名称最短,它有两个顶点作为参数,第一个顶点是图形的源,第二个顶点是目标。

So far i am trying to eliminate the edges that are connected to the source vertex , but i am getting an extra edge still in the vector edges it is connected to the source while all the other edges related to the source are removed. 到目前为止,我试图消除连接到源顶点的边,但是我仍然在连接到源的矢量边中保留了额外的边,而所有其他与源相关的边都被删除了。

to demonstrate the result , i initialized a graph has five vertices vers[0], vers[1], vers[2], vers[3], vers[4], and there are 10 edges connecting those vertices starting from eds[0], eds[1], ....eds[9]. 为了演示结果,我初始化了一个图,它具有五个顶点vers [0],vers [1],vers [2],vers [3],vers [4],并且有10条边从eds [0开始连接这些顶点],eds [1],.... eds [9]。

the source vertex is vers[2] is connected by 4 edges , so when applying the method shortest as it is shown in the code below i should get rid of all those 4 edges and end with 6 edges , but the result was that i got rid of 3 edges and i have 7 edges remained, the result is as follows: 源顶点是vers [2],由4条边连接,因此,如下面的代码所示,应用最短方法时,我应去除所有4条边并以6条边结束,但结果是我得到了除去3条边线,我剩下7条边线,结果如下:

Hello, This is a graph
0____1     5
0____3     4
0____4     6
1____3     5
1____4     7
2____4     8
3____4     3
size of edges 7
size of vertices 5

as you can notice , there still an edge connected to the source which is 2 , the problem is in this edge (by the way 8 is the weight of the edge) 如您所注意到的,仍然有一个连接到源的边为2,问题出在这条边上(顺便说一下,8是边的权重)

2____4     8

there is something wrong in the method shortest and specifically in the for loop , i hope you can help in finding my mistake. 最短的方法特别是for循环中存在问题,希望您能帮助发现我的错误。

Thanks in advance. 提前致谢。 Here is the code 这是代码

#include<iostream>
#include<vector>
#include <stdlib.h>   // for rand()
using namespace std;

const unsigned int N = 5;

class Vertex
{
 private:
     unsigned int id;                 // the name of the vertex  
public:   
    unsigned int get_id(){return id;};
    void set_id(unsigned int value) {id = value;};

    Vertex(unsigned int init_val = 0) :id (init_val){}      // constructor   
    ~Vertex() {};                                           // destructor
};


class Edge
{
  private:
    Vertex first_vertex;                 // a vertex on one side of the edge
    Vertex second_vertex;                // a vertex on the other side of the edge
    unsigned int weight;                 // the value of the edge ( or its weight )     
  public:   
    unsigned int get_weight() {return weight;};
    void set_weight(unsigned int value) {weight = value;};

    Vertex get_ver_1(){return first_vertex;};
    Vertex get_ver_2(){return second_vertex;};

    void set_first_vertex(Vertex v1) {first_vertex = v1;};
    void set_second_vertex(Vertex v2) {second_vertex = v2;};


    Edge(const Vertex& vertex_1 = 0, const Vertex& vertex_2 = 0, unsigned int init_weight = 0)
    : first_vertex(vertex_1), second_vertex(vertex_2), weight(init_weight)
     {
     }  


    ~Edge() {} ; // destructor

};


class Graph
{
private:
     std::vector<Vertex>   vertices;
     std::vector<Edge>   edges;  


public:
     Graph(vector<Vertex> ver_vector, vector<Edge> edg_vector)
    : vertices(ver_vector), edges(edg_vector){}

     ~Graph() {}

     vector<Vertex> get_vertices(){return vertices;}
     vector<Edge> get_edges(){return edges;}

     void set_vertices(vector<Vertex> vector_value) {vertices = vector_value;}
     void set_edges(vector<Edge> vector_ed_value) {edges = vector_ed_value;}

     unsigned int shortest(Vertex src, Vertex dis);


};



unsigned int Graph::shortest(Vertex src, Vertex dis) {
        vector<Vertex> ver_out;
        vector<Edge> track;

        for(unsigned int i = 0; i < edges.size(); ++i)
            {
                if((edges[i].get_ver_1().get_id() == src.get_id()) || (edges[i].get_ver_2().get_id() == src.get_id()))
                    {
                    track.push_back (edges[i]);

                    if(edges[i].get_ver_1().get_id() == src.get_id())
                        {ver_out.push_back (edges[i].get_ver_1());}
                    else
                        {ver_out.push_back (edges[i].get_ver_2());}

                    edges.erase(edges.begin() + i ); //****
                    }
            };


    }





int main()
{
cout<< "Hello, This is a graph"<< endl;

vector<Vertex> vers(5);
vers[0].set_id(0);
vers[1].set_id(1);
vers[2].set_id(2);
vers[3].set_id(3);
vers[4].set_id(4);

vector<Edge> eds(10);
eds[0].set_first_vertex(vers[0]);
eds[0].set_second_vertex(vers[1]);
eds[0].set_weight(5);   

eds[1].set_first_vertex(vers[0]);
eds[1].set_second_vertex(vers[2]);
eds[1].set_weight(9);

eds[2].set_first_vertex(vers[0]);
eds[2].set_second_vertex(vers[3]);
eds[2].set_weight(4);

eds[3].set_first_vertex(vers[0]);
eds[3].set_second_vertex(vers[4]);
eds[3].set_weight(6);

eds[4].set_first_vertex(vers[1]);
eds[4].set_second_vertex(vers[2]);
eds[4].set_weight(2);

eds[5].set_first_vertex(vers[1]);
eds[5].set_second_vertex(vers[3]);
eds[5].set_weight(5);

eds[6].set_first_vertex(vers[1]);
eds[6].set_second_vertex(vers[4]);
eds[6].set_weight(7);

eds[7].set_first_vertex(vers[2]);
eds[7].set_second_vertex(vers[3]);
eds[7].set_weight(1);

eds[8].set_first_vertex(vers[2]);
eds[8].set_second_vertex(vers[4]);
eds[8].set_weight(8);

eds[9].set_first_vertex(vers[3]);
eds[9].set_second_vertex(vers[4]);
eds[9].set_weight(3);


unsigned int path;

Graph graf(vers, eds);
path = graf.shortest(vers[2], vers[4]);


cout<<graf.get_edges()[0].get_ver_1().get_id() <<"____"<<graf.get_edges()[0].get_ver_2().get_id() <<"     "<<graf.get_edges()[0].get_weight()<< endl;  //test
cout<<graf.get_edges()[1].get_ver_1().get_id() <<"____"<<graf.get_edges()[1].get_ver_2().get_id() <<"     "<<graf.get_edges()[1].get_weight()<< endl;  //test
cout<<graf.get_edges()[2].get_ver_1().get_id() <<"____"<<graf.get_edges()[2].get_ver_2().get_id() <<"     "<<graf.get_edges()[2].get_weight()<< endl;  //test
cout<<graf.get_edges()[3].get_ver_1().get_id() <<"____"<<graf.get_edges()[3].get_ver_2().get_id() <<"     "<<graf.get_edges()[3].get_weight()<< endl;  //test
cout<<graf.get_edges()[4].get_ver_1().get_id() <<"____"<<graf.get_edges()[4].get_ver_2().get_id() <<"     "<<graf.get_edges()[4].get_weight()<< endl;  //test
cout<<graf.get_edges()[5].get_ver_1().get_id() <<"____"<<graf.get_edges()[5].get_ver_2().get_id() <<"     "<<graf.get_edges()[5].get_weight()<< endl;  //test
cout<<graf.get_edges()[6].get_ver_1().get_id() <<"____"<<graf.get_edges()[6].get_ver_2().get_id() <<"     "<<graf.get_edges()[6].get_weight()<< endl;  //test
//cout<<graf.get_edges()[7].get_ver_1().get_id() <<"____"<<graf.get_edges()[7].get_ver_2().get_id() <<"     "<<graf.get_edges()[7].get_weight()<< endl;  //test
//cout<<graf.get_edges()[8].get_ver_1().get_id() <<"____"<<graf.get_edges()[8].get_ver_2().get_id() <<"     "<<graf.get_edges()[8].get_weight()<< endl;  //test
//cout<<graf.get_edges()[9].get_ver_1().get_id() <<"____"<<graf.get_edges()[9].get_ver_2().get_id() <<"     "<<graf.get_edges()[9].get_weight()<< endl;  //test


cout<<"size of edges"<<graf.get_edges().size()<< endl;
cout<<"size of vertices"<<graf.get_vertices().size()<< endl;

return 0;
}

This is because you are effectively skipping some vector elements in your Graph::shortest for loop because you are incrementing i even when you erase current element. 这是因为您有效地跳过了Graph::shortest for循环中的某些矢量元素,因为即使erase当前元素也要增加i Change it to something like this to fix the problem: 将其更改为如下所示即可解决问题:

for (unsigned int i = 0; i < edges.size();) { // no ++i here
    if ((edges[i].get_ver_1().get_id() == src.get_id()) || (edges[i].get_ver_2().get_id() == src.get_id())) {
        track.push_back(edges[i]);

        if (edges[i].get_ver_1().get_id() == src.get_id()) {
            ver_out.push_back(edges[i].get_ver_1());
        } else {
            ver_out.push_back(edges[i].get_ver_2());
        }

        edges.erase(edges.begin() + i);
    } else {
        ++i; // increment only if not erasing
    }
}

Alternatively as per comment, using iterators: 或者根据注释,使用迭代器:

for (auto i = edges.begin(); i != edges.end();) {
    if ((i->get_ver_1().get_id() == src.get_id()) || (i->get_ver_2().get_id() == src.get_id())) {
        track.push_back(*i);

        if (i->get_ver_1().get_id() == src.get_id()) {
            ver_out.push_back(i->get_ver_1());
        } else {
            ver_out.push_back(i->get_ver_2());
        }

        i = edges.erase(i);
    } else {
        i++;
    }
}

You are also missing a return statement in that function. 您还缺少该函数中的return语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM