def test_add_vertice(self): d = Digraph() self.assertEquals(0, d.count_vertices()) d.add_vertice() self.assertEquals(1, d.count_vertices()) d.add_vertice() d.add_vertice() self.assertEquals(3, d.count_vertices())
def testCycle(self): d = Digraph(13) d.add_edge(4,2) d.add_edge(2,3) d.add_edge(3,2) d.add_edge(6,0) d.add_edge(0,1) d.add_edge(2,0) d.add_edge(11,12) d.add_edge(12,9) d.add_edge(9,10) d.add_edge(9,11) d.add_edge(7,9) d.add_edge(10,12) d.add_edge(11,4) d.add_edge(4,3) d.add_edge(3,5) d.add_edge(6,8) d.add_edge(8,6) d.add_edge(5,4) d.add_edge(0,5) d.add_edge(6,4) d.add_edge(6,9) d.add_edge(7,6) self.assertEquals(13, d.count_vertices()) finder = DirectedCycle(d) self.assertEquals([3,4,5,3], finder.cycle)
def test_add_edge_to_empty(self): d = Digraph() self.assertEquals(0, d.count_vertices()) d.add_vertice() d.add_vertice() d.add_vertice() d.add_edge(0, 1) d.add_edge(2, 0) self.assertEquals(2, d.count_edges())
class SymbolDigraph(object): """Like digraph but operates with strings, you can never remove edges""" def __init__(self,size=0): self.symboltable = {} self.names = [] self.digraph = Digraph(size) def add_vertice(self, edge_from): """Add named vertice""" index_from = self._check_index(edge_from) self._check_digraph_size(index_from) self.names[index_from] = edge_from def add_edge(self, edge_from, edge_to): """Add named edge link""" index_from = self._check_index(edge_from) index_to = self._check_index(edge_to) self._check_digraph_size(max(index_from, index_to)) self.digraph.add_edge(index_from, index_to) def _check_index(self, key): """if exists, returns id, otherwise adds a new one and also returns""" keyname = str(key) #print "_check_index", key, "len(keyname)", len(keyname) if key == None or not(len(keyname)): raise Exception("Edge name cannot be none") if self.index(keyname) == -1: index = len(self.symboltable) self.symboltable[keyname] = index self._check_names_size(index) self.names[index] = key return self.symboltable[keyname] def index(self, key): """returns internal index by key""" if self.symboltable.has_key(key): return self.symboltable[key] return -1 def _check_digraph_size(self, index): """resizes graph accordingly""" i = index - self.digraph.count_vertices() while i >= 0: self.digraph.add_vertice() i -= 1 def _check_names_size(self, index): """resizes graph accordingly""" i = index - len(self.names) while i >= 0: self.names.append(None) i -= 1 def count_vertices(self): """returns number of vertices (nodes)""" return self.digraph.count_vertices() def count_edges(self): """returns number of edges(links)""" return self.digraph.count_edges() def vertices(self): "Named vertices" return self.names def links(self, key): "Links from node" names = [] for linkid in self.digraph.links(self.index(key)): names.append(self.name(linkid)) return names def name(self, index): """Symbol by index""" return self.names[index]