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

How to transform a MSSQL CTE query to MySQL?

Unfortunately MySQL doesn’t support CTE (Common Table Expressions). This is long overdue IMO. Often, you can just use a subquery instead, but this particular CTE is recursive: it refers to itself inside the query. Recursive CTE’s are extremely useful for hierarchical data, but again: MySql doesn’t support them at all. You have to implement a … Read more