From 6bb930591e83b9b5651e61d8fb3cdbc965e84353 Mon Sep 17 00:00:00 2001 From: Keito Tobichi Date: Thu, 26 Jan 2023 04:58:44 +0900 Subject: [PATCH] examples: add more graphs examples, fix typo (#17113) --- examples/graphs/bfs2.v | 3 +- examples/graphs/dfs.v | 5 +-- examples/graphs/dfs2.v | 83 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 examples/graphs/dfs2.v diff --git a/examples/graphs/bfs2.v b/examples/graphs/bfs2.v index 203bc45317..6ceeb99f4e 100644 --- a/examples/graphs/bfs2.v +++ b/examples/graphs/bfs2.v @@ -60,9 +60,8 @@ fn departure(mut queue []string) string { // Creating aa map to initialize with of visited nodes .... all with false in the init // so these nodes are NOT VISITED YET fn visited_init(a_graph map[string][]string) map[string]bool { - mut array_of_keys := a_graph.keys() // get all keys of this map mut temp := map[string]bool{} // attention in these initializations with maps - for i in array_of_keys { + for i, _ in a_graph { temp[i] = false } return temp diff --git a/examples/graphs/dfs.v b/examples/graphs/dfs.v index 667eb3292c..b12c570b9c 100644 --- a/examples/graphs/dfs.v +++ b/examples/graphs/dfs.v @@ -27,7 +27,7 @@ fn main() { path_01 := depth_first_search_path(graph_01, 'A', 'F') println('\n Graph_01: a first path from node A to node F is: ${path_01.reverse()}') path_02 := depth_first_search_path(graph_02, 'A', 'H') - println('\n Graph_02: a first path from node A to node F is: ${path_02.reverse()}') + println('\n Graph_02: a first path from node A to node H is: ${path_02.reverse()}') } // Depth-First Search (BFS) allows you to find a path between two nodes in the graph. @@ -74,9 +74,8 @@ fn depth_first_search_path(graph map[string][]string, start string, target strin // Creating aa map to initialize with of visited nodes .... all with false in the init // so these nodes are NOT VISITED YET fn visited_init(a_graph map[string][]string) map[string]bool { - mut array_of_keys := a_graph.keys() // get all keys of this map mut temp := map[string]bool{} // attention in these initializations with maps - for i in array_of_keys { + for i, _ in a_graph { temp[i] = false } return temp diff --git a/examples/graphs/dfs2.v b/examples/graphs/dfs2.v new file mode 100644 index 0000000000..33b5db36ce --- /dev/null +++ b/examples/graphs/dfs2.v @@ -0,0 +1,83 @@ +// Author: CCS & KeitoTobi1 +// Backtracking Supported. +fn main() { + // Adjacency matrix as a map + // Example 01 + graph_01 := { + 'A': ['B', 'C'] + 'B': ['A', 'D', 'E'] + 'C': ['A', 'F'] + 'D': ['B'] + 'E': ['F', 'B', 'F'] + 'F': ['C', 'E'] + } + + // Example 02 + graph_02 := { + 'A': ['B', 'C', 'D'] + 'B': ['E'] + 'C': ['F'] + 'D': ['E'] + 'E': ['H'] + 'F': ['H'] + 'G': ['H'] + 'H': ['E', 'F', 'G'] + } + + // println('Graph: $graph') + path_01 := depth_first_search_path(graph_01, 'A', 'F') + println('\n Graph_01: all path pattern from node A to node F is:') + print_pattern(path_01) + path_02 := depth_first_search_path(graph_02, 'A', 'H') + println('\n Graph_02: all path pattern from node A to node F is:') + print_pattern(path_02) +} + +fn depth_first_search_path(adj map[string][]string, start string, target string) [][]string { + mut sol := Solution{} + mut path := []string{} + mut visited := visited_init(adj) + + // false ... not visited yet: {'A': false, 'B': false, 'C': false, 'D': false, 'E': false} + sol.find_pattern(adj, mut visited, start, target, mut path) + return sol.pattern +} + +struct Solution { +pub mut: + pattern [][]string +} + +fn (mut s Solution) find_pattern(adj map[string][]string, mut visited map[string]bool, node string, target string, mut path []string) { + path << node + visited[node] = true + if node == target { + print('\n Founded pattern: ${path}') + s.pattern << [path] + } + print('\n Exploring of node ${node} (true/false): ${adj[node]}') + for i, _ in adj[node] { + if !visited[adj[node][i]] { + mut temp := path.clone() + s.find_pattern(adj, mut visited, adj[node][i], target, mut temp) + } + } + visited[node] = false + print('\n Current: ${node} (only not visited) \n Visited: ${visited}') +} + +fn print_pattern(pat [][]string) { + for p in pat { + println(p) + } +} + +// Creating aa map to initialize with of visited nodes .... all with false in the init +// so these nodes are NOT VISITED YET +fn visited_init(adj map[string][]string) map[string]bool { + mut temp := map[string]bool{} + for i, _ in adj { + temp[i] = false + } + return temp +}