ts-ds-tool
Version:
Data structure and algorithm of TypeScript
30 lines (29 loc) • 1 kB
JavaScript
import DisjointSet from "../../../disjointset/DisjointSet";
export function isconnected(graph) {
const vertexs = graph.getVertexs();
const disjoinSet = new DisjointSet();
vertexs.forEach(vertex => {
const neighbors = vertex.getNeighbors();
if (neighbors && neighbors.length) {
neighbors.forEach(neighbor => {
const startVertex = vertex;
const endVertex = neighbor;
if (!disjoinSet.find(startVertex.Key)) {
disjoinSet.makeSet(startVertex.Key);
}
if (!disjoinSet.find(endVertex.Key)) {
disjoinSet.makeSet(endVertex.Key);
}
disjoinSet.union(startVertex.Key, endVertex.Key);
});
}
else {
disjoinSet.makeSet(vertex.Key);
}
});
const rootItems = disjoinSet.RootItems;
if (Object.keys(rootItems).length > 1) {
return false;
}
return true;
}