CONNECTED
COMPONENTS AND
SHORTEST PATH
Kaushik Koneru
kkoneru1@student.gsu.edu
Topics
1. Connected Components
1.1 Adjacency Matrix
1.2 Connectivity Matrix
1.3 Matrix of Connected Components
2. All-Pairs Shortest Path
Elements of Connected Components
Graph can be represented a G = (V, E)
V – 𝑣0, 𝑣1, 𝑣2 … … 𝑣 𝑛−1 = 𝑠𝑒𝑡 𝑜𝑓 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠 = 𝑛 𝑁𝑜𝑑𝑒𝑠
E - {𝐸0, 𝐸1, 𝐸2 … … 𝐸 𝑚−1} = 𝑠𝑒𝑡 𝑜𝑓 𝐸𝑑𝑔𝑒𝑠 = 𝑚 𝐸𝑑𝑔𝑒𝑠
Adjacency Matrix ( n x n )
𝑎𝑖𝑗 =
1 𝑖𝑓 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 𝑎𝑟𝑒 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑒𝑑
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Computing the Connectivity Matrix
Connectivity Matrix: It represents a n x n Matrix C of a graph G with n vertices. Such that
𝐶𝑖𝑗 =
1 𝑖𝑓𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑒𝑥𝑖𝑠𝑡 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Input  Adjacency Matrix A
Adjacency Matrix (A) ----Boolean Matrix Multiplication  Connectivity Matrix (C)
Boolean Matrix Multiplication
1.) Every Entry in Boolean Matrix is binary either 0 or 1
2.) Boolean Operations are Logical AND & Logical OR
Difference between Matrix Multiplication and Boolean Matrix Multiplication are
Logical AND replaces Multiplication
Logical OR replaces Addition
Logical AND  0 and 0 = 0; 0 and 1 = 0; 1 and 0 = 0; 1 and 1 = 1
Logical OR  0 and 0 = 0; 0 and 1 = 1; 1 and 0 = 1; 1 and 1 = 1
Steps to compute Connectivity Matrix
1. Generate Matrix B fromA (Adjacency Matrix)
– 𝑏𝑗𝑗 = 1 𝑤ℎ𝑒𝑟𝑒 0 < 𝑗 < 𝑛
– So,
• For all 0 ≤ 𝑗, 𝑘 ≤ 𝑛 − 1
– 𝑏𝑗𝑘 =
𝑎𝑗𝑘 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑗 ! = 𝑘
1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
⇒
1 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑗 𝑡𝑜 𝑣 𝑘 𝑤𝑖𝑡ℎ 𝑙𝑒𝑛𝑔𝑡ℎ 𝑙𝑒𝑠𝑠 𝑡ℎ𝑎𝑛 2
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
0 1 0
1 0 1
0 1 0
→
1 1 0
1 1 1
0 1 1
A B
Steps to compute Connectivity Matrix
2. Calculate 𝐵2
𝑓𝑟𝑜𝑚 𝐵 using Boolean matrix multiplication
This represents paths of less than or equal to 2
So, 𝐵2
=
1 1 1
1 1 1
1 1 1
such that
So, Similarly 𝐵 𝑛
gives connectivity of graph with less than or equal to length n. In a graph G with n
vertices it cannot have length with more than n – 1. So,
C = 𝐵 𝑁−1
𝑏𝑗𝑘
2
=
1 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑗 𝑡𝑜 𝑣 𝑘 𝑤𝑖𝑡ℎ 𝑙𝑒𝑛𝑔𝑡ℎ 𝑙𝑒𝑠𝑠 𝑡ℎ𝑎𝑛 3
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
C = 𝐵 𝑛−1
Better way to compute 𝐵 𝑛−1
• 𝐵 𝑛−1
can be easily calculated by multiplying
– 𝐵2
∗ 𝐵2
− 𝐵4
– 𝐵4
∗ 𝐵4
− 𝐵8
– Multiply log(𝑛 − 1) 𝑡𝑖𝑚𝑒𝑠 𝑎𝑛𝑑 𝑔𝑒𝑛𝑒𝑟𝑎𝑡𝑒𝑑 𝑜𝑛𝑒 𝑖𝑠 𝐶
– If n-1 is not a power of 2 then C = 𝐵 𝑚
• Where M is smallest power of 2 larger than n-1
Example: For n = 4 to get C we need to calculate 𝐵4
Hypercube Connectivity
Hypercube Connectivity Analysis
• Step 1, 2 and 3.2 are of constant time.
• Step 3.1 required O(logn) time.This is iterated for log(𝑛 − 1)
– Therefore t(n) ~= 𝑂(log2
𝑛) with n^3 processors
– Cost C(n) = 𝑂(𝑛3
log2
𝑛)
Connected Components
• With Connectivity Graph ( C ) of G, we can construct D matrix such that
– djk =
𝑣 𝑘, 𝑝𝑟𝑜𝑣𝑖𝑑𝑒𝑑 𝑡ℎ𝑎𝑡 𝑐𝑗𝑘 = 1
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
– 𝑑𝑗𝑘 contains name of all the vertices to which 𝑣𝑗 is connected.
• Connected Components are then found by assigning each vertex to a constant as follows
– 𝑣𝑗 𝑖𝑠 𝑎𝑠𝑠𝑖𝑔𝑛𝑒𝑑 𝑡𝑜 𝑎 𝑐𝑜𝑚𝑝𝑜𝑛𝑒𝑛𝑡 𝑙 𝑖𝑓 𝑙 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡 𝑖𝑛𝑑𝑒𝑥 𝑓𝑜𝑟 𝑤ℎ𝑖𝑐ℎ 𝑑𝑗𝑙 = ! 0
Hypercube Connected Components
Analysis of Hypercube connected
components
• Step 1 Required O(log^2 n) time
• Step 2 and 3.2 takes constant time
• Overall running time
– T(n) = 𝑂(log2 𝑛)
– P(n) = 𝑛3
– C(n) = cost = 𝑂(𝑛3
log2
𝑛 )
Example
All-Pair Shortest Path
• Weighted Graph
– Weight is applied on each of the edges of the graph.
– Weight can be cost, time, reliability or probability...
– 𝑤𝑖𝑗 =
< 𝑣𝑎𝑙𝑖𝑑 𝑛𝑢𝑚𝑏𝑒𝑟 > 𝑖𝑓 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 𝑎𝑟𝑒 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑒𝑑
0 𝑜𝑟 ∞ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
All-Pair Shortest Problem
• All-Pair Shortest Problem is to calculate the shortest distance between any vertex to any vertex and
representing in a matrix D
• 𝑑𝑖𝑗 = 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 inV
• Condition: no negative circle should present
• By using Matrix Multiplication model we can get the shortest distance from one
vertex to other vertex
𝑑𝑖𝑗
𝑘
= 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑖 𝑡𝑜 𝑣𝑗 𝑡ℎ𝑎𝑡 𝑔𝑜𝑒𝑠 𝑡ℎ𝑟𝑜𝑢𝑔ℎ 𝑎𝑡𝑚𝑜𝑠𝑡 𝑘 − 1 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠
so, 𝑑𝑖𝑗
1
= 𝑤𝑖𝑗
• To get Shortest distance between all the vertices we need to calculate 𝑑𝑖𝑗
𝑛−1
All-Pair Shortest Path Matrix
Multiplication
Difference between Matrix Multiplication and Boolean Matrix Multiplication are
Addition replaces Multiplication
Min replaces Addition
Calculating 𝑑𝑖𝑗
𝑘
• In order to compute 𝑑𝑖𝑗
𝑘
– 𝑑𝑖𝑗
𝑘
= min
𝑙
( 𝑑𝑖𝑙
𝑘
2
+ 𝑑𝑙𝑗
𝑘
2
)
Hypercube Shortest Paths
Analysis of Hypercube Shortest Paths
• Step 1 and 2.2 requires constant time
• There are log(𝑛 − 1) 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑜𝑓 2.1each of O(logn)
• So, 𝑡 𝑛 = 𝑂(log2
𝑛)
𝑝 𝑛 = 𝑛3
𝐶 𝑛 = 𝑂(𝑛3
log2
𝑛 )

Connected components and shortest path

  • 1.
    CONNECTED COMPONENTS AND SHORTEST PATH KaushikKoneru kkoneru1@student.gsu.edu
  • 2.
    Topics 1. Connected Components 1.1Adjacency Matrix 1.2 Connectivity Matrix 1.3 Matrix of Connected Components 2. All-Pairs Shortest Path
  • 3.
    Elements of ConnectedComponents Graph can be represented a G = (V, E) V – 𝑣0, 𝑣1, 𝑣2 … … 𝑣 𝑛−1 = 𝑠𝑒𝑡 𝑜𝑓 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠 = 𝑛 𝑁𝑜𝑑𝑒𝑠 E - {𝐸0, 𝐸1, 𝐸2 … … 𝐸 𝑚−1} = 𝑠𝑒𝑡 𝑜𝑓 𝐸𝑑𝑔𝑒𝑠 = 𝑚 𝐸𝑑𝑔𝑒𝑠 Adjacency Matrix ( n x n ) 𝑎𝑖𝑗 = 1 𝑖𝑓 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 𝑎𝑟𝑒 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑒𝑑 0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
  • 6.
    Computing the ConnectivityMatrix Connectivity Matrix: It represents a n x n Matrix C of a graph G with n vertices. Such that 𝐶𝑖𝑗 = 1 𝑖𝑓𝑡ℎ𝑒𝑟𝑒 𝑖𝑠 𝑎 𝑝𝑎𝑡ℎ 𝑒𝑥𝑖𝑠𝑡 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 Input  Adjacency Matrix A Adjacency Matrix (A) ----Boolean Matrix Multiplication  Connectivity Matrix (C)
  • 7.
    Boolean Matrix Multiplication 1.)Every Entry in Boolean Matrix is binary either 0 or 1 2.) Boolean Operations are Logical AND & Logical OR Difference between Matrix Multiplication and Boolean Matrix Multiplication are Logical AND replaces Multiplication Logical OR replaces Addition Logical AND  0 and 0 = 0; 0 and 1 = 0; 1 and 0 = 0; 1 and 1 = 1 Logical OR  0 and 0 = 0; 0 and 1 = 1; 1 and 0 = 1; 1 and 1 = 1
  • 8.
    Steps to computeConnectivity Matrix 1. Generate Matrix B fromA (Adjacency Matrix) – 𝑏𝑗𝑗 = 1 𝑤ℎ𝑒𝑟𝑒 0 < 𝑗 < 𝑛 – So, • For all 0 ≤ 𝑗, 𝑘 ≤ 𝑛 − 1 – 𝑏𝑗𝑘 = 𝑎𝑗𝑘 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑗 ! = 𝑘 1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 ⇒ 1 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑗 𝑡𝑜 𝑣 𝑘 𝑤𝑖𝑡ℎ 𝑙𝑒𝑛𝑔𝑡ℎ 𝑙𝑒𝑠𝑠 𝑡ℎ𝑎𝑛 2 0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 0 1 0 1 0 1 0 1 0 → 1 1 0 1 1 1 0 1 1 A B
  • 9.
    Steps to computeConnectivity Matrix 2. Calculate 𝐵2 𝑓𝑟𝑜𝑚 𝐵 using Boolean matrix multiplication This represents paths of less than or equal to 2 So, 𝐵2 = 1 1 1 1 1 1 1 1 1 such that So, Similarly 𝐵 𝑛 gives connectivity of graph with less than or equal to length n. In a graph G with n vertices it cannot have length with more than n – 1. So, C = 𝐵 𝑁−1 𝑏𝑗𝑘 2 = 1 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑗 𝑡𝑜 𝑣 𝑘 𝑤𝑖𝑡ℎ 𝑙𝑒𝑛𝑔𝑡ℎ 𝑙𝑒𝑠𝑠 𝑡ℎ𝑎𝑛 3 0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 C = 𝐵 𝑛−1
  • 10.
    Better way tocompute 𝐵 𝑛−1 • 𝐵 𝑛−1 can be easily calculated by multiplying – 𝐵2 ∗ 𝐵2 − 𝐵4 – 𝐵4 ∗ 𝐵4 − 𝐵8 – Multiply log(𝑛 − 1) 𝑡𝑖𝑚𝑒𝑠 𝑎𝑛𝑑 𝑔𝑒𝑛𝑒𝑟𝑎𝑡𝑒𝑑 𝑜𝑛𝑒 𝑖𝑠 𝐶 – If n-1 is not a power of 2 then C = 𝐵 𝑚 • Where M is smallest power of 2 larger than n-1 Example: For n = 4 to get C we need to calculate 𝐵4
  • 11.
  • 12.
    Hypercube Connectivity Analysis •Step 1, 2 and 3.2 are of constant time. • Step 3.1 required O(logn) time.This is iterated for log(𝑛 − 1) – Therefore t(n) ~= 𝑂(log2 𝑛) with n^3 processors – Cost C(n) = 𝑂(𝑛3 log2 𝑛)
  • 13.
    Connected Components • WithConnectivity Graph ( C ) of G, we can construct D matrix such that – djk = 𝑣 𝑘, 𝑝𝑟𝑜𝑣𝑖𝑑𝑒𝑑 𝑡ℎ𝑎𝑡 𝑐𝑗𝑘 = 1 0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 – 𝑑𝑗𝑘 contains name of all the vertices to which 𝑣𝑗 is connected. • Connected Components are then found by assigning each vertex to a constant as follows – 𝑣𝑗 𝑖𝑠 𝑎𝑠𝑠𝑖𝑔𝑛𝑒𝑑 𝑡𝑜 𝑎 𝑐𝑜𝑚𝑝𝑜𝑛𝑒𝑛𝑡 𝑙 𝑖𝑓 𝑙 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑚𝑎𝑙𝑙𝑒𝑠𝑡 𝑖𝑛𝑑𝑒𝑥 𝑓𝑜𝑟 𝑤ℎ𝑖𝑐ℎ 𝑑𝑗𝑙 = ! 0
  • 14.
  • 15.
    Analysis of Hypercubeconnected components • Step 1 Required O(log^2 n) time • Step 2 and 3.2 takes constant time • Overall running time – T(n) = 𝑂(log2 𝑛) – P(n) = 𝑛3 – C(n) = cost = 𝑂(𝑛3 log2 𝑛 )
  • 16.
  • 17.
    All-Pair Shortest Path •Weighted Graph – Weight is applied on each of the edges of the graph. – Weight can be cost, time, reliability or probability... – 𝑤𝑖𝑗 = < 𝑣𝑎𝑙𝑖𝑑 𝑛𝑢𝑚𝑏𝑒𝑟 > 𝑖𝑓 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 𝑎𝑟𝑒 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑒𝑑 0 𝑜𝑟 ∞ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
  • 18.
    All-Pair Shortest Problem •All-Pair Shortest Problem is to calculate the shortest distance between any vertex to any vertex and representing in a matrix D • 𝑑𝑖𝑗 = 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑣𝑖 𝑎𝑛𝑑 𝑣𝑗 inV • Condition: no negative circle should present • By using Matrix Multiplication model we can get the shortest distance from one vertex to other vertex 𝑑𝑖𝑗 𝑘 = 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑙𝑒𝑛𝑔𝑡ℎ 𝑓𝑟𝑜𝑚 𝑣𝑖 𝑡𝑜 𝑣𝑗 𝑡ℎ𝑎𝑡 𝑔𝑜𝑒𝑠 𝑡ℎ𝑟𝑜𝑢𝑔ℎ 𝑎𝑡𝑚𝑜𝑠𝑡 𝑘 − 1 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠 so, 𝑑𝑖𝑗 1 = 𝑤𝑖𝑗 • To get Shortest distance between all the vertices we need to calculate 𝑑𝑖𝑗 𝑛−1
  • 19.
    All-Pair Shortest PathMatrix Multiplication Difference between Matrix Multiplication and Boolean Matrix Multiplication are Addition replaces Multiplication Min replaces Addition
  • 20.
    Calculating 𝑑𝑖𝑗 𝑘 • Inorder to compute 𝑑𝑖𝑗 𝑘 – 𝑑𝑖𝑗 𝑘 = min 𝑙 ( 𝑑𝑖𝑙 𝑘 2 + 𝑑𝑙𝑗 𝑘 2 )
  • 21.
  • 22.
    Analysis of HypercubeShortest Paths • Step 1 and 2.2 requires constant time • There are log(𝑛 − 1) 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠 𝑜𝑓 2.1each of O(logn) • So, 𝑡 𝑛 = 𝑂(log2 𝑛) 𝑝 𝑛 = 𝑛3 𝐶 𝑛 = 𝑂(𝑛3 log2 𝑛 )