Find all mother vertex of a directed graph in ruby
Ruby program for Find all mother vertex of a directed graph. Here problem description and explanation.
# Ruby program for
# Find all Mother Vertex in a Graph
class AjlistNode
# Define the accessor and reader of class AjlistNode
attr_reader :id, :next
attr_accessor :id, :next
# Vertices node key
def initialize(id)
# Set value of node key
self.id = id
self.next = nil
end
end
class Vertices
# Define the accessor and reader of class Vertices
attr_reader :data, :next, :last
attr_accessor :data, :next, :last
def initialize(data)
self.data = data
self.next = nil
self.last = nil
end
end
class Graph
# Define the accessor and reader of class Graph
attr_reader :size, :node
attr_accessor :size, :node
# Number of Vertices
def initialize(size)
# Set value
self.size = size
self.node = Array.new(size) {nil}
self.setData()
end
# Set initial node value
def setData()
if (self.size <= 0)
print("\nEmpty Graph\n")
else
index = 0
while (index < self.size)
# Set initial node value
self.node[index] = Vertices.new(index)
index += 1
end
end
end
# Handling the request of adding new edge
def addEdge(start, last)
if (start >= 0 && start < self.size &&
last >= 0 && last < self.size)
# Safe connection
edge = AjlistNode.new(last)
if (self.node[start].next == nil)
self.node[start].next = edge
else
# Add edge at the end
self.node[start].last.next = edge
end
# Get last edge
self.node[start].last = edge
else
# When invalid nodes
print("\nHere Something Wrong\n")
end
end
def printGraph()
if (self.size > 0)
index = 0
# Print graph ajlist Node value
while (index < self.size)
print("\nAdjacency list of vertex ", index ," :")
temp = self.node[index].next
while (temp != nil)
# Display graph node
print(" ", self.node[temp.id].data)
# Visit to next edge
temp = temp.next
end
index += 1
end
end
end
# Check Path of given vertex
def dfs(point, path)
if (path[point])
# When alredy visit
return
else
# Collect the current node into the path
path[point] = true
temp = self.node[point].next
while (temp != nil)
# Visit to edge node
self.dfs(temp.id, path)
# Visit to next edge
temp = temp.next
end
end
end
def printMotherVertices()
if (self.size <= 0)
# No nodes
return
end
print("\nMother Vertex :")
path = Array.new(self.size) {false}
n = 0
status = true
count = 0
while (n < self.size)
# Check path of node n
self.dfs(n, path)
index = 0
# When current node are visit other nodes
# Then that is a Mother Vertex
while (index < self.size)
if (path[index] == false)
# When path not contain current node
status = false
end
# Reset element value
path[index] = false
index += 1
end
if (status)
# Display result node
print(" ", n)
count += 1
end
# Reset status
status = true
# Visit next node
n += 1
end
if (count == 0)
print("Node\n")
end
end
end
def main()
# 7 implies the number of nodes in graph
g = Graph.new(7)
# Connect node with an edge
g.addEdge(1, 0)
g.addEdge(1, 4)
g.addEdge(2, 5)
g.addEdge(3, 1)
g.addEdge(3, 2)
g.addEdge(3, 6)
g.addEdge(4, 3)
g.addEdge(5, 0)
# Print graph element
g.printGraph()
# Test
g.printMotherVertices()
end
main()
Output
Adjacency list of vertex 0 :
Adjacency list of vertex 1 : 0 4
Adjacency list of vertex 2 : 5
Adjacency list of vertex 3 : 1 2 6
Adjacency list of vertex 4 : 3
Adjacency list of vertex 5 : 0
Adjacency list of vertex 6 :
Mother Vertex : 1 3 4
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment