Knowee
Questions
Features
Study Tools

There is an undirected graph of n nodes. You are given a 2D array edges, where edges[i] = [ui, vi, lengthi] describes an edge between node ui and node vi with a traversal time of lengthi units.Additionally, you are given an array disappear, where disappear[i] denotes the time when the node i disappears from the graph and you won't be able to visit it.Notice that the graph might be disconnected and might contain multiple edges.Return the array answer, with answer[i] denoting the minimum units of time required to reach node i from node 0. If node i is unreachable from node 0 then answer[i] is -1. Example 1:Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]Output: [0,-1,4]Explanation:We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.For node 0, we don't need any time as it is our starting point.For node 1, we need at least 2 units of time to traverse edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.For node 2, we need at least 4 units of time to traverse edges[2].

Question

There is an undirected graph of n nodes. You are given a 2D array edges, where edges[i] = [ui, vi, lengthi] describes an edge between node ui and node vi with a traversal time of lengthi units.Additionally, you are given an array disappear, where disappear[i] denotes the time when the node i disappears from the graph and you won't be able to visit it.Notice that the graph might be disconnected and might contain multiple edges.Return the array answer, with answer[i] denoting the minimum units of time required to reach node i from node 0. If node i is unreachable from node 0 then answer[i] is -1. Example 1:Input: n = 3, edges = [[0,1,2],[1,2,1],[0,2,4]], disappear = [1,1,5]Output: [0,-1,4]Explanation:We are starting our journey from node 0, and our goal is to find the minimum time required to reach each node before it disappears.For node 0, we don't need any time as it is our starting point.For node 1, we need at least 2 units of time to traverse edges[0]. Unfortunately, it disappears at that moment, so we won't be able to visit it.For node 2, we need at least 4 units of time to traverse edges[2].

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can use Dijkstra's algorithm, which is a famous algorithm for finding the shortest paths from a single source in a graph with non-negative edge weights. Here are the steps:

  1. Initialize an array answer of size n with all elements as infinity except answer[0] which should be 0, because the distance from node 0 to itself is 0.

  2. Create a priority queue pq where each element is a pair of a node and its distance from node 0. Insert (0, 0) into pq, which means the distance from node 0 to itself is 0.

  3. While pq is not empty, do the following:

    • Remove the top element from pq, let's call it node.
    • If the distance of node is less than disappear[node], then for each neighbor v of node, if the distance of node plus the weight of the edge between node and v is less than answer[v] and less than disappear[v], then update answer[v] and insert v into pq.
  4. Finally, replace all infinity values in answer with -1, because if a node is unreachable from node 0, its distance should be -1 according to the problem statement.

  5. Return answer.

This algorithm works because it always considers the node with the shortest distance from node 0 that has not been visited yet. When it considers a node, it updates the distances of its neighbors. This process continues until all reachable nodes have been visited. The priority queue is used to efficiently find the node with the shortest distance. The disappear array is used to ensure that we don't visit a node after it disappears.

This problem has been solved

Similar Questions

You have an undirected, connected graph of n nodes labeled from 0 to n - 1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge.Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges. Example 1:Input: graph = [[1,2,3],[0],[0],[0]]Output: 4Explanation: One possible path is [1,0,2,0,3]Example 2:Input: graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]Output: 4Explanation: One possible path is [0,1,4,2,3] Constraints:n == graph.length1 <= n <= 120 <= graph[i].length < ngraph[i] does not contain i.If graph[a] contains b, then graph[b] contains a.The input graph is always connected.

You are roaming in a forest. This forest can be viewed as a graph with n nodes and m undirected edges. You roam the forest by traveling through the undirected edges from one node to another. You want to travel all the nodes at least once and stop when you have visited every node. Given that this is a connected graph, find the smallest lexicographical sequence of nodes that can be visited and return it in an array.The m undirected edges are given in the form of two arrays a and b, wherein there is an undirected edge between a[i] and b[i].Input FormatThe first line contains n, the number of nodes in the forest.The second line contains m, the number of edges in the forest.The next m lines contain the elements of the array a.The next line contains m, the number of edges in the forest.The next m lines contain the elements of the array b.Constraints2 <= n <= 1051 <= m <= 105The graph is a single connected component.If i!=j then (a[i], b[i]) != (a[j], b[j])Output FormatReturn an array denoting the lexicographically smallest sequence in which the nodes can be visited.

Alice and Bob have an undirected graph of n nodes and three types of edges:Type 1: Can be traversed by Alice only.Type 2: Can be traversed by Bob only.Type 3: Can be traversed by both Alice and Bob.Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.Return the maximum number of edges you can remove, or return -1 if Alice and Bob cannot fully traverse the graph. Example 1:Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]Output: 2Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.Example 2:Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]Output: 0Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.Example 3:Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]Output: -1Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.  Constraints:1 <= n <= 1051 <= edges.length <= min(105, 3 * n * (n - 1) / 2)edges[i].length == 31 <= typei <= 31 <= ui < vi <= nAll tuples (typei, ui, vi) are distinct.

Have the function ShortestPath(strArr) take strArr which will be an array of strings which models a non-looping Graph. The structure of the array will be as follows: The first element in the array will be the number of nodes N (points) in the array as a string. The next N elements will be the nodes which can be anything (A, B, C .. Brick Street, Main Street .. etc.). Then after the Nth element, the rest of the elements in the array will be the connections between all of the nodes. They will look like this: (A-B, B-C .. Brick Street-Main Street .. etc.). Although, there may exist no connections at all.An example of strArr may be: ["4","A","B","C","D","A-B","B-D","B-C","C-D"]. Your program should return the shortest path from the first Node to the last Node in the array separated by dashes. So in the example above the output should be A-B-D. Here is another example with strArr being ["7","A","B","C","D","E","F","G","A-B","A-E","B-C","C-D","D-F","E-D","F-G"]. The output for this array should be A-E-D-F-G. There will only ever be one shortest path for the array. If no path between the first and last node exists, return -1. The array will at minimum have two nodes. Also, the connection A-B for example, means that A can get to B and B can get to A.

n Depth First Search, how many times a node is visited?Marks : 1Negative Marks : 0Answer hereTwiceThriceEquivalent to number of indegree of the nodeOnce

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.