|
| 1 | +Tutorials |
| 2 | +========= |
| 3 | + |
| 4 | +We provide the following tutorials to show how ``pydatastructs`` |
| 5 | +APIs can help in solving complicated data structures and algorithms |
| 6 | +problems easily. For now the problems are abstract. However, we plan |
| 7 | +to add some examples showing usage of ``pydatastructs`` on real world |
| 8 | +data sets such as `Stanford Large Network Dataset Collection <https://snap.stanford.edu/data/>`_ |
| 9 | +and `Urban Dictionary Words And Definitions <https://www.kaggle.com/therohk/urban-dictionary-words-dataset>`_. |
| 10 | +If you are interested in playing around with the above datasets using our API, |
| 11 | +then please feel free to reach out to us on our community channels. |
| 12 | + |
| 13 | +Max-Min Stream |
| 14 | +-------------- |
| 15 | + |
| 16 | +In this problem, we will be dealing with a stream of integer numbers. We have to |
| 17 | +display the ``k``-th largest and ``k``-th smallest number for all the prefixes of the |
| 18 | +input stream. In simple words, after reading each number, we have to display |
| 19 | +the ``k``-th largest and ``k``-th smallest number up until that number in the stream. |
| 20 | +If the size of the stream is smaller than ``k`` then we will display the minimum |
| 21 | +for ``k``-th smallest and maximum for ``k``-th largest numbers respectively. |
| 22 | + |
| 23 | +**Input Format** |
| 24 | + |
| 25 | +The first line of input will contain the value, ``k``. After that, each line of |
| 26 | +input will contain an integer representing the new number of the stream. The stopping |
| 27 | +point of the stream will be denoted by 0. Note that stopping point i.e., 0 will also |
| 28 | +be considered a part of the input stream. |
| 29 | + |
| 30 | +**Output Format** |
| 31 | + |
| 32 | +Each line of the output should contain two space separated numbers, the first one |
| 33 | +representing the ``k``-th largest/maximum number and the second one representing |
| 34 | +the ``k``-th smallest/minimum number. |
| 35 | + |
| 36 | +>>> from pydatastructs import BinaryHeap, Queue |
| 37 | +>>> def modify_heaps(min_heap, max_heap, curr_num, k): |
| 38 | +... min_heap.insert(curr_num) |
| 39 | +... max_heap.insert(curr_num) |
| 40 | +... if min_heap.heap._num > k: |
| 41 | +... min_heap.extract() |
| 42 | +... if max_heap.heap._num > k: |
| 43 | +... max_heap.extract() |
| 44 | +... large, small = (max_heap.heap[0].key, min_heap.heap[0].key) |
| 45 | +... return large, small |
| 46 | +... |
| 47 | +>>> min_heap = BinaryHeap(heap_property='min') |
| 48 | +>>> max_heap = BinaryHeap(heap_property='max') |
| 49 | +>>> k = 2 |
| 50 | +>>> curr_nums = Queue(items=[4, 5, 8, 0]) # input stream as a list |
| 51 | +>>> curr_num = curr_nums.popleft() |
| 52 | +>>> large_small = [] |
| 53 | +>>> while curr_num != 0: |
| 54 | +... large, small = modify_heaps(min_heap, max_heap, curr_num, k) |
| 55 | +... large_small.append((large, small)) |
| 56 | +... curr_num = curr_nums.popleft() |
| 57 | +... |
| 58 | +>>> large, small = modify_heaps(min_heap, max_heap, curr_num, k) |
| 59 | +>>> large_small.append((large, small)) |
| 60 | +>>> print(large_small) |
| 61 | +[(4, 4), (5, 4), (5, 5), (4, 5)] |
| 62 | + |
| 63 | +Minimise Network Delay |
| 64 | +---------------------- |
| 65 | + |
| 66 | +In this problem there will be a network containing ``N`` nodes, labelled as 1 ... ``N``, and ``E`` edges. |
| 67 | +Any two nodes may be connected by an undirected edge ``E(u, v)`` and introduces a delay of time ``t(u, v)`` |
| 68 | +in transfer of information between the nodes ``u`` and ``v``. |
| 69 | + |
| 70 | +We will be given ``K`` queries where each query contains the source node and the destination node and |
| 71 | +we will be required to determine the minimum time it will take for a piece of information to start from |
| 72 | +the source node and reach at the destination node. |
| 73 | + |
| 74 | +We will assume that the size of information and the processing time at any node doesn’t affect the travel time. |
| 75 | + |
| 76 | +**Input Format** |
| 77 | + |
| 78 | +The first line will contain a single positive integer ``N``. |
| 79 | + |
| 80 | +The second line will contain a single positive integer ``E``. |
| 81 | + |
| 82 | +Then ``E`` lines will follow, each line containing three space separated integers. |
| 83 | +The first two denoting node labels connected by an undirected edge which introduces |
| 84 | +a time delay denoted by the third integer. |
| 85 | + |
| 86 | +After that the next line will contain a positive integer ``K``. |
| 87 | + |
| 88 | +Then ``K`` lines will follow each containing two space separated node labels, the |
| 89 | +first denoting the source node and the second one denoting the destination node for that query. |
| 90 | + |
| 91 | +**Output Format** |
| 92 | + |
| 93 | +``K`` lines, each containing the minimum time required for the ``k``-th query. |
| 94 | + |
| 95 | +>>> from pydatastructs import Graph, AdjacencyListGraphNode |
| 96 | +>>> from pydatastructs.graphs.algorithms import shortest_paths |
| 97 | +>>> N = 4 |
| 98 | +>>> E = 3 |
| 99 | +>>> nodes = [] |
| 100 | +>>> for n in range(N): |
| 101 | +... nodes.append(AdjacencyListGraphNode(str(n + 1))) |
| 102 | +... |
| 103 | +>>> u_v_t = [(1, 2, 1), (2, 3, 1), (3, 4, 1)] # edges and their time delay |
| 104 | +>>> graph = Graph(*nodes) |
| 105 | +>>> for e in range(E): |
| 106 | +... u, v, t = u_v_t[e] |
| 107 | +... graph.add_edge(str(u), str(v), t) |
| 108 | +... graph.add_edge(str(v), str(u), t) |
| 109 | +... |
| 110 | +>>> K = 3 |
| 111 | +>>> u_v = [(1, 4), (3, 2), (4, 3)] # queries |
| 112 | +>>> delays = [] |
| 113 | +>>> for k in range(K): |
| 114 | +... u, v = u_v[k] |
| 115 | +... delay = shortest_paths(graph, 'dijkstra', str(u))[0] |
| 116 | +... delays.append(delay[str(v)]) |
| 117 | +... |
| 118 | +>>> print(delays) |
| 119 | +[3, 1, 1] |
0 commit comments