Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Differential Equations and Linear Algebra

Authors: Jerry Farlow, James E. Hall, Jean Marie McDill, Beverly H. West

2nd edition

131860615, 978-0131860612

More Books

Students also viewed these Programming questions

Question

Define the four types of balance of payments measures.

Answered: 1 week ago

Question

1. Give them prompts, cues, and time to answer.

Answered: 1 week ago

Question

Write a paper about medication error system 2016.

Answered: 1 week ago

Question

=+a. Describe, in words, the event A and B.

Answered: 1 week ago

Question

=+b. Describe, in words, the event A or B.

Answered: 1 week ago