Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- finishing_time = {}
- 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()
- #print(adjmatrix)
- return adjmatrix
- def matrix_to_list():
- adjMatrix = read_adjmatrix()
- #print(adjMatrix)
- G = Graph()
- for i in range(len(adjMatrix)):
- #print(len(adjMatrix))
- G.adj_addnode()
- for j in range(len(adjMatrix)):
- #print(len(adjMatrix))
- if(adjMatrix[i][j]==1):
- G.adj_addedge(i,j)
- adjList = G.adj_list
- #print(adjList)
- return adjList
- def list_to_reverse_graph():
- matrix = []
- adjlist = matrix_to_list()
- number_of_zero = len(adjlist)
- for i in range(number_of_zero):
- matrix.append([0]*number_of_zero)
- # print(matrix)
- for a in range(len(adjlist)):
- for b in range(len(adjlist[a])):
- matrix[adjlist[a][b]][a] = 1
- list1 = []
- for a in range(len(matrix)):
- list1.append([])
- for b in range(len(matrix)):
- if matrix[a][b] == 1:
- list1[a].append(b)
- dict1 ={}
- for k,v in enumerate(list1):
- dict1[k] =v
- #print(dict1)
- return dict1
- def topological_sort():
- #isrev=1 is reversed graph;isrerev=0 is original graph
- list1 = matrix_to_list()
- graph ={}
- for k,v in enumerate(list1):
- graph[k] =v
- is_visit = dict((node,False) for node in graph )
- order = []
- def dfs(graph,start_node):
- for end_node in graph[start_node]:
- if not is_visit[end_node]:
- is_visit[end_node] = True
- dfs(graph,end_node)
- order.append(start_node)
- for start_node in graph:
- if not is_visit[start_node]:
- is_visit[start_node] = True
- dfs(graph,start_node)
- #order.append(" ")
- return order
- def topological_reverse_sort():
- #isrev=1 is reversed graph;isrerev=0 is original graph
- graph = list_to_reverse_graph()
- order_list = topological_sort()
- #print(order_list)
- is_visit = dict((node,False) for node in graph )
- order = []
- def dfs(graph,start_node):
- for end_node in graph[start_node]:
- if not is_visit[end_node]:
- is_visit[end_node] = True
- dfs(graph,end_node)
- order.append(start_node)
- for i in range(len(order_list)-1,-1,-1):
- start_node=order_list[i]
- if not is_visit[start_node]:
- is_visit[start_node] = True
- dfs(graph,start_node)
- order.append(" ")
- #print(order)
- return order
- def sep_list():
- list1 = topological_reverse_sort()
- #print(list1)
- list2=[]
- c = 0
- a=0
- for i in range(len(list1)):
- if list1[i] == " " :
- list2.append( list1[a:i])
- c +=1
- a = i+1
- for i in range(len(list2)):
- list2[i].sort()
- #print(list2)
- return list2
- def main():
- a = sep_list()
- f=open("components.txt","w")
- for i in range(len(a)):
- for j in range(len(a[i])):
- if j >=len(a[i])-1:
- f.write(str(a[i][j]))
- else:
- f.write(str(a[i][j])+",")
- f.write("\n")
- f.close()
- main()
Add Comment
Please, Sign In to add comment