Adjacency List and Adjacency Matrix in Python

Assuming: edges = [(‘a’, ‘b’), (‘a’, ‘b’), (‘a’, ‘c’)] Here’s some code for the matrix: from collections import defaultdict matrix = defaultdict(int) for edge in edges: matrix[edge] += 1 print matrix[‘a’, ‘b’] 2 And for the “list”: from collections import defaultdict adj_list = defaultdict(lambda: defaultdict(lambda: 0)) for start, end in edges: adj_list[start][end] += 1 print … Read more