-
-
Notifications
You must be signed in to change notification settings - Fork 629
Add Dijkstra in Visual Basic #5819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+120
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| Public Module Program | ||
|
|
||
| Private Const INF As Integer = &H3F3F3F3F | ||
|
|
||
| Private Sub ShowUsage() | ||
| Console.Error.WriteLine( | ||
| "Usage: please provide three inputs: " & | ||
| "a serialized matrix, a source node and a destination node" | ||
| ) | ||
| Environment.Exit(1) | ||
| End Sub | ||
|
|
||
|
|
||
| Private Function ParseIntegerList(input As String) As List(Of Integer) | ||
|
|
||
| If String.IsNullOrWhiteSpace(input) Then | ||
| ShowUsage() | ||
| End If | ||
|
|
||
| Return input.Split(","c, StringSplitOptions.RemoveEmptyEntries Or StringSplitOptions.TrimEntries) _ | ||
| .Select(Function(s) | ||
| Dim v As Integer | ||
| If Not Integer.TryParse(s, v) OrElse v < 0 Then | ||
| ShowUsage() | ||
| End If | ||
| Return v | ||
| End Function) _ | ||
| .ToList() | ||
|
|
||
| End Function | ||
|
|
||
|
|
||
| Private Function GetDimension(matrix As List(Of Integer)) As Integer | ||
| Dim n = CInt(Math.Sqrt(matrix.Count)) | ||
| If n * n <> matrix.Count Then Return -1 | ||
| Return n | ||
| End Function | ||
|
|
||
|
|
||
| Private Function Dijkstra(matrix As List(Of Integer), | ||
| n As Integer, | ||
| source As Integer, | ||
| destination As Integer) As Integer | ||
|
|
||
| Dim dist(n - 1) As Integer | ||
| Dim visited(n - 1) As Boolean | ||
|
|
||
| For i = 0 To n - 1 | ||
| dist(i) = INF | ||
| Next | ||
|
|
||
| dist(source) = 0 | ||
|
|
||
| Dim pq As New PriorityQueue(Of Integer, Integer) | ||
| pq.Enqueue(source, 0) | ||
|
|
||
| While pq.Count > 0 | ||
|
|
||
| Dim node = pq.Dequeue() | ||
|
|
||
| If visited(node) Then Continue While | ||
| visited(node) = True | ||
|
|
||
| If node = destination Then | ||
| Return dist(node) | ||
| End If | ||
|
|
||
| Dim baseIndex = node * n | ||
|
|
||
| For i = 0 To n - 1 | ||
|
|
||
| Dim weight = matrix(baseIndex + i) | ||
|
|
||
| If weight <= 0 Then Continue For | ||
| If visited(i) Then Continue For | ||
|
|
||
| Dim newDist = dist(node) + weight | ||
|
|
||
| If newDist < dist(i) Then | ||
| dist(i) = newDist | ||
| pq.Enqueue(i, newDist) | ||
| End If | ||
|
|
||
| Next | ||
|
|
||
| End While | ||
|
|
||
| Return If(dist(destination) = INF, -1, dist(destination)) | ||
|
|
||
| End Function | ||
|
|
||
|
|
||
| Public Function Main(args As String()) As Integer | ||
|
|
||
| If args.Length <> 3 Then ShowUsage() | ||
|
|
||
| Dim matrix = ParseIntegerList(args(0)) | ||
| Dim n = GetDimension(matrix) | ||
|
|
||
| Dim source As Integer | ||
| Dim dest As Integer | ||
|
|
||
| If n = -1 OrElse | ||
| Not Integer.TryParse(args(1), source) OrElse | ||
| Not Integer.TryParse(args(2), dest) OrElse | ||
| source < 0 OrElse source >= n OrElse | ||
| dest < 0 OrElse dest >= n Then | ||
| ShowUsage() | ||
| End If | ||
|
|
||
| Dim result = Dijkstra(matrix, n, source, dest) | ||
|
|
||
| If result = -1 Then ShowUsage() | ||
|
|
||
| Console.WriteLine(result) | ||
| Return 0 | ||
|
|
||
| End Function | ||
|
|
||
| End Module | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.