這裏是我的代碼::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
但整個時間等待你的問題的意見代碼質量