mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: add more graphs examples, fix typo (#17113)
This commit is contained in:
parent
86f8c55107
commit
6bb930591e
@ -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
|
// Creating aa map to initialize with of visited nodes .... all with false in the init
|
||||||
// so these nodes are NOT VISITED YET
|
// so these nodes are NOT VISITED YET
|
||||||
fn visited_init(a_graph map[string][]string) map[string]bool {
|
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
|
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
|
temp[i] = false
|
||||||
}
|
}
|
||||||
return temp
|
return temp
|
||||||
|
@ -27,7 +27,7 @@ fn main() {
|
|||||||
path_01 := depth_first_search_path(graph_01, 'A', 'F')
|
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()}')
|
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')
|
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.
|
// 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
|
// Creating aa map to initialize with of visited nodes .... all with false in the init
|
||||||
// so these nodes are NOT VISITED YET
|
// so these nodes are NOT VISITED YET
|
||||||
fn visited_init(a_graph map[string][]string) map[string]bool {
|
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
|
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
|
temp[i] = false
|
||||||
}
|
}
|
||||||
return temp
|
return temp
|
||||||
|
83
examples/graphs/dfs2.v
Normal file
83
examples/graphs/dfs2.v
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user