BFS Tree and DFS Tree Properties
(Modified from a textbook exercise.) Consider a connected graph and consider a vertex . Suppose we are doing breadth-first search and depth-first search traversals on the graph starting with the node , and further suppose that the breadth-first search tree and the depth-first search resulting from the two traversals are the same tree . Prove that in that case , that is, does not contain any edge that does not belong to T. Hint: Property 3.4 (for BFS tree) and Property 3.7 (for DFS tree). Suggested proof technique: by contradiction.
Given our connected graph where , first run and algorithms start from .
We will also define a spanning tree such that both the and are . We seek to show that , meaning there is no edge from not in .
As a setup for contradiction, let us assume that there is some edge that also is not in
We will also let be in layer and let be in layer .
Considering the edge that is not in . Given that is the BFS tree, by the Property (3.4), we know that:
Leaving us with two possibilities (1) or (2)
- If this is the case, that means that both and are on the same level in the tree. However, this is impossible given that ancestors and descendants must have different depths, forcing a contradiction.
- If this is the case, we can let . Naturally this means that one of these is an ancestor of the other. But their depths are apart, so one must be the direct child of the other. Given this parent/child edge that is part of , that means that IS a tree edge, and thus the statement is contradicted.
Since both possibilities lead to contradiction, therefore every edge must be in ,
All Topological Orders
Given the DAG below, list all possible topological orders and state how many there are. Do it manually (it won’t take too long). Additionally, run any Java program code you can obtain to verify the manually run result (you can use any data structure to represent the graph); cite the source of the program code (e.g., URL) in the homework answer, and submit the code as a separate file. (This is not a programming exercise but part of a written exercise.)
I was able to find 13 possible combinations. Unfortunately I did not handwrite this assignment, so there is no proof of work. I will provide a trace diagram to show SOME element of work.
- S G D A B H E I F C T
- S G D A H B E I F C T
- S G D A H E B I F C T
- S G D A H E I B F C T
- S G D A H E I F B C T
- S G D H A B E I F C T
- S G D H A E B I F C T
- S G D H A E I B F C T
- S G D H A E I F B C T
- S G H D A B E I F C T
- S G H D A E B I F C T
- S G H D A E I B F C T
- S G H D A E I F B C T
My rough procedure:
- remove node
- remove node
- remove node
- remove node
- remove node
- remove node
- remove node
- remove node
- remove node
Decision Tree Diagram: see figure 2
For the code, I used GeeksForGeeks’1 implementation. This implementation worked on numbers nodes, so I just used the mapping :
GeeksForGeeks’ code with our graph set:
import java.util.*;
class Graph {
int V;
List<Integer> adjListArray[];
private int count = 0;
public Graph(int V) {
this.V = V;
@SuppressWarnings("unchecked")
List<Integer> adjListArray[] = new LinkedList[V];
this.adjListArray = adjListArray;
for (int i = 0; i < V; i++) {
adjListArray[i] = new LinkedList<>();
}
}
public void addEdge(int src, int dest) {
this.adjListArray[src].add(dest);
}
private void allTopologicalSortsUtil(boolean[] visited,
int[] indegree, ArrayList<Integer> stack) {
boolean flag = false;
for (int i = 0; i < this.V; i++) {
if (!visited[i] && indegree[i] == 0) {
visited[i] = true;
stack.add(i);
for (int adjacent : this.adjListArray[i]) {
indegree[adjacent]--;
}
allTopologicalSortsUtil(visited, indegree, stack);
visited[i] = false;
stack.remove(stack.size() - 1);
for (int adjacent : this.adjListArray[i]) {
indegree[adjacent]++;
}
flag = true;
}
}
if (!flag) {
for (int idx = 0; idx < stack.size(); idx++) {
System.out.print(stack.get(idx));
if (idx < stack.size() - 1) System.out.print(" ");
}
System.out.println();
count++;
}
}
public void allTopologicalSorts() {
boolean[] visited = new boolean[this.V];
int[] indegree = new int[this.V];
for (int i = 0; i < this.V; i++) {
for (int var : this.adjListArray[i]) {
indegree[var]++;
}
}
ArrayList<Integer> stack = new ArrayList<>();
allTopologicalSortsUtil(visited, indegree, stack);
System.out.println("\nTotal topological orders: " + count);
}
public static void main(String[] args) {
Graph graph = new Graph(11);
graph.addEdge(0, 3); // S -> A
graph.addEdge(0, 2); // S -> D
graph.addEdge(0, 1); // S -> G
graph.addEdge(1, 2); // G -> D
graph.addEdge(1, 6); // G -> E
graph.addEdge(1, 5); // G -> H
graph.addEdge(2, 3); // D -> A
graph.addEdge(2, 6); // D -> E
graph.addEdge(3, 4); // A -> B
graph.addEdge(3, 6); // A -> E
graph.addEdge(4, 9); // B -> C
graph.addEdge(6, 9); // E -> C
graph.addEdge(6, 8); // E -> F
graph.addEdge(6, 7); // E -> I
graph.addEdge(5, 7); // H -> I
graph.addEdge(5, 6); // H -> E
graph.addEdge(7, 8); // I -> F
graph.addEdge(7, 10); // I -> T
graph.addEdge(8, 9); // F -> C
graph.addEdge(8, 10); // F -> T
graph.addEdge(9, 10); // C -> T
graph.allTopologicalSorts();
}
}