mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
examples: fix typos (#18229)
This commit is contained in:
parent
caee3935a5
commit
993546a0a2
28 changed files with 89 additions and 89 deletions
|
@ -1,11 +1,11 @@
|
|||
/*
|
||||
A V program for Bellman-Ford's single source
|
||||
shortest path algorithm.
|
||||
literaly adapted from:
|
||||
literally adapted from:
|
||||
https://www.geeksforgeeks.org/bellman-ford-algorithm-dp-23/
|
||||
// Adapted from this site... from C++ and Python codes
|
||||
|
||||
For Portugese reference
|
||||
For Portuguese reference
|
||||
http://rascunhointeligente.blogspot.com/2010/10/o-algoritmo-de-bellman-ford-um.html
|
||||
|
||||
code by CCS
|
||||
|
@ -24,7 +24,7 @@ mut:
|
|||
// building a map of with all edges etc of a graph, represented from a matrix adjacency
|
||||
// Input: matrix adjacency --> Output: edges list of src, dest and weight
|
||||
fn build_map_edges_from_graph[T](g [][]T) map[T]EDGE {
|
||||
n := g.len // TOTAL OF NODES for this graph -- its dimmension
|
||||
n := g.len // TOTAL OF NODES for this graph -- its dimensions
|
||||
mut edges_map := map[int]EDGE{} // a graph represented by map of edges
|
||||
|
||||
mut edge := 0 // a counter of edges
|
||||
|
@ -61,8 +61,8 @@ fn bellman_ford[T](graph [][]T, src int) {
|
|||
// Step 1: Initialize distances from src to all other
|
||||
// vertices as INFINITE
|
||||
n_vertex := graph.len // adjc matrix ... n nodes or vertex
|
||||
mut dist := []int{len: n_vertex, init: large} // dist with -1 instead of INIFINITY
|
||||
// mut path := []int{len: n , init:-1} // previous node of each shortest paht
|
||||
mut dist := []int{len: n_vertex, init: large} // dist with -1 instead of INFINITY
|
||||
// mut path := []int{len: n , init:-1} // previous node of each shortest path
|
||||
dist[src] = 0
|
||||
|
||||
// Step 2: Relax all edges |V| - 1 times. A simple
|
||||
|
@ -152,7 +152,7 @@ fn main() {
|
|||
// for index, g_value in [graph_01, graph_02, graph_03] {
|
||||
for index, g_value in [graph_01, graph_02, graph_03] {
|
||||
graph = g_value.clone() // graphs_sample[g].clone() // choice your SAMPLE
|
||||
// allways starting by node 0
|
||||
// always starting by node 0
|
||||
start_node := 0
|
||||
println('\n\n Graph ${index + 1} using Bellman-Ford algorithm (source node: ${start_node})')
|
||||
bellman_ford(graph, start_node)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Author: CCS
|
||||
// I follow literally code in C, done many years ago
|
||||
fn main() {
|
||||
// Adjacency matrix as a map
|
||||
// Adjacency matrix as a map
|
||||
graph := {
|
||||
'A': ['B', 'C']
|
||||
'B': ['A', 'D', 'E']
|
||||
|
@ -37,7 +37,7 @@ fn breadth_first_search_path(graph map[string][]string, start string, target str
|
|||
// Expansion of node removed from queue
|
||||
print('\n Expansion of node ${node} (true/false): ${graph[node]}')
|
||||
// take all nodes from the node
|
||||
for vertex in graph[node] { // println("\n ...${vertex}")
|
||||
for vertex in graph[node] { // println("\n ...${vertex}")
|
||||
// not explored yet
|
||||
if visited[vertex] == false {
|
||||
queue << vertex
|
||||
|
@ -79,7 +79,7 @@ fn build_path_reverse(graph map[string][]string, start string, final string, vis
|
|||
for i in array_of_nodes {
|
||||
if current in graph[i] && visited[i] == true {
|
||||
current = i
|
||||
break // the first ocurrence is enough
|
||||
break // the first occurrence is enough
|
||||
}
|
||||
}
|
||||
path << current // update the path tracked
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// I follow literally code in C, done many years ago
|
||||
|
||||
fn main() {
|
||||
// Adjacency matrix as a map
|
||||
// Adjacency matrix as a map
|
||||
// Example 01
|
||||
graph_01 := {
|
||||
'A': ['B', 'C']
|
||||
|
@ -44,7 +44,7 @@ fn depth_first_search_path(graph map[string][]string, start string, target strin
|
|||
|
||||
// check if this node is already visited
|
||||
if visited[node] == false {
|
||||
// if no ... test it searchin for a final node
|
||||
// if no ... test it and search for a final node
|
||||
visited[node] = true // means: node visited
|
||||
if node == target {
|
||||
path = build_path_reverse(graph, start, node, visited)
|
||||
|
@ -93,7 +93,7 @@ fn build_path_reverse(graph map[string][]string, start string, final string, vis
|
|||
for i in array_of_nodes {
|
||||
if current in graph[i] && visited[i] == true {
|
||||
current = i
|
||||
break // the first ocurrence is enough
|
||||
break // the first occurrence is enough
|
||||
}
|
||||
}
|
||||
path << current // updating the path tracked
|
||||
|
|
|
@ -22,7 +22,7 @@ $ ./an_executable.EXE
|
|||
Code based from : Data Structures and Algorithms Made Easy: Data Structures and Algorithmic Puzzles, Fifth Edition (English Edition)
|
||||
pseudo code written in C
|
||||
This idea is quite different: it uses a priority queue to store the current
|
||||
shortest path evaluted
|
||||
shortest path evaluated
|
||||
The priority queue structure built using a list to simulate
|
||||
the queue. A heap is not used in this case.
|
||||
*/
|
||||
|
@ -38,17 +38,17 @@ mut:
|
|||
// The "push" always sorted in pq
|
||||
fn push_pq[T](mut prior_queue []T, data int, priority int) {
|
||||
mut temp := []T{}
|
||||
lenght_pq := prior_queue.len
|
||||
pq_len := prior_queue.len
|
||||
|
||||
mut i := 0
|
||||
for i < lenght_pq && priority > prior_queue[i].priority {
|
||||
for i < pq_len && priority > prior_queue[i].priority {
|
||||
temp << prior_queue[i]
|
||||
i++
|
||||
}
|
||||
// INSERTING SORTED in the queue
|
||||
temp << NODE{data, priority} // do the copy in the right place
|
||||
// copy the another part (tail) of original prior_queue
|
||||
for i < lenght_pq {
|
||||
for i < pq_len {
|
||||
temp << prior_queue[i]
|
||||
i++
|
||||
}
|
||||
|
@ -59,16 +59,16 @@ fn push_pq[T](mut prior_queue []T, data int, priority int) {
|
|||
// Change the priority of a value/node ... exist a value, change its priority
|
||||
fn updating_priority[T](mut prior_queue []T, search_data int, new_priority int) {
|
||||
mut i := 0
|
||||
mut lenght_pq := prior_queue.len
|
||||
mut pq_len := prior_queue.len
|
||||
|
||||
for i < lenght_pq {
|
||||
for i < pq_len {
|
||||
if search_data == prior_queue[i].data {
|
||||
prior_queue[i] = NODE{search_data, new_priority} // do the copy in the right place
|
||||
prior_queue[i] = NODE{search_data, new_priority} // do the copy in the right place
|
||||
break
|
||||
}
|
||||
i++
|
||||
// all the list was examined
|
||||
if i >= lenght_pq {
|
||||
if i >= pq_len {
|
||||
print('\n This data ${search_data} does exist ... PRIORITY QUEUE problem\n')
|
||||
exit(1) // panic(s string)
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ fn dijkstra(g [][]int, s int) {
|
|||
mut n := g.len
|
||||
|
||||
mut dist := []int{len: n, init: -1} // dist with -1 instead of INIFINITY
|
||||
mut path := []int{len: n, init: -1} // previous node of each shortest paht
|
||||
mut path := []int{len: n, init: -1} // previous node of each shortest path
|
||||
|
||||
// Distance of source vertex from itself is always 0
|
||||
dist[s] = 0
|
||||
|
@ -223,13 +223,13 @@ fn main() {
|
|||
[5, 15, 4, 0],
|
||||
]
|
||||
|
||||
// To find number of coluns
|
||||
// To find number of columns
|
||||
// mut cols := an_array[0].len
|
||||
mut graph := [][]int{} // the graph: adjacency matrix
|
||||
// for index, g_value in [graph_01, graph_02, graph_03] {
|
||||
for index, g_value in [graph_01, graph_02, graph_03] {
|
||||
graph = g_value.clone() // graphs_sample[g].clone() // choice your SAMPLE
|
||||
// allways starting by node 0
|
||||
// always starting by node 0
|
||||
start_node := 0
|
||||
println('\n\n Graph ${index + 1} using Dijkstra algorithm (source node: ${start_node})')
|
||||
dijkstra(graph, start_node)
|
||||
|
|
|
@ -16,7 +16,7 @@ $ ./an_executable.EXE
|
|||
Code based from : Data Structures and Algorithms Made Easy: Data Structures and Algorithmic Puzzles, Fifth Edition (English Edition)
|
||||
pseudo code written in C
|
||||
This idea is quite different: it uses a priority queue to store the current
|
||||
shortest path evaluted
|
||||
shortest path evaluated
|
||||
The priority queue structure built using a list to simulate
|
||||
the queue. A heap is not used in this case.
|
||||
*/
|
||||
|
@ -32,17 +32,17 @@ mut:
|
|||
// The "push" always sorted in pq
|
||||
fn push_pq[T](mut prior_queue []T, data int, priority int) {
|
||||
mut temp := []T{}
|
||||
lenght_pq := prior_queue.len
|
||||
pg_len := prior_queue.len
|
||||
|
||||
mut i := 0
|
||||
for i < lenght_pq && priority > prior_queue[i].priority {
|
||||
for i < pg_len && priority > prior_queue[i].priority {
|
||||
temp << prior_queue[i]
|
||||
i++
|
||||
}
|
||||
// INSERTING SORTED in the queue
|
||||
temp << NODE{data, priority} // do the copy in the right place
|
||||
// copy the another part (tail) of original prior_queue
|
||||
for i < lenght_pq {
|
||||
for i < pg_len {
|
||||
temp << prior_queue[i]
|
||||
i++
|
||||
}
|
||||
|
@ -52,17 +52,17 @@ fn push_pq[T](mut prior_queue []T, data int, priority int) {
|
|||
// Change the priority of a value/node ... exist a value, change its priority
|
||||
fn updating_priority[T](mut prior_queue []T, search_data int, new_priority int) {
|
||||
mut i := 0
|
||||
mut lenght_pq := prior_queue.len
|
||||
mut pg_len := prior_queue.len
|
||||
|
||||
for i < lenght_pq {
|
||||
for i < pg_len {
|
||||
if search_data == prior_queue[i].data {
|
||||
prior_queue[i] = NODE{search_data, new_priority} // do the copy in the right place
|
||||
prior_queue[i] = NODE{search_data, new_priority} // do the copy in the right place
|
||||
break
|
||||
}
|
||||
i++
|
||||
// all the list was examined
|
||||
if i >= lenght_pq {
|
||||
// print('\n Priority Queue: ${prior_queue}')
|
||||
if i >= pg_len {
|
||||
// print('\n Priority Queue: ${prior_queue}')
|
||||
// print('\n These data ${search_data} and ${new_priority} do not exist ... PRIORITY QUEUE problem\n')
|
||||
// if it does not find ... then push it
|
||||
push_pq(mut prior_queue, search_data, new_priority)
|
||||
|
@ -118,7 +118,7 @@ fn prim_mst(g [][]int, s int) {
|
|||
mut n := g.len
|
||||
|
||||
mut dist := []int{len: n, init: -1} // dist with -1 instead of INIFINITY
|
||||
mut path := []int{len: n, init: -1} // previous node of each shortest paht
|
||||
mut path := []int{len: n, init: -1} // previous node of each shortest path
|
||||
|
||||
// Distance of source vertex from itself is always 0
|
||||
dist[s] = 0
|
||||
|
@ -216,7 +216,7 @@ fn main() {
|
|||
for index, g_value in [graph_01, graph_02, graph_03] {
|
||||
println('\n Minimal Spanning Tree of graph ${index + 1} using PRIM algorithm')
|
||||
graph = g_value.clone() // graphs_sample[g].clone() // choice your SAMPLE
|
||||
// starting by node x ... see the graphs dimmension
|
||||
// starting by node x ... see the graphs dimensions
|
||||
start_node := 0
|
||||
prim_mst(graph, start_node)
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ fn topog_sort_greedy(graph map[string][]string) []string {
|
|||
mut top_order := []string{} // a vector with sequence of nodes visited
|
||||
mut count := 0
|
||||
/*
|
||||
IDEA ( a greedy algorythm ):
|
||||
IDEA ( a greedy algorithm ):
|
||||
|
||||
1. choose allways the node with smallest input degree
|
||||
1. choose always the node with smallest input degree
|
||||
2. visit it
|
||||
3. put it in the output vector
|
||||
4. remove it from graph
|
||||
|
@ -94,7 +94,7 @@ fn remove_node_from_graph(node string, a_map map[string][]string) map[string][]s
|
|||
mut all_nodes := new_graph.keys() // get all nodes of this graph
|
||||
// FOR THE FUTURE with filter
|
||||
// for i in all_nodes {
|
||||
// new_graph[i] = new_graph[i].filter(index(it) != node)
|
||||
// new_graph[i] = new_graph[i].filter(index(it) != node)
|
||||
// }
|
||||
// A HELP FROM V discussion GITHUB - thread
|
||||
for key in all_nodes {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue