Data structures and supporting libraries

Overview and motivation

Linear Data Structure: Array

Problem’s link

:::::{tab-set} ::::{tab-item} Descriptions

:::: ::::{tab-item} IO format

:::: ::::{tab-item} Thought Process

:::: ::::{tab-item} AC

:::: :::::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

## Special Sorting Problems


- Inversion Index: count the number of swap to make a list into a sorted order
  - `O(n^2)`: actual runs bubble sort. 
  - `O(nlog(n))`:
    - Modification of Merge Sort: increment count by one when the `front right` is selected in the merging process before the `front left`. 
- Sorting in linear time:
  - Depending on data type
  - `O(n + k)` Counting Sort
  - `O(d * (n + k))` Radix Sort





<figure
  
>
  <picture>
    <!-- Auto scaling with imagemagick -->
    <!--
      See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
      https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
    -->
    
      
        <source
          class="responsive-img-srcset"
          
            srcset="/assets/img/courses/csc495/02-ds-algo/inversion_index-480.webp 480w,/assets/img/courses/csc495/02-ds-algo/inversion_index-800.webp 800w,/assets/img/courses/csc495/02-ds-algo/inversion_index-1400.webp 1400w,"
            type="image/webp"
          
          
            sizes="95vw"
          
        >
      
    
    <img
      src="/assets/img/courses/csc495/02-ds-algo/inversion_index.png"
      
      
        width="50%"
      
      
        height="auto"
      
      
      
        alt="Inversion Index Example"
      
      
      
        data-zoomable
      
      
        loading="lazy"
      
      onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
    >
  </picture>

  
</figure>





[Problem's link](https://open.kattis.com/problems/height)

:::::{tab-set}
::::{tab-item} Descriptions

- Students placement: Insertion Sort. 
- Count number of displacements due to insertion. 
  
::::
::::{tab-item} IO format

- First value is the `P` number of cases
- Each subsequent `21` integers:
  - First integer is case number (to be used in output)
  - Next twenty integers are the students' heights. 

::::
::::{tab-item} Thought Process

- Read each line and place 20 integers into array
- Perform insertion sort
  - Increase count if a swap is done. 
  
::::
::::{tab-item} AC

<script src="https://gist.github.com/linhbngo/3529c879f8375d9e7eb29f355805bc0c.js?file=kattis_height.java"></script>

::::
:::::

Linear Data Structure: Bitmask

Problem’s link

:::::{tab-set} ::::{tab-item} Descriptions

:::: ::::{tab-item} IO format

:::: ::::{tab-item} Thought Process

Bits patterns for 11173

:::: ::::{tab-item} AC

:::: :::::

Problem’s link

:::::{tab-set} ::::{tab-item} Descriptions

:::: ::::{tab-item} IO format

:::: ::::{tab-item} Thought Process

Bits patterns for 11581

:::: ::::{tab-item} AC

:::: :::::

1
2
3
4
5
6
7
8
9
10
11
12
13
14



## Big Integer


- Use [Java's BigInteger](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html) for this purpose



- Kattis: [Simple Addition](https://open.kattis.com/problems/simpleaddition)
- Kattis: [Wizardsof Odds](https://open.kattis.com/problems/wizardofodds)

Linked Data Structures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


## Non-linear Data Structures: Binary Heap (Priority Queue)


- [Java PriorityQueue](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html).
  - From node `i`:
    - To parent: `floor(i)` or `i>>1`
    - To left child: `2 * i` or `i<<1`
    - To right child: `2 * i + 1` or `(i<<1) + 1`
- Heap property:
  - Items on left and right subtree of root `x` are always smaller than `x`. 
  - The root is always the max-value item. 
- Highest value-item can be dequeued in `log(n)` time. 
- New item can be enqueue in `log(n)` time. 
- Important components for:
  - Prim's and Kruskal's algorithms for minimum spanning tree. 
  - Dijkstra's Single Source Shortest Path. 




[Problem's link](https://open.kattis.com/problems/knigsoftheforest)


:::::{tab-set}
::::{tab-item} Descriptions



::::
::::{tab-item} Thought Process

- PriorityQueue
  - Two queues
  - Rewrite comparator to custom compare. 
  - [Writing custom comparator](https://www.callicoder.com/java-priority-queue/)
  - [More description on comparator - how do they compare?](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html)

::::
::::{tab-item} AC

<script src="https://gist.github.com/linhbngo/3529c879f8375d9e7eb29f355805bc0c.js?file=kattis_knigsoftheforest.java"></script>

::::
:::::




- Kattis: [Stock Prices](https://open.kattis.com/problems/stockprices)

Hash Table

Problem’s link

:::::{tab-set} ::::{tab-item} Descriptions

:::: ::::{tab-item} Thought Process

:::: ::::{tab-item} AC

:::: :::::

```

Balanced Binary Search Tree (bBST)

Order Statistics Tree