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
=> 
>> g.add_node(:a)
=> a

>> g.add_nodes([:b, :c]) => [:b, :c]
>> g
=>

>> 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
=> 
>> g.get_edges
=> [[a, b], [b, c]]
>> g.nbr_edges
=> 2
>> puts g
a -> b
b -> a,c
c -> b
=> nil

please I want the code in( Ruby) not another language

Solutions

Expert Solution

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...
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? )
Construct a connected undirected graph with 25 or more nodes. - Draw your graph in a...
Construct a connected undirected graph with 25 or more nodes. - Draw your graph in a diagram. - Draw the adjacency matrix of your graph. - Draw the adjacency list of your graph. - Run the BFS algorithm on your graph and complete the chart discussed in class. Detailed steps, including the changes of node color, predecessor, etc., as discussed in class, are required.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT