def validateGraph(self, graph): components = StronglyConnectedComponents.stronglyConnectedComponents(graph) #print "************" #print graph #print components levels = {} for ix in range(len(components)): for c in components[ix]: levels[c] = ix def reachable(x): dirty = set([x]) reachable = set([]) while dirty: node = dirty.pop() if node not in reachable: reachable.add(node) for downstream in graph[node]: dirty.add(downstream) return reachable for n in graph: r = reachable(n) for reachableChild in r: self.assertTrue(levels[n] >= levels[reachableChild], (n,levels[n],reachableChild,levels[reachableChild])) for child in graph[n]: if n in reachable(child): self.assertTrue(levels[child] == levels[n]) else: self.assertTrue(levels[child] < levels[n])
def test_stronglyConnectedComponents_1(self): graph = { 0: [1,2], 1: [0,2], 2: [] } scc = StronglyConnectedComponents.stronglyConnectedComponents(graph) self.assertEqual( scc, [(2,), (1, 0)] )
def convertObjectWithDependencies(self, objectId, dependencyGraph, objectIdToObjectDefinition): stronglyConnectedComponents = \ StronglyConnectedComponents.stronglyConnectedComponents( dependencyGraph ) for stronglyConnectedComponent in stronglyConnectedComponents: self.convertStronglyConnectedComponent(dependencyGraph, stronglyConnectedComponent, objectIdToObjectDefinition) return self.convertedValues[objectId]
def test_stronglyConnectedComponents_2(self): graph = { 0: [4], 1: [0], 2: [1, 3], 3: [2], 4: [1], 5: [4, 1, 6], 6: [5, 2], 7: [6, 7] } scc = StronglyConnectedComponents.stronglyConnectedComponents(graph) self.assertEqual(scc, [(1, 4, 0), (3, 2), (6, 5), (7, )])
def _convertListMembers(self, listId, dependencyGraph, objectIdToObjectDefinition): stronglyConnectedComponents = \ StronglyConnectedComponents.stronglyConnectedComponents( dependencyGraph ) self._assertContainerDoesNotReferenceItself( listId, dependencyGraph, stronglyConnectedComponents) for stronglyConnectedComponent in stronglyConnectedComponents[:-1]: self.convertStronglyConnectedComponent(dependencyGraph, stronglyConnectedComponent, objectIdToObjectDefinition)
def convertObjectWithDependencies(self, objectId, dependencyGraph, objectIdToObjectDefinition): stronglyConnectedComponents = \ StronglyConnectedComponents.stronglyConnectedComponents( dependencyGraph ) for stronglyConnectedComponent in stronglyConnectedComponents: self.convertStronglyConnectedComponent( dependencyGraph, stronglyConnectedComponent, objectIdToObjectDefinition ) return self.convertedValues[objectId]
def test_stronglyConnectedComponents_2(self): graph = { 0: [4], 1: [0], 2: [1, 3], 3: [2], 4: [1], 5: [4,1,6], 6: [5, 2], 7: [6, 7] } scc = StronglyConnectedComponents.stronglyConnectedComponents(graph) self.assertEqual( scc, [(1, 4, 0), (3, 2), (6, 5), (7,)] )
def _convertListMembers(self, listId, dependencyGraph, objectIdToObjectDefinition): stronglyConnectedComponents = \ StronglyConnectedComponents.stronglyConnectedComponents( dependencyGraph ) self._assertContainerDoesNotReferenceItself( listId, dependencyGraph, stronglyConnectedComponents ) for stronglyConnectedComponent in stronglyConnectedComponents[:-1]: self.convertStronglyConnectedComponent( dependencyGraph, stronglyConnectedComponent, objectIdToObjectDefinition )
def validateGraph(self, graph): components = StronglyConnectedComponents.stronglyConnectedComponents( graph) #print "************" #print graph #print components levels = {} for ix in range(len(components)): for c in components[ix]: levels[c] = ix def reachable(x): dirty = set([x]) reachable = set([]) while dirty: node = dirty.pop() if node not in reachable: reachable.add(node) for downstream in graph[node]: dirty.add(downstream) return reachable for n in graph: r = reachable(n) for reachableChild in r: self.assertTrue( levels[n] >= levels[reachableChild], (n, levels[n], reachableChild, levels[reachableChild])) for child in graph[n]: if n in reachable(child): self.assertTrue(levels[child] == levels[n]) else: self.assertTrue(levels[child] < levels[n])