├── Visual ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png └── 6.png ├── DP ├── Sterling_thief.py ├── Maximum_increasing_subsequence_length.py ├── Matrix_chain_multiplication.py ├── Maximum_increasing_subsequence_sum.py ├── Minimum_pallindromic_cuts_dp.py ├── NQueen_Dp.py ├── Fibo.ipynb └── Subset_sum_problem.py ├── Searching ├── LinearSearch.ipynb └── BinarySearch.ipynb ├── Sorting ├── SelectionSort.ipynb ├── InsertionSort.ipynb ├── QuickSort.ipynb ├── HeapSort.ipynb ├── MergeSort.ipynb └── BubbleSort.ipynb ├── Graph ├── DFS.ipynb ├── BFS.ipynb ├── dijkstra.py ├── bellmanford.py ├── prims.py └── kruskal.py ├── MISC ├── Queue.ipynb └── Stack.ipynb ├── .gitignore ├── Linkedlist └── Linkedlist.ipynb ├── Tree ├── Heap.ipynb └── BST.ipynb ├── README.md ├── LICENSE └── svg ├── Model!Main_0.svg └── Model!DataFlowModel1!DFDDiagram1_1.svg /Visual/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/1.png -------------------------------------------------------------------------------- /Visual/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/2.png -------------------------------------------------------------------------------- /Visual/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/3.png -------------------------------------------------------------------------------- /Visual/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/4.png -------------------------------------------------------------------------------- /Visual/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/5.png -------------------------------------------------------------------------------- /Visual/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soumyadip007/Data-Structure-and-Algorithm-Using-Python/HEAD/Visual/6.png -------------------------------------------------------------------------------- /DP/Sterling_thief.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | n = int(input()) 3 | arr = list(map(int,input().split())) 4 | if n==1:print(arr[0]) 5 | elif n==2:print(max(arr[0],arr[1])) 6 | else: 7 | dp = [arr[0],max(arr[0],arr[1])] 8 | for i in range(2,n):dp.append(max(dp[i-1],arr[i]+dp[i-2])) 9 | print(dp[n-1]) -------------------------------------------------------------------------------- /DP/Maximum_increasing_subsequence_length.py: -------------------------------------------------------------------------------- 1 | def lis(arr,n): 2 | lis = [1]*n 3 | for i in range(1,n): 4 | for j in range(0,i): 5 | if arr[j]= set[i - 1]: 29 | subset[i][j] = (subset[i - 1][j] or 30 | subset[i - 1][j - set[i - 1]]) 31 | 32 | # uncomment this code to print table 33 | # for i in range(n+1): 34 | # for j in range(sum+1): 35 | # print (subset[i][j],end=" ") 36 | # print() 37 | return subset[n][sum] 38 | 39 | 40 | # Driver program to test above function 41 | if __name__ == '__main__': 42 | set = [3, 34, 4, 12, 5, 2] 43 | sum = 9 44 | n = len(set) 45 | if (isSubsetSum(set, n, sum) == True): 46 | print("Found a subset with given sum") 47 | else: 48 | print("No subset with given sum") 49 | 50 | # This code is contributed by 51 | # sahil shelangia. 52 | -------------------------------------------------------------------------------- /Sorting/SelectionSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdin", 10 | "output_type": "stream", 11 | "text": [ 12 | "Enter N 3\n", 13 | " 4\n", 14 | " 5\n", 15 | " 2\n" 16 | ] 17 | }, 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "Sorted array is:\n", 23 | "2\n", 24 | "4\n", 25 | "5\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "def Selection(arr):\n", 31 | " n = len(arr)\n", 32 | "\n", 33 | " for i in range(len(arr)): \n", 34 | " min_idx = i \n", 35 | " for j in range(i+1, len(arr)): \n", 36 | " if arr[min_idx] > arr[j]: \n", 37 | " min_idx = j \n", 38 | " arr[i], arr[min_idx] = arr[min_idx], arr[i] \n", 39 | " \n", 40 | "\n", 41 | "arr = []\n", 42 | "n=int(input(\"Enter N\"))\n", 43 | "for x in range(n):\n", 44 | " arr.append(int(input()))\n", 45 | "\n", 46 | "Selection(arr)\n", 47 | " \n", 48 | "print (\"Sorted array is:\")\n", 49 | "for i in range(len(arr)):\n", 50 | " print (arr[i])," 51 | ] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.7.4" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 4 75 | } 76 | -------------------------------------------------------------------------------- /Sorting/InsertionSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdin", 10 | "output_type": "stream", 11 | "text": [ 12 | "Enter N 4\n", 13 | " 3\n", 14 | " 2\n", 15 | " 5\n", 16 | " 6\n" 17 | ] 18 | }, 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "Sorted array is:\n", 24 | "2\n", 25 | "3\n", 26 | "5\n", 27 | "6\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "def Selection(arr):\n", 33 | " n = len(arr)\n", 34 | "\n", 35 | " for i in range(1, len(arr)): \n", 36 | " key = arr[i] \n", 37 | " j = i-1\n", 38 | " while j >=0 and key < arr[j] : \n", 39 | " arr[j+1] = arr[j] \n", 40 | " j -= 1\n", 41 | " arr[j+1] = key \n", 42 | " \n", 43 | "\n", 44 | "arr = []\n", 45 | "n=int(input(\"Enter N\"))\n", 46 | "for x in range(n):\n", 47 | " arr.append(int(input()))\n", 48 | "\n", 49 | "Selection(arr)\n", 50 | " \n", 51 | "print (\"Sorted array is:\")\n", 52 | "for i in range(len(arr)):\n", 53 | " print (arr[i])," 54 | ] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.7.4" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 4 78 | } 79 | -------------------------------------------------------------------------------- /Sorting/QuickSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Sorted array is:\n", 13 | "1\n", 14 | "5\n", 15 | "7\n", 16 | "8\n", 17 | "9\n", 18 | "10\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "def partition(arr,low,high): \n", 24 | " i = ( low-1 ) \n", 25 | " pivot = arr[high]\n", 26 | " \n", 27 | " for j in range(low , high): \n", 28 | " if arr[j] <= pivot: \n", 29 | " i = i+1 \n", 30 | " arr[i],arr[j] = arr[j],arr[i] \n", 31 | " arr[i+1],arr[high] = arr[high],arr[i+1] \n", 32 | " return ( i+1 ) \n", 33 | " \n", 34 | "def quickSort(arr,low,high): \n", 35 | " if low < high:\n", 36 | " pi = partition(arr,low,high) \n", 37 | " quickSort(arr, low, pi-1) \n", 38 | " quickSort(arr, pi+1, high) \n", 39 | " \n", 40 | "\n", 41 | "arr = [10, 7, 8, 9, 1, 5] \n", 42 | "n = len(arr) \n", 43 | "quickSort(arr,0,n-1) \n", 44 | "print (\"Sorted array is:\") \n", 45 | "for i in range(n): \n", 46 | " print (\"%d\" %arr[i]), " 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.4" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 4 71 | } 72 | -------------------------------------------------------------------------------- /Graph/DFS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "defaultdict(, {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]})\n", 13 | "2 \n", 14 | "0 \n", 15 | "1 \n", 16 | "3 \n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "from collections import defaultdict \n", 22 | "\n", 23 | "class Graph:\n", 24 | " \n", 25 | " def __init__(self):\n", 26 | " self.graph=defaultdict(list)\n", 27 | " \n", 28 | " def addEdge(self,u,v):\n", 29 | " self.graph[u].append(v)\n", 30 | " \n", 31 | " def DFS(self, v, visited):\n", 32 | " visited[v]=True\n", 33 | " print(v,\" \")\n", 34 | " for i in self.graph[v]:\n", 35 | " if visited[i]==False:\n", 36 | " self.DFS(i,visited)\n", 37 | " \n", 38 | " \n", 39 | "g = Grah() \n", 40 | "g.addEdge(0, 1) \n", 41 | "g.addEdge(0, 2) \n", 42 | "g.addEdge(1, 2) \n", 43 | "g.addEdge(2, 0) \n", 44 | "g.addEdge(2, 3) \n", 45 | "g.addEdge(3, 3) \n", 46 | "print(g.graph)\n", 47 | "visited = [False] * (len(g.graph)) \n", 48 | "g.DFS(2,visited) " 49 | ] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.7.4" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 4 73 | } 74 | -------------------------------------------------------------------------------- /MISC/Queue.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "3\n", 13 | "Dequeue: 10\n", 14 | "Dequeue: 20\n", 15 | "1\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "\n", 21 | "#FIFO - first -> first\n", 22 | "\n", 23 | "class Queue:\n", 24 | "\n", 25 | " def __init__(self):\n", 26 | " self.queue = []\n", 27 | "\n", 28 | " def isEmpty(self):\n", 29 | " return self.queue == []\n", 30 | "\n", 31 | " def enqueue(self, data):\n", 32 | " self.queue.append(data)\n", 33 | " \n", 34 | " def dequeue(self):\n", 35 | " data = self.queue[0]\n", 36 | " del self.queue[0]\n", 37 | " return data\n", 38 | " \n", 39 | " def peek(self):\n", 40 | " return self.queue[0]\n", 41 | "\n", 42 | " def sizeQueue(self):\n", 43 | " return len(self.queue)\n", 44 | "\n", 45 | "queue = Queue()\n", 46 | "queue.enqueue(10)\n", 47 | "queue.enqueue(20)\n", 48 | "queue.enqueue(30)\n", 49 | "print(queue.sizeQueue())\n", 50 | "print(\"Dequeue: \", queue.dequeue())\n", 51 | "print(\"Dequeue: \", queue.dequeue())\n", 52 | "print(queue.sizeQueue())" 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.4" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 4 77 | } 78 | -------------------------------------------------------------------------------- /MISC/Stack.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "3\n", 13 | "Popped: 3\n", 14 | "Popped: 2\n", 15 | "1\n", 16 | "Peek: 1\n", 17 | "1\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "\n", 23 | "class Stack:\n", 24 | "\n", 25 | " def __init__(self):\n", 26 | " self.stack = []\n", 27 | " \n", 28 | " def isEmpty(self):\n", 29 | " return self.stack == []\n", 30 | "\n", 31 | " def push(self, data):\n", 32 | " self.stack.append(data)\n", 33 | "\n", 34 | " def pop(self):\n", 35 | " data = self.stack[-1]\n", 36 | " del self.stack[-1]\n", 37 | " return data\n", 38 | "\n", 39 | " def peek(self):\n", 40 | " return self.stack[-1]\n", 41 | "\n", 42 | " def sizeStack(self):\n", 43 | " return len(self.stack)\n", 44 | " \n", 45 | "stack = Stack()\n", 46 | "stack.push(1)\n", 47 | "stack.push(2)\n", 48 | "stack.push(3)\n", 49 | "print(stack.sizeStack())\n", 50 | "print(\"Popped: \", stack.pop())\n", 51 | "print(\"Popped: \", stack.pop())\n", 52 | "print(stack.sizeStack())\n", 53 | "print(\"Peek:\", stack.peek())\n", 54 | "print(stack.sizeStack())" 55 | ] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.7.4" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 4 79 | } 80 | -------------------------------------------------------------------------------- /Sorting/HeapSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Sorted array is\n", 13 | "5\n", 14 | "6\n", 15 | "7\n", 16 | "11\n", 17 | "12\n", 18 | "13\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "def heapify(arr, n, i): \n", 24 | " largest = i \n", 25 | " l = 2 * i + 1 \n", 26 | " r = 2 * i + 2 \n", 27 | "\n", 28 | " if l < n and arr[i] < arr[l]: \n", 29 | " largest = l \n", 30 | " \n", 31 | " if r < n and arr[largest] < arr[r]: \n", 32 | " largest = r \n", 33 | " \n", 34 | " if largest != i: \n", 35 | " arr[i],arr[largest] = arr[largest],arr[i] # swap \n", 36 | " \n", 37 | " heapify(arr, n, largest) \n", 38 | " \n", 39 | " \n", 40 | "def heapSort(arr): \n", 41 | " n = len(arr) \n", 42 | " for i in range(n, -1, -1): \n", 43 | " heapify(arr, n, i) \n", 44 | " \n", 45 | " for i in range(n-1, 0, -1): \n", 46 | " arr[i], arr[0] = arr[0], arr[i] # swap \n", 47 | " heapify(arr, i, 0) \n", 48 | " \n", 49 | "arr = [ 12, 11, 13, 5, 6, 7] \n", 50 | "heapSort(arr) \n", 51 | "n = len(arr) \n", 52 | "print (\"Sorted array is\") \n", 53 | "for i in range(n): \n", 54 | " print (\"%d\" %arr[i]), " 55 | ] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.7.4" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 4 79 | } 80 | -------------------------------------------------------------------------------- /Graph/BFS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 22, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "defaultdict(, {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]})\n", 13 | "2 \n", 14 | "0 \n", 15 | "3 \n", 16 | "1 \n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "from collections import defaultdict \n", 22 | "\n", 23 | "class Graph:\n", 24 | " \n", 25 | " def __init__(self):\n", 26 | " self.graph=defaultdict(list)\n", 27 | " \n", 28 | " def addEdge(self,u,v):\n", 29 | " self.graph[u].append(v)\n", 30 | " \n", 31 | " def BFS(self, s):\n", 32 | " \n", 33 | " visited= [False]*len(self.graph)\n", 34 | " queue=[]\n", 35 | " \n", 36 | " queue.append(s)\n", 37 | " visited[s]=True\n", 38 | " \n", 39 | " while queue:\n", 40 | " \n", 41 | " s=queue.pop(0)\n", 42 | " print(s,\" \")\n", 43 | " \n", 44 | " for i in self.graph[s]:\n", 45 | " if visited[i]==False:\n", 46 | " visited[i]=True\n", 47 | " queue.append(i)\n", 48 | " \n", 49 | " \n", 50 | "g = Graph() \n", 51 | "g.addEdge(0, 1) \n", 52 | "g.addEdge(0, 2) \n", 53 | "g.addEdge(1, 2) \n", 54 | "g.addEdge(2, 0) \n", 55 | "g.addEdge(2, 3) \n", 56 | "g.addEdge(3, 3) \n", 57 | "print(g.graph)\n", 58 | "g.BFS(2) " 59 | ] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.4" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 4 83 | } 84 | -------------------------------------------------------------------------------- /Sorting/MergeSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdin", 10 | "output_type": "stream", 11 | "text": [ 12 | "Enter the list of numbers: 5 6 3 5 2 1\n" 13 | ] 14 | }, 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Sorted list: [1, 2, 3, 5, 5, 6]\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "def merge_sort(alist, start, end):\n", 25 | " if end - start > 1:\n", 26 | " mid = (start + end)//2\n", 27 | " merge_sort(alist, start, mid)\n", 28 | " merge_sort(alist, mid, end)\n", 29 | " merge_list(alist, start, mid, end)\n", 30 | " \n", 31 | "def merge_list(alist, start, mid, end):\n", 32 | " left = alist[start:mid]\n", 33 | " right = alist[mid:end]\n", 34 | " k = start\n", 35 | " i = 0\n", 36 | " j = 0\n", 37 | " while (start + i < mid and mid + j < end):\n", 38 | " if (left[i] <= right[j]):\n", 39 | " alist[k] = left[i]\n", 40 | " i = i + 1\n", 41 | " else:\n", 42 | " alist[k] = right[j]\n", 43 | " j = j + 1\n", 44 | " k = k + 1\n", 45 | " if start + i < mid:\n", 46 | " while k < end:\n", 47 | " alist[k] = left[i]\n", 48 | " i = i + 1\n", 49 | " k = k + 1\n", 50 | " else:\n", 51 | " while k < end:\n", 52 | " alist[k] = right[j]\n", 53 | " j = j + 1\n", 54 | " k = k + 1\n", 55 | " \n", 56 | " \n", 57 | "alist = input('Enter the list of numbers: ').split()\n", 58 | "alist = [int(x) for x in alist]\n", 59 | "merge_sort(alist, 0, len(alist))\n", 60 | "print('Sorted list: ', end='')\n", 61 | "print(alist)" 62 | ] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.7.4" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 4 86 | } 87 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /Graph/dijkstra.py: -------------------------------------------------------------------------------- 1 | import sys; 2 | import heapq; 3 | 4 | class Edge(object): 5 | 6 | def __init__(self, weight, startVertex, targetVertex): 7 | self.weight = weight; 8 | self.startVertex = startVertex; 9 | self.targetVertex = targetVertex; 10 | 11 | class Node(object): 12 | 13 | def __init__(self, name): 14 | self.name = name; 15 | self.visited = False; 16 | self.predecessor = None; 17 | self.adjacenciesList = []; 18 | self.minDistance = sys.maxsize; 19 | 20 | def __cmp__(self, otherVertex): 21 | return self.cmp(self.minDistance, otherVertex.minDistance); 22 | 23 | def __lt__(self, other): 24 | selfPriority = self.minDistance; 25 | otherPriority = other.minDistance; 26 | return selfPriority < otherPriority; 27 | 28 | class Algorithm(object): 29 | 30 | def calculateShortestPath(self, vertexList, startVertex): 31 | 32 | q = []; 33 | startVertex.minDistance = 0; 34 | heapq.heappush(q, startVertex); 35 | 36 | while q: 37 | 38 | actualVertex = heapq.heappop(q); 39 | 40 | for edge in actualVertex.adjacenciesList: 41 | u = edge.startVertex; 42 | v = edge.targetVertex; 43 | newDistance = u.minDistance + edge.weight; 44 | 45 | if newDistance < v.minDistance: 46 | v.predecessor = u; 47 | v.minDistance = newDistance; 48 | heapq.heappush(q, v); 49 | 50 | def getShortestPathTo(self, targetVertex): 51 | print("Shortest path to vertex is: ", targetVertex.minDistance); 52 | 53 | node = targetVertex; 54 | 55 | while node is not None: 56 | print("%s " % node.name); 57 | node = node.predecessor; 58 | 59 | node1 = Node("A"); 60 | node2 = Node("B"); 61 | node3 = Node("C"); 62 | node4 = Node("D"); 63 | node5 = Node("E"); 64 | node6 = Node("F"); 65 | node7 = Node("G"); 66 | node8 = Node("H"); 67 | 68 | edge1 = Edge(5,node1,node2); 69 | edge2 = Edge(8,node1,node8); 70 | edge3 = Edge(9,node1,node5); 71 | edge4 = Edge(15,node2,node4); 72 | edge5 = Edge(12,node2,node3); 73 | edge6 = Edge(4,node2,node8); 74 | edge7 = Edge(7,node8,node3); 75 | edge8 = Edge(6,node8,node6); 76 | edge9 = Edge(5,node5,node8); 77 | edge10 = Edge(4,node5,node6); 78 | edge11 = Edge(20,node5,node7); 79 | edge12 = Edge(1,node6,node3); 80 | edge13 = Edge(13,node6,node7); 81 | edge14 = Edge(3,node3,node4); 82 | edge15 = Edge(11,node3,node7); 83 | edge16 = Edge(9,node4,node7); 84 | 85 | node1.adjacenciesList.append(edge1); 86 | node1.adjacenciesList.append(edge2); 87 | node1.adjacenciesList.append(edge3); 88 | node2.adjacenciesList.append(edge4); 89 | node2.adjacenciesList.append(edge5); 90 | node2.adjacenciesList.append(edge6); 91 | node8.adjacenciesList.append(edge7); 92 | node8.adjacenciesList.append(edge8); 93 | node5.adjacenciesList.append(edge9); 94 | node5.adjacenciesList.append(edge10); 95 | node5.adjacenciesList.append(edge11); 96 | node6.adjacenciesList.append(edge12); 97 | node6.adjacenciesList.append(edge13); 98 | node3.adjacenciesList.append(edge14); 99 | node3.adjacenciesList.append(edge15); 100 | node4.adjacenciesList.append(edge16); 101 | 102 | 103 | vertexList = (node1,node2,node3, node4, node5, node6, node7, node8); 104 | 105 | algorithm = Algorithm(); 106 | algorithm.calculateShortestPath(vertexList, node1); 107 | algorithm.getShortestPathTo(node4); -------------------------------------------------------------------------------- /Graph/bellmanford.py: -------------------------------------------------------------------------------- 1 | import sys; 2 | 3 | class Node(object): 4 | 5 | def __init__(self, name): 6 | self.name = name; 7 | self.visited = False; 8 | self.predecessor = None; 9 | self.adjacenciesList = []; 10 | self.minDistance = sys.maxsize; 11 | 12 | class Edge(object): 13 | 14 | def __init__(self, weight, startVertex, targetVertex): 15 | self.weight = weight; 16 | self.startVertex = startVertex; 17 | self.targetVertex = targetVertex; 18 | 19 | class BellmanFord(object): 20 | 21 | HAS_CYCLE = False; 22 | 23 | def calculateShortestPath(self, vertexList, edgeList, startVertex): 24 | 25 | startVertex.minDistance = 0; 26 | 27 | for i in range(0,len(vertexList)-1): 28 | for edge in edgeList: 29 | 30 | u = edge.startVertex; 31 | v = edge.targetVertex; 32 | 33 | newDistance = u.minDistance + edge.weight; 34 | 35 | if newDistance < v.minDistance: 36 | v.minDistance = newDistance; 37 | v.predecessor = u; 38 | 39 | for edge in edgeList: 40 | if self.hasCycle(edge): 41 | print("Negative cycle detected..."); 42 | BellmanFord.HAS_CYCLE = True; 43 | return; 44 | 45 | def hasCycle(self, edge): 46 | if (edge.startVertex.minDistance + edge.weight) < edge.targetVertex.minDistance: 47 | return True; 48 | else: 49 | return False; 50 | 51 | def getShortestPathTo(self, targetVertex): 52 | 53 | if not BellmanFord.HAS_CYCLE: 54 | print("Shortest path exists with value: ", targetVertex.minDistance); 55 | 56 | node = targetVertex; 57 | 58 | while node is not None: 59 | print("%s " % node.name); 60 | node = node.predecessor; 61 | else: 62 | print("Negative cycle detected..."); 63 | 64 | 65 | node1 = Node("A"); 66 | node2 = Node("B"); 67 | node3 = Node("C"); 68 | node4 = Node("D"); 69 | node5 = Node("E"); 70 | node6 = Node("F"); 71 | node7 = Node("G"); 72 | node8 = Node("H"); 73 | 74 | edge1 = Edge(5,node1,node2); 75 | edge2 = Edge(8,node1,node8); 76 | edge3 = Edge(9,node1,node5); 77 | edge4 = Edge(15,node2,node4); 78 | edge5 = Edge(12,node2,node3); 79 | edge6 = Edge(4,node2,node8); 80 | edge7 = Edge(7,node8,node3); 81 | edge8 = Edge(6,node8,node6); 82 | edge9 = Edge(5,node5,node8); 83 | edge10 = Edge(4,node5,node6); 84 | edge11 = Edge(20,node5,node7); 85 | edge12 = Edge(1,node6,node3); 86 | edge13 = Edge(13,node6,node7); 87 | edge14 = Edge(3,node3,node4); 88 | edge15 = Edge(11,node3,node7); 89 | edge16 = Edge(9,node4,node7); 90 | 91 | edge17 = Edge(1,node1,node2); 92 | edge18 = Edge(1,node2,node3); 93 | edge19 = Edge(-3,node3,node1); 94 | 95 | node1.adjacenciesList.append(edge1); 96 | node1.adjacenciesList.append(edge2); 97 | node1.adjacenciesList.append(edge3); 98 | node2.adjacenciesList.append(edge4); 99 | node2.adjacenciesList.append(edge5); 100 | node2.adjacenciesList.append(edge6); 101 | node8.adjacenciesList.append(edge7); 102 | node8.adjacenciesList.append(edge8); 103 | node5.adjacenciesList.append(edge9); 104 | node5.adjacenciesList.append(edge10); 105 | node5.adjacenciesList.append(edge11); 106 | node6.adjacenciesList.append(edge12); 107 | node6.adjacenciesList.append(edge13); 108 | node3.adjacenciesList.append(edge14); 109 | node3.adjacenciesList.append(edge15); 110 | node4.adjacenciesList.append(edge16); 111 | 112 | 113 | vertexList = (node1,node2,node3, node4, node5, node6, node7, node8); 114 | #edgeList = (edge1,edge2,edge3,edge4,edge5,edge6,edge7,edge8,edge9,edge10,edge11,edge12,edge13,edge14,edge15,edge16); 115 | edgeList = (edge17, edge18, edge19); 116 | 117 | algorithm = BellmanFord(); 118 | algorithm.calculateShortestPath(vertexList, edgeList, node1); 119 | algorithm.getShortestPathTo(node7); -------------------------------------------------------------------------------- /Graph/prims.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Vertex(): 4 | 5 | def __init__(self, name): 6 | self.name = name 7 | self.visited = False 8 | self.predecessor = None 9 | self.adjacencyList = [] 10 | 11 | def __str__(self): 12 | return self.name 13 | 14 | class Edge(): 15 | 16 | def __init__(self, weight, startVertex, targetVertex): 17 | self.weight = weight 18 | self.startVertex = startVertex 19 | self.targetVertex = targetVertex 20 | 21 | def __lt__(self, other): 22 | selfPriority = self.weight; 23 | otherPriority = other.weight; 24 | return selfPriority < otherPriority; 25 | 26 | class PrimsJarnik(): 27 | 28 | def __init__(self, unvisitedList): 29 | self.unvisitedList = unvisitedList 30 | self.spanningTree = [] 31 | self.edgeHeap = [] 32 | self.fullCost = 0 33 | 34 | def calculateSpanningTree(self, vertex): 35 | self.unvisitedList.remove(vertex); 36 | while self.unvisitedList: 37 | 38 | for edge in vertex.adjacencyList: 39 | if edge.targetVertex in self.unvisitedList: 40 | heapq.heappush(self.edgeHeap, edge); 41 | 42 | minEdge = heapq.heappop(self.edgeHeap); 43 | 44 | if minEdge.targetVertex in self.unvisitedList: 45 | self.spanningTree.append(minEdge) 46 | print("Edge added to spanning tree: %s - %s" % (minEdge.startVertex.name,minEdge.targetVertex.name)) 47 | self.fullCost += minEdge.weight 48 | vertex = minEdge.targetVertex 49 | self.unvisitedList.remove(vertex) 50 | 51 | def getSpanningTree(self): 52 | return self.spanningTree 53 | 54 | def getCost(self): 55 | return self.fullCost 56 | 57 | 58 | vertexA = Vertex("A") 59 | vertexB = Vertex("B") 60 | vertexC = Vertex("C") 61 | vertexD = Vertex("D") 62 | vertexE = Vertex("E") 63 | vertexF = Vertex("F") 64 | vertexG = Vertex("G") 65 | 66 | edgeAB = Edge(2, vertexA, vertexB) 67 | edgeBA = Edge(2, vertexB, vertexA) 68 | edgeAE = Edge(5, vertexA, vertexE) 69 | edgeEA = Edge(5, vertexE, vertexA) 70 | edgeAC = Edge(6, vertexA, vertexC) 71 | edgeCA = Edge(6, vertexC, vertexA) 72 | edgeAF = Edge(10, vertexA, vertexF) 73 | edgeFA = Edge(10, vertexF, vertexA) 74 | edgeBE = Edge(3, vertexB, vertexE) 75 | edgeEB = Edge(3, vertexE, vertexB) 76 | edgeBD = Edge(3, vertexB, vertexD) 77 | edgeDB = Edge(3, vertexD, vertexB) 78 | edgeCD = Edge(1, vertexC, vertexD) 79 | edgeDC = Edge(1, vertexD, vertexC) 80 | edgeCF = Edge(2, vertexC, vertexF) 81 | edgeFC = Edge(2, vertexF, vertexC) 82 | edgeDE = Edge(4, vertexD, vertexE) 83 | edgeED = Edge(4, vertexE, vertexD) 84 | edgeDG = Edge(5, vertexD, vertexG) 85 | edgeGD = Edge(5, vertexG, vertexD) 86 | edgeFG = Edge(3, vertexF, vertexG) 87 | edgeGF = Edge(3, vertexG, vertexF) 88 | 89 | unvisitedList = [] 90 | unvisitedList.append(vertexA) 91 | unvisitedList.append(vertexB) 92 | unvisitedList.append(vertexC) 93 | unvisitedList.append(vertexD) 94 | unvisitedList.append(vertexE) 95 | unvisitedList.append(vertexF) 96 | unvisitedList.append(vertexG) 97 | 98 | vertexA.adjacencyList.append(edgeAB) 99 | vertexA.adjacencyList.append(edgeAC) 100 | vertexA.adjacencyList.append(edgeAE) 101 | vertexA.adjacencyList.append(edgeAF) 102 | vertexB.adjacencyList.append(edgeBA) 103 | vertexB.adjacencyList.append(edgeBD) 104 | vertexB.adjacencyList.append(edgeBE) 105 | vertexC.adjacencyList.append(edgeCA) 106 | vertexC.adjacencyList.append(edgeCD) 107 | vertexC.adjacencyList.append(edgeCF) 108 | vertexD.adjacencyList.append(edgeDB) 109 | vertexD.adjacencyList.append(edgeDC) 110 | vertexD.adjacencyList.append(edgeDE) 111 | vertexD.adjacencyList.append(edgeDG) 112 | vertexE.adjacencyList.append(edgeEA) 113 | vertexE.adjacencyList.append(edgeEB) 114 | vertexE.adjacencyList.append(edgeED) 115 | vertexF.adjacencyList.append(edgeFA) 116 | vertexF.adjacencyList.append(edgeFC) 117 | vertexF.adjacencyList.append(edgeFG) 118 | vertexG.adjacencyList.append(edgeGD) 119 | vertexG.adjacencyList.append(edgeGF) 120 | 121 | algorithm = PrimsJarnik(unvisitedList) 122 | algorithm.calculateSpanningTree(vertexD) 123 | print(algorithm.getCost()) -------------------------------------------------------------------------------- /Linkedlist/Linkedlist.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "3 \n", 13 | "122 \n", 14 | "12 \n", 15 | "31 \n", 16 | "0\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "\n", 22 | "class Node(object):\n", 23 | "\n", 24 | "\tdef __init__(self, data):\n", 25 | "\t\tself.data = data;\n", 26 | "\t\tself.nextNode = None;\n", 27 | "\t\t\n", 28 | "class LinkedList(object):\n", 29 | "\n", 30 | "\tdef __init__(self):\n", 31 | "\t\tself.head = None;\n", 32 | "\t\tself.size = 0;\n", 33 | "\t\t\n", 34 | "\t# O(1) !!!\t\n", 35 | "\tdef insertStart(self, data):\n", 36 | "\t\n", 37 | "\t\tself.size = self.size + 1;\n", 38 | "\t\tnewNode = Node(data);\n", 39 | "\t\t\n", 40 | "\t\tif not self.head:\n", 41 | "\t\t\tself.head = newNode;\n", 42 | "\t\telse:\n", 43 | "\t\t\tnewNode.nextNode = self.head;\n", 44 | "\t\t\tself.head = newNode;\n", 45 | "\t\t\t\n", 46 | "\tdef remove(self, data):\n", 47 | "\t\n", 48 | "\t\tif self.head is None:\n", 49 | "\t\t\treturn;\n", 50 | "\t\t\t\n", 51 | "\t\tself.size = self.size - 1;\n", 52 | "\t\t\n", 53 | "\t\tcurrentNode = self.head;\n", 54 | "\t\tpreviousNode = None;\n", 55 | "\t\t\n", 56 | "\t\twhile currentNode.data != data:\n", 57 | "\t\t\tpreviousNode = currentNode;\n", 58 | "\t\t\tcurrentNode = currentNode.nextNode;\n", 59 | "\t\t\t\n", 60 | "\t\tif previousNode is None:\n", 61 | "\t\t\tself.head = currentNode.nextNode;\n", 62 | "\t\telse:\n", 63 | "\t\t\tpreviousNode.nextNode = currentNode.nextNode;\t\t\t\n", 64 | "\t\t\n", 65 | "\t# O(1)\t\n", 66 | "\tdef size1(self):\n", 67 | "\t\treturn self.size;\n", 68 | "\t\t\n", 69 | "\t# O(N) not good !!!\t\n", 70 | "\tdef size2(self):\n", 71 | "\t\t\n", 72 | "\t\tactualNode = self.head;\n", 73 | "\t\tsize = 0;\n", 74 | "\t\t\n", 75 | "\t\twhile actualNode is not None:\n", 76 | "\t\t\tsize+=1;\n", 77 | "\t\t\tactualNode = actualNode.nextNode;\n", 78 | "\t\t\t\n", 79 | "\t\treturn size;\n", 80 | "\t\t\n", 81 | "\t# O(N)\n", 82 | "\tdef insertEnd(self, data):\n", 83 | "\t\n", 84 | "\t\tself.size = self.size + 1;\n", 85 | "\t\tnewNode = Node(data);\n", 86 | "\t\tactualNode = self.head;\n", 87 | "\t\t\n", 88 | "\t\twhile actualNode.nextNode is not None:\n", 89 | "\t\t\tactualNode = actualNode.nextNode;\n", 90 | "\t\t\t\n", 91 | "\t\tactualNode.nextNode = newNode;\n", 92 | "\t\t\n", 93 | "\tdef traverseList(self):\n", 94 | "\t\n", 95 | "\t\tactualNode = self.head;\n", 96 | "\t\t\n", 97 | "\t\twhile actualNode is not None:\n", 98 | "\t\t\tprint(\"%d \" % actualNode.data);\n", 99 | "\t\t\tactualNode = actualNode.nextNode;\n", 100 | "\n", 101 | "\n", 102 | "\t\t\t\n", 103 | "linkedlist = LinkedList();\n", 104 | "\n", 105 | "linkedlist.insertStart(12);\n", 106 | "linkedlist.insertStart(122);\n", 107 | "linkedlist.insertStart(3);\n", 108 | "linkedlist.insertEnd(31);\n", 109 | "\n", 110 | "linkedlist.traverseList();\n", 111 | "\n", 112 | "linkedlist.remove(3);\n", 113 | "linkedlist.remove(12);\n", 114 | "linkedlist.remove(122);\n", 115 | "linkedlist.remove(31);\n", 116 | "\n", 117 | "print(linkedlist.size1());\n" 118 | ] 119 | } 120 | ], 121 | "metadata": { 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.7.4" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 4 142 | } 143 | -------------------------------------------------------------------------------- /Graph/kruskal.py: -------------------------------------------------------------------------------- 1 | 2 | class Vertex(object): 3 | 4 | def __init__(self, name): 5 | self.name = name; 6 | self.node = None; # !!!! 7 | 8 | class Node(object): 9 | 10 | def __init__(self, height, nodeId, parentNode): 11 | self.height = height; 12 | self.nodeId = nodeId; 13 | self.parentNode = parentNode; 14 | 15 | class Edge(object): 16 | 17 | def __init__(self, weight, startVertex, targetVertex): 18 | self.weight = weight; 19 | self.startVertex = startVertex; 20 | self.targetVertex = targetVertex; 21 | 22 | def __cmp__(self, otherEdge): 23 | return self.cmp(self.weight, otherEdge.weight); 24 | 25 | def __lt__(self, other): 26 | selfPriority = self.weight; 27 | otherPriority = other.weight; 28 | return selfPriority < otherPriority; 29 | 30 | class DisjointSet(object): 31 | 32 | def __init__(self, vertexList): 33 | self.vertexList = vertexList; 34 | self.rootNodes = []; 35 | self.nodeCount = 0; 36 | self.setCount = 0; 37 | self.makeSets(vertexList); 38 | 39 | def find(self, node): 40 | 41 | currentNode = node; 42 | 43 | while currentNode.parentNode is not None: 44 | currentNode = currentNode.parentNode; 45 | 46 | root = currentNode; 47 | currentNode = node; 48 | 49 | while currentNode is not root: 50 | temp = currentNode.parentNode; 51 | currentNode.parentNode = root; 52 | currentNode = temp; 53 | 54 | return root.nodeId; 55 | 56 | def merge(self, node1, node2): 57 | 58 | index1 = self.find(node1); 59 | index2 = self.find(node2); 60 | 61 | if index1 == index2: 62 | return; # they are in the same set !!!! 63 | 64 | root1 = self.rootNodes[index1]; 65 | root2 = self.rootNodes[index2]; 66 | 67 | if root1.height < root2.height: 68 | root1.parentNode = root2; 69 | elif root1.height > root2.height: 70 | root2.parentNode = root1; 71 | else: 72 | root2.parentNode = root1; 73 | root1.height = root1.height + 1; 74 | 75 | def makeSets(self, vertexList): 76 | for v in vertexList: 77 | self.makeSet(v); 78 | 79 | def makeSet(self, vertex): 80 | node = Node(0, len(self.rootNodes),None); 81 | vertex.node = node; 82 | self.rootNodes.append(node); 83 | self.setCount = self.setCount + 1; 84 | self.nodeCount = self.nodeCount + 1; 85 | 86 | class KruskalAlgorithm(object): 87 | 88 | def spanningTree(self, vertexList, edgeList): 89 | 90 | disjointSet = DisjointSet(vertexList); 91 | spanningTree = []; 92 | 93 | edgeList.sort(); 94 | 95 | for edge in edgeList: 96 | 97 | u = edge.startVertex; 98 | v = edge.targetVertex; 99 | 100 | if disjointSet.find(u.node) is not disjointSet.find(v.node): 101 | spanningTree.append(edge); 102 | disjointSet.merge(u.node, v.node); # !!!! dot 103 | 104 | for edge in spanningTree: 105 | print(edge.startVertex.name," - ", edge.targetVertex.name); 106 | 107 | vertex1 = Vertex("a"); 108 | vertex2 = Vertex("b"); 109 | vertex3 = Vertex("c"); 110 | vertex4 = Vertex("d"); 111 | vertex5 = Vertex("e"); 112 | vertex6 = Vertex("f"); 113 | vertex7 = Vertex("g"); 114 | 115 | edge1 = Edge(2,vertex1,vertex2); 116 | edge2 = Edge(6,vertex1,vertex3); 117 | edge3 = Edge(5,vertex1,vertex5); 118 | edge4 = Edge(10,vertex1,vertex6); 119 | edge5 = Edge(3,vertex2,vertex4); 120 | edge6 = Edge(3,vertex2,vertex5); 121 | edge7 = Edge(1,vertex3,vertex4); 122 | edge8 = Edge(2,vertex3,vertex6); 123 | edge9 = Edge(4,vertex4,vertex5); 124 | edge10 = Edge(5,vertex4,vertex7); 125 | edge11 = Edge(5,vertex6,vertex7); 126 | 127 | 128 | vertexList = []; 129 | vertexList.append(vertex1); 130 | vertexList.append(vertex2); 131 | vertexList.append(vertex3); 132 | vertexList.append(vertex4); 133 | vertexList.append(vertex5); 134 | vertexList.append(vertex6); 135 | vertexList.append(vertex7); 136 | 137 | edgeList = []; 138 | edgeList.append(edge1); 139 | edgeList.append(edge2); 140 | edgeList.append(edge3); 141 | edgeList.append(edge4); 142 | edgeList.append(edge5); 143 | edgeList.append(edge6); 144 | edgeList.append(edge7); 145 | edgeList.append(edge8); 146 | edgeList.append(edge9); 147 | edgeList.append(edge10); 148 | edgeList.append(edge11); 149 | 150 | algorithm = KruskalAlgorithm(); 151 | algorithm.spanningTree(vertexList, edgeList); 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /Tree/Heap.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "20 \n", 13 | "15 \n", 14 | "10 \n", 15 | "7 \n", 16 | "6 \n", 17 | "5 \n", 18 | "4 \n", 19 | "2 \n", 20 | "0 \n", 21 | "-20 \n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "class Heap(object):\n", 27 | "\n", 28 | "\tHEAP_SIZE = 10\n", 29 | "\t\n", 30 | "\tdef __init__(self):\n", 31 | "\t\tself.heap = [0]*Heap.HEAP_SIZE;\n", 32 | "\t\tself.currentPosition = -1;\n", 33 | "\t\t\n", 34 | "\tdef insert(self, item):\n", 35 | "\t\n", 36 | "\t\tif self.isFull():\n", 37 | "\t\t\tprint(\"Heap is full..\");\n", 38 | "\t\t\treturn \n", 39 | "\t\t\t\n", 40 | "\t\tself.currentPosition = self.currentPosition + 1\n", 41 | "\t\tself.heap[self.currentPosition] = item\n", 42 | "\t\tself.fixUp(self.currentPosition)\n", 43 | "\t\t\n", 44 | "\tdef fixUp(self, index):\n", 45 | "\t\n", 46 | "\t\tparentIndex = int((index-1)/2)\n", 47 | "\t\t\n", 48 | "\t\twhile parentIndex >= 0 and self.heap[parentIndex] < self.heap[index]:\n", 49 | "\t\t\ttemp = self.heap[index]\n", 50 | "\t\t\tself.heap[index] = self.heap[parentIndex]\n", 51 | "\t\t\tself.heap[parentIndex] = temp\n", 52 | "\t\t\tindex=parentIndex\n", 53 | "\t\t\tparentIndex = (int)((index-1)/2)\n", 54 | "\t\t\t\n", 55 | "\tdef heapsort(self):\n", 56 | "\t\n", 57 | "\t\tfor i in range(0,self.currentPosition+1):\n", 58 | "\t\t\ttemp = self.heap[0]\n", 59 | "\t\t\tprint(\"%d \" % temp)\n", 60 | "\t\t\tself.heap[0] = self.heap[self.currentPosition-i]\n", 61 | "\t\t\tself.heap[self.currentPosition-i] = temp\n", 62 | "\t\t\tself.fixDown(0,self.currentPosition-i-1)\n", 63 | "\t\t\t\n", 64 | "\tdef fixDown(self, index, upto):\n", 65 | "\n", 66 | "\t\twhile index <= upto:\n", 67 | "\t\t\n", 68 | "\t\t\tleftChild = 2*index+1\n", 69 | "\t\t\trightChild = 2*index+2\n", 70 | "\t\n", 71 | "\t\t\tif leftChild < upto:\n", 72 | "\t\t\t\tchildToSwap = None\n", 73 | "\t\t\t\t\n", 74 | "\t\t\t\tif rightChild > upto:\n", 75 | "\t\t\t\t\tchildToSwap = leftChild\n", 76 | "\t\t\t\telse:\n", 77 | "\t\t\t\t\tif self.heap[leftChild] > self.heap[rightChild]:\n", 78 | "\t\t\t\t\t\tchildToSwap = leftChild\n", 79 | "\t\t\t\t\telse:\n", 80 | "\t\t\t\t\t\tchildToSwap = rightChild\n", 81 | "\t\t\t\t\n", 82 | "\t\t\t\tif self.heap[index] < self.heap[childToSwap]:\n", 83 | "\t\t\t\t\ttemp = self.heap[index]\n", 84 | "\t\t\t\t\tself.heap[index] = self.heap[childToSwap]\n", 85 | "\t\t\t\t\tself.heap[childToSwap] = temp\n", 86 | "\t\t\t\telse:\n", 87 | "\t\t\t\t\tbreak\n", 88 | "\t\t\t\t\n", 89 | "\t\t\t\tindex = childToSwap\n", 90 | "\t\t\telse:\n", 91 | "\t\t\t\tbreak;\t\t\t\t\t\t\t\n", 92 | "\t\t\t\n", 93 | "\tdef isFull(self):\n", 94 | "\t\tif self.currentPosition == Heap.HEAP_SIZE:\n", 95 | "\t\t\treturn True\n", 96 | "\t\telse:\n", 97 | "\t\t\treturn False\n", 98 | "\t\t\t\n", 99 | "\t\t\t\n", 100 | "if __name__ == \"__main__\":\n", 101 | "\n", 102 | "\theap = Heap()\n", 103 | "\theap.insert(10)\n", 104 | "\theap.insert(-20)\n", 105 | "\theap.insert(0)\n", 106 | "\theap.insert(2)\n", 107 | "\theap.insert(4)\n", 108 | "\theap.insert(5)\n", 109 | "\theap.insert(6)\n", 110 | "\theap.insert(7)\n", 111 | "\theap.insert(20)\n", 112 | "\theap.insert(15)\n", 113 | "\t\n", 114 | "\theap.heapsort()" 115 | ] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 3", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.7.4" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 4 139 | } 140 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Strcture-Algorithm-using-Python 2 | 3 | 4 | Topics: 5 | * Queues 6 | * Stacks 7 | * Doubly Linked Lists 8 | * Singly Linked Lists 9 | * Binary Search Trees 10 | * Tree Traversal 11 | * Sortings 12 | * Searchings 13 | * Dynamic Programming 14 | * Heap 15 | * Graph 16 | 17 | ### Queues 18 | * Should have the methods: `enqueue`, `dequeue`, and `len`. 19 | * `enqueue` should add an item to the back of the queue. 20 | * `dequeue` should remove and return an item from the front of the queue. 21 | * `len` returns the number of items in the queue. 22 | 23 | 24 | ![Image of Queue](https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/600px-Data_Queue.svg.png) 25 | 26 | ### Doubly Linked Lists 27 | * The `ListNode` class, which represents a single node in the doubly-linked list, has already been implemented for you. Inspect this code and try to understand what it is doing to the best of your ability. 28 | * The `DoublyLinkedList` class itself should have the methods: `add_to_head`, `add_to_tail`, `remove_from_head`, `remove_from_tail`, `move_to_front`, `move_to_end`, `delete`, and `get_max`. 29 | * `add_to_head` replaces the head of the list with a new value that is passed in. 30 | * `add_to_tail` replaces the tail of the list with a new value that is passed in. 31 | * `remove_from_head` removes the head node and returns the value stored in it. 32 | * `remove_from_tail` removes the tail node and returns the value stored in it. 33 | * `move_to_front` takes a reference to a node in the list and moves it to the front of the list, shifting all other list nodes down. 34 | * `move_to_end` takes a reference to a node in the list and moves it to the end of the list, shifting all other list nodes up. 35 | * `delete` takes a reference to a node in the list and removes it from the list. The deleted node's `previous` and `next` pointers should point to each afterwards. 36 | * `get_max` returns the maximum value in the list. 37 | * The `head` property is a reference to the first node and the `tail` property is a reference to the last node. 38 | 39 | ![Image of Doubly Linked List](https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Doubly-linked-list.svg/610px-Doubly-linked-list.svg.png) 40 | 41 | ### Binary Search Trees 42 | * Should have the methods `insert`, `contains`, `get_max`. 43 | * `insert` adds the input value to the binary search tree, adhering to the rules of the ordering of elements in a binary search tree. 44 | * `contains` searches the binary search tree for the input value, returning a boolean indicating whether the value exists in the tree or not. 45 | * `get_max` returns the maximum value in the binary search tree. 46 | * `for_each` performs a traversal of _every_ node in the tree, executing the passed-in callback function on each tree node value. There is a myriad of ways to perform tree traversal; in this case any of them should work. 47 | 48 | ![Image of Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Binary_search_tree.svg/300px-Binary_search_tree.svg.png) 49 | 50 | ### Heaps 51 | * Should have the methods `insert`, `delete`, `get_max`, `_bubble_up`, and `_sift_down`. 52 | * `insert` adds the input value into the heap; this method should ensure that the inserted value is in the correct spot in the heap 53 | * `delete` removes and returns the 'topmost' value from the heap; this method needs to ensure that the heap property is maintained after the topmost element has been removed. 54 | * `get_max` returns the maximum value in the heap _in constant time_. 55 | * `get_size` returns the number of elements stored in the heap. 56 | * `_bubble_up` moves the element at the specified index "up" the heap by swapping it with its parent if the parent's value is less than the value at the specified index. 57 | * `_sift_down` grabs the indices of this element's children and determines which child has a larger value. If the larger child's value is larger than the parent's value, the child element is swapped with the parent. 58 | 59 | ![Image of a Heap in Tree form](https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Max-Heap.svg/501px-Max-Heap.svg.png) 60 | 61 | ![Image of a Heap in Array form](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d2/Heap-as-array.svg/603px-Heap-as-array.svg.png) 62 | 63 | 64 | ### Sorting 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Tree/BST.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Removing node with two children....\n", 13 | "Removing a leaf node...\n", 14 | "5 \n", 15 | "13 \n", 16 | "14 \n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "\n", 22 | "class Node(object):\n", 23 | "\n", 24 | "\tdef __init__(self, data):\n", 25 | "\t\tself.data = data;\n", 26 | "\t\tself.leftChild = None;\n", 27 | "\t\tself.rightChild = None;\n", 28 | "\t\t\n", 29 | "class BinarySearchTree(object):\n", 30 | "\n", 31 | "\tdef __init__(self):\n", 32 | "\t\tself.root = None;\n", 33 | "\t\t\n", 34 | "\tdef insert(self, data):\n", 35 | "\t\tif not self.root:\n", 36 | "\t\t\tself.root = Node(data);\n", 37 | "\t\telse:\n", 38 | "\t\t\tself.insertNode(data, self.root);\n", 39 | "\t\t\t\n", 40 | "\t# O(logN) if the tree is balanced !!!!!!!!!!!!! --> it can reduced to O(N) --> AVL RBT are needed !!!!!\n", 41 | "\tdef insertNode(self, data, node):\n", 42 | "\t\n", 43 | "\t\tif data < node.data:\n", 44 | "\t\t\tif node.leftChild:\n", 45 | "\t\t\t\tself.insertNode(data, node.leftChild);\n", 46 | "\t\t\telse:\n", 47 | "\t\t\t\tnode.leftChild = Node(data);\n", 48 | "\t\telse:\n", 49 | "\t\t\tif node.rightChild:\n", 50 | "\t\t\t\tself.insertNode(data, node.rightChild);\n", 51 | "\t\t\telse:\n", 52 | "\t\t\t\tnode.rightChild = Node(data);\n", 53 | "\t\n", 54 | "\t# O(logN)\t\n", 55 | "\tdef removeNode(self, data, node):\n", 56 | "\t\n", 57 | "\t\tif not node:\n", 58 | "\t\t\treturn node;\n", 59 | "\t\t\t\n", 60 | "\t\tif data < node.data:\n", 61 | "\t\t\tnode.leftChild = self.removeNode(data, node.leftChild);\n", 62 | "\t\telif data > node.data:\n", 63 | "\t\t\tnode.rightChild = self.removeNode(data, node.rightChild);\n", 64 | "\t\telse:\n", 65 | "\t\t\t\n", 66 | "\t\t\tif not node.leftChild and not node.rightChild:\n", 67 | "\t\t\t\tprint(\"Removing a leaf node...\");\n", 68 | "\t\t\t\tdel node;\n", 69 | "\t\t\t\treturn None;\n", 70 | "\t\t\t\t\n", 71 | "\t\t\tif not node.leftChild: # node !!!\n", 72 | "\t\t\t\tprint(\"Removing a node with single right child...\");\n", 73 | "\t\t\t\ttempNode = node.rightChild;\n", 74 | "\t\t\t\tdel node;\n", 75 | "\t\t\t\treturn tempNode;\n", 76 | "\t\t\telif not node.rightChild: # node instead of self\n", 77 | "\t\t\t\tprint(\"Removing a node with single left child...\");\n", 78 | "\t\t\t\ttempNode = node.leftChild;\n", 79 | "\t\t\t\tdel node;\n", 80 | "\t\t\t\treturn tempNode;\n", 81 | "\t\t\t\n", 82 | "\t\t\tprint(\"Removing node with two children....\");\n", 83 | "\t\t\ttempNode = self.getPredecessor(node.leftChild); # self instead of elf + get predecessor \n", 84 | "\t\t\tnode.data = tempNode.data;\n", 85 | "\t\t\tnode.leftChild = self.removeNode(tempNode.data, node.leftChild);\n", 86 | "\t\t\n", 87 | "\t\treturn node; # !!!!!!!!!!!!\n", 88 | "\n", 89 | "\tdef getPredecessor(self, node):\n", 90 | "\t\n", 91 | "\t\tif node.rightChild:\n", 92 | "\t\t\treturn self.getPredeccor(node.rightChild);\n", 93 | "\t\t\t\n", 94 | "\t\treturn node;\n", 95 | "\t\t\n", 96 | "\tdef remove(self, data):\n", 97 | "\t\tif self.root:\n", 98 | "\t\t\tself.root = self.removeNode(data, self.root);\n", 99 | "\t\t\t\n", 100 | "\t\t# O(logN)\n", 101 | "\tdef getMinValue(self):\n", 102 | "\t\tif self.root:\n", 103 | "\t\t\treturn self.getMin(self.root);\n", 104 | "\t\t\t\n", 105 | "\tdef getMin(self, node):\n", 106 | "\t\n", 107 | "\t\tif node.leftChild:\n", 108 | "\t\t\treturn self.getMin(node.leftChild);\n", 109 | "\t\t\t\n", 110 | "\t\treturn node.data;\n", 111 | "\t\t\n", 112 | "\t\t# O(logN)\n", 113 | "\tdef getMaxValue(self):\n", 114 | "\t\tif self.root:\n", 115 | "\t\t\treturn self.getMax(self.root);\n", 116 | "\t\t\t\n", 117 | "\tdef getMax(self, node):\n", 118 | "\t\n", 119 | "\t\tif node.rightChild:\n", 120 | "\t\t\treturn self.getMax(node.rightChild);\n", 121 | "\t\t\t\n", 122 | "\t\treturn node.data;\n", 123 | "\t\t\n", 124 | "\tdef traverse(self):\n", 125 | "\t\tif self.root:\n", 126 | "\t\t\tself.traverseInOrder(self.root);\n", 127 | "\t\t\t\n", 128 | "\t\t\t# O(N)\n", 129 | "\tdef traverseInOrder(self, node):\n", 130 | "\t\n", 131 | "\t\tif node.leftChild:\n", 132 | "\t\t\tself.traverseInOrder(node.leftChild);\n", 133 | "\t\t\t\n", 134 | "\t\tprint(\"%s \" % node.data);\n", 135 | "\t\t\n", 136 | "\t\tif node.rightChild:\n", 137 | "\t\t\tself.traverseInOrder(node.rightChild);\n", 138 | "\t\t\t\n", 139 | "\t\t\t\n", 140 | "bst = BinarySearchTree();\n", 141 | "bst.insert(10);\n", 142 | "bst.insert(13);\n", 143 | "bst.insert(5);\n", 144 | "bst.insert(14);\n", 145 | "bst.remove(10);\n", 146 | "\n", 147 | "bst.traverse();" 148 | ] 149 | } 150 | ], 151 | "metadata": { 152 | "kernelspec": { 153 | "display_name": "Python 3", 154 | "language": "python", 155 | "name": "python3" 156 | }, 157 | "language_info": { 158 | "codemirror_mode": { 159 | "name": "ipython", 160 | "version": 3 161 | }, 162 | "file_extension": ".py", 163 | "mimetype": "text/x-python", 164 | "name": "python", 165 | "nbconvert_exporter": "python", 166 | "pygments_lexer": "ipython3", 167 | "version": "3.7.4" 168 | } 169 | }, 170 | "nbformat": 4, 171 | "nbformat_minor": 4 172 | } 173 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Sorting/BubbleSort.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "\n", 13 | "Enter 1 to create a new account\n", 14 | "Enter 2 to access an existing account\n", 15 | "Enter 3 to exit\n" 16 | ] 17 | }, 18 | { 19 | "name": "stdin", 20 | "output_type": "stream", 21 | "text": [ 22 | " 3\n" 23 | ] 24 | }, 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "\n", 30 | "\n", 31 | "Enter 1 to create a new account\n", 32 | "Enter 2 to access an existing account\n", 33 | "Enter 3 to exit\n" 34 | ] 35 | }, 36 | { 37 | "name": "stdin", 38 | "output_type": "stream", 39 | "text": [ 40 | " 2\n" 41 | ] 42 | }, 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "\n", 48 | "\n", 49 | "Enter your name: \n" 50 | ] 51 | }, 52 | { 53 | "name": "stdin", 54 | "output_type": "stream", 55 | "text": [ 56 | " Soumyadip\n" 57 | ] 58 | }, 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Enter your account number: \n" 64 | ] 65 | }, 66 | { 67 | "name": "stdin", 68 | "output_type": "stream", 69 | "text": [ 70 | " 21004\n" 71 | ] 72 | }, 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "\n", 78 | "Authentication Failed\n", 79 | "\n", 80 | "\n", 81 | "\n", 82 | "Enter 1 to create a new account\n", 83 | "Enter 2 to access an existing account\n", 84 | "Enter 3 to exit\n" 85 | ] 86 | }, 87 | { 88 | "name": "stdin", 89 | "output_type": "stream", 90 | "text": [ 91 | " 2\n" 92 | ] 93 | }, 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "\n", 99 | "\n", 100 | "Enter your name: \n" 101 | ] 102 | }, 103 | { 104 | "name": "stdin", 105 | "output_type": "stream", 106 | "text": [ 107 | " Soumyadip\n" 108 | ] 109 | }, 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "Enter your account number: \n" 115 | ] 116 | }, 117 | { 118 | "name": "stdin", 119 | "output_type": "stream", 120 | "text": [ 121 | " 21002\n" 122 | ] 123 | }, 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "\n", 129 | "Authentication Failed\n", 130 | "\n", 131 | "\n", 132 | "\n", 133 | "Enter 1 to create a new account\n", 134 | "Enter 2 to access an existing account\n", 135 | "Enter 3 to exit\n" 136 | ] 137 | }, 138 | { 139 | "name": "stdin", 140 | "output_type": "stream", 141 | "text": [ 142 | " 1\n" 143 | ] 144 | }, 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "\n", 150 | "\n", 151 | "Enter your name: \n" 152 | ] 153 | }, 154 | { 155 | "name": "stdin", 156 | "output_type": "stream", 157 | "text": [ 158 | " Soumya]\n" 159 | ] 160 | }, 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "Enter the initial deposit: \n" 166 | ] 167 | }, 168 | { 169 | "name": "stdin", 170 | "output_type": "stream", 171 | "text": [ 172 | " 10000\n" 173 | ] 174 | }, 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "\n", 180 | "Account creation has been successful. Your account number is 26130\n", 181 | "\n", 182 | "\n", 183 | "\n", 184 | "Enter 1 to create a new account\n", 185 | "Enter 2 to access an existing account\n", 186 | "Enter 3 to exit\n" 187 | ] 188 | }, 189 | { 190 | "name": "stdin", 191 | "output_type": "stream", 192 | "text": [ 193 | " 2\n" 194 | ] 195 | }, 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "\n", 201 | "\n", 202 | "Enter your name: \n" 203 | ] 204 | }, 205 | { 206 | "name": "stdin", 207 | "output_type": "stream", 208 | "text": [ 209 | " Soumya]\n" 210 | ] 211 | }, 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "Enter your account number: \n" 217 | ] 218 | }, 219 | { 220 | "name": "stdin", 221 | "output_type": "stream", 222 | "text": [ 223 | " 26130\n" 224 | ] 225 | }, 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "\n", 231 | "Authentication Successful\n", 232 | "\n", 233 | "\n", 234 | "\n", 235 | "Enter 1 to withdraw\n", 236 | "Enter 2 to deposit\n", 237 | "Enter 3 to display avialable balance\n", 238 | "Enter 4 to go back to the previous menu\n" 239 | ] 240 | }, 241 | { 242 | "name": "stdin", 243 | "output_type": "stream", 244 | "text": [ 245 | " 10\n" 246 | ] 247 | }, 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "\n", 253 | "\n", 254 | "Enter 1 to withdraw\n", 255 | "Enter 2 to deposit\n", 256 | "Enter 3 to display avialable balance\n", 257 | "Enter 4 to go back to the previous menu\n" 258 | ] 259 | }, 260 | { 261 | "name": "stdin", 262 | "output_type": "stream", 263 | "text": [ 264 | " 2\n" 265 | ] 266 | }, 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "\n", 272 | "\n", 273 | "Enter an amount to be deposited\n" 274 | ] 275 | }, 276 | { 277 | "name": "stdin", 278 | "output_type": "stream", 279 | "text": [ 280 | " 10\n" 281 | ] 282 | }, 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "\n", 288 | "Deposit was successful.\n", 289 | "Avaialble balance: 10010\n", 290 | "\n", 291 | "\n", 292 | "\n", 293 | "Enter 1 to withdraw\n", 294 | "Enter 2 to deposit\n", 295 | "Enter 3 to display avialable balance\n", 296 | "Enter 4 to go back to the previous menu\n" 297 | ] 298 | }, 299 | { 300 | "name": "stdin", 301 | "output_type": "stream", 302 | "text": [ 303 | " 1\n" 304 | ] 305 | }, 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "\n", 311 | "\n", 312 | "Enter a withdrawal amount\n" 313 | ] 314 | }, 315 | { 316 | "name": "stdin", 317 | "output_type": "stream", 318 | "text": [ 319 | " 5000\n" 320 | ] 321 | }, 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "\n", 327 | "Withdrawal was successful.\n", 328 | "Avaialble balance: 5010\n", 329 | "\n", 330 | "\n", 331 | "\n", 332 | "Enter 1 to withdraw\n", 333 | "Enter 2 to deposit\n", 334 | "Enter 3 to display avialable balance\n", 335 | "Enter 4 to go back to the previous menu\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "from abc import ABCMeta, abstractmethod\n", 341 | "from random import randint\n", 342 | "\n", 343 | "class Account(metaclass = ABCMeta):\n", 344 | " @abstractmethod\n", 345 | " def createAccount():\n", 346 | " return 0\n", 347 | " @abstractmethod\n", 348 | " def authenticate():\n", 349 | " return 0\n", 350 | " @abstractmethod\n", 351 | " def withdraw():\n", 352 | " return 0\n", 353 | " @abstractmethod\n", 354 | " def deposit():\n", 355 | " return 0\n", 356 | " @abstractmethod\n", 357 | " def displayBalance():\n", 358 | " return 0\n", 359 | "\n", 360 | "\n", 361 | "class SavingsAccount(Account):\n", 362 | " def __init__(self):\n", 363 | " # [key][0] => name ; [key][1] => balance\n", 364 | " self.savingsAccounts = {}\n", 365 | " def createAccount(self, name, initialDeposit):\n", 366 | " print()\n", 367 | " self.accountNumber = randint(10000, 99999)\n", 368 | " self.savingsAccounts[self.accountNumber] = [name, initialDeposit]\n", 369 | " print (\"Account creation has been successful. Your account number is \", self.accountNumber)\n", 370 | " print()\n", 371 | "\n", 372 | " def authenticate(self, name, accountNumber):\n", 373 | " print()\n", 374 | " if accountNumber in self.savingsAccounts.keys():\n", 375 | " if self.savingsAccounts[accountNumber][0] == name:\n", 376 | " print(\"Authentication Successful\")\n", 377 | " self.accountNumber = accountNumber\n", 378 | " print()\n", 379 | " return True\n", 380 | " else:\n", 381 | " print(\"Authentication Failed\")\n", 382 | " print()\n", 383 | " return False\n", 384 | " else:\n", 385 | " print(\"Authentication Failed\")\n", 386 | " print()\n", 387 | " return False\n", 388 | "\n", 389 | " def withdraw(self, withdrawalAmount):\n", 390 | " print()\n", 391 | " if withdrawalAmount > self.savingsAccounts[self.accountNumber][1]:\n", 392 | " print(\"Insufficient balance\")\n", 393 | " else:\n", 394 | " self.savingsAccounts[self.accountNumber][1] -= withdrawalAmount\n", 395 | " print(\"Withdrawal was successful.\")\n", 396 | " self.displayBalance()\n", 397 | " print()\n", 398 | "\n", 399 | " def deposit(self, depositAmount):\n", 400 | " print()\n", 401 | " self.savingsAccounts[self.accountNumber][1] += depositAmount\n", 402 | " print(\"Deposit was successful.\")\n", 403 | " self.displayBalance()\n", 404 | " print()\n", 405 | "\n", 406 | " def displayBalance(self):\n", 407 | " print(\"Avaialble balance: \",self.savingsAccounts[self.accountNumber][1])\n", 408 | "\n", 409 | "savingsAccount = SavingsAccount()\n", 410 | "while True:\n", 411 | " print()\n", 412 | " print(\"Enter 1 to create a new account\")\n", 413 | " print(\"Enter 2 to access an existing account\")\n", 414 | " print(\"Enter 3 to exit\")\n", 415 | " userChoice = int(input())\n", 416 | " print()\n", 417 | " if userChoice is 1:\n", 418 | " print()\n", 419 | " print(\"Enter your name: \")\n", 420 | " name = input()\n", 421 | " print(\"Enter the initial deposit: \")\n", 422 | " deposit = int(input())\n", 423 | " savingsAccount.createAccount(name, deposit)\n", 424 | " print()\n", 425 | " elif userChoice is 2:\n", 426 | " print()\n", 427 | " print(\"Enter your name: \")\n", 428 | " name = input()\n", 429 | " print(\"Enter your account number: \")\n", 430 | " accountNumber = int(input())\n", 431 | " authenticationStatus = savingsAccount.authenticate(name, accountNumber)\n", 432 | " print()\n", 433 | " if authenticationStatus is True:\n", 434 | " while True:\n", 435 | " print()\n", 436 | " print(\"Enter 1 to withdraw\")\n", 437 | " print(\"Enter 2 to deposit\")\n", 438 | " print(\"Enter 3 to display avialable balance\")\n", 439 | " print(\"Enter 4 to go back to the previous menu\")\n", 440 | " userChoice = int(input())\n", 441 | " print()\n", 442 | " if userChoice is 1:\n", 443 | " print()\n", 444 | " print(\"Enter a withdrawal amount\")\n", 445 | " withdrawalAmount = int(input())\n", 446 | " savingsAccount.withdraw(withdrawalAmount)\n", 447 | " print()\n", 448 | " elif userChoice is 2:\n", 449 | " print()\n", 450 | " print(\"Enter an amount to be deposited\")\n", 451 | " depositAmount = int(input())\n", 452 | " savingsAccount.deposit(depositAmount)\n", 453 | " print()\n", 454 | " elif userChoice is 3:\n", 455 | " print()\n", 456 | " savingsAccount.displayBalance()\n", 457 | " print()\n", 458 | " elif userChoice is 4:\n", 459 | " break\n", 460 | " elif userChoice is 3:\n", 461 | " quit()\n" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": null, 467 | "metadata": {}, 468 | "outputs": [], 469 | "source": [] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": null, 474 | "metadata": {}, 475 | "outputs": [], 476 | "source": [ 477 | "\n", 478 | "E" 479 | ] 480 | } 481 | ], 482 | "metadata": { 483 | "kernelspec": { 484 | "display_name": "Python 3", 485 | "language": "python", 486 | "name": "python3" 487 | }, 488 | "language_info": { 489 | "codemirror_mode": { 490 | "name": "ipython", 491 | "version": 3 492 | }, 493 | "file_extension": ".py", 494 | "mimetype": "text/x-python", 495 | "name": "python", 496 | "nbconvert_exporter": "python", 497 | "pygments_lexer": "ipython3", 498 | "version": "3.7.4" 499 | } 500 | }, 501 | "nbformat": 4, 502 | "nbformat_minor": 4 503 | } 504 | -------------------------------------------------------------------------------- /svg/Model!Main_0.svg: -------------------------------------------------------------------------------- 1 | UNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDCustomer+Customer Name+AccNumber+Address+Phone+createAcc()+deposit()+withdraw()Bank+custtDetails+loanDetails+transNo+transDate+transTime+giveLoan()+updateDetails()+collectMoney()+transaction()Account+AccNo+CustName+Balance+updateAcc()+checkAcc()11....**1 -------------------------------------------------------------------------------- /svg/Model!DataFlowModel1!DFDDiagram1_1.svg: -------------------------------------------------------------------------------- 1 | UNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDUNREGISTEREDCustomerDatabaseAccountDatabaseOpen AccountOnlineBankingLoginWithdrawDepositTransferFunsThird PartyOther BankBank ManegerCustomerNew accCustomer DetailsAccount DetilsLogin CredentialsMoney AmmountAccoubt Balance UpdateMoney AmmountAccount Destination and Money Ammout --------------------------------------------------------------------------------