File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Author: CCS & KeitoTobi1
2
+ // Backtracking Supported.
3
+ import datatypes
4
+
5
+ fn find_pattern (adj map [string ][]string , start string , target string ) [][]string {
6
+ mut visited := visited_init (adj)
7
+ mut queue := datatypes.Queue[[]string ]{}
8
+ mut res := [][]string {}
9
+ queue.push ([start])
10
+ for ! queue.is_empty () {
11
+ mut v := queue.pop () or { panic (err) }
12
+ node := v.last ()
13
+ if node == target {
14
+ res << v
15
+ }
16
+ print ('Expansion of node ${node} (true/false): ${adj[node]} ' )
17
+ for next in adj[node] {
18
+ if visited[next] == false {
19
+ mut copy := v.clone ()
20
+ copy << next
21
+ queue.push (copy)
22
+ visited[node] = true
23
+ }
24
+ }
25
+ print ('Expansion of node ${queue} (true/false): ${visited} \n ' )
26
+ }
27
+ return res
28
+ }
29
+
30
+ fn print_pattern (pat [][]string ) {
31
+ for p in pat {
32
+ println (p)
33
+ }
34
+ }
35
+
36
+ fn visited_init (adj map [string ][]string ) map [string ]bool {
37
+ mut temp := map [string ]bool {}
38
+ for i, _ in adj {
39
+ temp[i] = false
40
+ }
41
+ return temp
42
+ }
43
+
44
+ fn main () {
45
+ adj := {
46
+ 'A' : ['B' , 'C' ]
47
+ 'B' : ['A' , 'D' , 'E' ]
48
+ 'C' : ['A' , 'F' ]
49
+ 'D' : ['B' ]
50
+ 'E' : ['B' , 'F' ]
51
+ 'F' : ['C' , 'E' ]
52
+ }
53
+
54
+ path := find_pattern (adj, 'A' , 'F' )
55
+ println ('The all pattern path from node A to node F is:' )
56
+ print_pattern (path)
57
+ }
You can’t perform that action at this time.
0 commit comments