Your problem is solved by what is known as a "topological sort." Such a sort determines dependencies in a directed acyclic graph. I recently adapted a solution to this problem. Here is a simple python application that demonstrates its behavior:
# adapted from https://gist.github.com/kachayev/5910538
from collections import deque
GRAY, BLACK = 0, 1
def topological(graph):
order, enter, state = deque(), set(graph), {}
dot = "digraph X {\r\n"
for item in graph.keys():
dep = graph[item]
for d in dep:
dot += item + " -> " + str(d) + '\r\n'
dot += "}"
print(dot)
def dfs(node):
state[node] = GRAY
for k in graph.get(node, ()):
sk = state.get(k, None)
if sk == GRAY:
raise ValueError("cycle")
if sk == BLACK:
continue
enter.discard(k)
dfs(k)
#order.appendleft(node) # show highest to lowest
order.append(node) # show lowest to highest
state[node] = BLACK
while enter:
dfs(enter.pop())
return order
def main():
graph = {
'1': ['2'],
'2': ['4'],
'3': ['4'],
'4': ['5', '6'],
'6': ['7'],
}
try:
print(topological(graph))
except ValueError:
print("Cycle!")
if __name__ == "__main__":
main()
The output is
deque(['5', '7', '6', '4', '2', '1', '3'])
Please note also that my code creates a DOT 'digraph' string for visualization in GraphVis. You can safely leave that out once you gain confidence in the algorithm. You can reverse the commented and uncommented append
lines near the end to get the major nodes first. Also note, this solution determines a solution that satisfies the graph. There may be others, and it doesn't determine the order as you required, but the graph satisfaction is one correct answer.