Public Class Form1 Dim g As New Graph(5) Private Sub btnMakeGraph_Click(sender As Object, e As EventArgs) Handles btnMakeGraph.Click 'Simple Graph g.AddVertex("A") g.AddVertex("B") g.AddVertex("C") g.AddVertex("D") g.AddVertex("E") g.AddEdge(0, 1, 6) g.AddEdge(0, 3, 1) g.AddEdge(1, 0, 6) g.AddEdge(1, 2, 5) g.AddEdge(1, 3, 2) g.AddEdge(1, 4, 2) g.AddEdge(2, 1, 5) g.AddEdge(2, 4, 5) g.AddEdge(3, 0, 1) g.AddEdge(3, 1, 2) g.AddEdge(3, 4, 1) g.AddEdge(4, 1, 2) g.AddEdge(4, 2, 5) g.AddEdge(4, 3, 1) MsgBox("Graph Created") End Sub Private Sub btnGetVertex_Click(sender As Object, e As EventArgs) Handles btnShowVertex.Click g.ShowVertex(2) End Sub Private Sub btnShowMatrix_Click(sender As Object, e As EventArgs) Handles btnShowMatrix.Click Dim aMatrix(4, 4) As Double aMatrix = g.GetAdjacencyMatrix Dim stOut As String For x As Integer = 0 To 4 For y As Integer = 0 To 4 stOut = stOut & aMatrix(x, y) & " " Next stOut = stOut & vbNewLine Next MsgBox(stOut) End Sub Private Sub btnGetNeighbours_Click(sender As Object, e As EventArgs) Handles btnGetNeighbours.Click Dim stNeighbours As String stNeighbours = g.GetNeighbours(Me.txtNodeId.Text) MsgBox(stNeighbours) End Sub End Class Public Class Vertex Public Value As String Public Sub New(ByVal Value As String) Me.Value = Value End Sub End Class Public Class Graph Private vertices() As Vertex Private adjacencyMatrix(,) As Double Private iNumberOfVertices As Integer Private iMaxSize As Integer Public Sub New(iSize As Integer) iMaxSize = iSize 'for use by other methods iSize = iSize - 1 '0 based arrays ReDim vertices(iSize) ReDim adjacencyMatrix(iSize, iSize) Dim x, y As Integer For x = 0 To iSize - 1 For y = 0 To iSize - 1 adjacencyMatrix(x, y) = 0 Next Next iNumberOfVertices = 0 End Sub Public Sub AddVertex(ByVal value As String) vertices(iNumberOfVertices) = New Vertex(value) iNumberOfVertices += 1 End Sub Public Sub AddEdge(ByVal StartVertex As Integer, ByVal EndVertex As Integer, Weight As Double) adjacencyMatrix(StartVertex, EndVertex) = Weight adjacencyMatrix(EndVertex, StartVertex) = Weight 'undirected graph is symetrical End Sub Public Sub ShowVertex(ByVal v As Integer) MsgBox(vertices(v).Value) End Sub Public Function GetAdjacencyMatrix() Return adjacencyMatrix End Function Public Function GetNeighbours(VertexId) As String Dim stOut As String For y = 0 To iMaxSize - 1 If adjacencyMatrix(VertexId, y) > 0 Then stOut = stOut & y & " " End If Next Return stOut End Function End Class