コード例 #1
0
    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))
コード例 #2
0
 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
コード例 #3
0
    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
コード例 #4
0
 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
コード例 #5
0
 def out_edges(self, vi):
     if self._invalid(vi):
         raise GraphError(str(vi) + " is not a valid vertex.")
     return self._mat[vi]
コード例 #6
0
 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]
コード例 #7
0
 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
コード例 #8
0
 def add_vertex(self):
     raise GraphError("Adj-Matrix does not support 'add_vertex'.")