Convert between graph representations.Convert between graph representations.
What?
Let's say I have this graph:
1 \ \ 2 3 \ / \ / 4 I can represent it in 2 ways:
-
- A list of connected vertices.
[[1,3],[2,4],[3,4]]
- A list of connected vertices.
-
- A boolean matrix which shows where edges are:
c |1 2 3 4 --|------- 1 |0 0 1 0 2 |0 0 0 1 3 |1 0 0 1 4 |0 1 1 0 ----- or ----- [[0,0,1,0],[0,0,0,1],[1,0,0,1],[0,1,1,0]] You code should take input in the form of 1 and output it as 2. The graph is not directed (aka undirected graph). You can also accept input which is 0-indexed.
Test cases
[[1,2],[3,4]] -> [[0,1,0,0],[1,0,0,0],[0,0,0,1],[0,0,1,0]] [[1,3],[2,4],[3,4]] -> [[0,0,1,0],[0,0,0,1],[1,0,0,1],[0,1,1,0]] [[1,2],[2,3],[5,6]] -> [[0,1,0,0,0,0],[1,0,1,0,0,0],[0,1,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,1],[0,0,0,0,1,0]] [[1,2]] -> [[0,1],[1,0]] [[1,2],[4,5]] -> [[0,1,0,0,0],[1,0,0,0,0],[0,0,0,0,0],[0,0,0,0,1],[0,0,0,1,0]] [[4,5]] -> [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,1],[0,0,0,1,0]]