2021年8月3日 星期二

BFS VB.Net

 Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim fileContents As String

        fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\dfs.txt")

        Dim lines = fileContents.Trim().Split(vbNewLine)

        Dim gdict As New Dictionary(Of String, Array)

        For i = 0 To lines.Length - 1

            Dim cols = lines(i).Split(":")

            Dim sep = {Chr(34), Chr(39), " ", "[", "]", ","}

            Dim nod = cols(0).Trim().Split(sep, StringSplitOptions.RemoveEmptyEntries)

            Dim cnod = cols(1).Split(sep, StringSplitOptions.RemoveEmptyEntries)

            gdict.Add(nod(0), cnod)

        Next


        bfs(visited, gdict, "5")

        MsgBox(ss)

    End Sub


    Dim visited As New ArrayList

    Dim ss = ""


    Sub bfs(ByRef visited, ByRef graph, ByVal nod)

        Dim q As New Queue

        q.Enqueue(nod)

        While q.Count > 0

            Dim s = q.Dequeue()

            ss = ss & s & " "

            For Each neighbor In graph(s)

                If Not visited.contains(neighbor) Then

                    visited.add(neighbor)

                    q.Enqueue(neighbor)

                End If

            Next

        End While

    End Sub

End Class



沒有留言: