In: Computer Science
A graph consists of nodes and edges. An edge is an (unordered) pair of two distinct nodes in the graph. We create a new empty graph from the class Graph. We use the add_node method to add a single node and the add_nodes method to add multiple nodes. Nodes are identified by unique symbols. We call add_edge with two nodes to add an edge between a pair of nodes belonging to the graph. We can also ask a graph for the number of nodes and edges it contains, and for a list of its nodes and edges. The to_s method returns a string representing the graph's adjacency lists. Methods should not change the graph if called with invalid arguments; e.g., adding an edge that is already in the graph or that references a node that does not already belong to the graph. Your code does not have to generate the exact transcript that follows but it should provide this basic functionality. >> g = Graph.new
=> <Graph: 0, 0>
>> g.add_node(:a)
=> a
>> g.add_nodes([:b, :c])
=> [:b, :c]
>> g
=> <Graph: 3, 0>
=> >> g.get_nodes
=> [:a, :b, :c]
>> g.nbr_nodes
=> 3
>> g.add_edge(:a, :b)
=> [a, b]
>> g.add_edge(:b, :c)
=> [b, c]
>> g
=> <Graph: 3, 2>
>> g.get_edges
=> [[a, b], [b, c]]
>> g.nbr_edges
=> 2
>> puts g
a -> b
b -> a,c
c -> b
=> nil
please i need the code in Ruby
Facing a lot of issues in Ruby.
Here's the VB
Option Infer On
Imports System
Imports System.Collections.Generic
Imports Microsoft.VisualBasic
Public Class Graph 'Assuming a directed graph
Private edges As New
SortedDictionary(Of Char,List(Of Char))() 'A map to maintain the
edges
Private nodes As New SortedSet(Of
Char)() 'A set to maintain nodes, avoiding duplicacy
Private edge_count As Integer
'variable to keep track of number of edges
Public Sub add_node(ByVal node As
Char)
nodes.Add(node) 'Inserting node to set
End Sub
Public Sub add_nodes(ByVal vNodes
As List(Of Char))
For Each node As Char In vNodes
nodes.Add(node)
Next node
End Sub
Public Function add_edge(ByVal u As
Char, ByVal v As Char) As Boolean
'Checking if both the entered nodes are valid
and exists in the set of nodes
If nodes.find(u)=nodes.end() OrElse
nodes.find(v)=nodes.end() Then
Return
False
End If
edges(u).Add(v)
edge_count += 1
Return True
End Function
Public Function nbr_nodes() As
Integer
Return CInt(nodes.Count)
End Function
Public Function nbr_edges() As
Integer
Return edge_count
End Function
Public Function get_nodes() As
String
Dim stringOfNodes As String
For Each node As Char In nodes
stringOfNodes.push_back(node) 'Generating string representation of
the set of nodes
stringOfNodes &= ","
Next node
stringOfNodes.pop_back() 'Popping the extra ,
appened at the end
Return stringOfNodes
End Function
Public Function get_edges() As
String
Dim stringOfEdges As String
For Each it In edges 'Iterating through each
entry of edges map [Key]
For Each
adj In 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 &=
"],"
Next
adj
Next it
stringOfEdges.pop_back()
Return stringOfEdges
End Function
Public Function to_s() As
String
'Generating a string representation of the
adjacency lists
Dim adjacencyString As String
For Each it In edges
adjacencyString.push_back(it.first)
adjacencyString &= " -> "
For Each
adj In it.second
adjacencyString.push_back(adj)
adjacencyString &=
","
Next
adj
adjacencyString.pop_back()
adjacencyString &= vbLf
Next it
Return adjacencyString
End Function
End Class
Public Class GlobalMembers
'Code for testing
Shared Function Main() As Integer
Dim g As New
Graph()
g.add_node("a"c)
g.add_nodes(New
List(Of Char)() From {"b"c, "c"c})
Console.Write(g.get_nodes())
Console.Write(vbLf)
Console.Write(g.nbr_nodes())
Console.Write(vbLf)
g.add_edge("a"c,
"b"c)
g.add_edge("b"c,
"c"c)
Console.Write(g.get_edges())
Console.Write(vbLf)
Console.Write(g.nbr_edges())
Console.Write(vbLf)
Console.Write(g.to_s())
End Function
End Class