We run the dfs algorithm on a connected simple

Info iconThis preview shows pages 5–6. Sign up to view the full content.

View Full Document Right Arrow Icon
6. We run the DFS algorithm on a connected simple undirected graph (recall that simple means no self-loops and no parallel edges) with m edges and n vertices. How many backedges does the DFS execution find? Answer After DFS in a connected graph the discovery edges form a spanning tree. Since there are n vertices in this tree, there must be n - 1 edges. So there are n - 1 discovery edges and m - ( n - 1) = m - n + 1 backedges. 7. Explain how we use Dijkstra’s algorithm to find the shortest path between two given nodes in an unweighted graph. Explain why we should still prefer to use BFS for this task. Answer We make it into a weighted graph by assigning to each edge the same weight namley 1. Then sum-of-weights length is the same number-of-edges length. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
However, Dijkstra’s algorithm runs in O ( | E | + | V | log | V | ) while BFS runs in O ( | E | + | V | ) so BFS is asymptotically faster. 8. There exist DAGs with two or more vertices which is strongly connected, true or false? Answer FALSE. If there are two distinct vertices, u and v , strong connectivity implies there is directed path from u to v and another from v to u . Put these paths together, get a directed cycle which contradicts DAGness. 9. Let G be a DAG and s a vertex in G such that for any other vertex v there is a directed path from s to v in G . Then s is the first vertex listed in any topological ordering of G , true or false? Answer TRUE. Because if there is a path from a vertex u to another vertex w then u appears before w in any topological ordering: let the path be u->a->b->w , then u precedes a which precedes b which precedes w , by the topological ordering definition. Therefore s must appear before any other vertex. 10. The transitive closure of a digraph G has the same vertices as G and has a directed edge from u to v whenever there is a directed path in G from u to v . Then, the transitive closure of a DAG is also a DAG, true or false? Answer TRUE. Because if the transitive closure of G has a directed cycle then we can replace each edge e in this cycle by the path in G that caused e to be in the transitive closure, thus obtaining a directed cycle in G , contradiction. 11. A binary tree of the kind we used for heaps and such can be also regarded as a digraph, with the direction of the edges being always toward the leaves and away from the root (“downwards” in the usual tree diagrams we use). (a) If T is a binary tree, which of the traversals (preorder, inorder, postorder, level order(breadth first)) produces a list of nodes that is also a topological ordering of T regarded as a digraph? Answer Either preorder or level order (breadth-first) will do it. (b) If T is a binary heap containing distinct keys give a simple way of producing a list of the keys in T in the order given by a topological ordering of the nodes where the keys are stored, using only operations of the PriorityQueue interface. Justify your answer. Answer Perform repeatedly removeMin . This will extract the keys in increasing order of their value. Justification: if there is a path from key k 1 to key k 2 in the digraph then k 1 ’s node is an ancestor of k 2 ’s node. Hence, k 1 k 2 so k 1 will be removed from the heap before k 2 and therefore it will be listed before k 2 . It follows that the order is topological. 6
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page5 / 6

We run the DFS algorithm on a connected simple undirected...

This preview shows document pages 5 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online