I have an assignment where I have to write a method that performs a DFT of a directed graph. Here are the directed edges:
Node 1-->Node 2, Node 3
Node 2 -->Node 4
Node 3 -->Node 5
Node 4-->Node 5
From my understanding, after watching this video, doing a DFT of the above graph starting from Node 1 would output 1, 2, 4, 5, 3. My reasoning for this is that when looking at 1's edges, 2 would naturally come before 3, and then progresses linearly until it arrives at 5. Since 5 doesn't have any other edges besides it's connection to 4, the traversal would "unwind" back to Node 1, after which it would output Node 3.
However, the assignment is expecting the output 1, 3, 5, 4, 2. Where is the problem in my logic?
EDIT: I'm not sure what part of the assignment I misunderstood but the solution was that after printing the first element, traversing a node adds it to a stack, but the expected output is the order in which elements leave the stack. Navigating the graph, you start with Node 1 and travel to Node 2 first (because the assignment demands that you choose between nodes in their natural order), adding Node 2 to the stack. You then continue traversing the Nodes that Node 2 leads to, leading your stack to be 2, 4, 5. Then, returning to the choice between 2 and 3, you add 3 to the stack, pop off each element of the stack, outputting them as you go. Thus, 1 is printed first, then popping off the elements of the stack, you get 3, 5, 4, 2.