2011-10-21 45 views
1

這裏是我的代碼::Python關聯矩陣 - 如何去除頂點?

class incidenceMatrix: 
    def __init__(self, vertexNumber): 
     self.matrix = [] 
     for k in range(0, vertexNumber): 
      self.matrix += [[]] 
      #print self.matrix 

    def showGraph(self): 
     i = 1 
     for row in self.matrix: 
      print i, row 
      i += 1 

    def isEdge(self, v1, v2): 
     i = 1 
     for row in self.matrix: 
      if i == v1: 
       r1 = row 
      if i == v2: 
       r2 = row 
      i += 1 
     print r1, r2 
     for x in range(len(r1)): 
      if r1[x] == r2[x] and r1[x] + r2[x] > 0: 
       return True 
     return False 

    def addEdge(self, v1, v2): 
     i = 1 
     for row in self.matrix: 
      if i == v1: 
       row.append(1) 
      elif i == v2: 
       row.append(1) 
      else: 
       row.append(0) 
      i += 1 

    def removeEdge(self, v1, v2): 
     i = 1 
     for row in self.matrix: 
      if i == v1: 
       r1 = row 
      if i == v2: 
       r2 = row 
      i += 1 
     for x in range(len(r1)): 
      if r1[x] == r2[x] and r1[x] + r2[x] > 0: 
       col = x 
       r1[x] = 0 
       r2[x] = 0 
     for row in self.matrix: 
      if i == v1: 
       row = r1 
      if i == v2: 
       row = r2 
      i += 1 
     for row in self.matrix: 
      row[col] = 'X' 
      row.remove('X') 

def removeVertex(self, id): 
    pass 

if __name__ == '__main__': 
    GrafIM = incidenceMatrix(5) #verticesNumber 
    GrafIM.addEdge(2,3) 
    GrafIM.addEdge(1,3) 
    GrafIM.addEdge(2,1) 
    GrafIM.addEdge(5,2) 
    print GrafIM.isEdge(2,4) 
    GrafIM.showGraph() 
    print "-------" 
    GrafIM.removeEdge(2,5) 
    GrafIM.showGraph() 

這是關聯矩陣 我有幾個問題:

1)如何刪除頂點 - 方法?

2)如何使python風格的代碼更多?見問題3)

3)我有方法中使用「i」增量嗎?也許這可以是別的東西來寫 - 但如何?

編輯:

我現在看到了如何刪除頂點。 我剛纔刪除列wher這個頂點有1

但整個時間等待你的問題的意見代碼質量

回答

1

這裏的要求removeVertex()方法你提出的要求。此外,該代碼被擰緊機位與任何()枚舉()拉鍊()

class incidenceMatrix: 
    def __init__(self, vertexNumber): 
     self.matrix = [[] for k in range(vertexNumber)] 

    def showGraph(self): 
     for i, row in enumerate(self.matrix, 1): 
      print i, row 

    def isEdge(self, v1, v2): 
     return any(x and y for x, y in zip(self.matrix[v1-1], self.matrix[v2-1])) 

    def addEdge(self, v1, v2): 
     for i, row in enumerate(self.matrix, 1): 
      row.append(int(v1==i or v2==i)) 

    def removeEdge(self, v1, v2): 
     num_edges = len(self.matrix[0]) 
     for j in range(num_edges): 
      if self.matrix[v1-1][j] and self.matrix[v2-1][j]: 
       for row in self.matrix: 
        del row[j] 
       return 
     raise Exception('Edge(%d, %d) not found' % (v1, v2)) 

    def removeVertex(self, v): 
     targetrow = self.matrix.pop(v-1) # fetch and delete the target vertex 
     for col, selector in reversed(list(enumerate(targetrow))): 
      if selector:     # find columns that had an edge on the target row 
       for row in self.matrix: 
        del row[col]   # remove that column (because the edge is gone) 

if __name__ == '__main__': 
    GrafIM = incidenceMatrix(5) #verticesNumber 
    GrafIM.addEdge(2,3) 
    GrafIM.addEdge(1,3) 
    GrafIM.addEdge(2,1) 
    GrafIM.addEdge(5,2) 
    print GrafIM.isEdge(2,4) 
    for pair in [(2,3), (1,3), (2,1), (5,2)]: 
     print GrafIM.isEdge(*pair) 
    GrafIM.showGraph() 
    print "-------" 
    GrafIM.removeEdge(2,5) 
    GrafIM.showGraph() 
    print "-------" 
    GrafIM.removeVertex(2) 
    GrafIM.showGraph()  

注意,如果你贊成放棄基於1的索引Python的從零開始的索引,可以簡化代碼位(除去, 1從*枚舉()和刪除索引查找的-1

此外,您可能需要使用字典考慮不同的表示那更適合可以使用稀疏結構,並且可以在一定程度上簡化/加速代碼。

1

的回答2和3是使用enumerate而不是遞增計數器。例如您showGraph方法:

def showGraph(self): 
    i = 1 
    for row in self.matrix: 
     print i, row 
     i += 1 

可以簡化爲:

def showGraph(self): 
    for i, row in enumerate(self.matrix, 1): 
     print i, row