TIP103 - Unit 7 Session 2 - Graph Algorithm (Continued)
TIP103 - Unit 7 Session 2 - Graph Algorithm (Continued)
Session Problems
● Network Delay Time
● Redundant Connection
● Cheapest Flights Within K Stops
● Second Minimum Time to Reach Destination
● Number of Connected Components
1
Graphs Algorithms
(continued)
TIP103 Unit 7.2
Welcome to Unit 7, Session 2:
Graph Algorithms (continued)
2. Rename yourself with your pod number, full name, and pronouns:
3
As a community, we will...
4
Agenda
1 Dijkstra's Algorithm 0:00 - 0:10
2 Disjoint Sets and Union Find 0:10 - 0:20
3 UMPIRE Walkthrough 0:20 - 0:30
4 Break 0:35 - 0:40
5 Breakout Sessions 0:40 - 1:40
6 Post-Breakout Walkthrough 1:40 - 1:55
7 Wrap Up 1:55 - 2:00
5
10 minutes
Dijkstra's Algorithm
Check for Understanding 1 min
7
Check for Understanding 1 min
8
Shortest path without weights
9
Shortest path with weights
10
Why BFS won’t work
✱ Shortest path may not have the fewest edges
12
A Few Applications of Shortest Weighted
Path
✱ Driving directions
✱ Cheap flight itineraries
✱ Network routing
✱ Critical paths in project management
13
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
1
5 D ∞
E ∞
14
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
5 D ∞
E ∞
15
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
E ∞
16
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi.
2
E ∞
17
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (A, 0)
2
E ∞
queue: [ ]
18
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (A, 0)
2
b. Mark vi as visited.
E ∞
queue: [ ]
19
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial
∞ B C ∞
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (A, 0)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
queue: [ ]
20
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2
cost of ∞. the cheapest known distance from v0.
B C ∞
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (A, 0)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
21
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (A, 0)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
22
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost.
5 D ∞
2. While the queue is not empty
a. Dequeue node vi. (C, 1)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
23
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (C, 1)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
24
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (B, 2)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E ∞
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
25
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (B, 2)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E 7
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
26
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (D, 2)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E 7
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
27
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (D, 2)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E 74
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
28
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (E, 4)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E 4
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
queue: [ ]
29
Dijkstra's Algorithm
A 0
To find the distance of the shortest path from v0 to other nodes:
2 1
1. Initialize variables
a. Give v0 an initial cost of 0 and all other nodes an initial 2 B 1
C
cost of ∞. the cheapest known distance from v0.
b. Mark every node as unvisited. 1
c. Add v0 to a priority queue to hold nodes sorted by cost. D 2
5
2. While the queue is not empty
a. Dequeue node vi. (E, 4)
2
b. Mark vi as visited.
c. For every edge with weight w to an unvisited node vj: E 4
i. Set cost(vj) to min(cost(vj), cost(vi) + w).
ii. Add vj to the queue with its new weight.
queue: [ ]
30
Complexity of Dijkstra's Algorithm
✱ Time complexity: O(E log V) where
● V is the number of vertices in the graph
● E is the number of edges
● a min-heap is used to implement the priority queue
31
10 minutes
33
Disjoint sets
✱ Disjoint means "non-overlapping"
✱ Examples of disjoint sets
France Mexico
● Even integers and odd integers
● Countries by continent
✱ We represent the sets as a "forest" Italy
of trees, whose roots are
USA Canada
sometimes called "representative
elements" Germany
34
Disjoint sets
✱ Disjoint means "non-overlapping"
✱ Examples of disjoint sets
France Mexico
● Even integers and odd integers
● Countries by continent
✱ We represent the sets as a "forest" Italy
of trees, whose roots are
USA Canada
sometimes called "representative
elements" Germany
35
parent[0] = 0
parent[1] = 1 parent[2] = 2
parent[0] = 0
37
parent[0] = 0
find(1) returns 1
find(2) returns 0
4: Spain
Is find(n) always parent[n]? no
parent[4] = 3
find(4) returns 1
38
Union-Find implementation 1 0: Mexico
def find(x):
while parent[x] != x: 1: France 2: USA
x = parent[x]
return x
5: Greece 4: Spain
union(5, 4)
39
Union-Find implementation 1 0: Mexico
def find(x):
while parent[x] != x: 1: France 2: USA
x = parent[x]
return x
5: Greece 4: Spain
Time for find/union: O(h), where h is the number of elements in the biggest set. union(4, 5)
What if we could choose the parent that gave the shorter height? 40
Union-Find implementation 2 0: Mexico
def union(x,y):
xroot, yroot = find(x), find(y) 3: Italy
if xroot != yroot:
if rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else:
parent[xroot] = yroot rank[5] = 0 5: Greece 4: Spain
if rank[xroot] == rank[yroot]:
rank[yroot] += 1
union(4, 5)
Time for union: O(log n), where n is the number of nodes in the graph.
41
We can actually do even better with something called path compression.
Union-Find implementation 3 0: Mexico
def union(x,y):
xroot, yroot = find(x), find(y) 3: Italy
if xroot != yroot:
if rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else:
parent[xroot] = yroot rank[5] = 0 5: Greece 4: Spain
if rank[xroot] == rank[yroot]:
rank[yroot] += 1
union(4, 5)
42
Union-Find implementation 3 0: Mexico
def union(x,y):
xroot, yroot = find(x), find(y) 3: Italy
if xroot != yroot:
if rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else:
parent[xroot] = yroot rank[5] = 0 5: Greece 4: Spain
if rank[xroot] == rank[yroot]:
rank[yroot] += 1
union(4, 5)
43
Python Java
parent = [i for i in range(len(nodes))] int[] parent = new int[ncells];
rank = [0] * len(nodes) for (int i = 0; i < nodes.length; i++) {
parent[i] = i;
}
def find(x): int[] rank = new int[ncells];
if parent[x] != x:
parent[x] = find(parent[x]) int find(int x) {
if (parent[x] != x) {
return parent[x] parent[x] = find(parent[x]);
}
def union(x,y): return parent[x];
}
xroot, yroot = find(x), find(y)
if xroot != yroot: void union(int x, int y) {
if rank[xroot] > rank[yroot]: int xroot = find(x);
parent[yroot] = xroot int yroot = find(y);
if (xroot != yroot) {
else:
if (rank[xroot] > rank[yroot]) {
parent[xroot] = parent[yroot] parent[yroot] = xroot;
if rank[xroot] == rank[yroot]: } else {
rank[yroot] += 1 parent[xroot] = parent[yroot];
if (rank[xroot] == rank[yroot]) {
rank[yroot]++;
}
}
}
}
44
Union-Find Summary
✱ Union-Find is an algorithm for managing and merging sets.
✱ Most problems that can be solved with Union-Find can also be
solved by DFS or BFS.
✱ The efficiency of the basic implementation can be improved with:
● Union by rank, which keeps the trees balanced.
● Path compression, which shortens the paths to the
representative element.
✱ The space complexity for n elements is O(n).
✱ The time complexity (with the above improvements) is practically
O(1) for each union or find operation.
https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Time_complexity 45
10 minutes
UMPIRE Walkthrough
Number of Islands https://leetcode.com/problems/number-of-islands/
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's
(water), return the number of islands.
Example 1 Example 2
Output: 1 Output: 3
47
U-nderstand
✱ What are the constraints?
● m == grid.length (number of rows)
● n == grid[i].length (number of columns)
● 1 <= m, n <= 300
● grid[i][j] is '0' or '1'
Input: grid = [
✱ What is the definition of an island? ["1","1","0","0","0"],
["1","1","0","0","0"],
● one or more pieces of land ('1') ["0","0","1","0","0"],
connected horizontally or vertically ["0","0","0","1","1"]
]
48
M-atch
✱ Graph search
● depth-first search
● breadth-first search
✱ Union-find
● The sets are pieces of land.
● They should be merged if adjacent.
49
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
50
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
51
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
52
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
53
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
54
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
55
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its left neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its up neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
c. If its right neighbor is '1', combine them. ]
d. If its down neighbor is '1', combine them.
Output: 1
3. Return the number of sets.
56
P-lan
1. Create the initial parent and rank arrays.
Input: grid = [
2. For each element of the grid that is '1': ["1","1","0","1","0"],
a. If its right neighbor is '1', combine them. ["1","1","0","1","0"],
b. If its down neighbor is '1', combine them. ["0","1","1","1","0"],
["0","0","0","1","1"]
3. Return the number of sets. ]
Output: 1
57
Converting from grid position to index
columns
0 1 2 3
rows
1
58
Converting from grid position to index
Row-major ordering
columns
0 1 2 3
0 0 1 2 3
rows
1 4 5 6 7
2 8 9 10 11
index(row, col) =
59
Converting from grid position to index
Row-major ordering
columns
0 1 2 3
0 0 1 2 3
rows
1 4 5 6 7
2 8 9 10 11
https://leetcode.com/playground/oGKi2YUJ 61
I-mplement
Java int index = 0;
for (int r = 0; r < nrows; r++) {
class Solution { for (int c = 0; c < ncols; c++, index++) {
int[] parent, rank; if (grid[r][c] == '1') {
int nrows, ncols, ncells, nsets; nsets++;
if (r > 0 && grid[r-1][c] == '1') {
public int numIslands(char[][] grid) { // Index one row up is index-ncols
nrows = grid.length; union(index, index - ncols);
ncols = grid[0].length; }
ncells = nrows * ncols; if (c > 0 && grid[r][c-1] == '1') {
parent = new int[ncells]; // Index one left is index-1
union(index, index - 1);
for (int i = 0; i < ncells; i++) { }
parent[i] = i; }
} }
}
return nsets;
Union-Find implementation omitted. }
}
https://leetcode.com/playground/oGKi2YUJ 62
E-valuate
Let V be the number of cells and I the size of the biggest island.
63
Break Time!
Take 5 mins to step away from the computer.
Feel free to turn off your camera and return
promptly after 5 mins.
60 minutes
Breakout Sessions
Breakout Rooms 60 minutes
● Today's questions:
○ Network Delay Time (medium)
Questions?
○ Redundant Connection (medium) Post on Slack
○ Cheapest Flights Within K Stops (medium) and tag:
○ Second Minimum Time to Reach Destination (hard)
○ Number of Connected Components (medium, premium) @tip103-tas
66
Reflection 1 min
67
15 minutes
Post-Breakout Walkthrough
Network Delay Time https://leetcode.com/problems/network-delay-time
You are given a network of n nodes, labeled from 1 to n. You are also
given times, a list of travel times as directed edges times[i] = (ui, vi,
wi), where ui is the source node, vi is the target node, and wi is the time
it takes for a signal to travel from source to target.
We will send a signal from a given node k. Return the minimum time it
takes for all the n nodes to receive the signal. If it is impossible for all
the n nodes to receive the signal, return -1.
69
Network Delay Time
Example 1:
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2 2
1 1
Output: 2
1 3
1
70
Network Delay Time
Example 2:
1
Input: times = [[1,2,1]], n = 2, k = 1
Output: 1 1
71
Network Delay Time
Example 3:
1
Input: times = [[1,2,1]], n = 2, k = 2
Output: -1 1
72
M-atch
✱ Single-source shortest path in a weighted graph…
✱ Dijkstra's Algorithm
73
P-lan
1. Convert edge set to adjacency list.
2. Create a distance array and a priority queue holding k (distance 0).
3. While the queue is not empty:
a. Extract the node with the minimum distance.
b. For each neighbor of the node:
i. Calculate the cost to reach the neighbor through this node.
ii. If the new cost is lower:
1. Update the neighbor's distance value.
2. Add the neighbor to the priority queue.
4. If any nodes were unreached, return -1 (no path).
5. Otherwise, return the maximum distance.
74
I-mplement/R-eview
Python # Skip if we have already have a better (shorter) path
# to this node.
if time > distance[node]:
import heapq
continue
class Solution:
for neighbor, edge_time in graph[node]:
def networkDelayTime(self, times: List[List[int]],
# Calculate the total time to reach the neighbor.
n: int, k: int) -> int:
total_time = time + edge_time
# Create an adjacency list to represent the graph.
graph = {i: [] for i in range(1, n + 1)}
# If this time is better than its current recorded
for u, v, w in times:
# distance, update the distance and add the neighbor
graph[u].append((v, w))
# to the priority queue.
if total_time < distance[neighbor]:
# Initialize distance array and priority queue (min-heap).
distance[neighbor] = total_time
distance = {node: float('inf') for node in range(1, n + 1)}
heapq.heappush(pq, (total_time, neighbor))
distance[k] = 0
pq = [(0, k)]
# Check if all nodes are reachable and return the maximum
# time taken.
# Iterate over queue.
max_time = max(distance.values())
while pq:
return max_time if max_time < float('inf') else -1
# Extract the node with the minimum distance.
time, node = heapq.heappop(pq)
https://tinyurl.com/cp-tip103-72a 75
while (!pqueue.isEmpty()) {
I-mplement/R-eview // Extract the node in the queue with the minimum total distance.
Edge entry = pqueue.poll();
int time = entry.distance();
int node = entry.node();
https://tinyurl.com/cp-tip103-72a 76
E-valuate
Assume V is the number of nodes and E is the number of edges.
✱ Time complexity:
○ Building the adjacency list: O(V + E)
○ Initializing the distance array and priority queue: O(V)
■ Initializing the distance array: O(V)
■ Inserting source node into the priority queue: O(1)
○ Entries in queue: O(E)
■ Loop iterates at most V times. For each iteration, we
perform at most one insertion into the heap: O(log E)
Overall: O(EV log V)
77
E-valuate
Assume V is the number of nodes and E is the number of edges.
✱ Space complexity:
○ Adjacency list: O(V+E)
○ Distance array: O(V)
○ Priority queue: O(E)
Overall: O(V+E)
https://tinyurl.com/cp-tip103-72a
78
Redundant Connection https://leetcode.com/problems/redundant-connection/
79
Redundant Connection https://leetcode.com/problems/redundant-connection/
Example 1:
Input: edges = [[1,2], [1,3], [2,3]]
Output: [2,3] 1 2
80
Redundant Connection https://leetcode.com/problems/redundant-connection/
Example 1:
Input: edges = [[1,2], [1,3], [2,3]]
Output: [2,3] 1 2
81
Redundant Connection https://leetcode.com/problems/redundant-connection/
Example 1:
Input: edges = [[1,2], [1,3], [2,3]]
Output: [2,3] 1 2
82
Redundant Connection https://leetcode.com/problems/redundant-connection/
Example 1:
Input: edges = [[1,2], [1,3], [2,3]]
Output: [2,3] 1 2
83
M-atch
✱ We want to keep adding edges until doing so would add a cycle.
✱ Union-Find can keep track of which nodes are connected.
84
P-lan
1. Initialize Union-Find data structures.
2. Iterate over edges:
a. Extract nodes a and b from edges.
b. If find(a) == find(b), they are already connected, so adding
another edge would make a cycle. Return the current edge.
c. Otherwise, union(a, b) to indicate that they are connected.
3
85
P-lan
1. Initialize Union-Find data structures.
2. Iterate over edges:
a. Extract nodes a and b from edges.
b. If find(a) == find(b), they are already connected, so adding
another edge would make a cycle. Return the current edge.
c. Otherwise, union(a, b) to indicate that they are connected.
3
86
P-lan
1. Initialize Union-Find data structures.
2. Iterate over edges:
a. Extract nodes a and b from edges.
b. If find(a) == find(b), they are already connected, so adding
another edge would make a cycle. Return the current edge.
c. Otherwise, union(a, b) to indicate that they are connected.
3
87
P-lan
1. Initialize Union-Find data structures.
2. Iterate over edges:
a. Extract nodes a and b from edges.
b. If find(a) == find(b), they are already connected, so adding
another edge would make a cycle. Return the current edge.
c. Otherwise, union(a, b) to indicate that they are connected.
3
88
I-mplement/R-eview Java
class Solution {
https://tinyurl.com/cp-tip103-72b 90
5 minutes
Wrap Up
Exit Ticket 1 min
92
Exit Ticket
93
Exit Ticket 1 min
94
Exit Ticket
95
Exit Ticket 1 min
96
Exit Ticket
97
Before you leave...
❒ Complete the Session Survey [5 min]
○ Course: [TIP103-S1] Advanced Technical Interview Prep (Section 1)
○ Session #2
❒ Complete by 11:59pm PDT the night before next week's first session
○ HackerRank assessment → Link on the assignment tab
○ Warm up for next unit → Link on the warmup tab
98