Question

In: Computer Science

A graph consists of nodes and edges. An edge is an (unordered) pair of two distinct...

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

Solutions

Expert Solution

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


Related Solutions

A graph consists of nodes and edges. An edge is an (unordered) pair of two distinct...
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...
Given a connected graph G where edge costs are pair-wise distinct, prove or disprove that the...
Given a connected graph G where edge costs are pair-wise distinct, prove or disprove that the G has a unique MST. Please write Pseudo-code for the algorithms.
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges...
Consider an undirected graph G that has n distinct vertices. Assume n≥3. How many distinct edges will there be in any circuit for G that contains all the vertices in G? What is the maximum degree that any vertex in G can have? What is the maximum number of distinct edges G can have? What is the maximum number of distinct edges that G can have if G is disconnected?
Given a graph G = (V,E), the source-sink pair (s,t) and capacity of edges {C_e ≥...
Given a graph G = (V,E), the source-sink pair (s,t) and capacity of edges {C_e ≥ 0 | e ∈ E}, design a polynomial-time algorithm to find a set of edges S, such that for every edge e ∈ S, increasing C_e will lead to an increase of max-flow value between s and t. Show the correctness of your algorithm.
Instructions: Question 1: A directed graph G consists of • vertices (V ) (also called nodes)...
Instructions: Question 1: A directed graph G consists of • vertices (V ) (also called nodes) • edges (E) (also called links). An edge can have extra data. The number of nodes |V | and the number of edges |E| are denoted by n and m respectively. An undirected graph is like a directed graph except the edges do not have direction. Some more definitions: • An undirected graph is connected if there exists a path between all u, v...
Null graph,Nn, n=1,2,3,4...,the graph with n vertices and no edges. (N4=4 vertices with no edges) 4...
Null graph,Nn, n=1,2,3,4...,the graph with n vertices and no edges. (N4=4 vertices with no edges) 4 a) find a graph with 8 vertices with no 3-cycles and no induced sub graph isomorphic to N4 b)prove that every simple graph with 9 vertices with no 3-cycles has an induced sub graph isomorphic to N4
Prove that thickness of k17-{one edge} is 3 or 4? k17-{one edge} has 135 edges
Prove that thickness of k17-{one edge} is 3 or 4? k17-{one edge} has 135 edges
modify Graph to disallow parallel edges and self loops.
modify Graph to disallow parallel edges and self loops.
Determine the edge-connectivity of the petersen graph
Determine the edge-connectivity of the petersen graph
A chorded cycle in a graph is a cycle in the graph with one additional edge...
A chorded cycle in a graph is a cycle in the graph with one additional edge connecting two of the cycle vertices. Prove that every graph with minimum degree 3 contains a chorded cycle as a subgraph. (Hint: Consider a longest path in the graph. What does it tell you when a vertex is the end of a longest path? )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT