Professional Documents
Culture Documents
Lecture 1
L.J. Bel
iCSC 2018
Lecture I
Outline
1. Motivation
2. Sorting algorithms
3. Complexity analysis
4. Linear data structures
Lecture II
3
1. Motivation
4
Motivation
1. Everything running on your
● Algorithms
computer is an algorithm
● Data Structures 2. Analysing them is paramount to
writing, maintaining and
improving them
3. Several tools exist to help achieve
this
5
Motivation
6
2. Sorting algorithms
7
Sorting
6 5 3 1 8 7 2 4
1 2 3 4 5 6 7 8
8
Insertion sort
● In pseudocode:
i●
← 1“Naive” sorting algorithm
while i < length(A) take each element N steps
● One-by-one,
j←i
and move it N steps
while j > 0 and A[j-1] > A[j]
● swap
When alland
A[j] elements
A[j-1] have been
jmoved,
← j - 1 list is sorted!
end while By Swfung8 (Own work) [CC BY-SA 3.0],
i←i+1 via Wikimedia Commons
end while
Total: N² steps 9
Bubble sort
Total: N² steps 10
Intermezzo: Divide and conquer
11
Merge sort
13
Complexity – O
14
Complexity – O
16
Complexity – O
● Formal definition:
f(N) = O(g(N)) ↔
∃ N₀, M : ∀ N > N₀ : f(N) ≤ M g(N)
● In words: starting from N₀, f is bounded from
above by g (up to some constant M)
● Ω: lower bound
○ Formal definition:
f(N) = Ω(g(N)) ↔
∃ N₀, M : ∀ N > N₀ : f(N) ≥ M g(N)
○ In words: starting from N₀, f is bounded from
below by g (up to some constant M)
● Examples:
○ N = Ω(log(N))
By Cmglee (Own work) [CC BY-SA 4.0],
○ N² = Ω(N) via Wikimedia Commons
18
Complexity – Θ
● Θ: exact bound
○ Formal definition:
f(N) = Θ(g(N)) ↔
f(N) = O(g(N)) and f(N) = Ω(g(N))
● Examples:
○ N = Θ(N)
○ 2N = Θ(N)
○ 3N²+5N+1 = Θ(N²)
By Cmglee (Own work) [CC BY-SA 4.0],
○ N ≠ Θ(N²) (even though N = O(N²)) via Wikimedia Commons
19
One more sorting example: Quicksort
● Randomised-quicksort: select
the pivot randomly 20
Stable sorting
21
Comparison of algorithms
22
4. Linear data structures
23
Memory
24
Arrays
25
Dynamic arrays
*Amortised.
C++: std::vector
Python: list
C#: System.Collections.ArrayList
Java: java.util.ArrayList 26
Pointers
27
Linked list
C++: std::forward_list 28
Doubly Linked list
C++: std::list
C#: System.Collections.Generic.LinkedList
Java: java.util.LinkedList 29
Binary search
Lecture II
33
Recall: Binary search
● Searches a sorted linear data structure
● Takes Θ(log(N))
● … let’s use this as inspiration for a data structure!
● Tree structure
● Pointers between nodes
○ To the right: only larger
○ To the left: only smaller
● Allows easy sorted iteration
● Search/insert/delete:
all O(log(N))
35
Hash tables
*Amortised. 38
6. Abstract data types
39
Why “Abstract”?
● Abstract Data Type (ADT) does not define a real data structure
○ Only defines an interface
○ Implemented using one of the “real” data structures
● Usually limits operations compared to actual DS
● Enhances flexibility
Operations:
● Linked list
By Vegpuff (Own work) [CC BY-SA 3.0],
● Dynamic array via Wikimedia Commons 41
Stack
Operations:
● Linked list
● Dynamic array
42
Map
Operations:
C++: std::map
Typical implementations: C#: System.Collections.Generic
.SortedSet
● Binary Search Tree Java: java.util.TreeMap
○ Requires sortable keys
○ Can do indexed/range queries!
○ Fast with many insertions
● Hash Table Python: dict
C++: std::unordered_map
○ Generally very fast Java: java.util.HashMap
○ Space-efficient
○ Need to keep load factor under control... 45
Set
Adapted by L.J. Bel from Jorge Stolfi (Own work) [CC BY-SA 3.0], 46
via Wikimedia Commons
Set
Operations:
Adapted by L.J. Bel from Jorge Stolfi (Own work) [CC BY-SA 3.0], 47
via Wikimedia Commons
Set
C++: std::set
Typical implementations: C#: System.Collections.Generic.SortedSet
Java: java.util.TreeSet
● Binary Search Tree
● Hash Table Python: set (and frozenset)
● Bloom filter C++: std::unordered_set
C#: System.Collections.Generic.HashSet
Java: java.util.HashSet
48
Comparing ADTs
50
Graphs
51
Graphs
52
Graphs
Directed graph:
53
Pathfinding
54
Dijkstra’s algorithm
55
Dijkstra’s algorithm
56
Dijkstra’s algorithm
57
Dijkstra’s algorithm
58
Dijkstra’s algorithm
59
Dijkstra’s algorithm
60
Dijkstra’s algorithm
61
Dijkstra’s algorithm
62
Dijkstra’s algorithm
63
Dijkstra’s algorithm
65
Summary
● Concepts
● Divide and conquer
● Complexity
○ O, Θ, Ω
● (Un)stable sorting
● Pointers
66
Summary
● Concepts
● Sorting algorithms ● Insertion sort
● Bubble sort
● Merge sort
● Quicksort
67
Summary
● Concepts ● Arrays
● Sorting algorithms ● Dynamic arrays
● (Doubly) linked lists
● Data structures
● Binary search trees
● Hash tables
● (Directed) graphs
68
Summary
● Concepts
● Sorting algorithms ● Queues
● Stacks
● Data structures
● Maps
● Abstract data types ● Sets
69
Summary
● Concepts
● Sorting algorithms
● Binary search
● Data structures
● Dijkstra’s algorithm
● Abstract data types
● Algorithms
70