The farther backward you can look, the farther forward you will see.
Why do we need computational complexity analysis?
The limitation of post-hoc analysis
- The performance will vary according to different hardwares.
- The size of the data or the way the data is structured will also impact the performance.
Big O notation
On the contrary, the complexity analysis is platform-independent. There are two ways of doing complexity analysis, which are time complexity and space complexity. Big O notation, Big-omega notation and Big-theta notation are used to estimate the complexity function for arbitrarily large input. In this notes, I am only use the Big O notation to analysis the algorithm.
$$\mathcal{T}(n)=\mathcal{O}({f}(n))$$
Basic Algorithm
Sort
Graph
Construct a Graph
1
2
3
4
5
6
7
8
9
10
11graph = {
'A': ['B', 'F'],
'B': ['C', 'I', 'G'],
'C': ['B', 'I', 'D'],
'D': ['C', 'I', 'G', 'H', 'E'],
'E': ['D', 'H', 'F'],
'F': ['A', 'G', 'E'],
'G': ['B', 'F', 'H', 'D'],
'H': ['G', 'D', 'E'],
'I': ['B', 'C', 'D'],
}BFS
- Use a queue to traverse the vertex and its neighbour
- Use a set to record the visited vertex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22from collections import deque
"""
q = [A, ] -> q = []
cur = {A: ['B', 'F']}
visited = {} -> visited = {'A'}
q = [] -> q = ['B', 'F']
"""
def bfs(graph, start):
res = []
q = deque([start, ])
visited = set()
while q:
cur = q.popleft()
if cur not in visited:
res.append(cur)
visited.add(cur)
for node in graph[cur]:
q.append(node)
return res
print(bfs(GRAPH, 'A'))
DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def dfs(graph, start):
res = []
visited = set()
helper(res, graph, start, visited)
return res
def helper(res, graph, start, visited):
if start not in visited:
res.append(start)
visited.add(start)
for node in graph[start]:
if node not in visited:
helper(res, graph, node, visited)
print(dfs(GRAPH, 'A'))LeetCode
126, 127
Tree
Binary Search Tree(BST)
Reference