2021年8月3日 星期二

DFS VB.Net sample

 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

        dfs(visited, gdict, "5")

        MsgBox(ss)

    End Sub


    Dim visited As New ArrayList

    Dim ss = ""


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

        If Not visited.contains(nod) Then

            ss = ss & nod & " "

            visited.add(nod)

            For Each neighbor In graph(nod)

                dfs(visited, graph, neighbor)

            Next

        End If

    End Sub

End Class

dfs.txt
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : [],



沒有留言: