You're given a directed weighted graph, which has m edges and n vertices. Every edge's weight is nonnegative. The vertices are either in set S1 or in set S2 (S1 and S2 are disjoint). You need to find the shortest path between any pairs (v1, v2) (v1 is in S1, and v2 in S2). Show
The running time of the solution should be O(mlogn). 'Nonnegtive' and 'mlogn' remind me of Dijkstra, but I have no idea how to use Dijkstra for constant times to solve it. Thanks in advance. asked Apr 27 at 1:46
2 Dijkstra, but modified:
Running Dijkstra is answered Apr 27 at 13:43
tucuxitucuxi 17.1k2 gold badges42 silver badges73 bronze badges Welcome! If you've always wanted to learn and understand Dijkstra's algorithm, then this article is
for you. You will see how it works behind the scenes with a step-by-step graphical explanation. You will learn: Let's begin. ✨ Let's start with a brief introduction to graphs. Graphs are data structures used to represent "connections" between pairs of elements. This is a graphical representation of a graph: Nodes are represented with colored circles and edges are represented with lines that connect these circles. 💡 Tip: Two nodes are connected if there is an edge between them. Graphs are directly applicable to real-world scenarios. For example, we could
use graphs to model a transportation network where nodes would represent facilities that send or receive products and edges would represent roads or paths that connect them (see below). Graphs can be: 💡
Tip: in this article, we will work with undirected graphs. A weight graph is a graph whose edges have a "weight" or "cost". The weight of an edge can represent distance, time, or anything that models the "connection" between the pair of nodes it connects. For example, in the weighted graph below you can see a blue number next to each edge. This number is used to represent the weight of the
corresponding edge. 💡 Tip: These weights are essential for Dijkstra's Algorithm. You will see why in just a moment. Now that you know the basic concepts of graphs, let's start diving into this amazing algorithm.
Purpose and Use CasesWith Dijkstra's Algorithm, you can find the shortest path between nodes in a graph. Particularly, you can find the shortest path from a node (called the "source node") to all other nodes in the graph, producing a shortest-path tree. This algorithm is used in GPS devices to find the shortest path between the current location and the destination. It has broad applications in industry, specially in domains that require modeling networks. HistoryThis algorithm was created and published by Dr. Edsger W. Dijkstra, a brilliant Dutch computer scientist and software engineer. In 1959, he published a 3-page article titled "A note on two problems in connexion with graphs" where he explained his new algorithm. Dr. Edsger Dijkstra at ETH Zurich in 1994 (image by Andreas F. Borchert)During an interview in 2001, Dr. Dijkstra revealed how and why he designed the algorithm: What’s the shortest way to travel from Rotterdam to Groningen? It is the algorithm for the shortest path, which I designed in about 20 minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a 20-minute invention. In fact, it was published in 1959, three years later. The publication is still quite nice. One of the reasons that it is so nice was that I designed it without pencil and paper. Without pencil and paper you are almost forced to avoid all avoidable complexities. Eventually that algorithm became, to my great amazement, one of the cornerstones of my fame. — As quoted in the article Edsger W. Dijkstra from An interview with Edsger W. Dijkstra. ⭐ Unbelievable, right? In just 20 minutes, Dr. Dijkstra designed one of the most famous algorithms in the history of Computer Science. Basics of Dijkstra's Algorithm
RequirementsDijkstra's Algorithm can only work with graphs that have positive weights. This is because, during the process, the weights of the edges have to be added to find the shortest path. If there is a negative weight in the graph, then the algorithm will not work properly. Once a node has been marked as "visited", the current path to that node is marked as the shortest path to reach that node. And negative weights can alter this if the total weight can be decremented after this step has occurred. 🔹 Example of Dijkstra's AlgorithmNow that you know more about this algorithm, let's see how it works behind the scenes with a a step-by-step example. We have this graph: The algorithm will generate the shortest path from node 💡 Tip: For this graph, we will assume that the weight of the edges represents the distance between two nodes. We will have the shortest path from node Initially, we have this list of distances (please see the list below):
We also have this list (see below) to keep track of the nodes that have not been visited yet (nodes that have not been included in the path): 💡 Tip: Remember that the algorithm is completed once all nodes have been added to the path. Since we are choosing to start at node Now we need to start checking the distance from node 💡 Tip: This doesn't mean that we are immediately adding the two adjacent nodes to the shortest path. Before adding a node to this path, we need to check if we have found the shortest path to reach it. We are simply making an initial examination process to see the options available. We need to update the distances from node After updating the distances of the adjacent nodes, we need to:
If we check the list of distances, we can see that node In the diagram, we can represent this with a red edge: We mark it with a red square in the list to represent that it has been "visited" and that we have found the shortest path to this node: We cross it off from the list of unvisited nodes: Now we need to analyze the new adjacent nodes to find the shortest path to reach them. We will only analyze the nodes that are adjacent to the nodes that are already part of the shortest path (the path marked with red edges). Node Since
we already have the distance from the source node to node This distance is 7. Let's see why. To find the distance from the source node
to another node (in this case, node
Now that we have the distance to the adjacent nodes, we have to choose which node will be added to the path. We must select the unvisited node with the shortest (currently known) distance to the source node. From the list of distances, we can immediately detect that this is node We add it to the path graphically with a red border around the node and a red edge: We also mark it as visited by adding a small red square in the list of distances and crossing it off from the list of unvisited nodes: Now we need to repeat the process to find the shortest path from the source node to the new adjacent node, which is node You can see
that we have two possible paths Node But now we have another alternative. If we choose to follow the path Clearly, the first (existing) distance is shorter (7 vs. 14), so we will choose to keep the original path Therefore, we add this node to the path using the first alternative: We mark this node as visited and cross it off from the list of unvisited nodes: Now we repeat the process again. We need to check the new adjacent nodes that we have not visited so far. This time, these nodes are node We update the distances of these nodes to the source node, always trying to find a shorter path, if possible:
💡 Tip: Notice that we can only consider extending the shortest path (marked in red). We cannot consider paths that will take us through edges that have not been added to the shortest path (for example, we cannot form a path that goes through the edge We need to choose which unvisited node will be marked as visited now. In this case, it's node We also mark it as "visited" by adding a small red square in the list: And we cross it off from the list of unvisited nodes: And we repeat the process again. We check the adjacent nodes: node For node
Clearly, the first path is shorter, so we choose it for node For node
We mark the node with the shortest (currently known) distance as visited. In this case, node And we cross it off from the list of unvisited nodes: Now we have this path (marked in red): Only one node has not been visited yet, node There are three different paths that we can take
to reach node
We select the shortest path: We mark the node as visited and cross it off from the list of unvisited nodes: And voilà! We have the final result with the shortest path from node In the diagram, the red lines mark the edges that belong to the shortest path. You need to follow these edges to follow the shortest path to reach a given node in the graph starting from node For example, if you want to reach node 🔸 In Summary
I really hope you liked my article and found it helpful. Now you know how Dijkstra's Algorithm works behind the scenes. Follow me on Twitter @EstefaniaCassN and check out my online courses. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you find the shortest path in A weighted directed graph?Input: weighted, directed graph G = (V,E), with weight function w : E → R. δ(u, v) = { min{w(p)} if there is a path p from u to v , ∞ otherwise . A shortest path from vertex u to vertex v is then defined as any path p with weight w(p) = δ(u, v).
Can we find shortest path using DFS in weighted graph?And so, the only possible way for BFS (or DFS) to find the shortest path in a weighted graph is to search the entire graph and keep recording the minimum distance from source to the destination vertex.
What is A shortest path between two vertices in A weighted graph unique if the weights of edges are distinct give an example?(b) T F [3 points] If all edges in a graph have distinct weights, then the shortest path between two vertices is unique. Solution: FALSE. Even if no two edges have the same weight, there could be two paths with the same weight. For example, there could be two paths from s to t with lengths 3+5=8 and 2+6=8.
Would you use BFS to find the shortest path between two nodes in A weighted graph with arbitrary edge weights justify your answer?The idea is to use BFS. One important observation about BFS is that the path used in BFS always has the least number of edges between any two vertices. So if all edges are of same weight, we can use BFS to find the shortest path.
What algorithm is used to find the shortest path between two nodes?Dijkstra's algorithm (/ˈdaɪkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.
|