Advertisement
James1337

A Day in the Life of a Network Router

Jun 15th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.20 KB | None | 0 0
  1. #NoTrayIcon
  2.  
  3. ; A Day in the Life of a Network Router
  4. ; http://redd.it/287jxh
  5.  
  6. Global $Form, $idInput, $idOutput, $idButton
  7. $Form = GUICreate("A Day in the Life of a Network Router", 440, 360)
  8. GUISetFont(10, 1000, 0, "Courier New", $Form)
  9. $idInput = GUICtrlCreateEdit("Input", 20, 20, 400, 300)
  10. $idOutput = GUICtrlCreateInput("", 20, 320, 260, 20)
  11. $idButton = GUICtrlCreateButton("Find Path", 300, 320, 120, 20)
  12. GUISetState(@SW_SHOW, $Form)
  13.  
  14. Do
  15.     Switch GUIGetMsg()
  16.         Case -3
  17.             ExitLoop
  18.         Case $idButton
  19.             GUICtrlSetData($idOutput, FindPath(GUICtrlRead($idInput)))
  20.     EndSwitch
  21. Until False
  22. Exit
  23.  
  24. Func FindPath($sInput)
  25.     Local $sOutput = "", $aInput, $n, $x, $y
  26.     $aInput = StringSplit($sInput, @CRLF, 1)
  27.     $n = Number($aInput[1])
  28.     If ($aInput[0] <> $n+2) Then Return "ERROR"
  29.     Local $anMatrix[$n][$n]
  30.     Local $nStart, $nEnd, $aTemp, $i
  31.     $aTemp = StringSplit($aInput[$aInput[0]], " ")
  32.     If ($aTemp[0] <> 2) Then Return "ERROR"
  33.     $nStart = Asc($aTemp[1]) - 65
  34.     $nEnd = Asc($aTemp[2]) - 65
  35.     For $x = 0 To $n-1
  36.         $aTemp = StringSplit($aInput[$x+2], ",")
  37.         If ($aTemp[0] <> $n) Then Return "ERROR"
  38.         For $y = 0 To $n-1
  39.             ; I had to restructure the graph a bit,
  40.             ; because the algorithm wouldn't have worked otherwise.
  41.             If ($x = $y) Then
  42.                 $anMatrix[$x][$y] = 0
  43.             Else
  44.                 $anMatrix[$x][$y] = $aTemp[$y+1]
  45.                 If ($anMatrix[$x][$y] < 0) Then $anMatrix[$x][$y] = 10e10
  46.             EndIf
  47.         Next
  48.     Next
  49.     Local $anNext = Warshall($anMatrix)
  50.     Return Path($nStart, $nEnd, $anNext)
  51. EndFunc
  52.  
  53. Func Warshall($dist)
  54.     Local $n = UBound($dist), $next[$n][$n]
  55.     Local $u, $v, $k, $i, $j
  56.     For $u = 0 To $n-1
  57.         For $v = 0 To $n-1
  58.             If ($dist[$u][$v] > 0) Then
  59.                 $next[$u][$v] = $v
  60.             Else
  61.                 $next[$u][$v] = Null
  62.             EndIf
  63.         Next
  64.     Next
  65.     For $k = 0 To $n-1
  66.         For $i = 0 To $n-1
  67.             For $j = 0 To $n-1
  68.                 If ($dist[$i][$k] + $dist[$k][$j] < $dist[$i][$j]) Then
  69.                     $dist[$i][$j] = $dist[$i][$k] + $dist[$k][$j]
  70.                     $next[$i][$j] = $next[$i][$k]
  71.                 EndIf
  72.             Next
  73.         Next
  74.     Next
  75.     Return $next
  76. EndFunc
  77. Func Path($u, $v, ByRef $next)
  78.     If ($next[$u][$v] = Null) Then
  79.         Return ""
  80.     EndIf
  81.     Local $path = Chr(65 + $u)
  82.     While ($u <> $v)
  83.         $u = $next[$u][$v]
  84.         $path &= Chr(65 + $u)
  85.     WEnd
  86.     Return $path
  87. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement