Graph Problem
Dijkstra
Refer to :
http://nthucad.cs.nthu.edu.tw/~yyliu/personal/nou/04ds/dijkstra.html
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
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
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
|
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 |
Union Find
https://leetcode.com/problems/sentence-similarity-ii/
We can generally divide this algorithm into 2 part โ> "Unoin" and "Find"
- Union
Connect the undirected edge of the graph
- Find
Check whether two nodes is in the same set by checking whether they have identical root nodes.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Topological Sort
This kind if sort can only be used on directed-graph.
- We should firstly use a Hash Map to record the edges of the graph.
- Use an array to record whether the node has been visited.
- Use a Stack to sort the graph
Please refer to below figure:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
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.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 2
1 has benn considered. Same as Step 1.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
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.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 4
4 has been considered. Since 0 and 1 have been visited. Directly put 4 into stack.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 5
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 6
Pop out all elements in stack. That is 5 โ> 4 โ> 2 โ> 3 โ> 1 โ> 0
Bridge-finding algorithm
A bridge is the edge that will make the graph disconnected if it is removed. (As below)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Example
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 1
Perform DFS that give each node a order and give each edge a direction.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
Step 2
Find the lowest order the current node can reach via the directed graph you make in the step1.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
-
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 )
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Practice

Solution

Code Example โ> LeetCode: Critical connection in networks