def add_edge(self, vi, vj, val=1): if self._vnum == 0: raise GraphError("Cannot add edge tp empty graph.") if self._invalid(vi) or self._invalid(vj): raise GraphError(str(vi) + " or " + str(vj) + " is not a valid vertex.") row = self._mat[vi] i = 0 while i < len(row): if row[i][0] == vj: self._mat[vi][i] = (vj, val) return if row[i][0] > vj: break i += 1 self._mat[vi].insert((i, val))
def __init__(self, mat, unconn=0): vnum = len(mat) for x in mat: if len(x) != vnum: raise GraphError("Argument for 'Graph'.") self._mat = [mat[i][:] for i in range(vnum)] self._unconn = unconn self._vnum = vnum
def get_edge(self, vi, vj): if self._invalid(vi) or self._invalid(vj): raise GraphError(str(vi) + " or " + str(vj) + " is not a valid vertex.") for i, val in self._mat[vi]: if i == vj: return val return self._unconn
def __init__(self, mat=[], unconn=0): vnum = len(mat) for x in mat: if len(x) != vnum: raise GraphError("Argument for 'Graph'.") self._mat = [Graph._out_edges(mat[i], unconn) for i in range(vnum)] self._vnum = vum self._unconn = unconn
def out_edges(self, vi): if self._invalid(vi): raise GraphError(str(vi) + " is not a valid vertex.") return self._mat[vi]
def get_edge(self, vi, vj): if self._invalid(vi) or self._invalid(vj): raise GraphError(str(vi) + " or " + str(vj) + " is not a valid vertex.") return self._mat[vi][vj]
def add_edge(self, vi, vj, val=1): if self._invalid(vi) or self._invalid(vj): raise GraphError(str(vi) + " or " + str(vj) + " is not a valid vertex.") self._mat[vi][vj] = val
def add_vertex(self): raise GraphError("Adj-Matrix does not support 'add_vertex'.")