Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, data):
- self.data = data
- self.neighbors = []
- self.visited = False
- def add_neighbor(self, neighbor):
- self.neighbors.append(neighbor)
- def set_visited(self, visited):
- self.visited = visited
- def get_data(self):
- return self.data
- def get_neighbors(self):
- return self.neighbors
- def get_visited(self):
- return self.visited
- class Graph:
- def __init__(self):
- self.nodes = []
- def add_node(self, node):
- self.nodes.append(node)
- def topological_sort(self):
- stack = []
- for node in self.nodes:
- self.recursive(node, stack)
- for node in stack:
- print(node.get_data())
- def recursive(self, node, stack):
- if node.get_visited(): return
- node.set_visited(True)
- for nb in node.get_neighbors():
- self.recursive(nb, stack)
- stack.append(node)
- graph = Graph()
- node_A = Node('A')
- graph.add_node(node_A)
- node_B = Node('B')
- graph.add_node(node_B)
- node_C = Node('C')
- graph.add_node(node_C)
- node_D = Node('D')
- graph.add_node(node_D)
- node_E = Node('E')
- graph.add_node(node_E)
- node_F = Node('F')
- graph.add_node(node_F)
- node_G = Node('G')
- graph.add_node(node_G)
- node_H = Node('H')
- graph.add_node(node_H)
- node_I = Node('I')
- graph.add_node(node_I)
- node_J = Node('J')
- graph.add_node(node_J)
- node_A.add_neighbor(node_B)
- node_A.add_neighbor(node_F)
- node_B.add_neighbor(node_H)
- node_D.add_neighbor(node_C)
- node_D.add_neighbor(node_E)
- node_D.add_neighbor(node_I)
- node_E.add_neighbor(node_I)
- node_G.add_neighbor(node_A)
- node_G.add_neighbor(node_B)
- node_G.add_neighbor(node_C)
- node_I.add_neighbor(node_C)
- node_J.add_neighbor(node_E)
- graph.topological_sort()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement