Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Graph:
- def __init__(self):
- self.adj_list = []
- def adj_list(self):
- return self.adj_list
- def adj_addnode(self):
- self.adj_list.append([])
- def adj_addedge(self,srcNode,goalNode):
- self.adj_list[srcNode].append(goalNode)
- def read_adjmatrix():
- adjmatrix = []
- f = open("matrix.txt","r")
- line=f.readline()
- while line:
- line=line.split(',')
- for index, item in enumerate(line):
- line[index] = eval(item)
- adjmatrix.append(line)
- line = f.readline()
- f.close()
- return adjmatrix
- def main():
- adjMatrix = read_adjmatrix()
- G = Graph()
- for i in range(len(adjMatrix)):
- G.adj_addnode()
- for j in range(len(adjMatrix)):
- if(adjMatrix[i][j]==1):
- G.adj_addedge(i,j)
- adjList = G.adj_list
- file = open('list.txt','w')
- for i in range(len(adjList)):
- for j in range(len(adjList[i])):
- if(j<(len(adjList[i])-1)):
- file.write(str(adjList[i][j])+',')
- else:
- file.write(str(adjList[i][j]))
- file.write('\n')
- file.close()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment