170 likes | 337 Views
Lab12. 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
E N D
Lab12 closeness and betweenness
Goal • Task 1. • Closeness using Djikstra's Single-Source Shortest Path algorithm • Djikstra's algorithm is given. • Task 2. • Betweennessusing modified Floyd-Warshallalgorithms • Floyd-Warshallalgorithm 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 1 2 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 1 2 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 1 2 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 1 2 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 1 2 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-Warshallalgorithm for all vertices i in V do for all vertices j in V do G(i,j) = weight(i,j) A B C D EA 0 10 0 5 0B 10 0 5 5 10C 0 5 0 0 0D 5 5 0 0 20E 0 10 0 20 0 Matrix G
InterspaceA between any two nodes in hopes of finding a shorter path. A B C D EA 0 10 0 5 0B 10 0 5 5 10C 0 5 0 0 0D 5 5 0 0 20E 0 10 0 20 0 A B C D EA 0 10 0 5 0B 10 0 5 5 10C 0 5 0 0 0D 5 5 0 0 20E 0 10 0 20 0 Old Matrix G New Matrix G
InterspaceB between any two nodes in hopes of finding a shorter path. A B C D EA 0 10 0 5 0B 10 0 5 5 10C 0 5 0 0 0D 5 5 0 0 20E 0 10 0 20 0 A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 1015D 5 5 10 0 15E 20 10 1515 0 Old Matrix G New Matrix G
InterspaceC between any two nodes in hopes of finding a shorter path. A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 Old Matrix G New Matrix G
InterspaceD between any two nodes in hopes of finding a shorter path. A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 Old Matrix G New Matrix G
InterspaceE between any two nodes in hopes of finding a shorter path. A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 A B C D EA 0 10 15 5 20B 10 0 5 5 10C 15 5 0 10 15D 5 5 10 0 15E 20 10 15 15 0 Old Matrix G New Matrix G