简体   繁体   中英

add and subtract the edges in the graph through the list of adjacent vertices

Implementation of the graph through the adjacent vertices Good day. I have a task-write function for the graph through the adjacent add edge Remove the edge

But I do not know how to implement it. Need your help.

 struct Edge 
 {
   int mV; 
   int mW;
   float mWeight;
 };

 struct Node
 { 
  int mEnd; 
  float mWeight; 
 };

 using AdjacencyList = std::vector<Node>;
 using VertexList = std::vector<AdjacencyList>;
 class Graph
 {
   public:
   bool addEdge(const Edge& edge);
   bool removeEdge(const Edge& edge);
   private:
    VertexList mVertexList;
 };

 bool Graph::addEdge(const Edge& edge)
 {
if ((mAdjacencyLists[edge.mV].mEnd == true) && (mAdjacencyLists[edge.mW].mEnd == true) 
    && (mAdjacencyLists[edge.mV].mWeight == false) && (mAdjacencyLists[edge.mW].mEnd == false) && (edge.mV != edge.mW))
{
    Node node;
    mAdjacencyLists[edge.mV] = node.mEnd; // ???
    mAdjacencyLists[edge.mW] = node.mWeight; //???

}
}

 bool Graph::removeEdge(const Edge& edge)
 {
  if ((mAdjacencyLists[edge.mV].mEnd == true) && (mAdjacencyLists    [edge.mW].mEnd == true) && (mAdjacencyLists[edge.mV].mWeight == true) 
    && (mAdjacencyLists[edge.mW].mEnd == true) && (edge.mV != edge.mW))
   {
    // ???

    }

}

UPD(rewritten the code):

 bool Graph::addEdge(const Edge& edge)
 {
  mVertexList[edge.mV].push_back({ edge.mW, edge.mWeight });
  mVertexList[edge.mW].push_back({ edge.mV, edge.mWeight });
 }

 bool Graph::removeEdge(const Edge& edge)
 {
   auto ita = find_if(mVertexList[edge.mV].cbegin(), mVertexList  [edge.mV].cend(), [edge.mW](const Node& n) { return n.mEnd == edge.mW; });
   mVertexList[edge.mV].erase(ita);
   auto itb = find_if(mVertexList[edge.mW].cbegin(), mVertexList[edge.mW].cend(), [edge.mV](const Node& n) { return n.mEnd == edge.mV; });
   mVertexList[edge.mW].erase(itb);
 }

In this example i expect that you know number of vertices in the graph in forward.

class G {
    struct Neighbour{
        int _end;
        int _weight;
    };

    std::vector<std::list<Neighbour>> adj;

public:
    G(int verticesCount) : adj(verticesCount) {}

    void addEdge(int a, int b, int w) {
        assert(!hasEdge(a, b));
        adj[a].push_back({ b, w });
        adj[b].push_back({ a, w });
    }

    void dropEdge(int a, int b) {
        assert(hasEdge(a, b));
        auto ita = find_if(adj[a].cbegin(), adj[a].cend(), [b](const Neighbour& n) { return n._end == b; });
        adj[a].erase(ita);
        auto itb = find_if(adj[b].cbegin(), adj[b].cend(), [a](const Neighbour& n) { return n._end == a; });
        adj[b].erase(itb);
    }

    bool hasEdge(int a, int b) {
        auto it = find_if(adj[a].cbegin(), adj[a].cend(), [b](const Neighbour& n) { return n._end == b; });
        // here you might want to check if adjacency list for b also contains entry for the edge
        return it != adj[a].cend();
    }

    int edgeWeight(int a, int b) {
        auto it = find_if(adj[a].cbegin(), adj[a].cend(), [b](const Neighbour& n) { return n._end == b; });
        // the same as in hasEdge, some consistency check might be needed
        return it->_weight;
    }
};

void testG() {
    G g(4);

    g.addEdge(0, 1, 10);
    g.addEdge(1, 2, 20);
    g.addEdge(2, 3, 30);

    cout << boolalpha;
    cout << g.hasEdge(0, 1) << " w = " << g.edgeWeight(0, 1) << endl;
    cout << g.hasEdge(1, 2) << " w = " << g.edgeWeight(1, 2) << endl;
    cout << g.hasEdge(2, 3) << " w = " << g.edgeWeight(2, 3) << endl;
    g.dropEdge(1, 2);
    cout << g.hasEdge(1, 2) << endl;
}


int main() {
    testG();
    system("pause");
    return 0;
}

true w = 10
true w = 20
true w = 30
false

Storing graph with adjacency list representation leads to some information duplication, so it is good to have consistency checks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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