Graph Algorithm 2
Graph Algorithm 2
Jianxi Gao
3 2 2 2 3
5 4 3 4
5 5 1 1 4 2
1 0 1 5
Number of shortest paths
3 2 2 2 3
5 4 3 4
5 5 1 1 4 2
1 0 1 5
PATHS
A path is a sequence of nodes in which each node is adjacent to the next one.
Pi0,in of length n between nodes i0 and in is an ordered collection of n+1 nodes and n links
Procedure BFS(𝐺, 𝑠)
Input: Graph 𝐺 = (𝑉, 𝐸), directed or undirected; vertex 𝑠 ∈ 𝑉.
for all u ∈ 𝑉;
𝑑𝑖𝑠𝑡 𝑢 = ∞
𝑑𝑖𝑠𝑡 𝑠 = 0
𝑄 = [𝑠] (queue only contains 𝑠)
while 𝑄 is not empty
𝑢 = 𝑒𝑗𝑒𝑐𝑡(𝑄) (Remove the first element 𝑢 from 𝑄)
for all edges 𝑢, 𝑣 ∈ 𝐸:
if 𝑑𝑖𝑠𝑡 𝑣 = ∞:
𝑖𝑛𝑗𝑒𝑐𝑡 𝑄, 𝑣
𝑑𝑖𝑠𝑡 𝑣 = 𝑑𝑖𝑠𝑡 𝑢 + 1
Meeting princess
Meeting princes
1. 𝑢 = 𝐺,
2. 𝐺, 𝐷 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐷 = ∞
3. 𝑄 = 𝐷 , 𝑑𝑖𝑠𝑡 𝐷 = 𝑑𝑖𝑠𝑡 𝐺 + 1 = 1
4. 𝐺, 𝐻 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐻 = ∞
5. 𝑄 = 𝐷, 𝐻 , 𝑑𝑖𝑠𝑡 𝐻 = 𝑑𝑖𝑠𝑡 𝐺 + 1 = 1
Meeting princess BFS
5. 𝑄 = 𝐷, 𝐻 , 𝑑𝑖𝑠𝑡 𝐻 = 𝑑𝑖𝑠𝑡 𝐺 + 1 = 1
6. 𝑢 = 𝐷,
7. 𝐷, 𝐴 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐴 = ∞
8. 𝑄 = 𝐻, 𝐴 , 𝑑𝑖𝑠𝑡 𝐴 = 𝑑𝑖𝑠𝑡 𝐷 + 1 = 2
9. 𝐷, 𝐻 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐻 = 1 →continue
10.𝑢 = 𝐻
11. 𝐻, 𝐷 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐷 = 1 →continue
12. 𝑄 = 𝐴
Meeting princess BFS
12. 𝑄 = 𝐴
13. 𝑢 = 𝐴,
14. 𝐴, 𝐵 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐵 = ∞
15. 𝑄 = 𝐵 , 𝑑𝑖𝑠𝑡 𝐵 = 𝑑𝑖𝑠𝑡 𝐴 + 1 = 3
16. 𝐴, 𝐶 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐶 = ∞
17. 𝑄 = 𝐵, 𝐶 , 𝑑𝑖𝑠𝑡 𝐶 = 𝑑𝑖𝑠𝑡 𝐴 + 1 = 3
18. 𝐴, 𝐷 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐷 = 1 →continue
19. 𝑄 = 𝐵, 𝐶
Meeting princess BFS
20. 𝑢 = 𝐵,
21. 𝐵, 𝐸 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐸 = ∞
22. 𝑄 = 𝐶, 𝐸 , 𝑑𝑖𝑠𝑡 𝐸 = 𝑑𝑖𝑠𝑡 𝐵 + 1 = 4
23. 𝐵, 𝐹 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐹 = ∞
24. 𝑄 = 𝐶, 𝐸, 𝐹 , 𝑑𝑖𝑠𝑡 𝐹 = 𝑑𝑖𝑠𝑡 𝐵 + 1 = 4
25. 𝐵, 𝐴 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐴 = 3 →continue
26. 𝑄 = 𝐶, 𝐸, 𝐹
Meeting princess BFS
26. 𝑄 = 𝐶, 𝐸, 𝐹
27. 𝑢 = 𝐶,
28. 𝐶, 𝐴 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐴 = 3 →continue
30. 𝑄 = 𝐸, 𝐹
Meeting princess BFS
30. 𝑄 = 𝐸, 𝐹
31. 𝑢 = 𝐸,
32. 𝐸, 𝐼 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐼 = ∞
33. 𝑄 = 𝐹, 𝐼 , 𝑑𝑖𝑠𝑡 𝐼 = 𝑑𝑖𝑠𝑡 𝐸 + 1 = 5
34. 𝐸, 𝐽 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐽 = ∞
35. 𝑄 = 𝐹, 𝐼, 𝐽 , 𝑑𝑖𝑠𝑡 𝐽 = 𝑑𝑖𝑠𝑡 𝐸 + 1 = 5
(1) Is it correct?
Are you kidding me?
(2) Is it efficient?
𝑄 = 𝐵1 , 𝐵2 , … , 𝐵𝑟 d=2
𝑄 = [𝐶1, 𝐶2 , … , 𝐶𝑡 ] d=3
Is it efficient?
I
D 3
2
B G 1
H
C
Undirected links :
coauthorship links
Actor network
protein interactions
An Example of UNDIRECTED Graphs
kA =1
Question for a graph 𝐺 = (𝑉, 𝐸):
kB = 4
æ0 1 0 1ö N
4 ç ÷ ki = åA ij
ç 1 0 0 1÷ j =1
Aij =
ç0 0 0 1÷ N
3 ç ÷ k j = å Aij
2
è1 1 1 0ø i=1
1
Aij = A ji N N
L = å ki = å Aij
1 1
Aii = 0 2 i=1 2 ij
Is it efficient?
3. 𝑄 = 𝐷 , 𝑑𝑖𝑠𝑡 𝐷 = 𝑑𝑖𝑠𝑡 𝐺 + 1 = 1
4. 𝐺, 𝐻 ∈ 𝑬, 𝐚𝐧𝐝 𝑑𝑖𝑠𝑡 𝐻 = ∞
5. 𝑄 = 𝐷, 𝐻 , 𝑑𝑖𝑠𝑡 𝐻 = 𝑑𝑖𝑠𝑡 𝐺 + 1 = 1
Average Degree
COMPLETE GRAPH
L << Lmax
or
<k> <<N-1.
Part II
UNDIRECTED NETWORKS
Notation. G = (V,E)
V = nodes (or vertices).
E = edges (or arcs) between pairs of nodes
Captures pairwise relationship between objects.
Network size parameters: n = |V |, m = |E|.
L
A
F M V = {A,B,C,D,E,F,G,H,I, L,M}
I
E {A-D,B-B,B-D,C-D,C-D,D-F,F-G,G-H,H-I,L-M}
D
B G
H
A-D is the same as D-A
C
n = 10, m = 10.
Some graph applications
45
Tress
root r
parent of v
child of v
46
GUI tree
Reference: http://java.sun.com/docs/books/tutorial/uiswing/overview/anatomy.html
47
COVID-19
• It spreads so fast because of huge human
mobility
• The spreading process seems unpredictable
COVID-19
49
COVID-19: travel restrictions
https://science.rpi.edu/computer-science/news/new-covid-19-
50 model-reveals-need-better-travel-restriction-implementation
International airline network
A network of numbers.
89 72 14 4
98
59 45 20 0
95