Using BFS for topological sort

前端 未结 3 1017
太阳男子
太阳男子 2020-12-09 05:20

Can Breadth first Search be used for finding topological sorting of vertices and strongly connected components in a graph?

If yes how to do that? and If not why not?

3条回答
  •  时光说笑
    2020-12-09 06:13

    So generally the code for topologically sorting using DFS (depth first search) is much more straight forward, you run it and it backtracks since its recursive assigning numbers as it calls back to previous stack frames. BFS is less straight forward but still easy to understand.

    First, you must calculate the in-degree of all the vertices on the graph, this is because you must start at a vertex that has an in-degree of 0.

        int[] indegree = int[adjList.length];
        for(int i = 0; i < adjList.length; i++){
            for(Edge e = adjList[i]; e != null; e = e.next){
              indegree[e.vertexNum]++;
            }
          }
    

    So the code above iterates through the vertex array, then it iterates through a single vertex's edges(in this case its stored using linked list), then it increments the vertex that the edge is pointing to in the indegree array. So at the end of the outer loop you will have traversed each vertex's neighbors and calculated each vertex's in-degree.

    Second, you now must use BFS to actually topologically sort this graph. So this first snippet of code will only enqueue the vertices in the graph that have an in-degree of 0.

        Queue q = new Queue<>();
        for(int i = 0; i < indegree.length; i++){
          if(indegree[i] == 0){
            q.enqueue(i);
          }
        }
    

    Now, after enqueueing only vertices with in-degree of 0, you start the loop to assign topological numbers.

        while(!q.isEmpty()){
          int vertex = q.dequeue();
          System.out.print(vertex);
          for(Edge e = adjList[vertex]; e != null; e = e.next){
            if(--indegree[e.vnum] == 0){
              q.enqueue(e.vnum);
            }
          }
    

    So the print statement prints out the vertex number that corresponds to the vertex. So depending on the requirements of your program, you can change the code where the print statement is to something that stores the vertex numbers or the names or something along those lines. Other than that, I hope this helped answer the first question.

    Second Question

    As for the second part of the question, it's pretty simple.

    1.Create boolean array filled with false values, this will represent if the vertices have been visited or not.

    2.Create for loop iterating over the adjList array, inside this loop you will call bfs, after calling bfs you will iterate over the boolean array you created, checking if any value is false, if it is then the graph is not strongly connected and you can return "graph is not strongly connected" and end the program. At the end of each iteration of the outer for-loop (but after the inner for-loop) don't forget to reset your boolean array to all false values again.

    3.At this point the outer for loop is done and you can return true, it is your job to implement to bfs it should take in an integer and the visited boolean array you created as parameters.

提交回复
热议问题