Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion F中邻接列表的深度优先搜索_Recursion_F#_Depth First Search - Fatal编程技术网

Recursion F中邻接列表的深度优先搜索

Recursion F中邻接列表的深度优先搜索,recursion,f#,depth-first-search,Recursion,F#,Depth First Search,我的列表结构如下: 0 ; (0,0,G) ; [] 1 ; (9,0,B) ; [] 2 ; (9,9,R) ; [] 3 ; (-1,-1,E) ; [] 4 ; (-1,-1,E) ; [] 5 ; (-1,-1,E) ; [] 6 ; (-1,-1,E) ; [] 7 ; (-1,-1,E) ; [] 8 ; (-1,-1,E) ; [] 列表中的每个项都有一个索引、一个元组和一个包含邻居索引的邻近邻居列表 我的搜索从索引1或索引2开始,我需要从它们到索引0的所有路径。此外,我还需要一

我的列表结构如下:

0 ; (0,0,G) ; []
1 ; (9,0,B) ; []
2 ; (9,9,R) ; []
3 ; (-1,-1,E) ; []
4 ; (-1,-1,E) ; []
5 ; (-1,-1,E) ; []
6 ; (-1,-1,E) ; []
7 ; (-1,-1,E) ; []
8 ; (-1,-1,E) ; []
列表中的每个项都有一个索引、一个元组和一个包含邻居索引的邻近邻居列表

我的搜索从索引1或索引2开始,我需要从它们到索引0的所有路径。此外,我还需要一个包含路径上所有索引的列表

我已经使用递归实现了这一点,并且正在使用一个全局可变的bluechains来存储所有可能的路径。我希望在不使用全局变量的情况下执行相同的操作,但是我希望函数返回所有路径的列表,因为它会导致代码的其他部分出现问题

以下是我的函数的外观:

let rec traverseBlue index visited = 

    match index with 
    | 0 ->
        let newVisited = 0 :: visited
        blueChains <- newVisited :: blueChains
        printfn "Golden Cog Reached! Blue wins the game!"
        currentGameStatus <- WonByB 
    | ind -> 
        if not (List.contains ind visited) then
            let newVisited = ind :: visited
            blueChains <- newVisited :: blueChains
            printfn "INDEX: %i VISITED: %A" ind newVisited
            for i in tree.[ind].connectedNodes do
                traverseBlue i newVisited

我想为蓝链获得相同的值,但不使用全局变量

这是我最终所做的,多亏@glennsl解决了我的问题

let rec traverseBlue index visited oldBlueChains = 

let mutable blueChains = []
match index with 
| 0 ->
    let newVisited = 0 :: visited
    blueChains <- newVisited :: oldBlueChains
    printfn "Golden Cog Reached! Blue wins the game!"
    currentGameStatus <- WonByB 
    blueChains
| ind -> 
    if not (List.contains ind visited) then
        let newVisited = ind :: visited
        blueChains <- newVisited :: oldBlueChains
        printfn "INDEX: %i VISITED: %A" ind newVisited
        for i in tree.[ind].connectedNodes do
            blueChains <- (traverseBlue i newVisited blueChains) @ blueChains

这似乎并不完全取决于深度优先搜索的细节。看起来你只需要把它作为一个论点来传递,就像你已经用visted做的那样,然后在完成后返回它。你在做这件事时有什么特别的问题吗?我不确定我是否理解你的意思你已经在递归调用和访问参数之间传递状态了,对吗?所以也可以用蓝链来做。然后,您需要做的就是在完成后返回blueChains的最终值。
let rec traverseBlue index visited oldBlueChains = 

let mutable blueChains = []
match index with 
| 0 ->
    let newVisited = 0 :: visited
    blueChains <- newVisited :: oldBlueChains
    printfn "Golden Cog Reached! Blue wins the game!"
    currentGameStatus <- WonByB 
    blueChains
| ind -> 
    if not (List.contains ind visited) then
        let newVisited = ind :: visited
        blueChains <- newVisited :: oldBlueChains
        printfn "INDEX: %i VISITED: %A" ind newVisited
        for i in tree.[ind].connectedNodes do
            blueChains <- (traverseBlue i newVisited blueChains) @ blueChains