Added Breadth First Search
parent
b17b4cbb39
commit
d7efb7b1e3
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func getIdx(target int, nodes []int) int {
|
||||
for i := 0; i < len(nodes); i++ {
|
||||
if nodes[i] == target {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func notExist(target int, slice []int) bool {
|
||||
for i := 0; i < len(slice); i++ {
|
||||
if slice[i] == target {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func breadthFirstSearch(start, end int, nodes []int, edges [][]bool) bool {
|
||||
var route []int
|
||||
var queue []int
|
||||
startIdx := getIdx(start, nodes)
|
||||
queue = append(queue, startIdx)
|
||||
for len(queue) > 0 {
|
||||
now := queue[0]
|
||||
route = append(route, nodes[now])
|
||||
if len(queue) > 1 {
|
||||
queue = queue[1:]
|
||||
} else {
|
||||
queue = queue[0:]
|
||||
}
|
||||
for i := 0; i < len(edges[now]); i++ {
|
||||
if edges[now][i] && notExist(i, queue) {
|
||||
queue = append(queue, i)
|
||||
}
|
||||
edges[now][i] = false
|
||||
edges[i][now] = false
|
||||
}
|
||||
if route[len(route)-1] == end {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
nodes := []int{
|
||||
1, 2, 3, 4, 5, 6,
|
||||
}
|
||||
/*
|
||||
sample graph
|
||||
①-②
|
||||
| |
|
||||
③-④-⑤-⑥
|
||||
*/
|
||||
edges := [][]bool{
|
||||
{false, true, true, false, false, false},
|
||||
{true, false, false, true, false, false},
|
||||
{true, false, false, true, false, false},
|
||||
{false, true, true, false, true, false},
|
||||
{false, false, false, true, false, true},
|
||||
{false, false, false, false, true, false},
|
||||
}
|
||||
start := 1
|
||||
end := 6
|
||||
result := breadthFirstSearch(start, end, nodes, edges)
|
||||
fmt.Println(result)
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func InterpolationSearch(array []int, key int) int {
|
||||
|
||||
min, max := array[0], array[len(array)-1]
|
||||
|
||||
low, high := 0, len(array)-1
|
||||
|
||||
for {
|
||||
if key < min {
|
||||
return low
|
||||
}
|
||||
|
||||
if key > max {
|
||||
return high + 1
|
||||
}
|
||||
|
||||
// make a guess of the location
|
||||
var guess int
|
||||
if high == low {
|
||||
guess = high
|
||||
} else {
|
||||
size := high - low
|
||||
offset := int(float64(size-1) * (float64(key-min) / float64(max-min)))
|
||||
guess = low + offset
|
||||
}
|
||||
|
||||
// maybe we found it?
|
||||
if array[guess] == key {
|
||||
// scan backwards for start of value range
|
||||
for guess > 0 && array[guess-1] == key {
|
||||
guess--
|
||||
}
|
||||
return guess
|
||||
}
|
||||
|
||||
// if we guessed to high, guess lower or vice versa
|
||||
if array[guess] > key {
|
||||
high = guess - 1
|
||||
max = array[high]
|
||||
} else {
|
||||
low = guess + 1
|
||||
min = array[low]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
searchValue := 0
|
||||
|
||||
arr := []int{1, 5, 100, 0, -100, 15, 4, 102, 30, 1000}
|
||||
fmt.Println(arr)
|
||||
|
||||
var index = InterpolationSearch(arr, searchValue)
|
||||
|
||||
if index < 0 {
|
||||
fmt.Println("Not found")
|
||||
return
|
||||
} else {
|
||||
fmt.Println("Found at position: ", index)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue