All problems
Directed Adjacency List
Problem
Build an adjacency list for a directed graph from an edge list.
edges is a list of directed [u, v] pairs. Return a list of n lists where entry u holds the sorted out-neighbors of node u. Nodes with no outgoing edges get an empty list.
Hint
Only record u to v for each directed edge, then sort every neighbor list.
Test cases
case 1
in: [3,[[0,1],[1,2],[2,0]]]
expected: [[1],[2],[0]]
case 2
in: [4,[]]
expected: [[],[],[],[]]
case 3
in: [4,[[3,1],[0,3],[3,0]]]
expected: [[3],[],[],[0,1]]
case 4
in: [3,[[1,1],[2,0]]]
expected: [[],[1],[0]]
Starter codePython
def directed_adjacency_list(n, edges):
# Your code here
pass4 test cases · run your code against them in the editor