Shop OBEX P1 Docs P2 Docs Learn Events
Visual basic to pbasic — Parallax Forums

Visual basic to pbasic

kingbpkingbp Posts: 22
edited 2011-04-19 14:33 in BASIC Stamp
Hi I have a code for the Dijkstra's algorithm in visual basic 6 and i need to convert it to pbasi Is anybody here able to do that.

Option Explicit

Const nodeCount As Integer = 9 'number of nodes - 1
Type DijkEdge
weight As Integer 'distance from vertices that it is connected to
destination As Integer 'name of vertice that it is connected to
End Type

Type Vertex
connections(nodeCount) As DijkEdge 'hold information above for each connection
numConnect As Integer 'number of connections - 1
distance As Integer 'distance from all other vertices
isDead As Boolean 'distance calculated
name As Integer 'name of vertice
End Type

Public Sub dijkstra_shortest_Path()
Const infinity As Integer = 15000 'number that is larger than max distance
Dim i As Integer 'loop counter
Dim j As Integer 'loop counter
Dim sourceP As Integer 'point to determine distance to from all nodes
Dim inputData As String 'temp variable to ensure good data enterred
Dim graph(nodeCount) As Vertex 'all inforamtion for each point (see Vertex declaration above)
Dim nextP As Integer 'closest point that is not dead
Dim min As Integer 'distance of closest point not dead
Dim outString As String 'string to display the output
Dim goodSource As Boolean

'user enters source point data and ensured that it is correct
Do
goodSource = True
inputData = (InputBox("What is the source point: ", "Source Point between: 0 & " & nodeCount))
If IsNumeric(inputData) Then
sourceP = CInt(inputData)
If sourceP > nodeCount Or sourceP < 0 Then
MsgBox "Source point must be between 0 & " & nodeCount & "."
goodSource = False
End If
Else
MsgBox "Source point must be numeric and be between 0 & " & nodeCount & "."
goodSource = False
End If
Loop While Not goodSource
'get data so we can analyze the distances
Call populateGraph(graph)

'set default values to not dead and distances to infinity (unless distance is to itself)
For i = 0 To nodeCount
If graph(i).name = sourceP Then
graph(i).distance = 0
graph(i).isDead = False
Else:
graph(i).distance = infinity
graph(i).isDead = False
End If
Next i

For i = 0 To nodeCount
min = infinity + 1
'determine closest point that is not dead
For j = 0 To nodeCount
If Not graph(j).isDead And graph(j).distance < min Then
nextP = j
min = graph(j).distance
End If
Next j
'calculate distances from the closest point & to all of its connections
For j = 0 To graph(nextP).numConnect
If graph(graph(nextP).connections(j).destination).distance > graph(nextP).distance + graph(nextP).connections(j).weight Then
graph(graph(nextP).connections(j).destination).distance = graph(nextP).distance + graph(nextP).connections(j).weight
End If
Next j
'kill the value we just looked at so we can get the next point
graph(nextP).isDead = True
Next i

'display the distance from the source point to all other points
outString = ""
For i = 0 To nodeCount
outString = outString & "The distance between nodes " & sourceP & " and " & i & " is " & graph(i).distance & vbCrLf
Next i
MsgBox outString
End Sub

Comments

  • ZootZoot Posts: 2,227
    edited 2011-04-19 14:33
    What's your planned output? The debug terminal screen?
Sign In or Register to comment.