Test Yourself #1
Question 1:
The following are all of the topological numberings of the given graph:
Question 2:
static numberGraph(Graph G) throws CycleException { int N = G.numNodes(); mark all nodes unvisited; for each node k in the graph { if (node k is marked unvisited) { N = topNum(k, N); } } }
Question 3: The following code determines whether graph G is connected, assuming that each node has a list of its predecessors as well as a list of its successors:
static boolean isConnected(Graph G) { if G has 0 nodes return true else { mark all nodes "unvisited" pick a node n modifiedDfs(n) // mark all nodes reachable from n going // either forwards or backwards along edges } // if any node is NOT marked "visited", the graph is not connected for each node k { if k is marked "unvisited" return false } return true } private static void modifiedDfs(Node n) { mark n "visited" // go backwards along edges into n for each predecessor m { if m is marked "unvisited" modifiedDfs(m) } // go forwards along edges out of n for each succecessor m { if m is marked "unvisited" modifiedDfs(m) } }