Can someone help me optimize my DFS implementation in Python?
def dfs(graph, node):


This isn't a depth first search

    """Run DFS through the graph from the starting 
    node and return the nodes in order of finishing time.
    """
    seen = set()
    stack = {node}


You aren't using this as a stack, don't call it that

    while True:
       flag = True


avoid flag variables, they confound code. Also that's a very undescriptive name.

       for x in stack:
           if x not in seen:


Seen these are sets, you should be probably use: for x in stack - seen: which should be more efficient

                stack = stack.union(set(graph[x]))


Using this approach will make things rather slow, you just keep making stack bigger and bigger.

                seen = seen.union({x})


Use seen.add to add things to the set. Using union like this will be much slower

                flag = False
                break
        if flag:
            break
    return list(stack)


The set gives you no guarantees as to the order of the elements. I'm guessing that's now what you wanted. As it stands,this will end up giving you all the elements in your graph in a random order.

My approach

def breadth_first_search(graph, start):
    seen = set()
    current = {start}
    history = []

    while current:
         history.extend(current) # record when we found all these elements
         seen.update(current) # add them to the list of elements we've already seen
         neighbours = set() # generate a set of all elements that we can reach from these ones
         for node in current:
             neighbours.update(graph[node])
         current = neighbours - seen # the next round of elements is this one, except those we've already seen


    return history