lab12

17
Lab12 closeness and betweenness

Upload: chanda-riley

Post on 30-Dec-2015

27 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lab12

Lab12 closeness and betweenness

Page 2: Lab12

GoalTask 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.

Page 3: Lab12

ClosenessThe 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.

Page 4: Lab12

Example of closenessl1 =Cost(shortestPath(0,1))=2l2=Cost (shortestPath(0,2))=7l3=Cost (shortestPath(0,3))=8C(0)=max(l1,l2,l3)=max(2,7,8)=8

0

3

1

2

25

10 1

Page 5: Lab12

Example of closenessl1 =Cost(shortestPath(1,0))=2l2=Cost (shortestPath(1,2))=5l3=Cost (shortestPath(1,3))=6C(1)=max(l1,l2,l3)=max(2,5,6)=6

0

3

1

2

25

10 1

Page 6: Lab12

Example of closenessl1 =Cost(shortestPath(2,0))=7l2=Cost (shortestPath(2,1))=5l3=Cost (shortestPath(2,3))=1C(2)=max(l1,l2,l3)=max(7,5,1)=7

0

3

1

2

25

10 1

Page 7: Lab12

Example of closenessl1 =Cost(shortestPath(3,0))=8l2=Cost (shortestPath(3,1))=6l3=Cost (shortestPath(3,2))=1C(3)=max(l1,l2,l3)=max(8,6,1)=8

0

3

1

2

25

10 1

Page 8: Lab12

Algorithm:Let G = (V, E) be an undirected weighted graph Let c(i) = 0For 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.

Page 9: Lab12

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

Page 10: Lab12

Example of betweennessShortestPath(0,1)=0,1ShortestPath(0,2)=0,1,2ShortestPath(0,3)=0,1,2,3ShortestPath(1,2)=1,2ShortestPath(1,3)=1,2,3ShortestPath(2,3)=2,3betweenness[1]=2betweenness[2]=2

0

3

1

2

2 5

10 1

Page 11: Lab12

Floyd-Warshall algorithmThis 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]

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]

Page 12: Lab12

Example of Floyd-Warshall algorithmfor 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

Page 13: Lab12

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

New Matrix G

Interspace A 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

Old Matrix G

Page 14: Lab12

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

New Matrix G

Interspace B 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

Old Matrix G

Page 15: Lab12

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

New Matrix G

Interspace C 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

Old Matrix G

Page 16: Lab12

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

New Matrix G

Interspace D 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

Old Matrix G

Page 17: Lab12

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

New Matrix G

Interspace E 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

Old Matrix G