|
|
| This is an example of building a hash table. | | This is an example of building a hash table. |
| | | |
| n | #. sparse table or direct address table. | n | 1. sparse table or direct address table. |
| | | |
| **Assume that:** | | **Assume that:** |
| | | |
| n | #. Totle number of keys <= the size or the array. | n | * Totle number of keys <= the size or the array. |
| #. no 2 elements have the same keys. | | * no 2 elements have the same keys. |
| | | |
| **Algorythm:** | | **Algorythm:** |
| | | |
| n | #. Create A[0, max -1] | n | * Create A[0, max -1] |
| #. put key x into A[x] | | * put key x into A[x] |
| | | |
| **Example:** | | **Example:** |
| | | |
| n | .. code-block:: python | n | .. code-block:: python |
| | | |
| n | keys = 5,2,3,7 | n | keys = 5,2,3,7 |
| | | |
| t | A = list() | t | A = list() |
| A[2] = 2 | | A[2] = 2 |
| A[5] = 5 | | A[5] = 5 |
| A[3] = 3 | | A[3] = 3 |
| A[7] = 7 | | A[7] = 7 |
| | | |
| | | 2. Hash table |
| | | |
| | | **Assume that:** |
| | | |
| | | * keys = {0,1,2,3,4,n-1} |
| | | * total numebr of keys > the size of the array |
| | | |
| | | **Algorythm:** |
| | | |
| | | * A = list(0, max -1) |
| | | * put key x into A[ hash(x) ] |
| | | |
| | | hash function like md5 or sha256 hash(x) = x mod 6 |
| | | |
| | | If you have collisions there could be a problem. |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
|
|
| This is an example of building a hash table. | | This is an example of building a hash table. |
| | | |
| t | | t | #. sparse table or direct address table. |
| | | |
| | | **Assume that:** |
| | | |
| | | #. Totle number of keys <= the size or the array. |
| | | #. no 2 elements have the same keys. |
| | | |
| | | **Algorythm:** |
| | | |
| | | #. Create A[0, max -1] |
| | | #. put key x into A[x] |
| | | |
| | | **Example:** |
| | | |
| | | .. code-block:: python |
| | | |
| | | keys = 5,2,3,7 |
| | | |
| | | A = list() |
| | | A[2] = 2 |
| | | A[5] = 5 |
| | | A[3] = 3 |
| | | A[7] = 7 |
| | | |
| | | |
| | | |
| | | |
|
|
| i -= 1 | | i -= 1 |
| | | |
| t | | t | Most operating systems use heap tree to manage memory. |
| | | |
| | | |
| | | |
| | | hash table |
| | | ============== |
| | | |
| | | Don't use this in production! python has a datatype called dict, use that inste |
| | | ad!!! |
| | | |
| | | This is an example of building a hash table. |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| Extras | | Extras |
|
|
| """Turn any array or list (data) into a heap tree""" | | """Turn any array or list (data) into a heap tree""" |
| j = ( len( data ) / 2 ) - 1 | | j = ( len( data ) / 2 ) - 1 |
| t | for | t | i = j |
| | | while i >= 0: |
| | | current = data[i] |
| | | #insert_heap( current, i , length of subtree i - 1 ) |
| | | i -= 1 |
| | | |
| | | |
|
|
| """Turn any array or list (data) into a heap tree""" | | """Turn any array or list (data) into a heap tree""" |
| j = ( len( data ) / 2 ) - 1 | | j = ( len( data ) / 2 ) - 1 |
| t | for Extras | t | for |
| | | |
| | | |
| | | Extras |
| ============= | | ============ |
| | | |
| Note: | | Note: |
|
|
| """Turn any array or list (data) into a heap tree""" | | """Turn any array or list (data) into a heap tree""" |
| j = ( len( data ) / 2 ) - 1 | | j = ( len( data ) / 2 ) - 1 |
| n | | n | |
| | | |
| asdf | | |
| for Extras | | for Extras |
| ============= | | ============= |
| | | |
| | | |
| t | | t | Big-O notation compared to Complexity |
| | | ================================================ |
| | | |
| | | Always look for the worst case when finding the Big-O |
| | | |
| | | * O( 1 ) |
| | | |
| | | * Constant complexity |
| | | |
| | | * O( log n ) |
| | | |
| | | * Logarithmic complexity |
| | | |
| | | * O( n ) |
| | | |
| | | * Linear complexity |
| | | |
| | | * O( n log n ) |
| | | |
| | | * n log n complexity |
| | | |
| | | * O( n^b ) |
| | | |
| | | * Polynomial complexity |
| | | |
| | | * O( b^n ), where b>1 |
| | | |
| | | * Exponential complexity |
| | | |
| | | * O( n! ) |
| | | |
| | | * Factorial complexity |
| | | |
| | | |
|
|
| """Turn any array or list (data) into a heap tree""" | | """Turn any array or list (data) into a heap tree""" |
| j = ( len( data ) / 2 ) - 1 | | j = ( len( data ) / 2 ) - 1 |
| n | | n | |
| | | |
| | | asdf |
| for Extras | | for Extras |
| ============= | | ============= |
| | | |
| | | |
| t | Big-O notation compared to Complexity | t | |
| ================================================ | | |
| | | |
| Always look for the worst case when finding the Big-O | | |
| | | |
| * O( 1 ) | | |
| | | |
| * Constant complexity | | |
| | | |
| * O( log n ) | | |
| | | |
| * Logarithmic complexity | | |
| | | |
| * O( n ) | | |
| | | |
| * Linear complexity | | |
| | | |
| * O( n log n ) | | |
| | | |
| * n log n complexity | | |
| | | |
| * O( n^b ) | | |
| | | |
| * Polynomial complexity | | |
| | | |
| * O( b^n ), where b>1 | | |
| | | |
| * Exponential complexity | | |
| | | |
| * O( n! ) | | |
| | | |
| * Factorial complexity | | |
| | | |
| | | |
|
|
| | | |
| | | |
| t | Extras | t | python heap |
| | | ============= |
| | | |
| | | A heap is a binary tree where the parent is always larger than its children. |
| | | |
| | | Determine the last nodes that are leaves: (total number of items / 2) - 1 |
| | | |
| | | .. code-block:: python |
| | | |
| | | def build_heap( data ): |
| | | """Turn any array or list (data) into a heap tree""" |
| | | j = ( len( data ) / 2 ) - 1 |
| | | for Extras |
| ============= | | ============= |
| | | |
|
|
| | | |
| | | |
| t | | t | : ) |
| | | |
| | | |
| : )Extras | | Extras |
| ============= | | ============= |
| | | |
|
|
| print gcd( 287, 91 ) | | print gcd( 287, 91 ) |
| print gcd2( 287, 91 ) | | print gcd2( 287, 91 ) |
| t | | t | |
| | | |
| Extras | | : )Extras |
| ============= | | ============= |
| | | |
|
|
| break | | break |
| | | |
| t | | t | |
| | | |
| | | python recursion |
| | | ====================== |
| | | |
| | | .. code-block:: python |
| | | |
| | | def gcd( x, y ): |
| | | """Find greatest common divisor of two numbers recursion""" |
| | | |
| | | if y == 0: |
| | | return x |
| | | else: |
| | | return gcd( y, x%y ) |
| | | |
| | | def gcd2( x, y ): |
| | | """find the greatest common divisor of two numebrs""" |
| | | for i in range( 2, x ): |
| | | if x % i == 0 and y % i == 0: |
| | | return i; |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | print gcd( 287, 91 ) |
| | | print gcd2( 287, 91 ) |
| Extras | | Extras |
| ============= | | ============= |
|
|
| | | |
| | | |
| n | Sort an array or list | n | Bubble Sort an array or list |
| =================================================== | | =================================================== |
| | | |
| This is a bubble sort algo using python, don't use this in production this is fo | | This is a bubble sort algo using python, don't use this in production this is fo |
| r learning only! | | r learning only! |
| t | | t | |
| | | **BIGO( n^2 )** |
| | | |
| .. code-block:: python | | .. code-block:: python |
|
|
| while True: | | while True: |
| swapped = False | | swapped = False |
| t | for i in range( 0, len( mylist ) ): | t | for i in range( 0, len( mylist ) - 1 ): |
| if mylist[i] > mylist[i+1]: | | if mylist[i] > mylist[i+1]: |
| temp = mylist[i] | | temp = mylist[i] |
|
|
| =================================================== | | =================================================== |
| | | |
| n | | n | This is a bubble sort algo using python, don't use this in production this is fo |
| | | r learning only! |
| | | |
| n | asdfasdf | n | .. code-block:: python |
| | | |
| t | | t | def bubsort( mylist ): |
| | | while True: |
| | | swapped = False |
| | | for i in range( 0, len( mylist ) ): |
| | | if mylist[i] > mylist[i+1]: |
| | | temp = mylist[i] |
| | | mylist[i] = mylist[i+1] |
| | | mylist[i+1] = temp |
| | | swapped = True |
| | | if not swapped: |
| | | break |
| | | |
| Extras | | Extras |
|
|
| | | |
| | | |
| t | | t | Sort an array or list |
| | | =================================================== |
| | | |
| | | |
| | | asdfasdf |
| | | |
| | | |
|
|
| | | |
| the needle is the number we are searching for. | | the needle is the number we are searching for. |
| t | | t | |
| | | Note |
| | | We call this algorithm the binary search. |
| | | |
| .. code-block:: python | | .. code-block:: python |
|
|
| | | |
| # binary search | | # binary search |
| n | numbers = [0,11,25,38,41,54,66,79,80,94] | n | haystack = [0,11,25,38,41,54,66,79,80,94] |
| needle = 80 | | needle = 80 |
| | | |
| lo = 0 | | lo = 0 |
| n | hi = len( numbers ) | n | hi = len( haystack ) |
| | | |
| while lo < hi: | | while lo < hi: |
| mid = int( (lo + hi) / 2 ) | | mid = int( (lo + hi) / 2 ) |
| | | |
| n | if needle > numbers[ mid ]: | n | if needle > haystack[ mid ]: |
| lo = mid + 1 | | lo = mid + 1 |
| n | elif needle < numbers[ mid ]: | n | elif needle < haystack[ mid ]: |
| hi = mid | | hi = mid |
| else: | | else: |
| n | print "found %d which is the %d index of the list" % ( numbers[ mid ], | n | print "found %d which is the %d index of the list" % ( haystack[ mid ], |
| mid ) | | mid ) |
| break | | break |
| | | |
| **The Big-0 of this program is n O(logn) - base 2** | | **The Big-0 of this program is n O(logn) - base 2** |
| | | |
| t | This is a very good algorythm! | t | This is a very good algorithm! |
| | | |
| | | |
|
|
| | | |
| .. code-block:: python | | .. code-block:: python |
| t | | t | |
| | | # binary search |
| numbers = [0,11,25,38,41,54,66,79,80,94] | | numbers = [0,11,25,38,41,54,66,79,80,94] |
| needle = 80 | | needle = 80 |
|
|
| numbers = [0,11,25,38,41,54,66,79,80,94] | | numbers = [0,11,25,38,41,54,66,79,80,94] |
| needle = 80 | | needle = 80 |
| n | #needle = 0 | n | |
| #needle = 94 | | |
| | | |
| n | i = 0 | n | lo = 0 |
| j = len( numbers ) - 1 | | hi = len( numbers ) |
| | | |
| n | while i < j: | n | while lo < hi: |
| | | |
| n | mid = int( (i + j) / 2 ) | n | mid = int( (lo + hi) / 2 ) |
| | | |
| if needle > numbers[ mid ]: | | if needle > numbers[ mid ]: |
| n | i = mid + 1 | n | lo = mid + 1 |
| | | elif needle < numbers[ mid ]: |
| | | hi = mid |
| else: | | else: |
| n | j = mid - 1 | n | |
| | | |
| if needle == numbers[ mid ]: | | |
| print "found %d which is the %d index of the list" % ( numbers[ mid ], | | print "found %d which is the %d index of the list" % ( numbers[ mid ], |
| mid ) | | mid ) |
| break | | break |
| t | | t | |
| | | |
| * Worst case in 8 item list is 3 compares. | | * Worst case in 8 item list is 3 compares. |
|
|
| =================================================== | | =================================================== |
| | | |
| t | Try to search to see if the number 7 is in the list or array. | t | Try to search to see if the number 80 is in the list or array. |
| | | |
| the needle is the number we are searching for. | | the needle is the number we are searching for. |
|
|
| .. code-block:: python | | .. code-block:: python |
| | | |
| n | numbers = [-1,0,2,2,4,5,6,6,7,8,9] | n | numbers = [0,11,25,38,41,54,66,79,80,94] |
| needle = 7 | | needle = 80 |
| | | #needle = 0 |
| | | #needle = 94 |
| | | |
| n | i = 1 | n | i = 0 |
| j = len(numbers) # or 11 | | j = len( numbers ) - 1 |
| | | |
| while i < j: | | while i < j: |
| | | |
| n | m = int( i + j / 2 ) | n | mid = int( (i + j) / 2 ) |
| if needle < numbers[ m ]: | | if needle > numbers[ mid ]: |
| i = m + 1 | | i = mid + 1 |
| else: | | else: |
| n | j = m | n | j = mid - 1 |
| | | |
| t | if needle == m: | t | if needle == numbers[ mid ]: |
| print True | | print "found %d which is the %d index of the list" % ( numbers[ mid ], |
| | | mid ) |
| | | break |
| | | |
| | | |
|
|
| t | | t | Pseudo Code Algorithms with Python |
| | | ========================================= |
| | | |
| | | .. contents:: |
| | | |
| | | |
| Find the largest number in a list or array of numbers. | | Find the largest number in a list or array of numbers. |
|
|
| n | Always look for the worst case when finding the Big-O | n | |
| | | |
| * O( 1 ) | | |
| | | |
| * Constant complexity | | |
| | | |
| * O( log n ) | | |
| | | |
| * Logarithmic complexity | | |
| | | |
| * O( n ) | | |
| | | |
| * Linear complexity | | |
| | | |
| * O( n log n ) | | |
| | | |
| * n log n complexity | | |
| | | |
| * O( n^b ) | | |
| | | |
| * Polynomial complexity | | |
| | | |
| * O( b^n ), where b>1 | | |
| | | |
| * Exponential complexity | | |
| | | |
| * O( n! ) | | |
| | | |
| * Factorial complexity | | |
| | | |
| | | |
| Find the largest number in a list or array of numbers. | | Find the largest number in a list or array of numbers. |
| | | |
| | | |
| t | | t | Big-O notation compared to Complexity |
| | | ================================================ |
| | | |
| | | Always look for the worst case when finding the Big-O |
| | | |
| | | * O( 1 ) |
| | | |
| | | * Constant complexity |
| | | |
| | | * O( log n ) |
| | | |
| | | * Logarithmic complexity |
| | | |
| | | * O( n ) |
| | | |
| | | * Linear complexity |
| | | |
| | | * O( n log n ) |
| | | |
| | | * n log n complexity |
| | | |
| | | * O( n^b ) |
| | | |
| | | * Polynomial complexity |
| | | |
| | | * O( b^n ), where b>1 |
| | | |
| | | * Exponential complexity |
| | | |
| | | * O( n! ) |
| | | |
| | | * Factorial complexity |
| | | |
| | | |
|
|
| f | Always look for the worst case when finding the Big-O | f | Always look for the worst case when finding the Big-O |
| t | | t | |
| | | * O( 1 ) |
| | | |
| | | * Constant complexity |
| | | |
| | | * O( log n ) |
| | | |
| | | * Logarithmic complexity |
| | | |
| | | * O( n ) |
| | | |
| | | * Linear complexity |
| | | |
| | | * O( n log n ) |
| | | |
| | | * n log n complexity |
| | | |
| | | * O( n^b ) |
| | | |
| | | * Polynomial complexity |
| | | |
| | | * O( b^n ), where b>1 |
| | | |
| | | * Exponential complexity |
| | | |
| | | * O( n! ) |
| | | |
| | | * Factorial complexity |
| | | |
| | | |
|
|
| t | | t | Always look for the worst case when finding the Big-O |
| | | |
| | | |
| Find the largest number in a list or array of numbers. | | Find the largest number in a list or array of numbers. |
| ======================================================== | | ======================================================== |
|
|
| t | | No Differences Found | t | | No Differences Found |
|
|
| numbers = [5,6,2,7,6,2,0,4] | | numbers = [5,6,2,7,6,2,0,4] |
| | | |
| t | max = a[0] | t | max = numbers[0] |
| | | |
| for number in numbers: | | for number in numbers: |
|
|
| | | |
| for( int i = 2 ; i<= n ; i++ ){ } | | for( int i = 2 ; i<= n ; i++ ){ } |
| t | Worst case there will be at least two comparisons. | t | Worst case there will be at least two comparisons. Or at least n - 1 comparisons |
| | | . |
| | | |
| | | |
|
|
| | | |
| This is a very good algorythm! | | This is a very good algorythm! |
| t | | t | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | Extras |
| | | ============= |
| | | |
| | | Note: |
| | | Count the total number of comparisons in terms of n. |
| | | |
| | | | |
| | | | |
| | | | |
| | | |
| | | for( int i = 2 ; i<= n ; i++ ){ } |
| | | Worst case there will be at least two comparisons. |
| | | |
| | | |
| | | |
|
|
| the needle is the number we are searching for. | | the needle is the number we are searching for. |
| | | |
| n | numbers = [-1,0,2,2,4,5,6,6,7,8,9] | n | .. code-block:: python |
| needle = 7 | | |
| | | |
| n | i = 1 | n | numbers = [-1,0,2,2,4,5,6,6,7,8,9] |
| j = len(numbers) # or 11 | | needle = 7 |
| | | |
| n | while i < j: | n | i = 1 |
| | | j = len(numbers) # or 11 |
| | | |
| n | m = int( i + j / 2 ) | n | while i < j: |
| if needle < numbers[ m ]: | | |
| i = m + 1 | | |
| else: | | |
| j = m | | |
| | | |
| t | | t | m = int( i + j / 2 ) |
| | | if needle < numbers[ m ]: |
| | | i = m + 1 |
| | | else: |
| | | j = m |
| | | |
| if needle == m: | | if needle == m: |
| print True | | print True |
| | | |
| | | |
|
|
| Try to search to see if the number 7 is in the list or array. | | Try to search to see if the number 7 is in the list or array. |
| | | |
| n | | n | the needle is the number we are searching for. |
| | | |
| numbers = [-1,0,2,2,4,5,6,6,7,8,9] | | numbers = [-1,0,2,2,4,5,6,6,7,8,9] |
| n | | n | needle = 7 |
| | | |
| | | i = 1 |
| | | j = len(numbers) # or 11 |
| | | |
| | | while i < j: |
| | | |
| | | m = int( i + j / 2 ) |
| | | if needle < numbers[ m ]: |
| | | i = m + 1 |
| | | else: |
| | | j = m |
| | | |
| | | if needle == m: |
| | | print True |
| | | |
| | | |
| * Worst case in 8 item list is 3 compares. | | * Worst case in 8 item list is 3 compares. |
| **The Big-0 of this program is n O(logn) - base 2** | | **The Big-0 of this program is n O(logn) - base 2** |
| | | |
| t | | t | This is a very good algorythm! |
|
|
| | | |
| **The Big-0 of this program is n O(n)** | | **The Big-0 of this program is n O(n)** |
| t | | t | |
| | | |
| | | Search for a number in a Sorted array or list |
| | | =================================================== |
| | | |
| | | Try to search to see if the number 7 is in the list or array. |
| | | |
| | | numbers = [-1,0,2,2,4,5,6,6,7,8,9] |
| | | |
| | | * Worst case in 8 item list is 3 compares. |
| | | * Worst case in 16 item list is 4 compares. |
| | | * Worst case in 32 item list is 5 compares. |
| | | |
| | | **The Big-0 of this program is n O(logn) - base 2** |
| | | |
|
|
| max = number | | max = number |
| | | |
| t | **The Big-0 of this program is n O(n)**Search for a number in an array or list | t | **The Big-0 of this program is n O(n)** |
| | | |
| | | |
| | | Search for a number in an array or list |
| ============================================= | | ============================================= |
| | | |
|
|
| max = number | | max = number |
| | | |
| n | Search for a number in an array or list | n | **The Big-0 of this program is n O(n)**Search for a number in an array or list |
| ============================================= | | ============================================= |
| | | |
| if number == 0: | | if number == 0: |
| return True | | return True |
| t | | t | |
| | | **The Big-0 of this program is n O(n)** |
|
|
| n | | n | Find the largest number in a list or array of numbers. |
| | | ======================================================== |
| | | |
| n | numbers = [5,6,2,7,6,2,0,4] | n | .. code-block:: python |
| | | |
| n | max = a[0] | n | numbers = [5,6,2,7,6,2,0,4] |
| | | |
| n | | n | max = a[0] |
| | | |
| for number in numbers: | | for number in numbers: |
| | | |
| if number > max: | | if number > max: |
| max = number | | max = number |
| t | | t | |
| | | Search for a number in an array or list |
| | | ============================================= |
| | | |
| | | Try to search and see if the number 0 is in the list or array |
| | | |
| | | .. code-block:: python |
| | | |
| | | numbers = [5,6,2,7,6,2,0,4] |
| | | |
| | | for number in numbers: |
| | | if number == 0: |
| | | return True |
| | | |
|
|
| t | | t | |
| | | numbers = [5,6,2,7,6,2,0,4] |
| | | |
| | | max = a[0] |
| | | |
| | | for number in numbers: |
| | | |
| | | if number > max: |
| | | max = number |