Question: //C++ implementation for the problem#include#include #include#includeusing namespace std;class Graph{ //Assuming a directed graphprivate: map> edges; //A map to maintain the edges set nodes; //A set

//C++ implementation for the problem#include#include#include#includeusing namespace std;class Graph{                                            //Assuming a directed graphprivate:        map> edges;       //A map to maintain the edges        set nodes;                          //A set to maintain nodes, avoiding duplicacy        int edge_count;                                 //variable to keep track of number of edgespublic:        void add_node(char node) {                nodes.insert(node);                     //Inserting node to set        }        void add_nodes(vector vNodes) {                for(char node: vNodes)                        nodes.insert(node);        }        bool add_edge(char u, char v) {                //Checking if both the entered nodes are valid and exists in the set of nodes                if(nodes.find(u)==nodes.end() || nodes.find(v)==nodes.end())                        return false;                edges[u].push_back(v);                edge_count++;                return true;        }        int nbr_nodes() {                return (int)nodes.size();        }        int nbr_edges() {                return edge_count;        }        string get_nodes() {                string stringOfNodes;                for(char node: nodes)                {                        stringOfNodes.push_back(node);  //Generating string representation of the set of nodes                         stringOfNodes += ",";                }                stringOfNodes.pop_back();               //Popping the extra , appened at the end                return stringOfNodes;        }        string get_edges() {                string stringOfEdges;                for(auto it: edges) {                   //Iterating through each entry of edges map [Key]                        for(auto adj: it.second) {      //Iterating through the vector associated with each entry of the map [Value]                                stringOfEdges += "[";                                stringOfEdges.push_back(it.first);                                stringOfEdges += ",";                                stringOfEdges.push_back(adj);                                stringOfEdges += "],";                        }                }                stringOfEdges.pop_back();                return stringOfEdges;        }        string to_s() {                //Generating a string representation of the adjacency lists                string adjacencyString;                for(auto it: edges) {                        adjacencyString.push_back(it.first);                        adjacencyString += " -> ";                        for(auto adj: it.second) {                                adjacencyString.push_back(adj);                                adjacencyString += ",";                        }                        adjacencyString.pop_back();                        adjacencyString += "";                }                return adjacencyString;        }};//Code for testingint main() {        Graph g = Graph();        g.add_node('a');        g.add_nodes({'b','c'});        cout<

Please re-write the above code using Ruby language !

Step by Step Solution

3.47 Rating (150 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!