During the presentation, we will consider what advantages a front-end developer will get from knowledge of algorithms and data structures.
We will analyze real-world examples where this knowledge simplified logic and accelerated applications dramatically.
And finally we will understand the most necessary things that will allow us to determine the effectiveness of the code and easily improve it.
17. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
18. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n)
19. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n)
20. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
21. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic
22. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
23. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n)
for all n > n0
. }
24. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n)
for all n > n0
. }
c and
n0
are constants, f(n)
and
g(n) are functions over
non-negative integers
25. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n)
for all n > n0
. }
c and
n0
are constants, f(n)
and
g(n) are functions over
non-negative integers
26. Сложность алгоритма
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n)
for all n > n0
. }
c and
n0
are constants, f(n)
and
g(n) are functions over
non-negative integers
27. Algorithm Complexity
Big O notation, O
Omega notation, Ω
Theta Notation, θ
asymptotic notations, asymptotic complexity
O(n) Ο(log n) O(n * logn)
logarithmic polynomial
Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n)
for all n > n0
. }
c and
n0
are constants, f(n)
and
g(n) are functions over
non-negative integers
60. Big O
function simpleCalculate(number) {
const a = 1 + 2;
const b = 3 + 4;
console.log('calculating...');
return b - a + number;
}
simpleCalculate(8);
O(1)
61. Big O
function simpleCalculate(array) {
const a = 1 + 2;
const b = 3 + 4;
let additionalNumber = 0;
array.forEach(num => additionalNumber += num);
return b - a + additionalNumber;
}
simpleCalculate([1, 2, 5, 10, 1223]);
62. Big O
function simpleCalculate(array) {
const a = 1 + 2;
const b = 3 + 4;
let additionalNumber = 0;
array.forEach(num => additionalNumber += num);
return b - a + additionalNumber;
}
simpleCalculate([1, 2, 5, 10, 1223]);
O(n)
63. Big O
function simpleCalculate(array) {
const a = 1 + 2;
const b = 3 + 4;
const additionalNumber = array.length;
return b - a + additionalNumber;
}
simpleCalculate([1, 2, 5, 10, 1223]);
64. Big O
function simpleCalculate(array) {
const a = 1 + 2;
const b = 3 + 4;
const additionalNumber = array.length;
return b - a + additionalNumber;
}
simpleCalculate([1, 2, 5, 10, 1223]);
O(1)
65. Big O
function notSoSimpleCalculate(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
array[i] = array[i] + array[j];
}
}
return array;
}
notSoSimpleCalculate([1, 2, 3]);
66. Big O
function notSoSimpleCalculate(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
array[i] = array[i] + array[j];
}
}
return array;
}
notSoSimpleCalculate([1, 2, 3]);
O(n^2)
67. Размер входных данных, ед.
1 100 1000 10000
1
100
1000
10000
O(n^2) - квадратичная
Количествоопераций
68. Big O
function notSoSimpleCalculate(array) {
array.forEach(num => console.log(num));
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
array[i] = array[i] + array[j];
}
}
return array;
}
69. Big O
function notSoSimpleCalculate(array) {
array.forEach(num => console.log(num));
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
array[i] = array[i] + array[j];
}
}
return array;
}
O(n^2)
70. Big O
function mightBeSimpleCalculate(array) {
let total = 0;
array.forEach(num => {
const additional = array.indexOf(num) > 5 ? 5 : 1;
total = total + num + additional;
});
return array;
}
71. Big O
function mightBeSimpleCalculate(array) {
let total = 0;
array.forEach(num => {
const additional = array.indexOf(num) > 5 ? 5 : 1;
total = total + num + additional;
});
return array;
}
O(n^2)
72. Big O
function mightBeSimpleCalculate(array) {
let total = 0;
array.forEach(num => {
const additional = array.indexOf(num) > 5 ? 5 : 1;
total = total + num + additional;
});
return array;
}
O(n^2)
73. Big O
function mightBeSimpleCalculate(array) {
let total = 0;
array.forEach((num, index) => {
const additional = index > 5 ? 5 : 1;
total = total + num + additional;
});
return array;
}
O(n)
76. Пример роста в цифрах
O(1) - 1
Входные данные - 10000 элементов
77. Пример роста в цифрах
O(1) - 1
O(n) - 10000
Входные данные - 10000 элементов
78. Пример роста в цифрах
O(1) - 1
O(n) - 10000
O(n^2) - 100000000 (сто миллионов)
Входные данные - 10000 элементов
79. Пример роста в цифрах
O(1) - 1
O(n) - 10000
O(n^2) - 100000000 (сто миллионов)
O(n^3) - 1000000000000 (один триллион)
Входные данные - 10000 элементов
80. Пример роста в цифрах
O(1) - 1
O(n) - 10000
O(n^2) - 100000000 (сто миллионов)
O(n^3) - 1000000000000 (один триллион)
O(n!) очень много
100000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000……
Входные данные - 10000 элементов
182. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
183. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
const seattleData = {
name: 'Seattle',
coordinates: [
47.7471188,
-125.6412592,
],
state: 'WA',
country: 'USA',
weather: 'Sunny',
/.../
};
184. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
googleRoute.addNode(seattle);
googleRoute.addNode(sanFrancisco);
googleRoute.addNode(lasVegas);
googleRoute.addNode(saltLakeCity);
185. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
googleRoute.addNode(seattle);
googleRoute.addNode(sanFrancisco);
googleRoute.addNode(lasVegas);
googleRoute.addNode(saltLakeCity);
googleRoute.addEdge(seattle, sanFrancisco);
googleRoute.addEdge(sanFrancisco, lasVegas);
googleRoute.addEdge(lasVegas, saltLakeCity);
googleRoute.addEdge(saltLakeCity, sanFrancisco);
186. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
googleRoute.addNode(seattle);
googleRoute.addNode(sanFrancisco);
googleRoute.addNode(lasVegas);
googleRoute.addNode(saltLakeCity);
googleRoute.addEdge(seattle, sanFrancisco);
googleRoute.addEdge(sanFrancisco, lasVegas);
googleRoute.addEdge(lasVegas, saltLakeCity);
googleRoute.addEdge(saltLakeCity, sanFrancisco);
sanFrancisco.connections = [
seattle,
lasVegas,
saltLakeCity,
];
187. const googleRoute = new Graph();
const seattle = new Node('Seattle');
const sanFrancisco = new Node('San Francisco');
const lasVegas = new Node('Las Vegas');
const saltLakeCity = new Node('Salt Lake City');
googleRoute.addNode(seattle);
googleRoute.addNode(sanFrancisco);
googleRoute.addNode(lasVegas);
googleRoute.addNode(saltLakeCity);
googleRoute.addEdge(seattle, sanFrancisco);
googleRoute.addEdge(sanFrancisco, lasVegas);
googleRoute.addEdge(lasVegas, saltLakeCity);
googleRoute.addEdge(saltLakeCity, sanFrancisco);
sanFrancisco.connections = [
seattle,
lasVegas,
saltLakeCity,
];
seattle.connections = [
sanFrancisco
];
194. Где еще используются графы
1. Сеть (маршрутизация данных).
2. Логистика (маршруты, направления).
3. Телекоммуникации (планирование частот
вышек).
4. Социальные сети (возможные знакомые).
5. Рекомендации.
6. Схемы.
7. И еще очень много где.
282. Уже хорошо
работает
Array.sort()
Разные имплементации в
разных браузерах. (и в
разных версиях).
В целом, базируется на
сортировках слиянием и
быстрой сортировке (в
зависимости от типа данных в
массиве).
+ на небольших массивах (до
10 элементов) используется
сортировка вставками.
284. Заключение
● Вам не нужно знать все алгоритмы и структуры данных. Самые
необходимые знания, дающие больше всего пользы, в то же
время являются и самыми простыми в освоении.
285. Заключение
● Вам не нужно знать все алгоритмы и структуры данных. Самые
необходимые знания, дающие больше всего пользы, в то же
время являются и самыми простыми в освоении.
● Нет необходимости помнить все реализации, вы даже можете
забыть какие-то самые важные. Достаточно один раз
разобраться в них, чтобы иметь представление о возможностях.
286. Заключение
● Вам не нужно знать все алгоритмы и структуры данных. Самые
необходимые знания, дающие больше всего пользы, в то же
время являются и самыми простыми в освоении.
● Нет необходимости помнить все реализации, вы даже можете
забыть какие-то самые важные. Достаточно один раз
разобраться в них, чтобы иметь представление о возможностях.
● Анализируйте ваши алгоритмы. Задумывайтесь над их
эффективностью. Чем эффективнее код вы создаете, тем
производительнее и надежнее будет ваш продукт (и тем круче
вы станете как разработчик)
287. Материалы
Теория
● Оценка Сложности алгоритма
● Асимптотический анализ алгоритмов
● Структуры данных для самых маленьких
● The complexity of simple algorithms and data structures in JS
● Introduction to Data Structures and Algorithms
Практика
● Data Structures
● Fundamentals of Algorithms
● HackerRank