Refer to :
http://nthucad.cs.nthu.edu.tw/~yyliu/personal/nou/04ds/dijkstra.html
Find the minimum distance from a start node to any other nodes.
Algorithm:
Construct a hash map that record the minimum distance from a to others in the current step.
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Find the neighbors of a, and update the distance in the hash map
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Search for the minimum distance in hash map that has not been visited yet.(i.e. f node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 3 + 7 | 3 + 2 | โ | โ | 3 | โ |
Repeat 3 (i.e c node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 10 | 5 | โ | โ | 3 | โ |
Step4 | 0 | 10 | 5 | โ | 5 + 6 | 3 | 5 + 11 |
Repeat 3 (i.e b node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 10 | 5 | โ | โ | 3 | โ |
Step4 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step5 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Repeat 3 (i.e e node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 10 | 5 | โ | โ | 3 | โ |
Step4 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step5 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step6 | 0 | 10 | 5 | 11 + 13 | 11 | 3 | 16 |
Repeat 3 (i.e g node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 10 | 5 | โ | โ | 3 | โ |
Step4 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step5 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step6 | 0 | 10 | 5 | 24 | 11 | 3 | 16 |
Step7 | 0 | 10 | 5 | 24 | 11 | 3 | 16 |
Repeat 3 (i.e d node)
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
Step1 | โ | โ | โ | โ | โ | โ | โ |
Step2 | 0 | 32 | โ | โ | โ | 3 | โ |
Step3 | 0 | 10 | 5 | โ | โ | 3 | โ |
Step4 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step5 | 0 | 10 | 5 | โ | 11 | 3 | 16 |
Step6 | 0 | 10 | 5 | 24 | 11 | 3 | 16 |
Step7 | 0 | 10 | 5 | 24 | 11 | 3 | 16 |
Step8 | 0 | 10 | 5 | 24 | 11 | 3 | 16 |
https://leetcode.com/problems/sentence-similarity-ii/
We can generally divide this algorithm into 2 part โ> "Unoin" and "Find"
This kind if sort can only be used on directed-graph.
Please refer to below figure:
Step 1
In this step, 0 has been considered. Since there is no other node that 0 points to, we directly save 0 into stack and mark 0 as visited.
Step 2
1 has benn considered. Same as Step 1.
Step 3
2 has been considered. Since there is a 3 that 2 points to, we go to 3 and see whether there is a node that 3 points to. Since 1 has been visited, we put 3 into stack then put 2 into stack.
Step 4
4 has been considered. Since 0 and 1 have been visited. Directly put 4 into stack.
Step 5
Step 6
Pop out all elements in stack. That is 5 โ> 4 โ> 2 โ> 3 โ> 1 โ> 0
A bridge is the edge that will make the graph disconnected if it is removed. (As below)
Step 1
Perform DFS that give each node a order and give each edge a direction.
Step 2
Find the lowest order the current node can reach via the directed graph you make in the step1.
Step 3
For edge(c -> h), if disc( c ) < low( h ), then we call edge(c, h) is a bridge.
In this example, since only edge(c, g)'s holds the cindition disc( c ) < low( g )