UNPKG

maths.ts

Version:

Math utilities library for TypeScript, JavaScript and Node.js

59 lines (58 loc) 2.51 kB
/** * @author Hector J. Vasquez <ipi.vasquez@gmail.com> * * @licence * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Graph from '../structures/Graph'; import Vertex from '../structures/Vertex'; import { Logger } from '../algorithms'; export interface GraphSearchSolution { trail?: string[]; cost?: number; depth?: number; reachable: boolean; } /** * An helping Interface for handling the vertex in list. It helps to store * information about the vertexes and the path followed to get there as well * as the cost and depth for each vertex on list. */ export interface VertexElement { id: number; vertex: Vertex; trail: number[]; cost: number; depth: number; } /** * A generic implementation for transversing graph searching for an element. * This algorithm holds a list, defined by the interface VertexList. To be * able to execute graphSearch, an outFunction and an inFunction must be * provided. This functions will determine the list behavior. * * BFS, DFS, Uniform cost search and A* are implemented on this function by * sending their respective in and out functions for each implementation. * e.g. BFS sends Array.shift as next function and DFS send Array.pop instead. * @param graph The graph to be transversed. * @param source The starting vertex. * @param destination The goal vertex. * @param outFunction The out function. * @param inFunction The in function. * @param logger An optional logger to knowing more about the algorithm. * @return A GraphSearchSolution interface with its reachable element false * if the destination element was not found in the graph. The reachable * element will be true if the element was in the graph alongside with more * information about cost, depth and trail to get to the solution. */ export declare function graphSearch(graph: Graph, source: number, destination: number, outFunction: () => VertexElement, inFunction: (i: VertexElement) => any, logger?: Logger): GraphSearchSolution;