>

>
1x
go to beginning previous frame pause play next frame go to end

In the Single-Source Shortest Paths (SSSP) problem, we aim to find the shortest paths weights (and the actual paths) from a particular single-source vertex to all other vertices in a directed weighted graph (if such paths exist).


The SSSP problem is a(nother) very well-known Computer Science (CS) problem that every CS students worldwide need to be aware of and hopefully master.


The SSSP problem has several different efficient (polynomial) algorithms (e.g., Bellman-Ford, BFS, DFS, Dijkstra — 2 versions, and/or Dynamic Programming) that can be used depending on the nature of the input directed weighted graph, i.e. weighted/unweighted, with/without (negative weight) cycle, or structurally special (a tree/a DAG).


Remarks: By default, we show e-Lecture Mode for first time (or non logged-in) visitor.
If you are an NUS student and a repeat visitor, please login.

🕑

SSSP is one of the most frequent graph problem encountered in real-life. Every time we want to move from one place (usually our current location) to another (our destination), we will try to pick a short — if not the shortest — path.


SSSP algorithm(s) is embedded inside various map software like Google Maps and in various Global Positioning System (GPS) tool.


Pro-tip 1: Since you are not logged-in, you may be a first time visitor (or not an NUS student) who are not aware of the following keyboard shortcuts to navigate this e-Lecture mode: [PageDown]/[PageUp] to go to the next/previous slide, respectively, (and if the drop-down box is highlighted, you can also use [→ or ↓/← or ↑] to do the same),and [Esc] to toggle between this e-Lecture mode and exploration mode.

🕑

输入 1: 一个有向加权图 G(V, E), 不一定是连接着的, 其中 V/顶点可用于描述交叉点,交汇点 ,房屋,地标等,还有 E/边可用于描述街道,道路 ,大道,且具有适当的方向和权重/成本。


输入 2: 顾名思义, SSSP 问题有另一个输入:一个源顶点 sV.


Pro-tip 2: We designed this visualization and this e-Lecture mode to look good on 1366x768 resolution or larger (typical modern laptop resolution in 2021). We recommend using Google Chrome to access VisuAlgo. Go to full screen mode (F11) to enjoy this setup. However, you can use zoom-in (Ctrl +) or zoom-out (Ctrl -) to calibrate this.

🕑

SSSP 问题的目的是找到从 s 到每个顶点 uV 的最短路径权,表示为 δ(s, u) ( δ 发音为 'delta' ) 以及从 su 的实际最短路径。

路径 p 仅仅是沿改路径的边的权重的总和。

ss 的最短路径的权重是微不足道的:0。从 s 到任何不可到达顶点的最短路径也是微不足道的:+∞。

PS: 从 sv 的最短路径的权重,其中 (s, v) ∈ E ,不一定是 w(s, v) 的权重。请参阅下面的几张幻灯片来理解这一点。


Pro-tip 3: Other than using the typical media UI at the bottom of the page, you can also control the animation playback using keyboard shortcuts (in Exploration Mode): Spacebar to play/pause/replay the animation, / to step the animation backwards/forwards, respectively, and -/+ to decrease/increase the animation speed, respectively.

🕑

The outputs of all six (6) SSSP algorithms for the SSSP problem discussed in this visualization are these two arrays/Vectors:

  1. An array/Vector D of size V (D stands for 'distance')
    Initially, D[u] = 0 if u = s; otherwise D[u] = +∞ (a large number, e.g. 109)
    D[u] decreases as we find better (shorter) paths
    D[u]δ(s, u) throughout the execution of SSSP algorithm
    D[u] = δ(s, u) at the end of SSSP algorithm
  2. An array/Vector p of size V (p stands for 'parent'/'predecessor'/'previous')
    p[u] = the predecessor on best path from source s to u
    p[u] = NULL (not defined, we can use a value like -1 for this)
    This array/Vector p describes the resulting SSSP spanning tree
🕑

Initially, D[u] = +∞ (practically, a large value like 109) ∀uV\{s}, but D[s] = D[0] = 0.
Initially, p[u] = -1 (to say 'no predecessor') ∀uV.


Now click Dijkstra(0) — don't worry about the details as they will be explained later — and wait until it is over (approximately 10s on this small graph).


At the end of that SSSP algorithm, D[s] = D[0] = 0 (unchanged) and D[u] = δ(s, u)uV
e.g. D[2] = 6, D[4] = 7 (these values are stored as red text under each vertex).
At the end of that SSSP algorithm, p[s] = p[0] = -1 (the source has no predecessor), but p[v] = the origin of the red edges for the rest, e.g. p[2] = 0, p[4] = 2.


Thus, if we are at s = 0 and want to go to vertex 4, we will use shortest path 0 → 2 → 4 with path weight 7.

🕑

Some graphs contain negative weight edge(s) (not necessarily cyclic) and/or negative weight cycle(s). For example (fictional): Suppose you can travel forward in time (normal, edges with positive weight) or back in time by passing through time tunnel (special wormhole edges with negative weight), as the example shown above.


On that graph, the shortest paths from the source vertex s = 0 to vertices {1, 2, 3} are all ill-defined. For example 1 → 2 → 1 is a negative weight cycle as it has negative total path (cycle) weight of 15-42 = -27. Thus we can cycle around that negative weight cycle 0 → 1 → 2 → 1 → 2 → ... forever to get overall ill-defined shortest path weight of -∞.


However, notice that the shortest path from the source vertex s = 0 to vertex 4 is ok with δ(0, 4) = -99. So the presence of negative weight edge(s) is not the main issue. The main issue is the presence of negative weight cycle(s) reachable from source vertex s.

🕑

The main operation for all SSSP algorithms discussed in this visualization is the relax(u, v, w(u, v)) operation with the following pseudo-code:

relax(u, v, w_u_v)
if D[v] > D[u]+w_u_v // if the path can be shortened
D[v] = D[u]+w_u_v // we 'relax' this edge
p[v] = u // remember/update the predecessor
// update some other data structure(s) as necessary

For example, see relax(1,2,4) operation on the figure below: relax operation example

🕑

对于指定一个输入图,有两种不同的方法:

  1. 绘制图: 您可以绘制任何有向加权图作为输入图。
  2. 示例图: 您可以从我们选择的示例图列表中进行挑选,以帮助您入门。这些示例图具有不同的特征。
🕑

In this visualization, we will discuss 6 (SIX) SSSP algorithms.


We will start with the O(V×E) Bellman-Ford algorithm first as it is the most versatile (but also the slowest) SSSP algorithm. We will then discuss 5 (FIVE) other algorithms (including two variants of Dijkstra's algorithm) that solve special-cases of SSSP problem in a much faster manner.

🕑

The general purpose Bellman-Ford algorithm can solve all kinds of valid SSSP problem variants (expect one — the one that is ill-defined anyway, to be discussed soon), albeit with a rather slow O(V×E) running time. It also has an extremely simple pseudo-code:

for i = 1 to |V|-1 // O(V) here, so O(V×E×1) = O(V×E)
for each edge(u, v) ∈ E // O(E) here, e.g. by using an Edge List
relax(u, v, w(u, v)) // O(1) here

Without further ado, let's see a preview of how it works on the example graph above by clicking BellmanFord(0) (≈30s, and for now, please ignore the additional loop at the bottom of the pseudo-code).

🕑

Bellman-Ford algorithm can be made to run slightly faster on normal input graph, from the worst case of O(V×E) to just O(k×E) where k is the number of iterations of the outer loop of Bellman-Ford.


Discussion: How to do this? Is the speed-up significant?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

To convince the worldwide audience that Bellman-Ford algorithm works, let's temporarily move from visualization mode to proof mode for a few slides.


Theorem 1: If G = (V, E) contains no negative weight cycle, then the shortest path p from source vertex s to a vertex v must be a simple path.


Recall: A simple path is a path p = {v0, v1, v2, ..., vk}, (vi, vi+1) ∈ E, ∀ 0 ≤ i ≤ (k-1) and there is no repeated vertex along this path.

🕑
  1. Suppose the shortest path p is not a simple path
  2. Then p must contains one (or more) cycle(s) (by definition of non-simple path)
  3. Suppose there is a cycle c in p with positive weight (e.g., greenbluegreen on the left image) cycle
  4. If we remove c from p, then we will have a shorter 'shortest path' than our shortest path p
  5. A glaring contradiction, so p must be a simple path
🕑
  1. Even if c is actually a cycle with zero (0) total weight — it is possible according to our Theorem 1 assumption: no negative weight cycle (see the same greenbluegreen but on the right image), we can still remove c from p without increasing the shortest path weight of p cycle
  2. In conclusion, p is a simple path (from point 5) or can always be made into a simple path (from point 6)

In another word, shortest path p has at most |V|-1 edges from the source vertex s to the 'furthest possible' vertex v in G (in terms of number of edges in the shortest path — see the Bellman-Ford Killer example above).

🕑

Theorem 2: If G = (V, E) contains no negative weight cycle, then after Bellman-Ford algorithm terminates, we will have D[u] = δ(s, u), ∀ uV.


For this, we will use Proof by Induction and here are the starting points:


Consider the shortest path p from source vertex s to vertex vi where vi is defined as a vertex which the actual shortest path to reach it requires i hops (edges) from source vertex s. Recall from Theorem 1 that p will be simple path as we have the same assumption of no negative weight cycle.

🕑
  1. Initially, D[v0] = δ(s, v0) = 0, as v0 is just the source vertex s
  2. After 1 pass through E, we have D[v1] = δ(s, v1)
  3. After 2 pass through E, we have D[v2] = δ(s, v2)
  4. ...
  5. After k pass through E, we have D[vk] = δ(s, vk)
  6. When there is no negative weight cycle, the shortest path p is a simple path (see Theorem 1), thus the last iteration should be iteration |V|-1
  7. After |V|-1 pass through E, we have D[v|V|-1] = δ(s, v|V|-1), regardless the ordering of edges in E — see the Bellman-Ford Killer example above
🕑

Try running BellmanFord(0) on the 'Bellman-Ford Killer' example above. There are V = 7 vertices and E = 6 edges but the edge list E is configured to be at its worst possible order. Notice that after (V-1)×E = (7-1)*6 = 36 operations (~40s, be patient), Bellman-Ford will terminate with the correct answer and there is no way we can terminate Bellman-Ford algorithm earlier.

🕑

The only input graph that Bellman-Ford algorithm has issue is the input graph with negative weight cycle reachable from the source vertex s.


However, Bellman-Ford can be used to detect if the input graph contains at least one negative weight cycle reachable from the source vertex s by using the corollary of Theorem 2: If at least one value D[u] fails to converge after |V|-1 passes, then there exists a negative-weight cycle reachable from the source vertex s.


Now run BellmanFord(0) on the example graph that contains negative edges and a negative weight cycle. Please concentrate on the loop at the bottom of the pseudo-code.

🕑

Sometimes, the actual problem that we face is not the general form of the original problem. Therefore in this e-Lecture, we want to highlight five (5) special cases involving the SSSP problem. When we encounter any one of them, we can solve it with different and (much) faster algorithm than the generic O(V×E) Bellman-Ford algorithm. They are:

  1. On Unweighted Graphs: O(V+E) BFS,
  2. On Graphs without negative weight: O((V+E) log V) Dijkstra's algorithm,
  3. On Graphs without negative weight cycle: O((V+E) log V) Modified Dijkstra's,
  4. On Tree: O(V+E) DFS/BFS,
  5. On Directed Acyclic Graphs (DAG): O(V+E) Dynamic Programming (DP)
🕑

O(V+E) 广度优先搜索 (BFS) 算法可以解决有特殊情况的SSSP问题,当输入图是未加权时(所有边都具有单位权重1,在上面的例子 'CP3 4.3' 上尝试 BFS(5)),或者正常数加权(所有有边都具有相同的恒定权重,例如,你可以在上面的例图中用你所选择的任何正恒定权重去更改所有的边的权重)。

🕑

When the graph is unweighted — this appears quite frequently in real life — the SSSP problem can be viewed as a problem of finding the least number of edges traversed from the source vertex s to other vertices.


The BFS spanning tree from source vertex s produced by the fast O(V+E) BFS algorithm — notice the + sign — precisely fits the requirement.


Compared with the O(V×E) of Bellman-Ford — notice the × sign — it is a no-brainer to use BFS for this special case of SSSP problem.

🕑

Compared to the standard BFS in Graph Traversal module, we need to perform simple modifications to make BFS able to solve the unweighted version of the SSSP problem:

  1. First, we change the Boolean array visited into an Integer array D.
  2. At the start of BFS, instead of setting visited[u] = false, we set D[u] = 1e9 (a large number to symbolise +∞ or even -1 to symbolise 'unvisited' state, but we cannot use 0 as D[0] = 0) ∀uV\{s}; Then we set D[s] = 0
  3. We change the BFS main loop from
    if (visited[v] = 0) { visited[v] = 1 ... } // v is unvisited
    to
    if (D[v] = 1e9) { D[v] = D[u]+1 ... } // v is 1 step away from u
🕑

However, BFS will very likely produce wrong answer when run on weighted graphs as BFS is not actually designed for to solve the weighted version of SSSP problem. There may be a case that taking a path with more number of edges used produces lower total overall path weight than taking a path with minimum number of edges used — which is the output of BFS algorithm.


In this visualization, we will allow you to run BFS even on 'wrong' input graph for pedagogical purpose, but we will display a warning message at the end of the algorithm. For example, try BFS(0) on the general graph above and you will see that vertices {3,4} will have wrong D[3] and D[4] values (and also p[3] and p[4] values).


We will soon see Dijkstra's algorithm (2 implementation variants) for solving certain weighted SSSP problems in a faster way than the general Bellman-Ford algorithm.

🕑

The O((V+E) log V) Dijkstra's algorithm is the most frequently used SSSP algorithm for typical input: Directed weighted graph that has no negative weight edge at all, formally: ∀ edge(u, v) ∈ E, w(u, v) ≥ 0. Such weighted graph is very common in real life as travelling from one place to another always use positive time unit(s). Try Dijkstra(0) on one of the Example Graphs: CP4 4.16 shown above.

🕑

Dijkstra's algorithm maintains a set S (Solved) of vertices whose final shortest path weights have been determined. Initially S = {s}, the source vertex s only.


Then, it repeatedly selects vertex u in {V\S} with the minimum shortest path estimate, adds u to S, and relaxes all outgoing edges of u. Detailed proof of correctness of this Dijkstra's algorithm is usually written in typical Computer Science algorithm textbooks. For a simpler intuitive visual explanation on why this greedy strategy works, see this.


This entails the use of a Priority Queue as the shortest path estimates keep changing as more edges are processed. The choice of relaxing edges emanating from vertex with the minimum shortest path estimate first is greedy, i.e. use the "best so far", but we will see later that it can be proven that it will eventually ends up with an optimal result — if the graph has no negative weight edge.

🕑

In Dijkstra's algorithm, each vertex will only be extracted from the Priority Queue (PQ) once. As there are V vertices, we will do this maximum O(V) times.


ExtractMin() operation runs in O(log V) whether the PQ is implemented using a Binary Min Heap or using a balanced BST like AVL Tree.


Therefore this part is O(V log V).

🕑

Every time a vertex is processed, we relax its neighbors. In total, E edges are processed.


If by relaxing edge(u, v), we have to decrease D[v], we call the O(log V) DecreaseKey() operation in Binary Min Heap (harder to implement as C++ STL priority_queue/Python heapq/Java PriorityQueue does not support this operation efficiently yet) or simply delete the old entry and then re-insert a new entry in balanced BST like AVL Tree (which also runs in O(log V), but this is much easier to implement, just use C++ STL set/Java TreeSet — unfortunately not natively supported in Python).


Therefore, this part is O(E log V).


Thus in overall, Dijkstra's algorithm runs in O(V log V + E log V) = O((V+E) log V) time, which is much faster than the O(V×E) Bellman-Ford algorithm.

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

When the input graph contains at least one negative weight edge — not necessarily negative weight cycle — Dijkstra's algorithm can produce wrong answer.


Try Dijkstra(0) on one of the Example Graphs: CP3 4.18.


At the end of the execution of Dijkstra's algorithm, vertex 4 has wrong D[4] value as the algorithm started 'wrongly' thinking that subpath 0 → 1 → 3 is the better subpath of weight 1+2 = 3, thus making D[4] = 6 after calling relax(3,4,3). However, the presence of negative weight -10 at edge 2 → 3 makes the other subpath 0 → 2 → 3 eventually the better subpath of weight 10-10 = 0 although it started worse with path weight 10 after the first edge 0 → 2. This better D[3] = 0 is never propagated further due to the greedy nature of Dijkstra's algorithm, hence D[4] is wrong.

🕑

Dijkstra's algorithm can also be implemented differently. The O((V+E) log V) Modified Dijkstra's algorithm can be used for directed weighted graphs that may have negative weight edges but no negative weight cycle.


Such input graph appears in some practical cases, e.g., travelling using an electric car that has battery and our objective is to find a path from source vertex s to another vertex that minimizes overall battery usage. As usual, during acceleration (or driving on flat/uphill road), the electric car uses (positive) energy from the battery. However, during braking (or driving on downhill road), the electric car recharges (or use negative) energy to the battery. There is no negative weight cycle due to kinetic energy loss.


For example, try ModifiedDijkstra(0) on one of the Example Graphs: CP3 4.18 that has troubled the original version of Dijkstra's algorithm (see previous slide).

🕑

The key idea is the 'usage modification' done to C++ STL priority_queue/Python heapq/Java PriorityQueue to allow it to perform the required 'DecreaseKey' operation efficiently, i.e., in O(log V) time.


The technique is called 'Lazy Update' where we leave the 'outdated/weaker/bigger-valued information' in the Min Priority Queue instead of deleting it straight-away. As the items are ordered from smaller values to bigger values in a Min PQ, we are guaranteeing ourself that we will encounter the smallest/most-up-to-date item first before encountering the weaker/outdated item(s) later - which by then can be easily ignored.

🕑

On non-negative weighted graphs, the behavior of Modified Dijkstra's implementation is exactly the same as the Original Dijkstra's so we can use the same time complexity analysis of O((V+E) log V).


PS: We note that when we use the Modified Dijkstra's algorithm, there can be more items (up to E) in the Priority Queue than if we use the Original Dijkstra's algorithm (up to V). However, since O(log E) = O(log V^2) = O(2 log V) = O(log V), we still treat the Priority Queue operations as O(log V).


However, if the graph has at least one negative weight edge, the analysis is harder.

🕑
当输入图包含至少一个负权重边但没有负权重周期时 - 修改后的Dijkstra算法产生正确的答案。

尝试使用其中一个示例图表ModifiedDijkstra(0):CP3 4.18导致Dijkstra(0)出现问题。

在ModifiedDijkstra算法执行结束时,顶点4具有正确的D [4]值,因为虽然修改后的Dijkstra算法也开始“错误地”认为子路径0→1→3是权重1 + 2 = 3的更好的子路径,因此在调用relax(3,4,3)后使D [4] = 6。这里,修改后的Dijkstra算法在发现其他子路径0→2→3最终是权重10-10 = 0的更好子路径之后继续传播D [3] = 0.因此D [4]最终再次正确。然而,这是以比O((V + E)log V)可能运行(更多)操作为代价的。
🕑

Unfortunately, running ModifiedDijkstra(0) on the graph with negative weight cycle as shown on one of the Example Graphs: CP3 4.17 above will cause an endless loop (the animation is very long but we limit the number of loop to be 100 edges processed so your web browser will not hang).

🕑

Try ModifiedDijkstra(0) on the extreme corner case above that is very hard to derive without proper understanding of this algorithm and was part of Asia Pacific Informatics Olympiad (APIO) 2013 task set by Steven.


The Modified Dijkstra's algorithm will terminate with correct answer, but only after running exponential number of operations (each carefully constructed triangle raises the number of required operations by another power of two). Thus we cannot prematurely terminate Modified Dijkstra's in this worst case input situation.


However, such extreme corner case is rare and thus in practice, Modified Dijkstra's algorithm can be used on directed graphs that have some negative weighted edges as long as the graph has no negative weight cycle reachable from the source vertex s.

🕑

The O(V) Depth-First Search (DFS) algorithm can solve special case of SSSP problem, i.e. when the input graph is a (weighted) Tree.


In a Tree, there is only one unique and acylic path that connects two distinct vertices. Thus the unique path that connects the source vertex s to any another vertex uV is actually also the shortest path. For example, try DFS(0) on the Tree above.


Notice that for a (weighted) Tree, we can also use BFS. For example, try BFS(0) on the same Tree above.


Discussion: Why DFS (and also BFS) runs in O(V) instead of O(V+E) if the input is a (weighted) Tree?

🕑

The content of this interesting slide (the answer of the usually intriguing discussion point from the earlier slide) is hidden and only available for legitimate CS lecturer worldwide. This mechanism is used in the various flipped classrooms in NUS.


If you are really a CS lecturer (or an IT teacher) (outside of NUS) and are interested to know the answers, please drop an email to stevenhalim at gmail dot com (show your University staff profile/relevant proof to Steven) for Steven to manually activate this CS lecturer-only feature for you.


FAQ: This feature will NOT be given to anyone else who is not a CS lecturer.

🕑

在不是Tree的任何其他图形上运行时,DFS很可能会产生错误的答案。我们将为此类案例显示警告消息,但我们不会阻止您出于教学目的而尝试此功能。

例如,在上面的一般图表上尝试DFS(0),您将看到顶点{4}将具有错误的D [4]值(以及错误的p [4]值),因为DFS(0)深入0→1→首先是3→4,一直回溯到顶点0并且最终访问0→2但是由于DFS之前已经访问过顶点4,所以不能处理边缘2→4。

🕑

The O(V+E) Dynamic Programming algorithm can solve special case of SSSP problem, i.e. when the input graph is a Directed Acyclic Graph (DAG) thus we can find at least one topological order of the DAG and process the edge relaxation according to this topological order.


For example, try DP(0) on the example DAG above. First, it computes one (there are other) possible topological order using either the O(V+E) DFS or the BFS/Kahn's algorithm outlined in Graph Traversal module. For example, assume one topological order is {0,2,1,3,4,5}. Then, it relaxes the outgoing edges of vertices listed in that topological order. After just one O(V+E) pass, we will have correct D[u] values ∀uV.

🕑

On the Modified Dijkstra's killer example shown above, DP(0) works fast as the graph is actually a DAG, albeit having negative weight edge. As the graph is a DAG, there will not be any negative weight cycle to worry about.


However, DP will not work for any non DAG as non DAG contains at least one cycle and thus no topological order can be found within that cycle.

🕑

DP algorithm for solving SSSP on DAG is also called one-pass Bellman-Ford algorithm as it replaces the outermost V-1 loop (we do not know the correct order so we just repeat until the maximum possible) with just one topological order pass (we know that this is (one of) the correct order(s) of this DAG).


Compare DP(0) (relax E edges just once — according to topological order of its vertices) versus BellmanFord(0) (relax E edges in random order, V-1 times) on the same example DAG above.

🕑

We have lots of other stuffs on top of this basic explanation of SSSP algorithms for SSSP problems.


Meanwhile, you are allowed to use/modify our implementation code for Bellman-Ford/Bellman-Ford-Moore/Dijkstra's Algorithms:
bellman_ford.cpp/bellman_ford_moore.cpp/dijkstra.cpp
bellman_ford.java/bellman_ford_moore.java/dijkstra.java
bellman_ford.py/bellman_ford_moore.py/dijkstra.py
bellman_ford.ml/bellman_ford_moore.ml/dijkstra.ml

🕑

有关此SSSP问题及其各种算法的一些有趣问题,请在 SSSP 培训模块中练习(无需登陆)。


但是,对于注册用户,您应该登陆然后转到 Main Training Page 以正式清除此模块(清除其它先决模块),此类成就将记录在你的账户中。

🕑

We also have a few programming problems that somewhat requires the usage of the correct SSSP algorithm: Kattis - hidingplaces and Kattis - shortestpath1.


Try to solve them and then try the many more interesting twists/variants of this interesting SSSP problem.


Advertisement: Buy Competitive Programming textbook to read more on this interesting problem.


You have reached the last slide. Return to 'Exploration Mode' to start exploring!

Note that if you notice any bug in this visualization or if you want to request for a new visualization feature, do not hesitate to drop an email to the project leader: Dr Steven Halim via his email address: stevenhalim at gmail dot com.

🕑

Edit Graph

图示

BellmanFord(s)

BFS(s)

Dijkstra(s)

DFS(s)

DP(s)

>

Unweighted

Weighted

Negative Weight

Corner Case

Special Case

s =

执行

s =

执行

s =

原版的

修改的

s =

执行

s =

执行

We use cookies to improve our website.
By clicking ACCEPT, you agree to our use of Google Analytics for analysing user behaviour and improving user experience as described in our Privacy Policy.
By clicking reject, only cookies necessary for site functions will be used.

ACCEPT REJECT
关于 团队 使用条款 隐私政策

关于

VisuAlgo于2011年由Steven Halim博士创建,是一个允许学生以自己的速度自学基础知识,从而更好地学习数据结构与算法的工具。
VisuAlgo包含许多高级算法,这些算法在Steven Halim博士的书(“Competitive Programming”,与他的兄弟Felix Halim博士合作)和其他书中有讨论。今天,一些高级算法的可视化/动画只能在VisuAlgo中找到。
虽然本网站是专门为新加坡国立大学(NUS)学生学习各种数据结构和算法类(例如CS1010,CS2040,CS3230,CS3233,CS4234)而设,但我们作为在线学习的倡导者,我们非常希望世界各地的好奇的头脑能发现这些非常有用的算法可视化。
VisuAlgo不是从一开始就设计为在小触摸屏(例如智能手机)上工作良好,因为为了满足许多复杂算法可视化,需要大量的像素和点击并拖动手势进行交互。为得到良好的用户体验,最低屏幕分辨率应为1024x768,并且本网站只有首页相对适合移动设备。但是,我们正在测试一个准备在2022年4月发布的移动版本。
VisuAlgo是一个正在进行的项目,更复杂的可视化仍在开发中。
最令人兴奋的发展是自动问题生成器和验证器(在线测验系统),允许学生测试他们的基本数据结构和算法的知识。这些问题是通过一些随机生成的规则,学生的答案会在提交给我们的评分服务器后立即自动分级。这个在线测验系统,当它被更多的世界各地的CS教师采用,应该能从技术上消除许多大学的典型计算机科学考试手动基本数据结构和算法问题。通过在通过在线测验时设置小(但非零)的重量,CS教练可以(显着地)增加他/她的学生掌握这些基本问题,因为学生具有几乎无限数量的可以立即被验证的训练问题他们参加在线测验。培训模式目前包含12个可视化模块的问题。我们将很快添加剩余的12个可视化模块,以便VisuAlgo中的每个可视化模块都有在线测验组件。
VisuAlgo支持三种语言:英语,中文,印尼语。目前,我们还以各种语言写了有关VisuAlgo的公共注释:
id, kr, vn, th.

团队

项目领导和顾问(2011年7月至今)
Dr Steven Halim, Senior Lecturer, School of Computing (SoC), National University of Singapore (NUS)
Dr Felix Halim, Senior Software Engineer, Google (Mountain View)

本科生研究人员 1 (Jul 2011-Apr 2012)
Koh Zi Chun, Victor Loh Bo Huai

最后一年项目/ UROP学生 1 (Jul 2012-Dec 2013)
Phan Thi Quynh Trang, Peter Phandi, Albert Millardo Tjindradinata, Nguyen Hoang Duy

最后一年项目/ UROP学生 2 (Jun 2013-Apr 2014)
Rose Marie Tan Zhao Yun, Ivan Reinaldo

本科生研究人员 2 (May 2014-Jul 2014)
Jonathan Irvin Gunawan, Nathan Azaria, Ian Leow Tze Wei, Nguyen Viet Dung, Nguyen Khac Tung, Steven Kester Yuwono, Cao Shengze, Mohan Jishnu

最后一年项目/ UROP学生 3 (Jun 2014-Apr 2015)
Erin Teo Yi Ling, Wang Zi

最后一年项目/ UROP学生 4 (Jun 2016-Dec 2017)
Truong Ngoc Khanh, John Kevin Tjahjadi, Gabriella Michelle, Muhammad Rais Fathin Mudzakir

最后一年项目/ UROP学生 5 (Aug 2021-Dec 2022)
Liu Guangyuan, Manas Vegi, Sha Long, Vuong Hoang Long

最后一年项目/ UROP学生 6 (Aug 2022-Apr 2023)
Lim Dewen Aloysius, Ting Xiao

List of translators who have contributed ≥100 translations can be found at statistics page.

致谢
本项目运营资金是由NUS教学与学习发展中心(CDTL)的教学增进款慷慨提供的。

使用条款

VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only "payment" that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook/Twitter/Instagram/TikTok posts, course webpages, blog reviews, emails, etc.

If you are a data structure and algorithm student/instructor, you are allowed to use this website directly for your classes. If you take screen shots (videos) from this website, you can use the screen shots (videos) elsewhere as long as you cite the URL of this website (https://visualgo.net) and/or list of publications below as reference. However, you are NOT allowed to download VisuAlgo (client-side) files and host it on your own website as it is plagiarism. As of now, we do NOT allow other people to fork this project and create variants of VisuAlgo. Using the offline copy of (client-side) VisuAlgo for your personal usage is fine.

Note that VisuAlgo's online quiz component is by nature has heavy server-side component and there is no easy way to save the server-side scripts and databases locally. Currently, the general public can only use the 'training mode' to access these online quiz system. Currently the 'test mode' is a more controlled environment for using these randomly generated questions and automatic verification for real examinations in NUS.

List of Publications

This work has been presented briefly at the CLI Workshop at the ICPC World Finals 2012 (Poland, Warsaw) and at the IOI Conference at IOI 2012 (Sirmione-Montichiari, Italy). You can click this link to read our 2012 paper about this system (it was not yet called VisuAlgo back in 2012) and this link for the short update in 2015 (to link VisuAlgo name with the previous project).

This work is done mostly by my past students. 

Bug Reports or Request for New Features

VisuAlgo is not a finished project. Dr Steven Halim is still actively improving VisuAlgo. If you are using VisuAlgo and spot a bug in any of our visualization page/online quiz tool or if you want to request for new features, please contact Dr Steven Halim. His contact is the concatenation of his name and add gmail dot com.

隐私政策

Version 1.1 (Updated Fri, 14 Jan 2022).

Disclosure to all visitors: We currently use Google Analytics to get an overview understanding of our site visitors. We now give option for user to Accept or Reject this tracker.

Since Wed, 22 Dec 2021, only National University of Singapore (NUS) staffs/students and approved CS lecturers outside of NUS who have written a request to Steven can login to VisuAlgo, anyone else in the world will have to use VisuAlgo as an anonymous user that is not really trackable other than what are tracked by Google Analytics.

For NUS students enrolled in modules that uses VisuAlgo: By using a VisuAlgo account (a tuple of NUS official email address, NUS official student name as in the class roster, and a password that is encrypted on the server side — no other personal data is stored), you are giving a consent for your module lecturer to keep track of your e-lecture slides reading and online quiz training progresses that is needed to run the module smoothly. Your VisuAlgo account will also be needed for taking NUS official VisuAlgo Online Quizzes and thus passing your account credentials to another person to do the Online Quiz on your behalf constitutes an academic offense. Your user account will be purged after the conclusion of the module unless you choose to keep your account (OPT-IN). Access to the full VisuAlgo database (with encrypted passwords) is limited to Steven himself.

For other NUS students, you can self-register a VisuAlgo account by yourself (OPT-IN).

For other CS lecturers worldwide who have written to Steven, a VisuAlgo account (your (non-NUS) email address, you can use any display name, and encrypted password) is needed to distinguish your online credential versus the rest of the world. Your account will be tracked similarly as a normal NUS student account above but it will have CS lecturer specific features, namely the ability to see the hidden slides that contain (interesting) answers to the questions presented in the preceding slides before the hidden slides. You can also access Hard setting of the VisuAlgo Online Quizzes. You can freely use the material to enhance your data structures and algorithm classes. Note that there can be other CS lecturer specific features in the future.

For anyone with VisuAlgo account, you can remove your own account by yourself should you wish to no longer be associated with VisuAlgo tool.