closeness and betweenness Goal Task 1. Closeness using Djikstra's Single-Source Shortest Path algorithm Djikstra's algorithm is given. Task 2. Betweenness using modified Floyd-Warshall algorithms Floyd-Warshall algorithm is given. Modify the algorithm to determine the betweenness. You do not have to use modified Floyd-Warshall algorithms. You can come up with your own algorithm. Closeness The closeness of a node is defined as the maximum of all the shortest path cost between it and all the other vertices in the graph. Example of closeness l1 =Cost(shortestPath(0,1))=2 l2=Cost (shortestPath(0,2))=7 l3=Cost (shortestPath(0,3))=8 C(0)=max(l1,l2,l3)=max(2,7,8)=8 2 1 5 0 2 10 1 3 Example of closeness l1 =Cost(shortestPath(1,0))=2 l2=Cost (shortestPath(1,2))=5 l3=Cost (shortestPath(1,3))=6 C(1)=max(l1,l2,l3)=max(2,5,6)=6 2 1 5 0 2 10 1 3 Example of closeness l1 =Cost(shortestPath(2,0))=7 l2=Cost (shortestPath(2,1))=5 l3=Cost (shortestPath(2,3))=1 C(2)=max(l1,l2,l3)=max(7,5,1)=7 2 1 5 0 2 10 1 3 Example of closeness l1 =Cost(shortestPath(3,0))=8 l2=Cost (shortestPath(3,1))=6 l3=Cost (shortestPath(3,2))=1 C(3)=max(l1,l2,l3)=max(8,6,1)=8 2 1 5 0 2 10 1 3 Algorithm: Let G = (V, E) be an undirected weighted graph Let c(i) = 0 For all vertices j in V Let p = shortestPath(i, j) if length(p)>c(i) Let c(i) = length(p) Note: Use Djikstra's Single-Source Shortest Path algorithm to determine the shortest path cost. Betweenness Let G = (V, E) be an undirected weighted graph For all vertices a, b, in V where a != b Let p = shortestPath(a, b) For all vertices u in p where u != a and u != b betweenness[u] = betweenness[u] + 1 Example of betweenness ShortestPath(0,1)=0,1 ShortestPath(0,2)=0,1,2 ShortestPath(0,3)=0,1,2,3 ShortestPath(1,2)=1,2 ShortestPath(1,3)=1,2,3 ShortestPath(2,3)=2,3 betweenness[1]=2 betweenness[2]=2 2 1 5 0 2 10 1 3 Floyd-Warshall algorithm This algorithm calculates the length of the shortest path between all nodes of a graph in O(V3) time. Note that it doesn’t actually find the paths, only their lengths. G[j,i] is the shortest distance so far from i to j. for all vertices i in V do for all vertices j in V do G(i,j) = weight(i,j) for all vertices i in V do for all vertices j in V do if (G[j,i] > 0) for all vertices k in V do if (G[i,k] > 0) if (G[j,k] == 0) or (G[j,k] > G[j,i]+G[i,k]) G[j,k] = G[j,i]+G[i,k] Example of Floyd-Warshall algorithm ABCDE A 0 10 0 5 0 B 10 0 5 5 10 C05000 D 5 5 0 0 20 E 0 10 0 20 0 Matrix G for all vertices i in V do for all vertices j in V do G(i,j) = weight(i,j) Interspace A between any two nodes in hopes of finding a shorter path. ABCDE A 0 10 0 5 0 B 10 0 5 5 10 C05000 D 5 5 0 0 20 E 0 10 0 20 0 Old Matrix G ABCDE A 0 10 0 5 0 B 10 0 5 5 10 C05000 D 5 5 0 0 20 E 0 10 0 20 0 New Matrix G Interspace B between any two nodes in hopes of finding a shorter path. ABCDE A 0 10 0 5 0 B 10 0 5 5 10 C05000 D 5 5 0 0 20 E 0 10 0 20 0 Old Matrix G ABC DE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 New Matrix G Interspace C between any two nodes in hopes of finding a shorter path. ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 Old Matrix G ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 New Matrix G Interspace D between any two nodes in hopes of finding a shorter path. ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 Old Matrix G ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 New Matrix G Interspace E between any two nodes in hopes of finding a shorter path. ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 Old Matrix G ABCDE A 0 10 15 5 20 B 10 0 5 5 10 C 15 5 0 10 15 D 5 5 10 0 15 E 20 10 15 15 0 New Matrix G