Answer after dfs in a connected graph the discovery

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

View Full Document Right Arrow Icon
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
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
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern