From d7efb7b1e36cfe94318c99f494f4e61b07767689 Mon Sep 17 00:00:00 2001 From: dwij2812 Date: Fri, 25 Sep 2020 21:56:00 +0530 Subject: [PATCH] Added Breadth First Search --- README.md | 2 +- searching/breadthFirstSearch.go | 72 ++++++++++++++++++++++++++++++++ searching/interpolationSearch.go | 65 ---------------------------- 3 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 searching/breadthFirstSearch.go delete mode 100644 searching/interpolationSearch.go diff --git a/README.md b/README.md index a2cd36b..f056ffb 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Algorithms * [binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm) * [linear search](https://en.wikipedia.org/wiki/Linear_search) * [jump search](https://en.wikipedia.org/wiki/Jump_search) - * [interpolation search](https://en.wikipedia.org/wiki/Interpolation_search) + * [breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search) #### Collections diff --git a/searching/breadthFirstSearch.go b/searching/breadthFirstSearch.go new file mode 100644 index 0000000..7719d3f --- /dev/null +++ b/searching/breadthFirstSearch.go @@ -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) +} diff --git a/searching/interpolationSearch.go b/searching/interpolationSearch.go deleted file mode 100644 index 9ff4ed9..0000000 --- a/searching/interpolationSearch.go +++ /dev/null @@ -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 - } -}