Guest User

Untitled

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. class Graph:
  2. def __init__(self):
  3. self.adj_list = []
  4.  
  5. def adj_list(self):
  6. return self.adj_list
  7.  
  8. def adj_addnode(self):
  9. self.adj_list.append([])
  10.  
  11. def adj_addedge(self,srcNode,goalNode):
  12. self.adj_list[srcNode].append(goalNode)
  13.  
  14. def read_adjmatrix():
  15. adjmatrix = []
  16. f = open("matrix.txt","r")
  17. line=f.readline()
  18. while line:
  19. line=line.split(',')
  20. for index, item in enumerate(line):
  21. line[index] = eval(item)
  22. adjmatrix.append(line)
  23. line = f.readline()
  24. f.close()
  25.  
  26. return adjmatrix
  27.  
  28. def main():
  29. adjMatrix = read_adjmatrix()
  30. G = Graph()
  31. for i in range(len(adjMatrix)):
  32. G.adj_addnode()
  33. for j in range(len(adjMatrix)):
  34. if(adjMatrix[i][j]==1):
  35. G.adj_addedge(i,j)
  36. adjList = G.adj_list
  37. file = open('list.txt','w')
  38. for i in range(len(adjList)):
  39. for j in range(len(adjList[i])):
  40. if(j<(len(adjList[i])-1)):
  41. file.write(str(adjList[i][j])+',')
  42. else:
  43. file.write(str(adjList[i][j]))
  44. file.write('\n')
  45. file.close()
  46.  
  47. if __name__ == '__main__':
  48. main()
Add Comment
Please, Sign In to add comment