UNPKG

@ai-on-browser/data-analysis-models

Version:

Data analysis model package without any dependencies

731 lines (730 loc) 24.5 kB
/** * Exception for graph class */ export class GraphException extends Error { /** * @param {string} message Error message * @param {*} value Some value */ constructor(message: string, value: any); value: any; } /** * Edge of graph */ export class Edge { /** * @param {number} from Index of the starting node of the edge * @param {number} to Index of the end node of the edge * @param {unknown} [value] Value of the edge * @param {boolean} [direct] `true` if the edge is direct */ constructor(from: number, to: number, value?: unknown, direct?: boolean); 0: number; 1: number; value: unknown; direct: boolean; weighted: boolean; get from(): number; get to(): number; get value0(): unknown; } /** * Graph class */ export default class Graph { /** * Returns graph from adjacency matrix. * @param {number[][] | boolean[][]} mat Adjacency matrix * @returns {Graph} Graph from adjacency matrix */ static fromAdjacency(mat: number[][] | boolean[][]): Graph; /** * Returns complete graph. * @param {number} n Size of the graph * @returns {Graph} Complete graph */ static complete(n: number): Graph; /** * Returns complete bipartite graph. * @param {number} n Size of the first group * @param {number} m Size of the second group * @returns {Graph} Complete bipartite graph */ static completeBipartite(n: number, m: number): Graph; /** * Returns cycle graph. * @param {number} n Size of the graph * @param {boolean} [direct] Direct graph or not * @returns {Graph} Cycle graph */ static cycle(n: number, direct?: boolean): Graph; /** * Returns wheel graph. * @param {number} n Size of the graph * @returns {Graph} Wheel graph */ static wheel(n: number): Graph; /** * Returns windmill graph. * @param {number} k Size of the sub complete graph * @param {number} n Number of the sub complete graph * @returns {Graph} Windmill graph */ static windmill(k: number, n: number): Graph; /** * Returns named graph * @param {'balaban 10 cage' | 'bidiakis cube' | 'biggs smith' | 'brinkmann' | 'bull' | 'butterfly' | 'chvatal' | 'clebsch' | 'coxeter' | 'desargues' | 'diamond' | 'durer' | 'errera' | 'folkman' | 'foster' | 'franklin' | 'frucht' | 'goldner-harary' | 'golomb' | 'gray' | 'grotzsch' | 'harries' | 'heawood' | 'herschel' | 'hoffman' | 'holt' | 'kittell' | 'markstrom' | 'mcgee' | 'meredith' | 'mobius kantor' | 'moser spindle' | 'nauru' | 'pappus' | 'petersen' | 'poussin' | 'robertson' | 'shrikhande' | 'sousselier' | 'sylvester' | 'tutte' | 'tutte coxeter' | 'wagner' | 'wells'} name Name of the graph * @returns {Graph} Named graph */ static fromName(name: 'balaban 10 cage' | 'bidiakis cube' | 'biggs smith' | 'brinkmann' | 'bull' | 'butterfly' | 'chvatal' | 'clebsch' | 'coxeter' | 'desargues' | 'diamond' | 'durer' | 'errera' | 'folkman' | 'foster' | 'franklin' | 'frucht' | 'goldner-harary' | 'golomb' | 'gray' | 'grotzsch' | 'harries' | 'heawood' | 'herschel' | 'hoffman' | 'holt' | 'kittell' | 'markstrom' | 'mcgee' | 'meredith' | 'mobius kantor' | 'moser spindle' | 'nauru' | 'pappus' | 'petersen' | 'poussin' | 'robertson' | 'shrikhande' | 'sousselier' | 'sylvester' | 'tutte' | 'tutte coxeter' | 'wagner' | 'wells'): Graph; /** * @param {number | unknown[]} [nodes] Number of nodes or values of nodes * @param {([number, number] | {0?: number, 1?: number, from?: number, to?: number, value?: unknown, direct?: boolean} | Edge)[]} [edges] Edges */ constructor(nodes?: number | unknown[], edges?: ([number, number] | { 0?: number; 1?: number; from?: number; to?: number; value?: unknown; direct?: boolean; } | Edge)[]); /** @private */ private _nodes; /** @private */ private _edges; /** * Number of nodes * @type {number} */ get order(): number; /** * Number of edges * @type {number} */ get size(): number; /** * Nodes * @type {unknown[]} */ get nodes(): unknown[]; /** * Edges * @type {Edge[]} */ get edges(): Edge[]; /** * Returns a string of DOT format. * @returns {string} String of DOT format */ toDot(): string; /** * Returns a string represented this graph. * @returns {string} String represented this graph */ toString(): string; /** * Returns a copy of this graph. * @returns {Graph} Copied grpah */ copy(): Graph; /** * Return degree of the node. * @overload * @param {number} k Index of target node * @param {boolean} [undirect] Count undirected edges * @param {boolean | 'in' | 'out'} [direct] Count directed edges * @returns {number} Degree of the node */ degree(k: number, undirect?: boolean, direct?: boolean | 'in' | 'out'): number; /** * Return degree of the node. * @overload * @param {number} k Index of target node * @param {'in' | 'out'} direct Count only directed edges. * @returns {number} Degree of the node */ degree(k: number, direct: 'in' | 'out'): number; /** * Return indexes of adjacency nodes. * @overload * @param {number} k Index of target node * @param {boolean} [undirect] Check undirected edges * @param {boolean | 'in' | 'out'} [direct] Check directed edges * @returns {number[]} Indexes of adjacency nodes */ adjacencies(k: number, undirect?: boolean, direct?: boolean | 'in' | 'out'): number[]; /** * Return indexes of adjacency nodes. * @overload * @param {number} k Index of target node * @param {'in' | 'out'} direct Check only directed edges * @returns {number[]} Indexes of adjacency nodes */ adjacencies(k: number, direct: 'in' | 'out'): number[]; /** * Returns indexes of each components. * @returns {number[][]} Indexes of each components */ components(): number[][]; /** * Returns indexes of each biconnected components. * @returns {number[][]} Indexes of each biconnected components */ biconnectedComponents(): number[][]; /** * Returns diameter of this graph. * @returns {number} Diameter */ diameter(): number; /** * Returns eccentricity at k of this graph. * @param {number} k Index of target node * @returns {number} Eccentricity */ eccentricity(k: number): number; /** * Returns radius of this graph. * @returns {number} Radius */ radius(): number; /** * Returns indexes of center of this graph. * @returns {number[]} Indexes of center */ center(): number[]; /** * Returns girth of this graph. * @returns {number} Girth */ girth(): number; /** * Returns index of all cliques. * @overload * @returns {number[][][]} Index of cliques */ clique(): number[][][]; /** * Returns index of cliques. * @overload * @param {number} k Size of clique * @returns {number[][]} Index of cliques */ clique(k: number): number[][]; /** * Returns chromatic number of this graph. * @returns {number} Chromatic number */ chromaticNumber(): number; /** * Returns chromatic number of this graph with Welch-Powell algorithm. * @returns {number} Chromatic number */ chromaticNumberWelchPowell(): number; /** * Returns chromatic index of this graph. * @returns {number} Chromatic index */ chromaticIndex(): number; /** * Returns indexes of articulation (cut) nodes. * @returns {number[]} Indexes of articulation nodes */ articulations(): number[]; /** * Returns indexes of articulation (cut) nodes with checking each node. * @returns {number[]} Indexes of articulation nodes */ articulationsEachNodes(): number[]; /** * Returns indexes of articulation (cut) nodes with checking lowlinks. * @returns {number[]} Indexes of articulation nodes */ articulationsLowLink(): number[]; /** * Returns edges of bridge. * @returns {Edge[]} Bridge edges */ bridges(): Edge[]; /** * Returns edges of bridge with checking lowlinks. * @returns {Edge[]} Bridge edges */ bridgesLowLink(): Edge[]; /** * Add the node. * @param {unknown} [value] Value of the node */ addNode(value?: unknown): void; /** * Returns the node value. * @overload * @param {number} k Index of the node * @returns {unknown} Node value */ getNode(k: number): unknown; /** * Returns the node value. * @overload * @param {number[]} [k] Index of the node * @returns {unknown[]} Node value */ getNode(k?: number[]): unknown[]; /** * Remove the node. * @param {number} k Index of the node */ removeNode(k: number): void; /** * Remove all nodes. */ clearNodes(): void; /** * Add the edge. * @param {number} from Index of the starting node of the edge * @param {number} to Index of the end node of the edge * @param {unknown} [value] Value of the edge * @param {boolean} [direct] `true` if the edge is direct */ addEdge(from: number, to: number, value?: unknown, direct?: boolean): void; /** * Returns the edges. * @overload * @param {number} from Index of the starting node of the edge * @param {number} to Index of the end node of the edge * @param {boolean} [undirect] Get undirected edges or not * @param {boolean | 'forward' | 'backward'} [direct] Get directed edges or not * @returns {Edge[]} Edges between `from` and `to` */ getEdges(from: number, to: number, undirect?: boolean, direct?: boolean | 'forward' | 'backward'): Edge[]; /** * Returns the edges. * @overload * @param {number} from Index of the starting node of the edge * @param {number} to Index of the end node of the edge * @param {'forward' | 'backward'} direct Get only directed edges * @returns {Edge[]} Edges between `from` and `to` */ getEdges(from: number, to: number, direct: 'forward' | 'backward'): Edge[]; /** * Remove the edges. * @param {number} from Index of the starting node of the edge * @param {number} to Index of the end node of the edge * @param {boolean | null} [direct] `null` to remove direct and undirect edges, `true` to remove only direct edges, `false` to remove only undirect edges. */ removeEdges(from: number, to: number, direct?: boolean | null): void; /** * Remove all edges. */ clearEdges(): void; /** * Returns adjacency matrix * @returns {number[][]} Adjacency matrix */ adjacencyMatrix(): number[][]; /** * Returns adjacency list * @param {'both' | 'in' | 'out'} [direct] Indegree or outdegree * @returns {number[][]} Adjacency list */ adjacencyList(direct?: 'both' | 'in' | 'out'): number[][]; /** * Returns degree matrix. * @param {'both' | 'in' | 'out'} [direct] Indegree or outdegree * @returns {number[][]} Degree matrix */ degreeMatrix(direct?: 'both' | 'in' | 'out'): number[][]; /** * Returns laplacian matrix. * @returns {number[][]} Laplacian matrix */ laplacianMatrix(): number[][]; /** * Returns if this is null graph or not. * @returns {boolean} `true` if this is null graph */ isNull(): boolean; /** * Returns if this is edgeless graph or not. * @returns {boolean} `true` if this is edgeless graph */ isEdgeless(): boolean; /** * Returns if this is undirected graph or not. * @returns {boolean} `true` if this is undirected graph */ isUndirected(): boolean; /** * Returns if this is directed graph or not. * @returns {boolean} `true` if this is directed graph */ isDirected(): boolean; /** * Returns if this is mixed graph or not. * @returns {boolean} `true` if this is mixed graph */ isMixed(): boolean; /** * Returns if this is oriented graph or not. * @returns {boolean} `true` if this is oriented graph */ isOriented(): boolean; /** * Returns if this is weighted graph or not. * @returns {boolean} `true` if this is weighted graph */ isWeighted(): boolean; /** * Returns if this is simple graph or not. * @returns {boolean} `true` if this is simple graph */ isSimple(): boolean; /** * Returns if this is connected graph or not. * @returns {boolean} `true` if this is connected graph */ isConnected(): boolean; /** * Returns if this is biconnected graph or not. * @returns {boolean} `true` if this is biconnected graph */ isBiconnected(): boolean; /** * Returns if this is tree or not. * @returns {boolean} `true` if this is tree */ isTree(): boolean; /** * Returns if this is forest or not. * @returns {boolean} `true` if this is forest */ isForest(): boolean; /** * Returns if this is bipartite graph or not. * @returns {boolean} `true` if this is bipartite graph */ isBipartite(): boolean; /** * Returns if this is complete graph or not. * @returns {boolean} `true` if this is complete graph */ isComplete(): boolean; /** * Returns if this is regular graph or not. * @param {number} [n] Degree of vertices * @returns {boolean} `true` if this is regular graph */ isRegular(n?: number): boolean; /** * Returns if this is plainer graph or not. * @returns {boolean} `true` if this is plainer graph */ isPlainer(): boolean; /** * Returns if this is plainer graph or not with add-path algorithm. * * On the Cutting Edge: Simplified O(n) Planarity by Edge Addition * https://xuzijian629.hatenablog.com/entry/2019/12/14/163726 * @returns {boolean} `true` if this is plainer graph */ isPlainerAddEdge(): boolean; /** * Returns if this is plainer graph or not with add-vertex algorithm. * * Hopcroft, J. and Tarjan, R. "Efficient Planarity Testing", J. ACM, Vol. 21, No. 4, pp. 549-568 (1974) * 西関 隆夫. "32. グラフの平面性判定法", 情報処理, Vol. 24, No. 4, pp. 521-528 (1983) * K. S. Booth, "Testing for the Consecutive Ones Property, Interval Graphs, and Graph Planarity Using PQ-Tree Algorithms", Journal of computer and system sciences, 13, pp. 335-379 (1976) * @returns {boolean} `true` if this is plainer graph */ isPlainerAddVertex(): boolean; _stNumbering(s?: number, t?: any): number[]; /** * Returns if this is symmetric graph or not. * @returns {boolean} `true` if this is symmetric graph */ isSymmetric(): boolean; /** * Returns if this is directed acyclic graph or not. * @returns {boolean} `true` if this is directed acyclic graph */ isDAG(): boolean; /** * Returns if this is separable graph or not. * @returns {boolean} `true` if this is separable graph */ isSeparable(): boolean; /** * Returns if this is Eulerian graph or not. * @returns {boolean} `true` if this is Eulerian graph */ isEulerian(): boolean; /** * Returns if this is semi-Eulerian graph or not. * @returns {boolean} `true` if this is semi-Eulerian graph */ isSemiEulerian(): boolean; /** * Returns if this is Hamiltonian graph or not. * @returns {boolean} `true` if this is Hamiltonian graph */ isHamiltonian(): boolean; /** * Returns if this is semi-Hamiltonian graph or not. * @returns {boolean} `true` if this is semi-Hamiltonian graph */ isSemiHamiltonian(): boolean; /** * Returns if this has cycle or not. * @returns {boolean} `true` if this has cycle */ hasCycle(): boolean; /** * Returns if this has cycle or not with depth-first search. * @returns {boolean} `true` if this has cycle */ hasCycleDFS(): boolean; /** * Returns if this has cycle or not with checking each node. * @returns {boolean} `true` if this has cycle */ hasCycleEachNodes(): boolean; /** * Returns graph of directed edges converted to undirected. * @returns {Graph} Undirected graph */ toUndirected(): Graph; /** * Returns a graph with multiple edges and loops removed. * @returns {Graph} Simple graph */ toSimple(): Graph; /** * Returns (sub) graph isomorphism maps from 'g' to this (sub) graph. * @param {Graph} g Other graph * @returns {number[][]} Isomorphism maps from 'g' to this (sub) graph */ isomorphism(g: Graph): number[][]; /** * Returns (sub) graph isomorphism maps from 'g' to this (sub) graph with Ullmann algorithm. * @param {Graph} g Other graph * @returns {number[][]} Isomorphism maps from 'g' to this (sub) graph */ isomorphismUllmann(g: Graph): number[][]; /** * Returns (sub) graph isomorphism maps from 'g' to this (sub) graph with VF2 algorithm. * @param {Graph} g Other graph * @returns {number[][]} Isomorphism maps from 'g' to this (sub) graph */ isomorphismVF2(g: Graph): number[][]; /** * Returns induced sub graph. * @param {number[]} k Selected indexes * @returns {Graph} Induced sub graph */ inducedSub(k: number[]): Graph; /** * Returns complement graph. * @returns {Graph} Complement graph */ complement(): Graph; /** * Returns line graph. * @returns {Graph} Line graph */ line(): Graph; /** * Contract this graph. * @param {number} a Index of node * @param {number} b Index of node */ contraction(a: number, b: number): void; /** * Subdivision this graph. * @param {number} a Index of node * @param {number} b Index of node */ subdivision(a: number, b: number): void; /** * Cleave the node. * @param {number} a Index of node */ cleaving(a: number): void; /** * Take the disjoint union of this graph and other graph. * @param {Graph} g Other graph */ disjointUnion(g: Graph): void; /** * Substitute other graph at the node. * @param {number} k Index of the node * @param {Graph} g Other graph */ substitution(k: number, g: Graph): void; /** * Take the cartesian product of this graph and other graph. * @param {Graph} g Other graph * @returns {Graph} Cartesian producted graph */ cartesianProduct(g: Graph): Graph; /** * Take the tensor product of this graph and other graph. * @param {Graph} g Other graph * @returns {Graph} Tensor producted graph */ tensorProduct(g: Graph): Graph; /** * Take the strong product of this graph and other graph. * @param {Graph} g Other graph * @returns {Graph} Strong producted graph */ strongProduct(g: Graph): Graph; /** * Take the lexicographic product of this graph and other graph. * @param {Graph} g Other graph * @returns {Graph} Lexicographic producted graph */ lexicographicProduct(g: Graph): Graph; /** * Returns shortest path. * @overload * @param {null} [from] Index of start nodes * @returns {{length: number, path: number[]}[][]} Shortest length and path for all nodes */ shortestPath(from?: null): { length: number; path: number[]; }[][]; /** * Returns shortest path. * @overload * @param {number} from Index of start nodes * @returns {{length: number, prev: number, path: number[]}[]} Shortest length and path for all nodes */ shortestPath(from: number): { length: number; prev: number; path: number[]; }[]; /** * Returns shortest path with breadth first search algorithm. * @param {number} from Index of start node * @returns {{length: number, prev: number, path: number[]}[]} Shortest length and path for all nodes */ shortestPathBreadthFirstSearch(from: number): { length: number; prev: number; path: number[]; }[]; /** * Returns shortest path with Dijkstra's algorithm. * @param {number} from Index of start node * @returns {{length: number, prev: number, path: number[]}[]} Shortest length and path for all nodes */ shortestPathDijkstra(from: number): { length: number; prev: number; path: number[]; }[]; /** * Returns shortest path with Bellman–Ford algorithm. * @param {number} from Index of start node * @returns {{length: number, prev: number, path: number[]}[]} Shortest length and path for all nodes */ shortestPathBellmanFord(from: number): { length: number; prev: number; path: number[]; }[]; /** * Returns shortest path with Floyd–Warshall algorithm. * @returns {{length: number, path: number[]}[][]} Shortest length and path for all nodes */ shortestPathFloydWarshall(): { length: number; path: number[]; }[][]; /** * Returns minimum spanning tree. * @returns {Graph} Minimum spanning tree */ minimumSpanningTree(): Graph; /** * Returns minimum spanning tree with Prim's algorithm. * @returns {Graph} Minimum spanning tree */ minimumSpanningTreePrim(): Graph; /** * Returns minimum spanning tree with Kruskal's algorithm. * @returns {Graph} Minimum spanning tree */ minimumSpanningTreeKruskal(): Graph; /** * Returns minimum spanning tree with Borůvka's algorithm. * @returns {Graph} Minimum spanning tree */ minimumSpanningTreeBoruvka(): Graph; /** * Returns Hamiltonian path * @param {number} [from] Index of start node * @returns {number[][]} Hamiltonian path */ hamiltonianPath(from?: number): number[][]; /** * Returns Hamiltonian path with dynamic programming * @param {number} [from] Index of start node * @returns {number[][]} Hamiltonian path */ hamiltonianPathDynamicProgramming(from?: number): number[][]; /** * Returns Hamiltonian cycle * @returns {number[][]} Hamiltonian cycle */ hamiltonianCycle(): number[][]; /** * Returns cut size. * @param {number[]} s Subset * @param {number[]} t Subset * @returns {number} Cut size */ cut(s: number[], t: number[]): number; /** * Returns minimum cut. * @param {number} [minv] Minimum number for subset * @returns {[number, number[][]]} Cut value and subset nodes */ mincut(minv?: number): [number, number[][]]; /** * Returns minimum cut. * @param {number} [minv] Minimum number for subset * @returns {[number, number[][]]} Cut value and subset nodes */ mincutBruteForce(minv?: number): [number, number[][]]; /** * Returns minimum cut. * @param {number} [minv] Minimum number for subset * @param {number} [startnode] Start node index * @returns {[number, number[][]]} Cut value and subset nodes */ mincutStoerWagner(minv?: number, startnode?: number): [number, number[][]]; /** * Returns minimum cut. * @param {number} [minv] Minimum number for subset * @param {number} [trials] Trial count * @returns {[number, number[][]]} Cut value and subset nodes */ mincutKargers(minv?: number, trials?: number): [number, number[][]]; /** * Returns minimum cut. * @param {number} [minv] Minimum number for subset * @param {number} [trials] Trial count * @returns {[number, number[][]]} Cut value and subset nodes */ mincutKargersStein(minv?: number, trials?: number): [number, number[][]]; _mincutKargersStein0(amat: any, nodes: any, minv: any): any; /** * Returns bisection cut. * @returns {[number, number[][]]} Cut value and subset nodes */ bisectionSpectral(): [number, number[][]]; }