Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update chapter 14 sorting #6

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/chapter_14_sorting.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/graph_data_structure.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/graph_search.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/graph_search_application.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/suffix_array.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"suffix_array.ipynb","version":"0.3.2","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"3s7lvSxaj9Fi","colab_type":"text"},"cell_type":"markdown","source":["## sort cyclic shifts of string s + '$' to construct the suffix array"]},{"metadata":{"id":"JjYArMUrkDIu","colab_type":"code","colab":{}},"cell_type":"code","source":["from collections import OrderedDict\n","\n","def getCharOrder(s):\n"," n = len(s)\n"," numChars = 256\n"," count = [0]*numChars # totally 256 chars, if you want, can print it out to see these chars\n"," \n"," order = [0]*(n)\n"," \n"," #count the occurrence of each char\n"," for c in s:\n"," count[ord(c)] += 1\n"," \n"," # prefix sum of each char\n"," for i in range(1, numChars):\n"," count[i] += count[i-1]\n"," \n"," # assign from count down to be stable\n"," for i in range(n-1,-1,-1):\n"," count[ord(s[i])] -=1\n"," order[count[ord(s[i])]] = i # put the index into the order instead the suffix string\n"," \n"," return order\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"CXthy2Rqt6nm","colab_type":"code","colab":{}},"cell_type":"code","source":["def getCharClass(order, cls):\n"," n = len(order)\n"," cls = [0]*n\n"," # if it all differs, then cls[i] = order[i]\n"," cls[order[0]] = 0 #the 6th will be 0\n"," for i in range(1, n):\n"," # use order[i] as index, so the last index\n"," if s[order[i]] != s[order[i-1]]:\n"," print('diff',s[order[i]],s[order[i-1]])\n"," cls[order[i]] = cls[order[i-1]] + 1\n"," else:\n"," cls[order[i]] = cls[order[i-1]]\n"," return cls\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"yfCeF3b77m4M","colab_type":"code","colab":{}},"cell_type":"code","source":["'''It is a counting sort using the first part as class'''\n","def sortDoubled(s, L, order, cls):\n"," n = len(s)\n"," count = [0] * n\n"," new_order = [0] * n\n"," # their key is the class\n"," for i in range(n):\n"," count[cls[i]] += 1\n"," \n"," # prefix sum\n"," for i in range(1, n):\n"," count[i] += count[i-1]\n"," \n"," # assign from count down to be stable\n"," # sort the first half\n"," for i in range(n-1, -1, -1):\n"," start = (order[i] - L + n) % n #get the start index of the first half, \n"," count[cls[start]] -= 1\n"," new_order[count[cls[start]]] = start\n"," \n"," return new_order"],"execution_count":0,"outputs":[]},{"metadata":{"id":"2I1nIvImB3O8","colab_type":"code","colab":{}},"cell_type":"code","source":["def updateClass(order, cls, L):\n"," n = len(order)\n"," new_cls = [0]*n\n"," # if it all differs, then cls[i] = order[i]\n"," new_cls[order[0]] = 0 #the 6th will be 0\n"," for i in range(1, n):\n"," cur_order, prev_order = order[i], order[i-1]\n"," # use order[i] as index, so the last index\n"," if cls[cur_order] != cls[prev_order] or cls[(cur_order+L) % n] != cls[(prev_order+L) % n]:\n"," new_cls[cur_order] = new_cls[prev_order] + 1\n"," else:\n"," new_cls[cur_order] = new_cls[prev_order]\n"," return new_cls"],"execution_count":0,"outputs":[]},{"metadata":{"id":"2Wv3muUHkNl_","colab_type":"code","colab":{}},"cell_type":"code","source":["\n","def cyclic_shifts_sort(s):\n"," s = s + '$'\n"," n = len(s)\n"," order = getCharOrder(s)\n"," cls = getCharClass(s, order)\n"," print(order, cls)\n"," L = 1\n"," while L < n:\n"," order = sortDoubled(s, 1, order, cls)\n"," cls = updateClass(order, cls, L)\n"," print(order, cls)\n"," L *= 2\n"," \n"," return order"],"execution_count":0,"outputs":[]},{"metadata":{"id":"OpqYr3Z6rsM_","colab_type":"code","outputId":"eb382cee-6f8d-459c-911f-10c955b45dad","executionInfo":{"status":"ok","timestamp":1549232253713,"user_tz":480,"elapsed":336,"user":{"displayName":"Li Yin","photoUrl":"https://lh5.googleusercontent.com/-KgiTKdqPRUg/AAAAAAAAAAI/AAAAAAAAAIE/aHQ6xO5vQpY/s64/photo.jpg","userId":"13365523799853678553"}},"colab":{"base_uri":"https://localhost:8080/","height":146}},"cell_type":"code","source":["s = 'ababaa'\n","cyclic_shifts_sort(s)"],"execution_count":32,"outputs":[{"output_type":"stream","text":["diff a $\n","diff b a\n","[6, 0, 2, 4, 5, 1, 3] [1, 2, 1, 2, 1, 1, 0]\n","[6, 5, 4, 0, 2, 1, 3] [3, 4, 3, 4, 2, 1, 0]\n","[6, 5, 4, 0, 2, 3, 1] [3, 6, 4, 5, 2, 1, 0]\n","[6, 5, 4, 0, 2, 3, 1] [3, 6, 4, 5, 2, 1, 0]\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":["[6, 5, 4, 0, 2, 3, 1]"]},"metadata":{"tags":[]},"execution_count":32}]},{"metadata":{"id":"5nNbGVRTjvvL","colab_type":"text"},"cell_type":"markdown","source":[""]}]}
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"suffix_array.ipynb","version":"0.3.2","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"3s7lvSxaj9Fi","colab_type":"text"},"cell_type":"markdown","source":["## sort cyclic shifts of string s + '$' to construct the suffix array"]},{"metadata":{"id":"JjYArMUrkDIu","colab_type":"code","colab":{}},"cell_type":"code","source":["from collections import OrderedDict\n","\n","def getCharOrder(s):\n"," n = len(s)\n"," numChars = 256\n"," count = [0]*numChars # total of 256 chars, if you want, can print it out to see these chars\n"," \n"," order = [0]*(n)\n"," \n"," #count the occurrence of each char\n"," for c in s:\n"," count[ord(c)] += 1\n"," \n"," # prefix sum of each char\n"," for i in range(1, numChars):\n"," count[i] += count[i-1]\n"," \n"," # assign from count down to be stable\n"," for i in range(n-1,-1,-1):\n"," count[ord(s[i])] -=1\n"," order[count[ord(s[i])]] = i # put the index into the order instead the suffix string\n"," \n"," return order\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"CXthy2Rqt6nm","colab_type":"code","colab":{}},"cell_type":"code","source":["def getCharClass(order, cls):\n"," n = len(order)\n"," cls = [0]*n\n"," # if it all differs, then cls[i] = order[i]\n"," cls[order[0]] = 0 #the 6th will be 0\n"," for i in range(1, n):\n"," # use order[i] as index, so the last index\n"," if s[order[i]] != s[order[i-1]]:\n"," print('diff',s[order[i]],s[order[i-1]])\n"," cls[order[i]] = cls[order[i-1]] + 1\n"," else:\n"," cls[order[i]] = cls[order[i-1]]\n"," return cls\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"yfCeF3b77m4M","colab_type":"code","colab":{}},"cell_type":"code","source":["'''It is a counting sort using the first part as class'''\n","def sortDoubled(s, L, order, cls):\n"," n = len(s)\n"," count = [0] * n\n"," new_order = [0] * n\n"," # their key is the class\n"," for i in range(n):\n"," count[cls[i]] += 1\n"," \n"," # prefix sum\n"," for i in range(1, n):\n"," count[i] += count[i-1]\n"," \n"," # assign from count down to be stable\n"," # sort the first half\n"," for i in range(n-1, -1, -1):\n"," start = (order[i] - L + n) % n #get the start index of the first half, \n"," count[cls[start]] -= 1\n"," new_order[count[cls[start]]] = start\n"," \n"," return new_order"],"execution_count":0,"outputs":[]},{"metadata":{"id":"2I1nIvImB3O8","colab_type":"code","colab":{}},"cell_type":"code","source":["def updateClass(order, cls, L):\n"," n = len(order)\n"," new_cls = [0]*n\n"," # if it all differs, then cls[i] = order[i]\n"," new_cls[order[0]] = 0 #the 6th will be 0\n"," for i in range(1, n):\n"," cur_order, prev_order = order[i], order[i-1]\n"," # use order[i] as index, so the last index\n"," if cls[cur_order] != cls[prev_order] or cls[(cur_order+L) % n] != cls[(prev_order+L) % n]:\n"," new_cls[cur_order] = new_cls[prev_order] + 1\n"," else:\n"," new_cls[cur_order] = new_cls[prev_order]\n"," return new_cls"],"execution_count":0,"outputs":[]},{"metadata":{"id":"2Wv3muUHkNl_","colab_type":"code","colab":{}},"cell_type":"code","source":["\n","def cyclic_shifts_sort(s):\n"," s = s + '$'\n"," n = len(s)\n"," order = getCharOrder(s)\n"," cls = getCharClass(s, order)\n"," print(order, cls)\n"," L = 1\n"," while L < n:\n"," order = sortDoubled(s, 1, order, cls)\n"," cls = updateClass(order, cls, L)\n"," print(order, cls)\n"," L *= 2\n"," \n"," return order"],"execution_count":0,"outputs":[]},{"metadata":{"id":"OpqYr3Z6rsM_","colab_type":"code","outputId":"eb382cee-6f8d-459c-911f-10c955b45dad","executionInfo":{"status":"ok","timestamp":1549232253713,"user_tz":480,"elapsed":336,"user":{"displayName":"Li Yin","photoUrl":"https://lh5.googleusercontent.com/-KgiTKdqPRUg/AAAAAAAAAAI/AAAAAAAAAIE/aHQ6xO5vQpY/s64/photo.jpg","userId":"13365523799853678553"}},"colab":{"base_uri":"https://localhost:8080/","height":146}},"cell_type":"code","source":["s = 'ababaa'\n","cyclic_shifts_sort(s)"],"execution_count":32,"outputs":[{"output_type":"stream","text":["diff a $\n","diff b a\n","[6, 0, 2, 4, 5, 1, 3] [1, 2, 1, 2, 1, 1, 0]\n","[6, 5, 4, 0, 2, 1, 3] [3, 4, 3, 4, 2, 1, 0]\n","[6, 5, 4, 0, 2, 3, 1] [3, 6, 4, 5, 2, 1, 0]\n","[6, 5, 4, 0, 2, 3, 1] [3, 6, 4, 5, 2, 1, 0]\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":["[6, 5, 4, 0, 2, 3, 1]"]},"metadata":{"tags":[]},"execution_count":32}]},{"metadata":{"id":"5nNbGVRTjvvL","colab_type":"text"},"cell_type":"markdown","source":[""]}]}
2 changes: 1 addition & 1 deletion Colab Codes/Colab Notebooks/tree_data_structure.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"tree_data_structure.ipynb","version":"0.3.2","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"3A2mprldYvXe","colab_type":"text"},"cell_type":"markdown","source":["### N-aray Tree"]},{"metadata":{"id":"h5LNx9p_ZxtH","colab_type":"text"},"cell_type":"markdown","source":["#### Define Tree Node"]},{"metadata":{"id":"9R_XYWC2Yz-G","colab_type":"code","colab":{}},"cell_type":"code","source":["class NaryNode:\n"," '''Define a n-ary node'''\n"," def __init__(self, n, val):\n"," self.children = [None] * n\n"," self.val = val\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"Xx5WEdJDZ4-q","colab_type":"text"},"cell_type":"markdown","source":["#### Define a Tree and implement operations"]},{"metadata":{"id":"qtAos_DOZwkw","colab_type":"code","colab":{}},"cell_type":"code","source":[""],"execution_count":0,"outputs":[]},{"metadata":{"id":"wLPjQrfnZbQY","colab_type":"text"},"cell_type":"markdown","source":["### Binary Tree"]},{"metadata":{"id":"oUZYimKbZ1ZL","colab_type":"text"},"cell_type":"markdown","source":["#### Define Tree Node"]},{"metadata":{"id":"ZKknhNURZdA0","colab_type":"code","colab":{}},"cell_type":"code","source":["class BinaryNode:\n"," '''Define a classical binary tree node'''\n"," def __init__(self, val):\n"," self.left = None\n"," self.right = None\n"," self.val = val"],"execution_count":0,"outputs":[]},{"metadata":{"id":"yPukXXkXZ_zn","colab_type":"text"},"cell_type":"markdown","source":["#### Define a Tree and implement operations\n"," 1\n"," / \\ \n"," 2 3\n"," / \\ \\\n","4 5 6 "]},{"metadata":{"id":"CyXwZ9sf8dyG","colab_type":"code","colab":{}},"cell_type":"code","source":["root = BinaryNode(1)\n","left = BinaryNode(2)\n","right = BinaryNode(3)\n","root.left = left\n","root.right = right\n","left.left = BinaryNode(4)\n","left.right = BinaryNode(5)\n","right.right = BinaryNode(6)"],"execution_count":0,"outputs":[]},{"metadata":{"id":"gOHqKKfY_Ug0","colab_type":"code","colab":{}},"cell_type":"code","source":["def constructTree(a, idx):\n"," '''construct a binary tree recursively from input array a'''\n"," if idx >= len(a):\n"," return None\n"," node = BinaryNode(a[idx])\n"," node.left = constructTree(a, 2*idx + 1)\n"," node.right = constructTree(a, 2*idx + 2)\n"," return node"],"execution_count":0,"outputs":[]},{"metadata":{"id":"STvuN-5p_5Di","colab_type":"code","colab":{}},"cell_type":"code","source":["nums = [1, 2, 3, 4, 5, None, 6]\n","root = constructTree(nums, 0)"],"execution_count":0,"outputs":[]},{"metadata":{"id":"hrJC3v5ZAvD-","colab_type":"text"},"cell_type":"markdown","source":["#### To show the nodes at each level, we use LevelOrder function to print out the tree:"]},{"metadata":{"id":"uPxVdjRrA0UB","colab_type":"code","colab":{}},"cell_type":"code","source":["def LevelOrder(root):\n"," q = [root]\n"," while q:\n"," new_q = []\n"," for n in q:\n"," if n is not None:\n"," print(n.val, end=',')\n"," if n.left:\n"," new_q.append(n.left)\n"," if n.right:\n"," new_q.append(n.right)\n"," q = new_q\n"," print('\\n')\n"],"execution_count":0,"outputs":[]},{"metadata":{"id":"Pl2HGcHkA4rT","colab_type":"code","outputId":"2282a8ed-e1c1-4dae-b83c-0c2a8c19b470","executionInfo":{"status":"ok","timestamp":1550969967703,"user_tz":480,"elapsed":336,"user":{"displayName":"Li Yin","photoUrl":"https://lh5.googleusercontent.com/-KgiTKdqPRUg/AAAAAAAAAAI/AAAAAAAAAIE/aHQ6xO5vQpY/s64/photo.jpg","userId":"13365523799853678553"}},"colab":{"base_uri":"https://localhost:8080/","height":127}},"cell_type":"code","source":["LevelOrder(root)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["1,\n","\n","2,3,\n","\n","4,5,None,6,\n","\n"],"name":"stdout"}]},{"metadata":{"id":"2edkOHbCCV7n","colab_type":"text"},"cell_type":"markdown","source":[""]}]}
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"tree_data_structure.ipynb","version":"0.3.2","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"metadata":{"id":"3A2mprldYvXe","colab_type":"text"},"cell_type":"markdown","source":["### N-ary Tree"]},{"metadata":{"id":"h5LNx9p_ZxtH","colab_type":"text"},"cell_type":"markdown","source":["#### Define Tree Node"]},{"metadata":{"id":"9R_XYWC2Yz-G","colab_type":"code","colab":{}},"cell_type":"code","source":["class NaryNode:\n"," '''Define a n-ary node'''\n"," def __init__(self, n, val):\n"," self.children = [None] * n\n"," self.val = val\n"," "],"execution_count":0,"outputs":[]},{"metadata":{"id":"Xx5WEdJDZ4-q","colab_type":"text"},"cell_type":"markdown","source":["#### Define a Tree and implement operations"]},{"metadata":{"id":"qtAos_DOZwkw","colab_type":"code","colab":{}},"cell_type":"code","source":[""],"execution_count":0,"outputs":[]},{"metadata":{"id":"wLPjQrfnZbQY","colab_type":"text"},"cell_type":"markdown","source":["### Binary Tree"]},{"metadata":{"id":"oUZYimKbZ1ZL","colab_type":"text"},"cell_type":"markdown","source":["#### Define Tree Node"]},{"metadata":{"id":"ZKknhNURZdA0","colab_type":"code","colab":{}},"cell_type":"code","source":["class BinaryNode:\n"," '''Define a classical binary tree node'''\n"," def __init__(self, val):\n"," self.left = None\n"," self.right = None\n"," self.val = val"],"execution_count":0,"outputs":[]},{"metadata":{"id":"yPukXXkXZ_zn","colab_type":"text"},"cell_type":"markdown","source":["#### Define a Tree and implement operations\n"," 1\n"," / \\ \n"," 2 3\n"," / \\ \\\n","4 5 6 "]},{"metadata":{"id":"CyXwZ9sf8dyG","colab_type":"code","colab":{}},"cell_type":"code","source":["root = BinaryNode(1)\n","left = BinaryNode(2)\n","right = BinaryNode(3)\n","root.left = left\n","root.right = right\n","left.left = BinaryNode(4)\n","left.right = BinaryNode(5)\n","right.right = BinaryNode(6)"],"execution_count":0,"outputs":[]},{"metadata":{"id":"gOHqKKfY_Ug0","colab_type":"code","colab":{}},"cell_type":"code","source":["def constructTree(a, idx):\n"," '''construct a binary tree recursively from input array a'''\n"," if idx >= len(a):\n"," return None\n"," node = BinaryNode(a[idx])\n"," node.left = constructTree(a, 2*idx + 1)\n"," node.right = constructTree(a, 2*idx + 2)\n"," return node"],"execution_count":0,"outputs":[]},{"metadata":{"id":"STvuN-5p_5Di","colab_type":"code","colab":{}},"cell_type":"code","source":["nums = [1, 2, 3, 4, 5, None, 6]\n","root = constructTree(nums, 0)"],"execution_count":0,"outputs":[]},{"metadata":{"id":"hrJC3v5ZAvD-","colab_type":"text"},"cell_type":"markdown","source":["#### To show the nodes at each level, we use the LevelOrder function to print out the tree:"]},{"metadata":{"id":"uPxVdjRrA0UB","colab_type":"code","colab":{}},"cell_type":"code","source":["def LevelOrder(root):\n"," q = [root]\n"," while q:\n"," new_q = []\n"," for n in q:\n"," if n is not None:\n"," print(n.val, end=',')\n"," if n.left:\n"," new_q.append(n.left)\n"," if n.right:\n"," new_q.append(n.right)\n"," q = new_q\n"," print('\\n')\n"],"execution_count":0,"outputs":[]},{"metadata":{"id":"Pl2HGcHkA4rT","colab_type":"code","outputId":"2282a8ed-e1c1-4dae-b83c-0c2a8c19b470","executionInfo":{"status":"ok","timestamp":1550969967703,"user_tz":480,"elapsed":336,"user":{"displayName":"Li Yin","photoUrl":"https://lh5.googleusercontent.com/-KgiTKdqPRUg/AAAAAAAAAAI/AAAAAAAAAIE/aHQ6xO5vQpY/s64/photo.jpg","userId":"13365523799853678553"}},"colab":{"base_uri":"https://localhost:8080/","height":127}},"cell_type":"code","source":["LevelOrder(root)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["1,\n","\n","2,3,\n","\n","4,5,None,6,\n","\n"],"name":"stdout"}]},{"metadata":{"id":"2edkOHbCCV7n","colab_type":"text"},"cell_type":"markdown","source":[""]}]}
Loading