【问题标题】:shortest path aassignment (hard)最短路径分配(硬)
【发布时间】:2022-08-04 10:24:59
【问题描述】:

这个难题是在我的一个面试任务中提出的,需要用打字稿来解决。我解决了它并以某种方式想分享它,因为在线没有解决方案

有函数ShortestPath(strArr) take strArr 这将是一个字符串数组 对非循环图进行建模。数组的结构如下:

数组中的第一个元素将是 数组中的节点数 N(点)作为字符串。接下来的 N 个元素将是可以是任何东西的节点 (A、B、C .. 砖街、主街 .. 等)。然后在第 N 个元素之后,数组中的其余元素 将是所有节点之间的连接。它们看起来像这样: (A-B、B-C .. Brick Street-Main Street .. 等)。虽然,可能根本不存在任何联系。

strArr 的一个例子可能是:

 [\"4\",\"A\",\"B\",\"C\",\"D\",\"A-B\",\"B-D\",\"B-C\",\"C-D\"]. 

它可能有助于通过以下方式可视化图表 绘制节点及其连接。您的程序应该返回从第一个节点到 数组中的最后一个节点,用破折号分隔。所以在上面的例子中,输出应该是 A-B-D。这是另一个 例如 strArr 为 [\"7\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G \",\"A-B\",\"A-E\",\"B-C\",\"C-D\",\"D-F\",\"E-D\",\"F-G\"]。 这个数组的输出应该是 A-E-D-F-G。数组只会有一个最短路径。 如果第一个和最后一个节点之间不存在路径,则返回 -1。该数组至少有两个节点。 此外,例如连接 A-B 意味着 A 可以到达 B,B 可以到达 A。

    标签: typescript algorithm graph shortest-path


    【解决方案1】:
    type Edges = string[][];
    interface Graph {
      [key: string]: string;
    }
    type Queue = [string, number][];
    
    function ShortestPath(strArr) {
      const nodesCount: string = strArr[0];
      const firstElement: string = strArr[1];
      const lastElement = strArr[Number(nodesCount)];
      const connections = strArr.slice(Number(nodesCount) + 1, strArr.length);
    
      const res = [];
      let edges: Edges = [];
    
      for (let connection of connections) {
        edges.push(connection.split("-"));
      }
      let graph = buildGraph(edges);
      const neighborsOfFirst = graph[firstElement];
      const visited = new Set();
      for (let node of neighborsOfFirst) {
        if (node in visited) continue;
        const path = bfs(graph, firstElement, lastElement);
        res.push(path);
        const updatedConnections = connections.filter(
          (connection) => !connection.includes(node)
        );
        const newEdges = [];
        for (let connection of updatedConnections) {
          newEdges.push(connection.split("-"));
        }
        graph = buildGraph(newEdges);
      }
      const ans = findShortestArray(res);
      // console.log("ans",ans[0].join("-"))
      return ans[0].join("-");
    }
    
    const findShortestArray = (arr = []) => {
      const res = arr.reduce((acc, val, index) => {
        if (!index || val.length < acc[0].length) {
          return [val];
        }
        if (val.length === acc[0].length) {
          acc.push(val);
        }
        return acc;
      }, []);
      return res;
    };
    
    const buildGraph = (edges: Edges) => {
      const graph = {};
      for (let node of edges) {
        const [node1, node2] = node;
        if (!graph[node1]) {
          graph[node1] = [];
        }
        if (!graph[node2]) {
          graph[node2] = [];
        }
        graph[node1].push(node2);
        graph[node2].push(node1);
      }
      return graph;
    };
    
    const bfs = (graph: Graph, source: string, target: string) => {
      const res = [];
      const visited = new Set([source, 0]);
      const queue: Queue = [[source, 0]];
      while (queue.length > 0) {
        const [node, distance] = queue.shift();
        res.push(node);
        if (node === target) return res;
        for (let neighbor of graph[node]) {
          if (!visited.has(neighbor)) {
            visited.add(neighbor);
    
            queue.push([neighbor, distance + 1]);
          }
        }
      }
      return -1;
    };
    
    // keep this function call here
    // @ts-ignore
    console.log(ShortestPath(readline()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 2014-08-07
      • 2018-05-03
      相关资源
      最近更新 更多