Mathematics

Dijkstra's Algorithm

Dijkstra's Algorithm is a method for finding the shortest path between nodes in a graph. It starts at a specified node and explores the neighboring nodes, updating the shortest path to each node as it progresses. The algorithm continues until it has visited all nodes and determined the shortest path to each.

Written by Perlego with AI-assistance

12 Key excerpts on "Dijkstra's Algorithm"

  • Book cover image for: Principles of Computational Cell Biology
    eBook - ePub

    Principles of Computational Cell Biology

    From Protein Complexes to Cellular Networks

    • Volkhard Helms(Author)
    • 2018(Publication Date)
    • Wiley-VCH
      (Publisher)
    Generally, a graph may have many edges between many vertices. The same pair of vertices may even be linked by more than one edge. Edges can be bidirectional or unidirectional. In most cases, the only information given by an edge is that there is a relationship between the two vertices connected and the information is stored in the vertex itself. However, some graphs have numerical weights assigned to each edge. Such graphs can be used to solve other types of problems such as the traveling salesman problem.
    Graphs can be searched according to two general strategies. In a breadth‐first‐search, the graph search algorithm starts at the root vertex and visits all vertices that are neighbors of the root vertex. Then, it continues to find the unexplored neighbor vertices of the closest vertices, and so forth, until the goal is reached. In contrast, depth‐first search algorithms begin at the root vertex and follow each branch as far as possible. A backtracking scheme is then used to identify the next branch that is searched next.
    Studying the complexity and efficiency of graph search algorithms is an area computer scientists are deeply interested in. One of the best‐known algorithms is Dijkstra's Algorithm. This efficient algorithm constructs the shortest path between two vertices in a given graph.

    4.4 Dijkstra's Algorithm

    Dijkstra's Algorithm is named after its inventor, the Dutch computer scientist Edsger Dijkstra. This algorithm solves the task of finding the single‐source shortest path for a directed graph with nonnegative edge weights. Let us consider an example where the vertices of a graph represent cities and the weights of the edges connecting them correspond to the distances between cities along direct roads. In this case, Dijkstra's Algorithm will return the shortest route between two cities (Figure 4.6 ). A generalized version of the algorithm developed by Richard Bellman and Lester Ford can deal with both negative and non‐negative edge weights.
    Figure 4.6
  • Book cover image for: Simulation for Applied Graph Theory Using Visual C++
    The algorithm was proposed by the Dutch computer scientist Edsger Dijkstra in 1959. Dijkstra’s algorithm computes the shortest path by recursively selecting the unvisited ver-tex with the lowest distance to each unvisited neighbor. For a graph with n nodes hav-ing nonnegative weights on the edges, the method computes the path with the least cost between a pair of nodes with a complexity of O ( n 2 ). Dijkstra’s algorithm computes the shortest path from the source node to its destination by recursively computing the shortest paths from the source node to all other nodes in the graph first. The last node is the desti-nation node whose minimum distance from the source node is obtained after the shortest paths to other nodes have been computed. The algorithm starts with the source node as the initial node s 0 . It then searches for all nodes adjacent to s 0 and the costs from s 0 to the nodes are noted. The vertex s 1 with the minimum cost is marked. Next, the costs from s 0 to the adjacent nodes of s 1 through s 1 are calculated. The costs are compared to the costs from the other adjacent nodes of s 0 and the lowest values for the nodes are marked as the costs from s 0 to the nodes. The process is repeated to other nodes to produce the shortest paths s j for j = 2, 3,…, n − 1 until the destina-tion node is reached. The minimum cost for the destination node (the last node) is marked as s n , and this value stands as the final solution. Dijkstra’s algorithm involves a loop for updating the shortest distances from the source node to other nodes in the recursive process. It is summarized as follows: Given: Graph G ( V , E ) for V = [ v i ], E = [ Wt ( i , j )] with | V | = n for i , j = 1, 2,…, n . Problem: To find the shortest path from the source Sv to destination Dv , for Sv , Dv ∈ V .
  • Book cover image for: Graphs and Networks
    • S. R. Kingan(Author)
    • 2022(Publication Date)
    • Wiley
      (Publisher)
    In operations research prob- lems, vertices correspond to stages in a manufacturing process and the weight on a path from s to t corresponds to the cost of going from stage s to stage t. The goal is to find a sequence of activities from beginning to end that minimizes the cost. Map websites give driving directions based on shortest path algorithms. The exact v 1 v 3 v 6 v 8 v 2 v 5 v 4 v 7 t s 3 5 10 2 1 2 3 4 6 3 7 4 2 2 Figure 5.3 A Weighted Digraph. 5.3 Shortest Path Algorithms 125 algorithms used are proprietary and take into consideration various different defi- nitions of “weight” including not only distance, but also travel time in traffic, road quality, and tolls. The core, however, is a shortest path algorithm. In this section we will assume the weights are non-negative. A key idea used in Dijkstra’s Algorithm is the optimal substructure property of shortest paths (Lemma 2.2.3). A shortest path from s to t contains within it a shortest path from s to any other vertex in the path before t. Second, Dijkstra’s Algorithm is based on BFS for digraphs, which is like Algo- rithm 5.1.2, except that edges are only followed along their direction. Dijkstra’s Algorithm advances across the frontier of undiscovered vertices, but it also has to keep track of weights. We need one more piece of notation before presenting the algorithm. We will use d(𝑣), where 𝑣 is a vertex in the graph, to represent the length of the shortest path from s to 𝑣 known at each step. The algorithm starts with d(s) = 0 and d(𝑣) = ∞ for all vertices 𝑣 ≠ s, and assigns values to d(𝑣) as it proceeds and discovers paths of finite length. Dijkstra’s algorithm presented in the following text is taken from Roberts (2005). Algorithm 5.3.1. Dijkstra’s Algorithm Input: A directed graph G with a non-negative weight function 𝑤 on pairs of ver- tices, and two vertices s and t. Output: A shortest path from s to t. (1) Let A be a set of vertices initially containing only s.
  • Book cover image for: Practical Discrete Mathematics
    • Ryan T. White, Archana Tikayat Ray(Authors)
    • 2021(Publication Date)
    • Packt Publishing
      (Publisher)
    As a result, a brute-force approach such as this is clearly limited! It would take an entirely unrealistic amount of time to test this many paths. Also, this 100-vertex graph is quite small, especially when you consider the fact that one practical application—map apps such as Google Maps—place a vertex at every intersection between pairs of roads within entire cities and beyond. This would mean there are over 12,000 vertices in New York City alone!
    While this brute-force method is easy to understand, it is clearly infeasible, so we need a more strategic approach to solve the problem in a useful amount of time. We need an efficient way to find the shortest paths between specified vertices on networks or directed networks, assuming, of course, that a solution exists. This is what Dijkstra's Algorithm does, so let's learn about it!

    Dijkstra's Algorithm for Finding Shortest Paths

    In this section, we will learn about Dijkstra's Algorithm for finding the shortest paths, consider the process in simple terms, and apply the algorithm by hand to a small network.
    The most common algorithm for finding the shortest paths on a network is Dijkstra's Algorithm. It was named after the Dutch computer scientist Edsger W. Dijkstra , who constructed it in 1956, but since computing was such a new field at the time, there were so few academic journals dedicated to computing that he did not publish his findings until 1959.
    We will first learn about the method in intuitive terms using the small network from Figure 9.5 so that we can understand the ideas behind the approach. This understanding is important because there are many variations of the algorithm and we hope you will learn to adapt it to solve your own problems!
    Just like the previous section, we will seek the shortest path from v 1 to v 2 . Since it is a small network, we were able to find that there are such paths using brute force. The paths were as follows:
    v 1 – v 3 – v 2 and v 1 – v 3 – v 4 – v 2
    Each of these has a length of 3 units. We will actually construct the short paths from v 1 to every other vertex in the network along the way to find the shortest path from v 1 to v 2
  • Book cover image for: Algorithm Design and Applications
    • Michael T. Goodrich, Roberto Tamassia(Authors)
    • 2014(Publication Date)
    • Wiley
      (Publisher)
    There is an interesting ap- proach for solving this single-source shortest problem based on the greedy method (Chapter 10), which is known as Dijkstra’s algorithm. The second single-source algorithm we discuss, the Bellman-Ford algorithm, is for the case where edges can have negative weights, and it does not use a greedy strategy. The next single-source algorithm we consider also allows for negative- weight edges, but it is specialized for directed acyclic graphs and it is instead based on a greedy strategy. 400 Chapter 14. Shortest Paths 14.2 Dijkstra’s Algorithm A productive approach for applying the greedy method pattern to the single-source shortest-path problem is to perform a “weighted” breadth-first search starting at v. In particular, we can use the greedy method to develop an algorithm that iteratively grows a “cloud” of vertices out of v, with the vertices entering the cloud in order of their distances from v. Thus, in each iteration, the next vertex chosen is the vertex outside the cloud that is closest to v. The algorithm terminates when no more vertices are outside the cloud, at which point we have a shortest path from v to every other vertex of G. This approach is a simple, but nevertheless powerful, example of the greedy method. A Greedy Method for Finding Shortest Paths Applying the greedy method to the single-source shortest-path problem in this way results in an algorithm known as Dijkstra’s algorithm. In order to simplify our description of Dijkstra’s algorithm, we assume in the following that the input graph G is undirected (that is, all its edges are undirected) and simple (that is, it has no self-loops and no parallel edges). Hence, we denote the edges of G as unordered vertex pairs (u, z ). We leave the description of Dijkstra’s algorithm so that it works for a weighted directed graph as an exercise (R-14.2).
  • Book cover image for: Computer Science, Algorithms and Complexity
    • Adele Kuzmiakova(Author)
    • 2020(Publication Date)
    • Arcler Press
      (Publisher)
    In case one represents non-deterministic abstract equipment as a graph, whereby vertices define states and edges define possible transitions, then the shortest path algorithms may be applied to determine an optimal series of choices for achieving Computer Science, Algorithms and Complexity 64 a particular goal state, or establishing lesser bounds on the period needed to achieve a particular state. For instance, if vertices signify the conditions of a puzzle such as the Rubik’s Cube, with each directed edge corresponding to one move or turn, then the shortest route algorithms may be applied to determine a solution which utilizes the least possible number of moves (Lau, 2006). For a networking or telecommunicating mindset, the briefest path problem is oftentimes known as the min-delay route problem and typically tied with a broad path problem. For instance, the algorithm might seek the briefest (min-delay) broadest path, or broadest shortest (min-delay) route. A rather lighthearted application involves the popular game of “6 degrees of separation” which seeks to find the briefest path in graphs. Different other applications, typically studied in operations studies, include plant or facility outline, robotics, transportation, as well as VLSI framework. • Road Networks: The road network may be regarded as a graph having positive weights. These nodes represent road intersections and every border of the graph can be linked with a path segment between a pair of junctions. Besides, the average weight of the edge may relate to the distance of the related road section, the time required to cross the segment, and the rate of navigating the segment. By using directed edges, it is also feasible to model single-way streets. These graphs are unique in a sense that a couple of edges are more significant than others, especially for long-distance travelling (example, highways). The property has largely been formalized employing the concept of highway dimension.
  • Book cover image for: Optimization Algorithms for Networks and Graphs
    • James Evans(Author)
    • 2017(Publication Date)
    • CRC Press
      (Publisher)
    The preceding sections considered the problem of finding various shortest paths. Often, however, knowledge of the second, third, fourth, etc., shortest paths between two vertices is useful. For example, an airline might want to know the runner-up shortest flight routes between Springfield and Ankara in case one of its clients could not take the shortest flight route due to visa difficulties, flight cancellations, or airline strikes along the shortest flight route.
    This section first presents an algorithm, called the double-sweep algorithm, that finds the k shortest path lengths between a specified vertex and all other vertices in the graph. Next, this section presents two algorithms, called the generalized Floyd algorithm and the generalized Dantzig algorithm, that find the k shortest path lengths between every pair of vertices in the graph.
    The Dijkstra, Floyd, and Dantzig algorithms were able to construct various shortest paths. These algorithms essentially consisted of performing a sequence of two arithmetic operations, addition and minimization. These two operations were performed on single numbers that represented either arc lengths or path lengths. For example, equation (1) , which defines the Dijkstra algorithm, consists exclusively of addition and minimization. The same is true for equation (3) , which defines the Floyd algorithm, and equations (4) −(7), which define the Dantzig algorithm.
    The algorithms to be presented in this section (double-sweep algorithm, generalized Floyd algorithm, and generalized Dantzig algorithm) also consist exclusively of addition and minimization operations. However, these operations are performed not on single numbers (as with the previous algorithms) but on sets of k distinct numbers that represent the lengths of paths or arcs. With this as motivation, let
    Rk
    denote the set of all vectors (d 1 , d 2 ,
    …, dk
    ) with the property that d 1 < d 2
    < … < dk .
    Thus, the components of a member of
    Rk
    are distinct and arranged in ascending order. For example, ( − 3, − 1, 0, 4, 27) ∈
    R5 .
    Let a = (a 1 , a 2 ,
    … , ak
    ) and b = (b 1 , b 2 ,
    …, bk
    ) be two members of
    Rk
  • Book cover image for: Advanced Algorithms and Data Structures
    • Marcello La Rocca(Author)
    • 2021(Publication Date)
    • Manning
      (Publisher)
    Like BFS, Dijkstra’s algorithm takes a graph and a source vertex as input (optionally a goal vertex as well), and computes the minimum distance from the source to the goal (or equivalently, with the same asymptotic running time, to all the other vertices in the graph). Differently than for BFS, though, in Dijkstra’s algorithm the distance between two vertices is measured in terms of edges’ weight. Consider figure 14.17, which shows a directed, weighted graph that models the map shown in figure 14.16.
    Figure 14.17 Overlaying a directed weighted graph over the map in figure 14.16. Edges’ weights are the distances (in meters) between the intersections modeled by the edge’s vertices.
    In this context, the minimum distance between two vertices u and v is the minimum sum, across all paths from u to v , of the weights of edges in the path. If there is no such path, that is, if there is no way to go from u to v , then the distance between them is considered to be infinite.
    Figure 14.18 shows, on a simpler example graph, how Dijkstra’s algorithm works. It is similar to BFS, with two main differences:
    • The metric used is the sum of weights instead of path lengths.
    • Consequently, the container needs to be used to keep track of the next vertices to be visited: we can’t make do with a plain queue anymore; we need a priority queue.
    Everything else, the logic of the algorithm and the auxiliary data used, is similar to BFS. That’s very convenient for us because we can rewrite this algorithm from listing 14.2 with minimal changes. If you think that this similarity is a coincidence, though, hold your breath untill section 14.5.11

    14.4.2 Implementation

    Listing 14.6 describes Dijkstra’s algorithm in detail. Comparing it to listing 14.2, you can see how it resembles the BFS algorithm, so much so that we can use the same algorithm shown in listing 14.3 to reconstruct the shortest path for Dijkstra’s as well. Nonetheless, we need to be even more careful about performance in this case.
  • Book cover image for: Binary Digital Image Processing
    eBook - PDF
    • Stéphane Marchand-Maillet, Yazid M. Sharaiha(Authors)
    • 1999(Publication Date)
    • Academic Press
      (Publisher)
    The algo- rithms proposed in this section will typically find a single (not necessarily unique) optimal solution. For algorithms that enumerate all shortest paths between two vertices, the reader is referred to the relevant literature (e.g. see [~9]). Algorithmic graph theory gives a general framework for solving the shortest path problem [47]. Given a graph G = (V, A) and lengths for all arcs in A, we limit the study to the case where all arc lengths are positive (i.e. l(u, v) > 0 for any (u, v) E A). Given s E V and t E V as start and end vertices, respectively, the shortest path problem is that of finding a path P~t in G such that the length of this path l(P~t ) is minimum among all possible paths P~t from s to t in G. Trivial properties can readily be given for the shortest path P~t. [ --Initialisation --] for all vertices u in V do d(u) +- o A+- [ --Main search --] while The list of vertices A is not empty do (1) Get and remove the vertex u from the list A (2) for each vertex v in the forward star of u do if d(v) > d(u) + l(u, v) then d(v) + pred(v) +- u Add v into the list of vertices A (3) [ ~ Path Retrieval --] if d(t) < +oc then Backtrack the tree structure from t to s to get P2t else No path exists between s and t [ Initialise the list of vertices A with s ] [ Store u as predecessor of v ] [d(t) = +~ ] Algorithm 3.1: The generic shortest path algorithm 86 BINARY DIGITAL IMAGE PROCESSING Proposition 3.14: Given a graph G = (V, A), a start vertex s c V and an end vertex t E V and assuming that there exists a shortest path Pst from s to t, then, (i) Pst does not contain any cycle. (ii) For any vertex u in Pst, there exists a shortest path Psu from s to u which is a subset of P~t. (iii) For any vertex u e V, l(P~t ) < l(P~) + l(Put ). l(Psu ) + l(Put ) for any vertex u e Pst In particular, l ( Pst ) = Typically, solutions for the shortest path problem are based on Proposition 3.14(ii) and (iii). The outline of the shortest path search method is as follows.
  • Book cover image for: Network Routing
    eBook - ePub

    Network Routing

    Algorithms, Protocols, and Architectures

    • Deep Medhi, Karthik Ramasamy(Authors)
    • 2017(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    Figure 2.1 . Here, the shortest path is 1-4-3-6 with path cost 3. Through this procedure, we can find longer paths such as 1-2-3-6 (path cost 4), 1-4-5-6 (path cost 4), and 1-4-3-5-6 (path cost 4). It is easy to see that paths so determined may have one or more links in common.
    Suppose that we want to find k-shortest link disjoint paths. In this case, we need to temporarily delete all the links on the shortest path, and run Dijkstra's Algorithm again on the reduced graph—this will then give the next shortest link disjoint path; we can continue this process until we find k-shortest link disjoint paths. Sometimes it might not be possible to find two or more link disjoint paths, if the reduced graph is isolated into more than one networks. Consider again Figure 2.1 . Here, the shortest path from node 1 to node 6 is 1-4-3-6 with path cost 3. If we temporarily delete the links in this path, we find the next link-disjoint shortest path to be 1-2-4-5-6 of path cost 5. If we now delete links in this path, node 1 becomes isolated in the newly obtained reduced graph.
    In Algorithm 2.15 , we present a k-shortest paths algorithm that is based on an idea, originally outlined in [882] (see also [545 ,649] ). It uses an auxiliary list S in order to track/determine longer paths. Algorithm 2.15 presents a formal description with each step that conveys the idea behind it.

    2.11 Summary

    We first start with notations. In discussing different shortest path algorithms, we used a set of notations. While the notations might look confusing at first, there is some structure to those notations used here.
    First, for a link i-k connecting node i and node k, the link cost for the additive case was denoted by
    d
    i k
    while the link cost for the non-additive case was denoted by
    b
    i k
    . From a computational results point of view, we needed to track the minimum path cost between nodes i and j
  • Book cover image for: Data Structure Practice
    eBook - PDF

    Data Structure Practice

    for Collegiate Programming Contests and Education

    • Yonghui Wu, Jiande Wang(Authors)
    • 2016(Publication Date)
    • CRC Press
      (Publisher)
    423 Chapter 13 Algorithms of Best Paths Given a weighted, directed graph G = ( V , E ), each edge is with a real-valued weight. The weight of path P = ( v 0 , v 1 , … , v k ) is the sum of weights of its constituent edges: w p w v v i i i k ( ) ( , ) = -= ∑ 1 1 The weight of the shortest path (longest path) from vertex u to vertex v is defined as follows: δ ( , ) min(max){ ( ) } u v w p u v p u p =  →  if there is a path from to v ∞      otherwise The best path from vertex u to vertex v is defined as any path with weight w ( p ) = δ ( u , v ). In this chapter, three kinds of algorithms are introduced: 1. The Warshall algorithm is used to get the transitive closure for a graph. 2. The Floyd–Warshall algorithm is used to get all-pairs best paths in a graph. 3. The Dijkstra algorithm, Bellman–Ford algorithm, and shortest path faster algorithm (SPFA) are used to get single-source shortest paths in a graph. 13.1 Warshall Algorithm and Floyd–Warshall Algorithm The Warshall algorithm is used to compute the transitive closure of a relation for a graph. Suppose relation R is represented by digraph G . All vertices in G are v 1 , v 2 , … , v n . The graph G ′ for the transitive closure t ( R ) can be obtained from G as follows. If there exists a path from vertex v i to vertex v j in G , then an arc from v i to v j is added in G ′ . The adjacency matrix A for G ′ is defined as follows. If there exists a path from vertex v i to vertex v j , then A [ i ][ j ] = 1, and vertex v j is reachable from vertex v i ; otherwise, A [ i ][ j ] = 0, and vertex v j isn’t reachable from vertex v i . That 424 ◾ Data Structure Practice: For Collegiate Programming Contests and Education is, computing the transitive closure of a relation is to determine whether every pair of vertices are reachable or not. It is a problem of transitive closure for a graph. Suppose there is a sequence of square matrices order n A (0) , … , A ( n ) , where each element in square matrices is 0 or 1.
  • Book cover image for: Handbook of Graph Theory, Combinatorial Optimization, and Algorithms
    • Krishnaiyan "KT" Thulasiraman, Subramanian Arumugam, Andreas Brandstädt, Takao Nishizeki, Krishnaiyan "KT" Thulasiraman, Subramanian Arumugam, Andreas Brandstädt, Takao Nishizeki(Authors)
    • 2016(Publication Date)
    For some of the other transitive closure algorithms (see [25–33]). Syslo and Dzikiewicz [34] discuss computational experiences with several of the transitive closure algorithms. Melhorn [35] discusses the transitive closure problem in the context of general path problems in graphs. Several additional references on this topic can also be found in [35]. 2.5 SHORTEST PATHS Let G be a connected directed graph in which each directed edge is associated with a finite real number called the length of the edge. The length of an edge directed from a vertex i to a vertex j is denoted by w ( i, j ). If there is no edge directed from vertex i to vertex j , then w ( i, j ) = ∞ . The length of a directed path in G is the sum of the lengths of the edges in the path. A minimum length directed s − t path is called a shortest path from s to t . The length of a shortest directed s − t path, if it exists, is called the distance from s to t , and it is denoted as d ( s, t ). We assume that the vertices are denoted as 1 , 2 , . . ., n . In this section we consider the following two problems: 1. Single Source Shortest Paths Problem : Find the shortest paths from a specified vertex s to all other vertices in G . 2. All-Pairs Shortest Paths Problem : Find the shortest paths between all the ordered pairs of vertices in G . 36 Handbook of Graph Theory, Combinatorial Optimization, and Algorithms These two problems arise in several optimization problems. For example, finding a minimum cost flow in a transport network involves finding a shortest path from the source to the sink in the network [36] (see also Chapter 5). 2.5.1 Single Source Shortest Paths: Bellman–Ford–Moore Algorithm We first make a few observations and assumptions that do not cause any loss of generality of the algorithms to be discussed in this section. • Vertex 1 will serve as the source vertex. Algorithms for the shortest path problems start with an initial estimate of the distance of each vertex from the source vertex 1.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.