-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGraphCenter.py
47 lines (41 loc) · 1.11 KB
/
GraphCenter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Center: design a linear-time algorithm to find a
vertex such that its maximum distance from any
other vertex is minimized.
"""
from BreadthFirstSearch import BreadthFirstSearch as BFS
from Cycle import Cycle
from ConnectedComponent import CC
class Center(object):
def __init__(self, G):
if self._hasCycle(G):
print "graph must be acyclic"
return
if not G.E():
print "graph has no edges"
return
if not self._isConnected(G):
print 'graph is not connected'
return
self._maxdistances = [0 for _ in range(G.V())]
self._findCenter(G)
def _isConnected(self, G):
cc = CC(G)
return cc.count() == 1
def _hasCycle(self, G):
cycle = Cycle(G)
return cycle.hasCycle()
def _findCenter(self, G):
for source in range(G.V()):
maxdist = 0
abfs = BFS(G, source)
for v in range(G.V()):
if abfs.hasPathTo(v):
dist = abfs.distTo(v)
if maxdist < dist:
maxdist = dist
self._maxdistances[source] = maxdist
def center(self):
"return vertex with shortest max distance to other vertices"
minmaxdist = min(self._maxdistances)
return self._maxdistances.index(minmaxdist)