Skip to main content
Post Reopened by D.W.
edited body
Source Link
user114370
user114370

SoIt represents a list of lowercase char vertices with uppercase constrained predecessors and edges weighted by different dotted distances. So here I'm looking for a comparison based on the time complexity of the algorithms, related to an analysis of the cases where the number of edges increases, at a fixed number of vertices. There is a reference solution in Python which is correct and fast, but I fail to see where is the fundamental difference between the two algorithms, behind the other minor code differences (also because the languages are different).

Start by receiving the graph topology and starting point
    Define a mindistance variable
    and a list of completed solutions, initially empty

    Start looping at the queue of partial solutions
        So a single partial solution is dequeued
        It computes possible vertices based on current predecessors
        Check if nothing remains and adds it to the completed alternatives otherwise updating mindistance
        Otherwise loop as in classical BFS
        For all possible moves = reachable vertices
                
                For each one applies above said single step
                If done updates mindistance and completed alternatives 
                Otherwise enqueue such partial solution 
                
  
    Finally select the min alternative by distance.

So here I'm looking for a comparison based on the time complexity of the algorithms, related to an analysis of the cases where the number of edges increases, at a fixed number of vertices. There is a reference solution in Python which is correct and fast, but I fail to see where is the fundamental difference between the two algorithms, behind the other minor code differences (also because the languages are different).

Start by receiving the graph topology and starting point
    Define a mindistance variable
    and a list of completed solutions, initially empty

    Start looping at the queue of partial solutions
        So a single partial solution is dequeued
        It computes possible vertices based on current predecessors
        Check if nothing remains and adds it to the completed alternatives otherwise updating mindistance
        Otherwise loop as in classical BFS
        For all possible moves = reachable vertices
                
                For each one applies above said single step
                If done updates mindistance and completed alternatives 
                Otherwise enqueue such partial solution 
                
  
    Finally select the min alternative by distance.

It represents a list of lowercase char vertices with uppercase constrained predecessors and edges weighted by different dotted distances. So here I'm looking for a comparison based on the time complexity of the algorithms, related to an analysis of the cases where the number of edges increases, at a fixed number of vertices. There is a reference solution in Python which is correct and fast, but I fail to see where is the fundamental difference between the two algorithms, behind the other minor code differences (also because the languages are different).

Start by receiving the graph topology and starting point
    Define a mindistance variable
    and a list of completed solutions, initially empty

    Start looping at the queue of partial solutions
        So a single partial solution is dequeued
        It computes possible vertices based on current predecessors
        Check if nothing remains and adds it to the completed alternatives updating mindistance
        Otherwise loop as in classical BFS
        For all possible moves = reachable vertices
                
                For each one applies above said single step
                If done updates mindistance and completed alternatives 
                Otherwise enqueue such partial solution 
                
  
    Finally select the min alternative by distance.
edited body
Source Link
user114370
user114370
1 single step receives as input which is the move index i
2 then it updates the distance of the current solution draft by summing the distance to the vertex Ii
3 it updates the current position as of vertex Ii
4 it also updates the list of predecessors by including vertex i
5 it computes the new tree of reachable vertices starting from the `fullGrid` and taking into account the newly updated list of predecessors
1 single step receives as input which is the move index i
2 then it updates the distance of the current solution draft by summing the distance to the vertex I
3 it updates the current position as of vertex I
4 it also updates the list of predecessors by including vertex i
5 it computes the new tree of reachable vertices starting from the `fullGrid` and taking into account the newly updated list of predecessors
1 single step receives as input which is the move index i
2 then it updates the distance of the current solution draft by summing the distance to the vertex i
3 it updates the current position as of vertex i
4 it also updates the list of predecessors by including vertex i
5 it computes the new tree of reachable vertices starting from the `fullGrid` and taking into account the newly updated list of predecessors
deleted 1367 characters in body
Source Link
user114370
user114370
let1 nextsingle (step:Step) (i:int) (solution: Solution) (fullGrid: Map<char,Map<char,Grid>>) : Solutionreceives =
as input which is letthe branchesmove =index solution.tree.branchesi
    let2 distancethen =it solution.tree.distanceupdates +the branches.[i].distance
    let area = branches.[i].area  
    //let newbranches, back, keys =
    match step with
    | SpaceStep ->
       of failwiththe "notcurrent expectedsolution withdraft smartby grid"
summing the distance to |the KeyStepvertex ->I
    3 it updates the letcurrent keysposition =as areaof ::vertex solution.keysI
4 it also updates the list of predecessors letby gridincluding =vertex fullGrid.[area]i
  5 it computes the new tree letof treereachable =vertices grid2treestarting areafrom distancethe keys`fullGrid` grid
and taking into account the newly updated list {keys=keys;of tree=tree}predecessors

Notice that this is a constrained version of the TSP where each vertex can have as constraint a list of predecessors.

let findSolution (keynum:int) (solution: Solution) (fullGrid: Map<char,Map<char,Grid>>) : Solution option =
    let mutable solution_queue : queue<Solution> = MyQueue.empty
