Find all mother vertex of a directed graph in vb.net
Vb program for Find all mother vertex of a directed graph. Here more information.
' Include namespace system
Imports System
' Vb.net program for
' Find all Mother Vertex in a Graph
Public Class AjlistNode
' Vertices node key
Public id As Integer
Public [next] As AjlistNode
Public Sub New(ByVal id As Integer)
' Set value of node key
Me.id = id
Me.next = Nothing
End Sub
End Class
Public Class Vertices
Public data As Integer
Public [next] As AjlistNode
Public last As AjlistNode
Public Sub New(ByVal data As Integer)
Me.data = data
Me.next = Nothing
Me.last = Nothing
End Sub
End Class
public Class Graph
' Number of Vertices
Public size As Integer
Public node As Vertices()
Public Sub New(ByVal size As Integer)
' Set value
Me.size = size
Me.node = New Vertices(size){}
Me.setData()
End Sub
' Set initial node value
Public Sub setData()
if (Me.size <= 0) Then
Console.WriteLine( vbLf &"Empty Graph")
Else
Dim index As Integer = 0
While index < Me.size
' Set initial node value
Me.node(index) = New Vertices(index)
index += 1
End While
End IF
End Sub
' Handling the request of adding new edge
Public Sub addEdge(ByVal start As Integer,
ByVal last As Integer)
if (start >= 0 AndAlso start < Me.size AndAlso
last >= 0 AndAlso last < Me.size) Then
' Safe connection
Dim edge As AjlistNode = New AjlistNode(last)
if (Me.node(start).[next] Is Nothing) Then
Me.node(start).[next] = edge
Else
' Add edge at the end
Me.node(start).last.[next] = edge
End IF
' Get last edge
Me.node(start).last = edge
Else
' When invalid nodes
Console.WriteLine( vbLf &"Here Something Wrong")
End IF
End Sub
Public Sub printGraph()
if (Me.size > 0) Then
' Print graph ajlist Node value
Dim index As Integer = 0
While index < Me.size
Console.Write( vbLf &"Adjacency list of vertex " +
index.ToString() + " :")
Dim temp As AjlistNode = Me.node(index).[next]
while (temp IsNot Nothing)
' Display graph node
Console.Write(" " +
Me.node(temp.id).data.ToString())
' Visit to next edge
temp = temp.[next]
End While
index += 1
End While
End If
End Sub
' Check Path of given vertex
Public Sub dfs(ByVal point As Integer,
ByVal path As Boolean())
if (path(point)) Then
' When alredy visit
Return
Else
' Collect the current node into the path
path(point) = True
Dim temp As AjlistNode = Me.node(point).[next]
while (temp IsNot Nothing)
' Visit to edge node
Me.dfs(temp.id, path)
' Visit to next edge
temp = temp.[next]
End While
End IF
End Sub
Public Sub printMotherVertices()
if (Me.size <= 0) Then
' No nodes
Return
End If
Console.Write( vbLf &"Mother Vertex :")
Dim path As Boolean() = New Boolean(Me.size){}
Dim n As Integer = 0
Dim status As Boolean = True
Dim count As Integer = 0
while (n < Me.size)
' Check path of node n
Me.dfs(n, path)
' When current node are visit other nodes
' Then that is a Mother Vertex
Dim index As Integer = 0
While index < Me.size
if (path(index) = False) Then
' When path not contain current node
status = False
End If
' Reset element value
path(index) = False
index += 1
End While
if (status) Then
' Display result node
Console.Write(" " + n.ToString())
count += 1
End If
' Reset status
status = True
' Visit next node
n += 1
End While
if (count = 0) Then
Console.WriteLine("Node")
End If
End Sub
Public Shared Sub Main(ByVal args As String())
' 7 implies the number of nodes in graph
Dim g As Graph = New Graph(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 Sub
End Class
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