JSFiddle - React, Tailwind, and code Playground
JavaScript
// It will return an object like Vertices{node: EdgesTo{node,node}, node:...}
function toGraph(arr) {
var graph = {}; // this will hold the node "IDs"
for (var i = 0; i < arr.length; i++) {
// "create node" if the it's not added in the graph yet
graph[arr[i][0]] = graph[arr[i][0]] || {};
graph[arr[i][1]] = graph[arr[i][1]] || {};
// add bidirectional "edges" to the "vertices"
// Yes, we set the value to null, but what's important is to add the key.
graph[arr[i][0]][arr[i][1]] = null;
graph[arr[i][1]][arr[i][0]] = null;
}
return graph;
}
// to be called after getting the result from toGraph(arr)
function getSubGraphs(graph) {
var subGraphs = []; // array of connected vertices
var visited = {};
for (var i in graph) { // for every node...
var subGraph = dfs(graph, i, visited); // ... we call dfs
if (subGraph != null) // if vertex is not added yet in another graph
subGraphs.push(subGraph);
}
return subGraphs;
}
// it will return an array of all connected nodes in a subgraph
function dfs(graph, node, visited) {
if (visited[node]) return null; // node is already visited, get out of here.
var subGraph = [];
visited[node] = true;
subGraph.push(node);
for (var i in graph[node]) {
var result = dfs(graph, i, visited);
if (result == null) continue;
subGraph = subGraph.concat(result);
}
return subGraph;
}
var myArray = [
["a2", "a5"],
["a3", "a6"],
["a4", "a5"],
["a7", "a9"]
];
console.log(toGraph(myArray));
console.log(getSubGraphs(toGraph(myArray)));