Start by receiving the solution_queuegraph <-topology enqueueand solution_queuestarting solutionpoint
    letDefine mutablea mindistance : int option = Nonevariable
    letand mutablea alternativeslist :of Solutioncompleted listsolutions, =initially List.empty

    while (MyQueue.length solution_queue > 0) do
     Start looping at letthe solutionqueue =of dequeuepartial &solution_queuesolutions
        let solution = {solution with tree =So grid2treea solution.tree.areasingle solution.tree.distancepartial solution.keys fullGrid.[solution.tree.area]}
        let branches =is solution.tree.branchesdequeued
        ifIt computes (branchespossible =vertices [||]based )on thencurrent predecessors
           Check if solution.keys.Length = keynum 
            then updateMin &mindistance &alternatives solution
      nothing remains else
and adds it to the completed alternatives otherwise matchupdating mindistance with
        | Some d when d < solution.tree.distance + (solution.tree.branches |> Array.map (fun t -> t.distance) |> Array.min) -> () 
        | _ ->
        let indexes =
            [|0..branches.Length-1|]
            |> Array.sortBy(fun idx -> ((if isKey branches.[idx].area then 0 else 1) , branches.[idx].distance))
       Otherwise forloop ias in indexes do
            if branches.[i].area = '#' then 
                failwith "not expected with smart grid" 
           classical elseBFS
          For all ifpossible branches.[i].areamoves = Space then
                failwith "not expected with smartreachable grid"vertices
            else
            if (Char.IsLower branches.[i].area) then
                let solutionNext = next KeyStep i solution fullGrid
            For each one applies ifabove solutionNext.keys.Lengthsaid =single keynumstep
                then  updateMin &mindistance &alternatives solutionNext
         If done updates mindistance and completed alternatives else
                solution_queue <-Otherwise enqueue solution_queue solutionNext
            else
            ifsuch (Char.IsUpperpartial branches.[i].area)solution then
                failwith "not expected with smart grid"
  
    match alternatives with
    | [] -> None
    | alternatives ->
       Finally alternativesselect |>the List.minBy(funmin aalternative ->by a.tree.distance) |> Some.
let next (step:Step) (i:int) (solution: Solution) (fullGrid: Map<char,Map<char,Grid>>) : Solution =
    let branches = solution.tree.branches
    let distance = solution.tree.distance + branches.[i].distance
    let area = branches.[i].area  
    //let newbranches, back, keys =
    match step with
    | SpaceStep ->
        failwith "not expected with smart grid"
    | KeyStep ->
        let keys = area :: solution.keys
        let grid = fullGrid.[area]
        let tree = grid2tree area distance keys grid
        {keys=keys; tree=tree}
let findSolution (keynum:int) (solution: Solution) (fullGrid: Map<char,Map<char,Grid>>) : Solution option =
    let mutable solution_queue : queue<Solution> = MyQueue.empty
    solution_queue <- enqueue solution_queue solution
    let mutable mindistance : int option = None
    let mutable alternatives : Solution list = List.empty

    while (MyQueue.length solution_queue > 0) do
        let solution = dequeue &solution_queue
        let solution = {solution with tree = grid2tree solution.tree.area solution.tree.distance solution.keys fullGrid.[solution.tree.area]}
        let branches = solution.tree.branches
        if  (branches = [||] ) then 
            if solution.keys.Length = keynum 
            then updateMin &mindistance &alternatives solution
        else
        match mindistance with
        | Some d when d < solution.tree.distance + (solution.tree.branches |> Array.map (fun t -> t.distance) |> Array.min) -> () 
        | _ ->
        let indexes =
            [|0..branches.Length-1|]
            |> Array.sortBy(fun idx -> ((if isKey branches.[idx].area then 0 else 1) , branches.[idx].distance))
        for i in indexes do
            if branches.[i].area = '#' then 
                failwith "not expected with smart grid" 
            else
            if branches.[i].area = Space then
                failwith "not expected with smart grid"
            else
            if (Char.IsLower branches.[i].area) then
                let solutionNext = next KeyStep i solution fullGrid
                if solutionNext.keys.Length = keynum
                then  updateMin &mindistance &alternatives solutionNext
                else
                solution_queue <- enqueue solution_queue solutionNext
            else
            if (Char.IsUpper branches.[i].area) then
                failwith "not expected with smart grid"
  
    match alternatives with
    | [] -> None
    | alternatives ->
        alternatives |> List.minBy(fun a -> a.tree.distance) |> Some
1 single step receives as input which is the move index i
2 then it updates the distance of the current solution draft by summing the distance to the vertex I
3 it updates the current position as of vertex I
4 it also updates the list of predecessors by including vertex i
5 it computes the new tree of reachable vertices starting from the `fullGrid` and taking into account the newly updated list of predecessors

Notice that this is a constrained version of the TSP where each vertex can have as constraint a list of predecessors.

Start by receiving the graph topology and starting point
    Define a mindistance variable
    and a list of completed solutions, initially empty

    Start looping at the queue of partial solutions
        So a single partial solution is dequeued
        It computes possible vertices based on current predecessors
        Check if nothing remains and adds it to the completed alternatives otherwise updating mindistance
        Otherwise loop as in classical BFS
        For all possible moves = reachable vertices
                
                For each one applies above said single step
                If done updates mindistance and completed alternatives 
                Otherwise enqueue such partial solution 
                
  
    Finally select the min alternative by distance.
added 1170 characters in body
Source Link
user114370
user114370
Loading
Post Closed as "Not suitable for this site" by D.W.
added 285 characters in body
Source Link
user114370
user114370
Loading
added 9 characters in body
Source Link
user114370
user114370
Loading
Source Link
user114370
user114370
Loading