pairsListToGraphMap static method

Map pairsListToGraphMap(
  1. List<List> data
)

Input: [0, 2, 3, 4, 0, 6, 5, 6, 2, 3, 0, 1, 0, 4, 0, 113, 113, 114, 111, 112]

OutPut: {0: {2: 1, 6: 1, 1: 1, 4: 1, 113: 1}, 2: {0: 1, 3: 1}, 6: {0: 1, 5: 1}, 1: {0: 1}, 4: {0: 1, 3: 1}, 113: {0: 1, 114: 1}, 3: {2: 1, 4: 1}, 5: {6: 1}, 114: {113: 1}, 111: {112: 1}, 112: {111: 1}}

Implementation

static Map pairsListToGraphMap(List<List> data) {
  Map layout = Map();
  Map graph = Map();
  Set ids = Set();
  data.forEach((element) {
    ids.addAll(element);
  });

  for (var id in ids) {
    layout[id] = data
        .where((e) => e.contains(id))
        .map((e) => e.firstWhere((x) => x != id))
        .toList();
  }

  layout.forEach((id, value) {
    if (graph[id] == null) graph[id] = {};
    layout[id].forEach((aid) {
      graph[id][aid] = 1;
      if (graph[aid] == null) graph[aid] = {};
      graph[aid][id] = 1;
    });
  });
  return graph;
}