Repository navigation
Expand file tree
/
Copy pathkey.cpp
More file actions
175 lines (131 loc) · 3.53 KB
/
Copy pathkey.cpp
File metadata and controls
175 lines (131 loc) · 3.53 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// array
int cap;
int size;
int* arr;
void resize(int c) {
a = new int[c];
for (int i = 0; i < size; i++) {
a[i] = arr[i];
}
delete[] arr;
arr = a;
cap = c;
}
if (size == cap) { resize(cap * 2); } // grow
if (size == (cap / 4)) { resize(cap / 2); } // shrink
// linkedlist
node *prev = nullptr;
node *curr = head;
for (int i = 0; i < index; i++) {
prev = curr;
curr = curr->next;
}
(
prev->next = in; // insert @ index
in->next = curr;
)
prev->next = curr->next; // delete @ index
// hashtable
entry* table;
int i = hash(key, cap);
while (table[i].occupy || table[i].tombstone) {
if (table[i].occupy && table[i].key == key) { return; }
else { i = (i + 1) % cap; }
}
// dfs
vector<vector<int>> list; // list of lists
vector<vector<int>> matrix; // 2d matrix
vector<bool> visited;
void dfs (int root) {
visited[root] = true;
// adj. list
for (int neighbour : list[root]) {
if (!visited[neighbour]) { dfs(neighbour); }
}
// adj. matrix
for (int neighbour = 0; neighbour < len(matrix); neighbour++) {
if (matrix[root][neighbour] && !visited[neighbour]) { dfs(neighbour); }
}
}
// bfs
vector<vector<int>> list; // list of lists
vector<vector<int>> matrix; // 2d matrix
void bfs (int root) {
queue<int> queue;
vector<bool> visited;
queue.push(root);
visited[root] = true;
while (!queue.empty()) {
int q = queue.front();
queue.pop();
// adj. list
for (int neighbour : list[q]) {
if (!visited[neighbour]) {
visited[neighbour] = true;
queue.push(neighbour);
}
}
// adj. matrix
for (int neighbour = 0; neighbour < len(matrix); neighbour++) {
if (matrix[q][neighbour] && !visited[neighbour]) {
visited[neighbour] = true;
queue.push(neighbour);
}
}
}
}
// mergesort
// sort pairs
void merge (int *array, int low, int mid, int high) {
for (int i = 0; i < (mid - low + 1); i++) { left[i] = array[low + i]; }
for (int i = 0; i < (high - mid); i++) { right[i] = array[mid + 1 + i]; }
int i = 0; // left
int j = 0; // right
int k = low; // array
while (i < len(left) && j < len(right)) {
if (left[i] <= right[j]) {
array[k] = left[i];
i++;
}
else {
array[k] = right[j];
j++;
}
}
while (i < len(left)) {
array[k] = left[i];
i++;
k++;
}
while (j < len(right)) {
array[k] = right[j];
j++;
k++;
}
}
int* mergesort (int *array, int low, int high) {
if (low < high) {
// recursion on left, right
merge(array, low, mid, high);
}
}
// quicksort
int partition (int *array, int low, int high) {
int pivot = array[mid;]
std::swap(array[mid], array[high]); // pivot to end
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
std::swap(array[i], array[j]); // real swap if i != j
}
}
std::swap(array[i + 1], array[high]); // pivot to pos
return i + 1; // pivot index
}
int* quicksort (int *array, int low, int high) {
if (low < high) {
int pivot = partition(array, low, high);
// recursion on pivot left, pivot right
}
}