From 4f6344017642ee6d2af69b6832966293d297b77d Mon Sep 17 00:00:00 2001 From: "J2GLOBAL\\don.johnson" Date: Wed, 14 Jun 2017 12:12:35 -0700 Subject: [PATCH 1/2] added search algorithm examples --- search/binary_search.py | 29 ++++++++++++++++++++++ search/bmh_search.py | 45 ++++++++++++++++++++++++++++++++++ search/breadth_first_search.py | 26 ++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 search/binary_search.py create mode 100644 search/bmh_search.py create mode 100644 search/breadth_first_search.py diff --git a/search/binary_search.py b/search/binary_search.py new file mode 100644 index 00000000..45c48ff6 --- /dev/null +++ b/search/binary_search.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +""" + Binary Search + ------------- + Recursively partitions the list until the `key` is found. + Time Complexity: O(lg n) + Psuedo Code: http://en.wikipedia.org/wiki/Binary_search +""" + + +def search(seq, key): + lo = 0 + hi = len(seq) - 1 + + while hi >= lo: + mid = lo + (hi - lo) // 2 + if seq[mid] < key: + lo = mid + 1 + elif seq[mid] > key: + hi = mid - 1 + else: + return mid + return False + + +test_list = [1, 6, 8, 9] +bsearch = search(test_list, 6) +print bsearch diff --git a/search/bmh_search.py b/search/bmh_search.py new file mode 100644 index 00000000..ab2b9778 --- /dev/null +++ b/search/bmh_search.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +""" + BMH Search + ---------- + Search that attempts to find a substring in a string. Uses a bad-character + shift of the rightmost character of the window to compute shifts. + Time: Complexity: O(m + n), where m is the substring to be found. + Space: Complexity: O(m), where m is the substring to be found. + Psuedo Code: https://github.com/FooBarWidget/boyer-moore-horspool +""" + + + +def search(text, pattern): + """ + Takes a string and searches if the `pattern` is substring within `text`. + :param text: A string that will be searched. + :param pattern: A string that will be searched as a substring within + `text`. + :rtype: The indices of all occurences of where the substring `pattern` + was found in `text`. + """ + + pattern_length = len(pattern) + text_length = len(text) + offsets = [] + if pattern_length > text_length: + return offsets + bmbc = [pattern_length] * 256 + for index, char in enumerate(pattern[:-1]): + bmbc[ord(char)] = pattern_length - index - 1 + bmbc = tuple(bmbc) + search_index = pattern_length - 1 + while search_index < text_length: + pattern_index = pattern_length - 1 + text_index = search_index + while text_index >= 0 and \ + text[text_index] == pattern[pattern_index]: + pattern_index -= 1 + text_index -= 1 + if pattern_index == -1: + offsets.append(text_index + 1) + search_index += bmbc[ord(text[search_index])] + + return offsets diff --git a/search/breadth_first_search.py b/search/breadth_first_search.py new file mode 100644 index 00000000..ed642a64 --- /dev/null +++ b/search/breadth_first_search.py @@ -0,0 +1,26 @@ +""" + breadth_first_search.py + Iterative implementation of BFS algorithm on a graph. + Breadth First Search Overview: + ------------------------ + Used to traverse trees, tree structures or graphs. + Starts at a selected node (root) and explores the nearest + neighbor branches before proceeding further. + Time Complexity: O(E + V) + E = Number of edges + V = Number of vertices (nodes) + Pseudocode: https://en.wikipedia.org/wiki/Breadth-first_search +""" + + + +def bfs(graph, start): + if start not in graph or graph[start] is None or graph[start] == []: + return None + visited, queue = set(), [start] + while queue: + vertex = queue.pop(0) + if vertex not in visited: + visited.add(vertex) + queue.extend(graph[vertex] - visited) + return visited From 4c5251d5e8b4de11e18328838fddf12ce5679804 Mon Sep 17 00:00:00 2001 From: "J2GLOBAL\\don.johnson" Date: Mon, 19 Jun 2017 13:13:28 -0700 Subject: [PATCH 2/2] removed search examples --- search/binary_search.py | 29 ---------------------- search/bmh_search.py | 45 ---------------------------------- search/breadth_first_search.py | 26 -------------------- 3 files changed, 100 deletions(-) delete mode 100644 search/binary_search.py delete mode 100644 search/bmh_search.py delete mode 100644 search/breadth_first_search.py diff --git a/search/binary_search.py b/search/binary_search.py deleted file mode 100644 index 45c48ff6..00000000 --- a/search/binary_search.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python - -""" - Binary Search - ------------- - Recursively partitions the list until the `key` is found. - Time Complexity: O(lg n) - Psuedo Code: http://en.wikipedia.org/wiki/Binary_search -""" - - -def search(seq, key): - lo = 0 - hi = len(seq) - 1 - - while hi >= lo: - mid = lo + (hi - lo) // 2 - if seq[mid] < key: - lo = mid + 1 - elif seq[mid] > key: - hi = mid - 1 - else: - return mid - return False - - -test_list = [1, 6, 8, 9] -bsearch = search(test_list, 6) -print bsearch diff --git a/search/bmh_search.py b/search/bmh_search.py deleted file mode 100644 index ab2b9778..00000000 --- a/search/bmh_search.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -""" - BMH Search - ---------- - Search that attempts to find a substring in a string. Uses a bad-character - shift of the rightmost character of the window to compute shifts. - Time: Complexity: O(m + n), where m is the substring to be found. - Space: Complexity: O(m), where m is the substring to be found. - Psuedo Code: https://github.com/FooBarWidget/boyer-moore-horspool -""" - - - -def search(text, pattern): - """ - Takes a string and searches if the `pattern` is substring within `text`. - :param text: A string that will be searched. - :param pattern: A string that will be searched as a substring within - `text`. - :rtype: The indices of all occurences of where the substring `pattern` - was found in `text`. - """ - - pattern_length = len(pattern) - text_length = len(text) - offsets = [] - if pattern_length > text_length: - return offsets - bmbc = [pattern_length] * 256 - for index, char in enumerate(pattern[:-1]): - bmbc[ord(char)] = pattern_length - index - 1 - bmbc = tuple(bmbc) - search_index = pattern_length - 1 - while search_index < text_length: - pattern_index = pattern_length - 1 - text_index = search_index - while text_index >= 0 and \ - text[text_index] == pattern[pattern_index]: - pattern_index -= 1 - text_index -= 1 - if pattern_index == -1: - offsets.append(text_index + 1) - search_index += bmbc[ord(text[search_index])] - - return offsets diff --git a/search/breadth_first_search.py b/search/breadth_first_search.py deleted file mode 100644 index ed642a64..00000000 --- a/search/breadth_first_search.py +++ /dev/null @@ -1,26 +0,0 @@ -""" - breadth_first_search.py - Iterative implementation of BFS algorithm on a graph. - Breadth First Search Overview: - ------------------------ - Used to traverse trees, tree structures or graphs. - Starts at a selected node (root) and explores the nearest - neighbor branches before proceeding further. - Time Complexity: O(E + V) - E = Number of edges - V = Number of vertices (nodes) - Pseudocode: https://en.wikipedia.org/wiki/Breadth-first_search -""" - - - -def bfs(graph, start): - if start not in graph or graph[start] is None or graph[start] == []: - return None - visited, queue = set(), [start] - while queue: - vertex = queue.pop(0) - if vertex not in visited: - visited.add(vertex) - queue.extend(graph[vertex] - visited) - return visited