Perplexity

LucaMs

Expert
Licensed User
Longtime User
Write Dijkstra's algorithm in B4X language

B4X:
Sub Dijkstra(graph As Map, startNode As String) As Map
    Dim distances As Map
    distances.Initialize
    For Each node In graph.Keys
        distances.Put(node, 1000000) 'set initial distance to infinity'
    Next
    distances.Put(startNode, 0) 'set distance to start node to zero'
   
    Dim visitedNodes As List
    visitedNodes.Initialize
   
    Dim currentNode As String = startNode
   
    Do While currentNode <> ""
        visitedNodes.Add(currentNode)
       
        For Each neighbor In graph.Get(currentNode)
            Dim tentativeDistance As Int = distances.Get(currentNode) + neighbor.Value
           
            If tentativeDistance < distances.Get(neighbor.Key) Then
                distances.Put(neighbor.Key, tentativeDistance)
            End If
        Next
       
        Dim smallestDistance As Int = 1000000 'initialize smallest distance to infinity'
        Dim nextNode As String = ""
       
        For Each node In graph.Keys
            If Not(visitedNodes.Contains(node)) And (distances.Get(node) < smallestDistance) Then
                smallestDistance = distances.Get(node)
                nextNode = node
            End If
        Next
       
        currentNode = nextNode
       
    Loop
   
    Return distances 'return the shortest path from the start node to all other nodes'
End Sub
B4X:
'Example usage:'
Sub ExampleUsage()
   Dim graph As Map
 
   graph.Initialize
 
   graph.Put("A", CreateMap("B":2, "C":3))
   graph.Put("B", CreateMap("A":2, "C":1))
   graph.Put("C", CreateMap("A":3, "B":1))
 
   Log(Dijkstra(graph, "A"))
End Sub

[not tested]
 

LucaMs

Expert
Licensed User
Longtime User
My next question to Perplexity will be:

Invent an app that will surely be very successful and write all the source code in B4X language

😁

I know what its answer will be:
"You can't predict whether an app will be successful"


P.S. Its answer:
As an AI language model, I cannot invent an app that will surely be very successful. However, I can provide you with a sample B4X code for a simple calculator app that performs basic arithmetic operations such as addition, subtraction, multiplication, and division.

Etc.
1678389926434.png
 

LucaMs

Expert
Licensed User
Longtime User
Over the past few days, the Perplexity.ai site has been changed (improved).

I didn't register and therefore couldn't save the conversation I just had with "him". I have the link to this conversation but I don't know if the conversation itself will be deleted and above all I don't know when.

I'd say it's interesting, especially for B4X beginners but not only for them (the source code can be useful even to those who don't know SQL).

My questions are in Italian but do you know how to translate them.

I wanted to see what the source code it would generate would look like. Not only; mainly I had done a similar test some time ago and the code it generated was not published in its entirety. At the time, however, I didn't know that I could continue the conversation, as is the case in ChatGPT.

I would say it was a good test and I hope you can read it, starting from this link:
https://www.perplexity.ai/search/da69eed3-c138-4974-922a-93c9bf1cb06e?s=u
[Let me know if the link no longer works. To fix this, I saved all the html this time (I'll post it eventually, with the questions in English)]


Note that it sometimes answers me in Italian, other times in English; it does as it pleases 😄
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Over the past few days, the Perplexity.ai site has been changed (improved).

I didn't register and therefore couldn't save the conversation I just had with "him". I have the link to this conversation but I don't know if the conversation itself will be deleted and above all I don't know when.

I'd say it's interesting, especially for B4X beginners but not only for them (the source code can be useful even to those who don't know SQL).

My questions are in Italian but do you know how to translate them.

I wanted to see what the source code it would generate would look like. Not only; mainly I had done a similar test some time ago and the code it generated was not published in its entirety. At the time, however, I didn't know that I could continue the conversation, as is the case in ChatGPT.

I would say it was a good test and I hope you can read it, starting from this link:
https://www.perplexity.ai/search/da69eed3-c138-4974-922a-93c9bf1cb06e?s=u


Note that it sometimes answers me in Italian, other times in English; it does as he pleases 😄
Also note that Perlexity takes a second or two to produce source code, I... a lot longer. It would definitely take me more than 2 seconds just to read the question!

Beware that very often the code it produces is wrong (it generates Java code, even if you specifically asked for B4J code or, very often, it invents names of libraries, classes or methods that don't actually exist)

Anyway, it's a useful tool, if used well.
 
Top