DS1 Stack Merged
DS1 Stack Merged
VinUni, CECS
1
Learning Objectives
– deletion (pop)
• Applications
– Maze Problem
– Evaluation of expression
Push, Pop, Empty, …
• Why?
– To enhance your understanding of data structure !
Stack Size?
Array-based Stack
Implementation
• Why? Some Algorithm size()
programming return t + 1
languages (e.g., C)
do not provide Algorithm pop()
built-in stack if empty() // t=-1?
throw StackEmpty
support else
– Add elements from left t t 1
to right return S[t + 1]
– A variable, top or t,
keeps track of the index
of the top element
…
S
0 1 2 t N-1
Array-based Stack
Implementation
• The array storing
the stack elements Algorithm push(o)
may become full if t = S.size() 1
then throw StackFull
– Throw a StackFull else {
exception? t t + 1
• No need to worry in S[t] o
java.util.Stack. Why? }
– Alternatively, stack
could be resized (e.g.,
reallocated) and data
copied
…
S
0 1 2 t
In “static” environment
(e.g., C)
In C
(e.g., static environment)…
#include <stdio.h> // Resize the array
#include <stdlib.h> int new_size = 10;
arr = (int *)realloc(arr, new_size * sizeof(int));
int main() { if (arr == NULL) {
int *arr; printf("Memory reallocation failed\n");
int size = 5; return 1;
}
// Allocate initial memory
arr = (int *)malloc(size * sizeof(int)); // Initialize new elements
if (arr == NULL) { for (int i = size; i < new_size; i++) {
printf("Memory allocation failed\n"); arr[i] = (i + 1) * 2; // Just an example initialization
return 1; }
}
// Print resized array
// Initialize the array printf("Resized array: ");
for (int i = 0; i < size; i++) { for (int i = 0; i < new_size; i++) {
arr[i] = i + 1; printf("%d ", arr[i]);
} }
printf("\n");
// Print original array
printf("Original array: "); // Free allocated memory
for (int i = 0; i < size; i++) { free(arr);
printf("%d ", arr[i]);
} return 0;
printf("\n"); }
Details, Details, …
// Resize the array
• See difference? int new_size = 10;
newarr = (int *)realloc(arr, new_size * sizeof(int));
// Resize the array if (newarr == NULL) {
int new_size = 10; printf("Memory reallocation failed\n");
arr = (int *)realloc(arr, new_size * sizeof(int)); return 1;
if (arr == NULL) { }
printf("Memory reallocation failed\n");
return 1; // Initialize new elements
} for (int i = size; i < new_size; i++) {
newarr[i] = (i + 1) * 2;
// Initialize new elements }
for (int i = size; i < new_size; i++) { oldarr = arr;
arr[i] = (i + 1) * 2; arr = newarr;
} free (oldarr);
return 0; return 0;
} }
Array-based Stack
Implementation
• Performance
– Let N be the array size
• Space usage: O(N) (must be pre-allocate array of size N)
• Each operation (e.g., push, pop, …) runs in O(1)
• Limitation
– The maximum size of the stack must be defined a priori and
cannot be changed
– Trying to push a new element into a full stack can causes a
run-time error
Stack implementation using
Linked List
nodes
t
elements
Stack implementation using
Linked List
nodes
t
elements
Application: System Stack
• Many others
– Slammer Worm
(2003)
– Blaster Worm (2003)
– Hearbleed (2014)
– EternalBlue (2017)
– …
• Don’t be
surprised if
there is another
buffer overflow
attack tomorrow
Stack Application Example:
Maze Problem
• 2D array to model the map
– 0: opened (can move), 1: blocked (cannot move)
– int maze[i][j]
Enter 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1
1 0 0 0 1 1 0 1 1 1 0 0 1 1 1
0 1 1 0 0 0 0 1 1 1 1 0 0 1 1
1 1 0 1 1 1 1 0 1 1 0 1 1 0 0
1 1 0 1 0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 0 1 1 1 0 1 0 0 1 0 1
0 0 1 1 0 1 1 1 0 1 0 0 1 0 1
0 1 1 1 1 0 0 1 1 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 0 0 0 0 0
0 0 1 1 1 1 1 0 0 0 1 1 1 1 0
0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 Exit
Maze Problem
Maze Problem
COMP1020
Scope
Maze Problem
• Items
– (x,y,dir) : at (x,y), try to move dir
• Strategy
– Try each direction unless it is not yet visited
– When moving forward, use stack to store the current location a
nd the next search direction
– If all directions are blocked, pop the previous location and nex
t search direction to continue
Maze Problem
q m ove[q].a m ove[q].b
N -1 0
NE -1 1
W [i][j-1] X [i][j+1] E E 0 1
SE 1 1
S 1 0
[i] [j]
SW 1 -1
W 0 -1
NW -1 -1
[i+1][j-1] [i+1][j] [i+1][j+1]
Table of moves
SW S SE
Move from x[I][j] to SW (x[g][h])
g = I + move[sw].a;
h = j + move[sw].b;
chatGPT’s
Maze Code in Java
push
pop
Different Behavior?
https://www.calcont.in/Calculator/Postfix_calculator/
Postfix Notation
• Benefits
– No parenthesis
– No operator priority
– Simple to evaluate (left to right scan)
• Example
– Infix: A/B-C+D*E-A*C
– Postfix: AB/C-DE*+AC*-
• Another Example
– Infix: “A*B/C”
– Postfix: “AB*C/”
– Prefix: “/*ABC”
Stack Applications
Valid/Balanced
Parenthesis Problem
• The Balanced Parentheses problem is a classic problem in computer
science and involves checking whether a given string containing
different types of parentheses (such as round brackets (), curly
braces {}, and square brackets []) is balanced or not.
– A string is considered "balanced" if every opening bracket has a
corresponding closing bracket in the correct order.
– String test1 = "{ [ ( ) ( ) ] }"; // Balanced (space added for clarity)
– String test2 = "{ [ ( ] ) }"; // Not Balanced
– String test3 = "( ( ( ) ) )"; // Balanced
– String test4 = "( ( ( ) )"; // Not Balanced
Potential Stack Questions
VinUni, CECS
1
Learning Objectives
Queue
front/head rear/tail
3
Queue Methods
• Among
Numerous
examples
Priority Queue
• Impressive!
chatGPT is
Equally Impressive
DIY:
Try to do the same
with MaxHeap, too
More Subtle Example
26
More Subtle Example
27
Naïve Implementation of
Queue
Risks of Naïve
Implementation of Queue
1. Fixed Size Limitation
•Problem: Arrays have a fixed size, which means the queue can only hold a
predetermined number of elements.
•Solution: …
2. Wasted Space
•Problem: When elements are dequeued, the space they occupied in the array is not
reused.
•Solution: ...
3. Resizing Overhead
•Problem: If you choose to resize the array when it becomes full, this operation can be
costly.
•Solution: …
4. Complexity of Circular Queue
•Problem: Implementing a circular queue can be more complex than a simple array-based
queue.
•Solution: …
5. Index Management
•Problem: Keeping track of the front and rear indices can be tricky, especially when the
queue wraps around.
•Solution: …
Naïve Implementation of
Queue using Array
Array Implementation of
Circular Queue
Why Should I bother?
I am using Java!
• You probably don’t have to worry about it when
using methods defined in java.util.LinkedList
class
– It is a convenient and dynamic data structure!!
– You still want to make sure that “corner cases” are properly
handled
– Corner cases (or rare/extreme inputs) are notoriously difficult
to test properly
• Some programming languages (e.g., C) lack
built-in support for Abstract Data Types
Potential Questions
Potential Questions
Sorting Algorithms
VinUni, CECS
1
Learning Objectives
• Big O notations
• Linear Search vs Binary Search
• Bubble Sort
– Brute-force (straightforward) implementation
– Possibilities to improve brute-force bubble sort algorithms
• Selection Sort
• Insertion Sort
• Merge Sort
• Shell Sort
• Quick Sort
Comparison: Linear Search
vs Binary Search
• Time Complexity: Best case, average case, wor
st case
• O(N) vs O(log N)
• “Management” of ordered array, however, is m
ore complex
“Big O”, Omega, Theta
Notations
• Big O is enough for now ;)
Criteria for
Time Complexity Analysis
There are diverse criteria
examples:
- # of for/while loop iterations
- # of visiting a particular line
- # of calls for a particular function
-…
sample(n):
factorial(n):
sum ← 0
if n = 0 or n=1 return 1
for i ← 1 to n-1
else return n * factorial(n-1)
for j ← i+1 to n
sum ← sum + A[i]*A[j]
return sum
Examples of Running Time
sample1(A[], n):
constant time
k= n/ 2 independent of n
return A[k]
sample2(A[], n):
sum ← 0
for i ← 1 to n
sum ← sum + A[i] proportional to n
return sum
Examples of Running Time
sample3(A[], n):
sum ← 0
for i ← 1 to n
proportional to n2
for j ← 1 to n
sum← sum+ A[i]*A[j]
return sum
sample4(A[], n):
sum ← 0
for i ← 1 to n
for j ← 1 to n 𝑛
k ← maximum among randomly chosen elements
2
sum ← sum + k
return sum
why proportional to n3 ?
Examples of Running Time
sample5(A[], n):
sum ← 0
for i ← 1 to n
for j ← i to n
sum ← sum + A[i]*A[j]
return sum
proportional to n2
Examples of Running Time
factorial(n): T(1) = c
if (n=1) return 1 T(n) = c + T(n-1)
return n*factorial(n-1)
proportional to n
// Traverse through all array elements // Traverse through all array elements
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n - 1; i++) {
// Last i elements are already sorted // Last i elements are already sorted
for (int j = 0; j < n - i - 1; j++) { for (int j = i; j < n - i - 1; j++) {
// Swap if elements are out of order // Swap if elements are out of order
if (arr[j] > arr[j + 1]) { if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1] // Swap arr[j] and arr[j + 1]
int temp = arr[j]; int temp = arr[j];
arr[j] = arr[j + 1]; arr[j] = arr[j + 1];
arr[j + 1] = temp; arr[j + 1] = temp;
} }
} }
} }
} }
Complexity Analysis
of Bubble Sort
• Can you do it yourself?
– Is best case really O(N)? public class BubbleSort {
38
• Merge Sort
Just don’t memorize;
Convince Yourself
Recursive MergeSort
Beyond the
Scope of COMP 1020
• Good example where curiosity and creativity
result in an effective algorithm
FYI
Shell Sort
54
Sir C.A.R. Hoare
Naïve Quick Sort
VinUni, CECS
1
Learning Objectives
3
Tree Terminology
4
Level, Depth, Height, …
5
Level, Depth, Height, …
DIY
Don’t Get Confused !!!
“Appearance”
(Not about data contents)
• Has nothing to
do with
“contents”
• We will learn
later that it is
NOT a binary
search tree
– OK if it does not
make sense now
Complete Binary Tree
• Useful as it can be
easily stored in array
– Location of child nodes can
be computed
– Root at [0]
• That’s why some
tree-based data
structures (e.g.,
MaxHeap, MinHeap)
enforce complete
binary tree
requirements
Heap
14 9 30
MaxHeap 12 7 6 3 25
10 8 6 5
14 9 30
Not a 12 7 6 25 40
MaxHeap!
Why? 10 6 5
Full/Complete Tree and Heap
14 5
12 7 12 7
10 8 6 8
Full Binary Tree? Y Full Binary Tree? Y
Complete Binary Tree? Y Complete Binary Tree? N
MaxHeap? Y MaxHeap? N
MinHeap? N MinHeap? N
2 9
4 7 4 7
10 6 10
• Becomes essentially
equivalent to linked
list
• Applications do not
have control over
input orders
– Need smart algorithms to
prevent tree from being
skewed
– AVL tree or Red-Black tree
Binary Search Tree (BST)
• Intuitive Algorithm
– Could result in skewed
(e.g., inefficient) BST
LinkedList and
Tree Structure
Advantages Disadvantages
• Dynamic Size Allocation • Extra Memory Overhead
• Efficient Insertions and • Slower Access Time
Deletions • Complex
• Flexible Structure Implementation
• Memory Utilization • Cache Inefficiency
• Easy Implementation of • Garbage Collection
Parent and Child Links Issues (in some
languages)
– Not in Java
– Manual deallocation
required in C or C++
BST and Java Code
BST and Java Code
Java and Binary Tree
We will
study
“simpler”
AVL tree
instead
Binary Tree Traversal
• Preorder
• Inorder
• Postorder
One of KU’s Data Structure
Exam Question
• Credit to Prof. Won-Ki Jeong
• Draw a binary tree whose inorder and
postorder traversal sequences are as follows:
– Inorder : “A D E F C I H B G”
– Postorder : “A D F I H C G B E”
Binary Search Tree (BST)
insert(t, x):
◀ t : root node, x : key to insert
30
if (t=NIL)
r.key ← x ◀ r: new node
20 40
return r
if (x < t.key)
t.left ← insert(t.left, x) 25
return t
else t.right ← insert(t.right, x)
return t
Examples of Insertion
30 30 30 30
20 20 20 40
25 25
30 30
20 40 20 40
10 25 10 25 35
(e) (f)
Deletion in Binary Search Trees
t:root node
r: node to deleted
Deletion in Binary Search
Trees
sketch_delete(t, r):
◀ t : root node, r : node to delete
if (r is a leaf node) ◀ Case 1
Just throw away r
else if (r has only one child) ◀ Case 2
Let r’s parent links to the (only) child of r
else ◀ Case 3
Remove the minimum node s of r’s right subtree,
and copy the key of s to node r
Example: Case 1
55 55
15 60 15 60
8 28 90 8 28 90
r
3 18 30 3 30
48 48
38 50 38 50
33 33
32 36 32 36
15 60 15 60 15 60
8 28 90 8 28 90 8 28 90
r
3 18 30 3 18 3 18 48
48 48 38 50
38 50 38 50
33
33 33 32 36
32 36 32 36
(a) r has only one child (b) Remove r (c) Put r’s (only) child at r’s location
Example: Case 3
55 55
15 60 15 60
r
8 28 90 8 r 90
3 18 45 3 18 45
41 48 41 48
s s
30 50 30 50
38 38
33 33
32 36 32 36
15 60 15 60
s s
8 30 90 8 30 90
3 18 45 3 18 45
41 48 41 48
s 50 38 50
38
33
33
32 36
32 36
(c) Move s to r’s location (d) Move s’s (only) child to s’s location
(copy s to r)
DIY
• Insert 2 ?
• Insert 55 ?
– Tree might become “less
balanced”, but it is still a
BST
• Delete 30 ?
• Delete 52 ?
• Delete 50 ?
– Choose inorder
predecessor?
– Choose inorder
successor?
Another KU’s Data
Structure Exam Question
• Credit to Prof. Won-Ki Jeong
• Assume that you insert a new element x to a binary search tree
that does not contain x. After inserting x, you delete x
immediately from the tree. If you repeat this insertion-deletion
operation several times (each time with different value x), would
the tree be same as the initial one? If yes, then explain why. If no,
then give a counter example.
Another KU’s Data
Structure Exam Question
• Credit to Prof. Won-Ki Jeong
• Assume that you delete a leaf node x from a binary search tree
and insert x back to the tree immediately. If you repeat this
deletion-insertion operation several times (each time with different
x value), would the tree be same as the initial one? If yes, then
explain why. If no, then give a counter example.
3
Search on AVL Tree
Q R S P
S T T R
LR? RL?
Can be confusing!
#2
#1
LR? RL?
Can be confusing!
#2
#1
Summary of
AVL Tree Rotation
AVL Tree Insertion
Recommended Exercises
DIY. Strongly
Recommended
Same Values,
Different Orders
• “Internal” structure of AVL tree may vary
depending on the order of insertion
– [10, 25, 50, 20, 40, 30]
– [10, 20, 25, 30, 40, 50]
• But, same performance (e.g., time complexity)
when insertion/deletion operations are applied
• Still height-balanced
[10, 25, 50, 20, 40, 30]
[10, 20, 25, 30, 40, 50]
AVL Tree and
Node Deletion
Unbalanced AVL Tree on
Deletion
• What happen if we
delete 20, 30, and
40 in that order?
Red-Black Tree
1
0
7 4
0
3 8 3 4
0 5
3 6
1 5 2 0
5
0
2
5
Properties
VinUni, CECS
1
Learning Objectives
8 8
9
4 7 3 5
8 8
2 1 3
4 7 3 5
2 1 3
MinHeap
MinHeap
Heap
14 9 30
Max heap
12 7 6 3 25
10 8 6 5
2 10 11
Min heap 7 4 20 21
83
10 8 6 50
Search on MaxHeap (or
MinHeap)
14
12 7
10 8 6
14
10 12
8 7 6
14
8 12
7 6 10
Never Stop Asking Yourself
Insertion, MaxHeap
MaxHeap Insertion
Example
MinHeap Insertion
20
15 17
8 10 6 9
2
Example: Insert 21
• Step 1
20
15 17
8 10 6 9
2 21
Create node at the end of the heap
Example: Insert 21
• Step 2
20
15 17
21 10 6 9
2 8
Bubbling up : 8 <-> 21
Example: Insert 21
• Step 3
20
21 17
15 10 6 9
2 8
Bubbling up : 15 <-> 21
Example: Insert 21
• Step 4
21
20 17
15 10 6 9
2 8
Bubbling up : 20 <-> 21, done!
DIY
21
20 17
15 10 6 9
2 8
Impressive indeed !!
Deletion, MaxHeap
21
20 17
15 10 6 9
2 8
MaxHeap Deletion
20 17
15 10 6 9
Copy data of the last element to the top and delete the last node
MaxHeap Deletion
• Step 2
20
8 17
15 10 6 9
• Step 3
20
15 17
8 10 6 9
VinUni, CECS
1
Learning Objectives
•Graph Terminology
•Graph Representation
–Adjacency Matrix
–Adjacency List
•Graph Traversal
–Breath-first search (using FIFO Queue)
–Depth-first search (using LIFO Stack)
•Shortest Path Algorithm
–Dijkstra’s algorithm
–There are many others, but beyond the scope of COMP 1020
8
FYI Only.
Not in the exam^^
Graph Terminology:
Vertex (Node), Edge
A B A graph with four vertices and five edges
D C
10
Directed Graph
13
Degree, Neighbor
14
In-Degree, Out-Degree
deg−(3)=2
deg+(3)=2
15
In-Degree, Out-Degree
16
Complete Graph
17
Cycle
18
Path and Cycle
19
Connected Graph
Connected
component
(subset)
20
Graph vs Tree
21
DAG
(Directed Acyclic Graph)
•A directed acyclic graph (DAG) is a graph that ha
s NO directed cycle.
22
DAG or not?
23
Why Study Graph Theory?
Graph-based Algorithms
Graph-based Algorithms
Graph Representation
– Adjacency List
27
Adjacency Matrix and
Undirected Graph
B A B C D E
A 0 1 1 1 0
A
B 1 0 0 0 0
D C 1 0 0 1 0
symmetric
D 1 0 1 0 1
C
E 0 0 0 1 0
E
30
Adjacency Matrix and
Directed Graph
B A B C D E
A 0 1 1 0 0
A
B 0 0 0 0 0
D Symmetric?
C 0 0 0 0 0
D 1 0 1 0 1
C
E 0 0 0 0 0
E
31
Adjacency List
32
Adjacency List: Another Example
B A B C D
B A
A
D C A D
D A C E
C
E D
E
B A C
A B A
D C
D A C E
C
E
E
33
Dense vs Sparse Graph
Breadth-First Graph Search
An Example BFS
Animation
Starting vertex: a
Queue a b f i c e g d h Done !
Depth-First Graph Search
An Example DFS
Animation
Starting vertex: a
ef
g
h
d
c
bi
a
Done !!
Stack
BFS vs DFS
Breadth-First Graph Search
BFS and Shortest Path
More Complex
BFS Example
• Perform BFS and find the shortest path from A
to J
B C
D E F G
H I
J
Minimum Spanning Trees
• Definition
– A spanning tree of least cost
– Cost: edge weights
• Greedy algorithms
– Find optimal solution in each stage
– Kruskal algorithm, Prim algorithm
– Constraints for minimum-cost spanning tree
• Use edges within the graph
• Use exactly n-1 edges
• We may not use edges that produce a cycle
Kruskal’s Algorithm
0 0 0
28
10 1 1 10 1
14
16
5 5 5
6 6 6
24 2 2 2
25
4 18 4 4
12
22
3 3 3
0 0 0
10 1 10 1 10 1
14 14
16
5 5 5
6 6 6
2 2 2
4 4 4
12 12 12
3 3 3
0 0
10 1 10 1
14 14
16 16
5 5
6 6
2 2
25
4 4
12 12
22 22
3 3
(g) (h)
E
3 2
B 2
F
4 3
D
2 E
5 6
3 2 E
3 2
A 3 B 2
C F B 2
F
D
2 D
3 2
A 3
C 3
A C
Prim Algorithm
0 0 0
10 1 10 1 10 1
5 5 5
0 6 6 6
28 2 2 2
25 25
4 4 4
10 1
14 22
16 3 3 3
5
6 (a) (b) (c)
24 2
25
4 18
0 0 0
12
22 10 1 10 1 10 1
3
14
16 16
5 5 5
6 6 6
2 2 2
25 25 25
4 4 4
12 12 12
22 22 22
3 3 3
VinUni, CECS
1
Learning Objectives
1930~2002
Edsger W. Dijkstra
Real-World Applications of
Dijkstra’s Algorithm
• Navigation and Route Planning (e.g., Google
Maps, GPS systems, Transportation apps like
Uber, Waze, Grab, …)
– Robotics and Autonomous Vehicles, too
– Airline and railway scheduling as well
• Network Routing (Internet &
Telecommunications)
• AI and Game Development (e.g., A* algorithm)
• Social networks and recommendation systems
(e.g., people you may know)
• Electrical grid and circuit design
• …
Dijkstra’s
Shortest Path Algorithm
Weighted Graph
4 A 1
3
B C
2 5
D
Dijkstra Algorithm
in Action
Source : 0
1 9
5
3 4 6
10
0 2 2 6
5 2
7 3
3
S : {}
Dijkstra Algorithm
in Action
Step 1 (Initially) inf
Source : 0
1 9 inf
5
0 inf 3 4 6 inf
10
0 2 2 inf 6
inf 5 2
7 3
3
S : {0}
Dijkstra Algorithm
in Action
Step 2 5 = min(inf, d(0)+5 = 5)
Source : 0
1 9 inf
5
0 10 3 4 6 inf
10
0 2 2 inf 6
5 2
7 3
3
7 = min(inf, d(0)+7)=7)
S : {0}
Dijkstra Algorithm
in Action
Step 3 5
Source : 0
1 9 inf
5
0 10 3 4 6 inf
10
0 2 2 inf 6
7 5 2
7 3
3
S : {0, 1}
Dijkstra Algorithm
in Action
Step 4 5
Source : 0 14 = min(inf, d(1)+9=14)
1 9
5
0 10 3 4 6 inf
10
0 2 2 inf 6
7 5 2
7 3
3
S : {0, 1}
Dijkstra Algorithm
in Action
Step 5 5
Source : 0
1 9 14
5
0 10 3 4 6 inf
10
0 2 2 inf 6
7 5 2
7 3
3
S : {0, 1, 3}
Dijkstra Algorithm
in Action
Step 6 5
Source : 0
1 9 14
5
0 10 3 4 6 inf
10
0 2 2 6
7 5 2
7 3
3 10 = min(inf, d(3)+3=10)
S : {0, 1, 3}
Dijkstra Algorithm
in Action
Step 7 5
Source : 0
1 9 14
5
0 10 3 4 6 inf
10
0 2 2 6
7 5 2
7 3
3 10
S : {0, 1, 3, 2}
Dijkstra Algorithm
in Action
Step 8 5
Source : 0
1 9 13 = min(14,d(2)+3)
5
0 10 3 4 6 inf
10
0 2 2 6
7 5 2
7 3
3 10 = min(10, d(2)+2)
S : {0, 1, 3, 2}
Dijkstra Algorithm
in Action
Step 9 5
Source : 0
1 9 13
5
0 10 3 4 6 inf
10
0 2 2 6
7 5 2
7 3
3 10
S : {0, 1, 3, 2, 5}
Dijkstra Algorithm
in Action
Step 10 5
Source : 0
1 9 13
5
0 10 3 4 6 12
10
0 2 2 6
7 5 2
7 3
3 10
S : {0, 1, 3, 2, 5}
Dijkstra Algorithm
in Action
Step 11 5
Source : 0
1 9 13
5
0 10 3 4 6 12
10
0 2 2 6
7 5 2
7 3
3 10
S : {0, 1, 3, 2, 5, 6}
Dijkstra Algorithm
in Action
Step 12 5
Source : 0
1 9 13
5
0 10 3 4 6 12
10
0 2 2 6
7 5 2
7 3
3 10
S : {0, 1, 3, 2, 5, 6, 4}
Dijkstra’s Shortest Path
Algorithm
• Though possibly intuitive, it can be difficult to
understand
• There are lots of visualization “helps” available
– YouTube, chatGPT, search engine, …
DIY
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Dijkstra’s Algorithm:
Step-by-Step
A
4 2
3
B C
6 5
2 7
D E F
1 5
Understand Dijkstra’s
Algorithm in Java
Constraints on
Dijkstra’s Algorithm
DIY
5 C
A -10
4
B
-1
C
A -1
1
B
Dijksta vs Bellman-Ford
Dijksta vs Bellman-Ford
Chromatic number = 3
[source]
36
Greedy Algorithm
to Graph Coloring
•
37
Example: Graph Coloring
1. Start with vertex 1 and use the first color.
2. Vertex 2 doesn’t have any adjacent colors. Thus, we use the first one as well.
3. Vertex 3 has a blue vertex adjacent to it. Thus, we use the second one.
4. Similarly, vertex 4 has a blue vertex adjacent to it twice, so we also use the second one.
5. Vertex 5 has blue and green adjacent to it. Therefore, we have to use the third color for it.
6. Vertex 6 now has blue, green, and red vertices adjacent to it. As a result, we have to use the new
fourth color.
7. Finally, vertex 7 has blue, green, and orange adjacents. Since the third color (red) is not adjacent, we
can use it.
38
Greedy Coloring Algorithm
C D
Where Greedy Algorithm
Failed
Potential Questions
Hash Table and Collision
Handling Algorithms
VinUni, CECS
1
Learning Objectives
• Array or LinkedList
– Overall O(n) time
• Binary search trees
– Expected O(log n) time search, insertion, deletion
– But, O(n) in the worst case
• Balanced binary search trees
– Guarantees O(log n) time search, insertion, and deletion
– AVL tree
• Hash table
– Expected O(1) time search, insertion, and deletion
Hashing
Wikipedia
Hashing: key to index is computed using some rules (i.e., hash function)!
Hash Table
• b = 26, s = 2, n = 10
• Keys : GA, D, A, G, L ,A2, A1, A3, A4, E
• Hash function : A~Z to 0~25, first character
– A -> 0
– A2 -> 0
– D -> 3
– G -> 6
– GA -> 6
Simple Hashing Example
• Modulo arithmetic
– h(x) = x mod tableSize
– tableSize is recommended to
be prime. Why?
• Multiplication method
– h(x) = (xA mod 1) * tableSize
– A: constant in (0, 1)
• Determines the “distribution
pattern” of hash function
• Usually not a random choice
(e.g., 0.6180339887
suggested by Knuth)
– tableSize
p
is not critical, usually
2 for an integer p
Hash Table Design Issues
• Choice of hash function
– Easy to compute
– Avoid collision as much as possible
• Overflow handling method
• Size of hash table
– If too small, the collision occurs often
– If too large, “resource” waste
• Application of Hash table in real-world applic
ations
– Emergency calls and location identification
– …
Collision Resolution
…
22 123
…
Collision:
– Double hashing
• hi(x) = (h0(x) + i•β(x)) % tableSize
• β(x): another hash function Full version:
hi(x) = (h0(x) + ai2+bi+c) % tableSize
Separate Chaining
• Table[ ] is a table[ ]
header array of
…
linked lists
• No interference 22 123 224 22
between keys
not collided
– Open addressing
may interfere…
…
…
n-1
Separate Chaining
• h[k] = k%17 [0] 0 34
– Deletion? 11 28 45
[12] 12 29
• Overhead? 30
[16] 33
Other Approaches:
Open Addressing
• Rehashing
– Use a series of different hash functions h1, h2, ..., hm
– Examining A[hj(k)] for j=1, 2, ..., m
– Minimize clustering
• Random probing
– Examining A[(h(k)+s(i))%b] for i= 1, 2, ..., b-1
– s(i) : pseudo random number between 1 to b-1, each number is
generated only once
Open Addressing:
Linear Probing
• Insert 123, 24, 224, 22, 729, … in this order
– Insertion algorithm is intuitive, if non-trivial
table
Linear probing 0 []
hi(x) = (h0(x) + i) mod tableSize
…
bad w/ primary clustering
22 123 h0(123)=h0(224)=h0(22)=h0(729)=22
23 224 h0(729) + 1
24 24 h0(729) + 2 h0(24) = 24
i=3 25
26 22 h0(729) + 3
h3(729) = (h0(729) + 3) % 101 x = 729 729 h0(729) + 4
100
Linear Probing and
Deletion
• Item might not be at the first place we hash to (e.g.,
delete 22)
• Other items (e.g., with different hash value) might exist
in between table
0 []
…
22 123 h0(123)=h0(224)=h0(22)=h0(729)=22
23 224 h0(729) + 1
24 24 h0(729) + 2 h0(24) = 24
i=3 25 22 h0(729) + 3
h3(729) = (h0(729) + 3) % 101 x = 729 729 h0(729) + 4
…
Linear probing with
hi(x) = (h0(x) + i) mod 101
100
Linear Probing:
Deletion Impact on Search
Hash function: 0 13 0 13 0 13
hi(x) = (h0(x) + i) mod 13 1 1 1 1 DELETED
2 15 2 15 2 15
3 16 3 16 3 16
4 28 4 28 4 28
5 31 5 31 5 31
6 38 6 38 6 38
7 7 7 7 7 7
8 20 8 20 8 20
9 9 9
10 10 10
11 11 11
12 25 12 25 12 25
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3
• Insert 3
– h(3)=3%10=3, A[3] = 3
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 7
• Insert 7
– h(7) = 7%10=7
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 7
• Insert 13
X
– h(13)=13%10=3=collision & overflow!
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 7
• Insert 13
– (h(13)+1)%10 = (3+1)%10 = 4
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 7
• Insert 23
X
– h(23) = 23%10 = 3 = collision & overflow!
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 7
• Insert 23
X
– (h(23)+1)%10=4=collision & overflow!
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 23 7
• Insert 23
– (h(23)+2)%10=5
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 23 26 7
• Insert 26
– h(26) = 26%10 = 6
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 23 26 7 36
• Insert 36
– h(36) = 36%10 = 6 = collision & overflow!
– Next available bucket: 8
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 23 26 7 36
• Same color : same hash value group
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 23 26 7 36
• Delete 23
– Search the right cluster if there is a key to shift to left
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 26 7 36
• Delete 23
– A(26) = 6, A(7) = 7, A(36) = 8 (due to collision at 6) : no shi
fting required
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 26 7 36
• Delete 7
– Search right cluster (which is 36)
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 26 36
• Delete 7
– A(36) = 7 (since 6 is collision and 7 is empty)
– What happens if you don’t shift or mark it as deleted?
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 26 36
• Delete 7
– Shift 36 to left
Linear Probing
• Divisor = b (# of buckets) = 10
• h(k) = k%10, A(k)=(h(k)+j)%b
0 3 6 9
3 13 26 D 36
• Cons
– Clustering
– Worst case O(n)
– Delete can be expensive
Quadratic Probing
…
hi(x) = (h0(x) + i2) mod tableSize 22 123
2
23 224 h0(224) +1
24
2
26 22 h0(224) +2
…
i=2
2
x = 224 31 729 h0(224) +3
h2(224) = (h0(224) + 22) % 101
…
Quadratic probing with
hi(x) = (h0(x) + i2) mod 101
Why Quadratic Probing?
Behavioral Difference?
DIY