All files / src/essence index.ts

100% Statements 24/24
100% Branches 8/8
100% Functions 4/4
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                          4x               14x 1x     13x 13x   13x 23x 23x 1x   22x 22x   22x 2x   20x   10x               2x               2x 2x 2x 2x 1x     1x    
/**
 * @file To get graph essencial information.
 * @file.zh-CN 获取图的基本信息
 * @module essence
 */
 
import Graph from '../Graph';
 
/**
 * @description Check if the object is a graph.
 * @description.zh-CN 检查对象是否为图。
 */
export function isGraph(obj: any) {
  return obj instanceof Graph;
}
 
/**
 * @description Check if the graph is a simple graph.
 * @description.zh-CN 检查图是否为简单图。
 */
export function isSimpleGraph(graph: Graph<any, any, any, any>) {
  if (graph.isMultigraph()) {
    return false;
  }
 
  const edges = graph.edges();
  const edgeStack = new Map();
 
  for (let i = 0; i < edges.length; i++) {
    const edge = edges[i];
    if (edge.v === edge.w) {
      return false;
    }
    const [v, w] = [edge.v, edge.w].sort();
    const key = `${v}-${w}`;
 
    if (edgeStack.has(key)) {
      return false;
    }
    edgeStack.set(key, true);
  }
  return true;
}
 
/**
 * @description Check if the graph is a null graph.
 * @description.zh-CN 检查图是否为空图。
 */
export function isNullGraph(graph: Graph) {
  return graph.nodes().length === 0;
}
 
/**
 * @description Check if the graph contains Self loops.
 * @description.zh-CN 检查图是否包含自环。
 */
export function hasSelfLoop(graph: Graph) {
  const edges = graph.edges();
  for (let i = 0; i < edges.length; i++) {
    const edge = edges[i];
    if (edge.v === edge.w) {
      return true;
    }
  }
  return false;
}