Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Find all edges in a graph which belong
1. FIND ALL EDGES IN A
GRAPH WHICH BELONG
TO ANY SHORTEST PATH
FROM SOURCE TO
Concepts used – BFS only
2. QUESTION
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
1 56 7
12 13
Source Destination
Graph containing only those
edges belonging to shortest
path from 1 to 5
From To
5 7
5 13
7 6
13 12
6 1
12 1
Solution Edges
List
Expected Time Complexity –
O(V+E)
3. APPROACH
1
2 3 4
56
8 9 10 11
12 13
7
Source Destination
Shortest Path are from 1 to 5
are –
1 – 6 – 7 – 5
1 – 12 – 13 – 5
4. APPROACH
1
2 3 4
56
8 9 10 11
12 13
7
Source Destination
Steps –
1. Apply BFS from Source node.
2. Store edges which are part of
shortest distance to that node.
9. APPROACH
1
2 3 4
56 7
8 9 10
12 13
Source Destination
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
This edge also gives
Shortest distance to 5 =
3 using path 1 – 12 – 13
– 5
So include this edge too
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
10. APPROACH
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Inclusion of edge from
4 to 5 gives distance
from 1 to 5 = 4 which is
larger than shortest
distance to 5 (equal to
3). So we will not
include it in our parent-
child table.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
11. APPROACH
Node Shortest
Distance
From
Source
1 0
2 1
6 1
8 1
12 1
3 2
7 2
9 2
13 2
10 3
4 3
5 3
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Inclusion of edge from
11 to 5 gives distance
from 1 to 5 = 5 which is
larger than shortest
distance to 5 (equal to
3). So we will not
include it in our parent-
child table.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13
12. APPROACH
1
2 3 4
56 7
8 9 10 11
12 13
Source Destination
Next Step –
Apply BFS/DFS from Destination using
parent child table and store all the
edges which come while traversing the
graph.
Do not continue the BFS/DFS to the
edges of source node. So stop at
source node.
Stop the BFS at when is
Destination. Do not
continue the BFS as it
will include unnecessary
edges.
Child Paren
t
2 1
6 1
8 1
12 1
3 2
7 6
9 8
13 12
5 7, 13