Skip to content

Instantly share code, notes, and snippets.

@VitorDiToro
Last active September 20, 2016 12:55
Show Gist options
  • Save VitorDiToro/d459a1a35f761d076c8d24b2552f5ab8 to your computer and use it in GitHub Desktop.
Save VitorDiToro/d459a1a35f761d076c8d24b2552f5ab8 to your computer and use it in GitHub Desktop.
Caixeiro_Guloso.py
def guloso(mat, start):
notInGraph = list(range(len(mat)))
notInGraph.remove(start)
pos = start
comprimento = len(mat)
distanciaTotal = 0
while(notInGraph != []):
dist = float('inf')
for i in range(comprimento):
if ((dist > mat[pos][i]) & (i in notInGraph)):
dist = mat[pos][i]
destino = i
distanciaTotal += dist
notInGraph.remove(destino)
pos = destino
distanciaTotal += mat[pos][start] #retorna ao vertice inicial
print("Guloso -> Distancia =",distanciaTotal)
def main():
mat = [[float('inf'),3,7,5],
[3,float('inf'),4,8],
[7,4,float('inf'),3],
[5,8,3,float('inf')]]
start = 0
guloso(mat, start)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment