# But we can do better here is a second solution

• Notes
• 11

This preview shows pages 8–11. Sign up to view the full content.

But we can do better. Here is a second solution. Consider first DAGs. In a DAG there is at least one node of in-degree 0. If the DAG has two or more nodes of indegree 0 then it does not have a potential root. Given a general digraph G first determine all the strongly connected (SC) components. If the nodes in an SC component are collapsed together and the edges between them are erased then we get a DAG. Now G has a potential root iff this DAG has only one node of indegree 0. All this can be tested in time O ( | E | + | V | ). PSEUDOCODE: 1) Call a method implementing Kosaraju’s algo that finds all SC components. [[Assume the result is an array sc of size | V | such that sc[v] is an integer between 0 and K - 1 denoting the SC component to which v belongs. Here K is the number of SC components and we know that 1 K ≤ | V | .]] 2) Create an array of integers compINdeg of size K and initialize it to all 0’s. 3) For each edge v-->w if sc[v] is different from sc[w] then increment compINdeg[sc[w]] 8

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

[[Now compINdeg[k] is going to be the number of edges of G that begin at a node outside SC component k and end inside SC component k .]] 4) If exactly one of entries in compINdeg[k] is 0 return true. Otherwise return false. ANALYSIS: Step 1: O ( | E | + | V | ) Step 2: O ( K ) Step 3: O ( | E | ) Step 4: O ( K ) Since K ≤ | V | the whole algo runs in time O ( | E | + | V | ). Grading guidelines: 4pts each for parts (a) and (b). 12pts for a solution to part (c) that runs in linear time, O ( | E | + | V | ). 6pts for a solution to part (c) that runs in quadratic time, O ( | V | ( | E | + | V | )). 11. (10 pts) Consider a basic Queue interface with the methods enqueue and dequeue . Suppose we want to extend this to a new interface MinQueue that includes these methods and two more methods called, findMin and deleteMin . Assuming that the elements in the queue are of type Comparable and distinct (to avoid complications), findMin must return the smallest element in the queue without deleting it, while deleteMin must delete the smallest element in the queue. Describe an implementation of MinQueue such that each of the four operations takes O (1) worst-case time. Answer: In the original version this problem had only findMin . Adding deleteMin to the interface was an embarrassing mistake. As one student astutely observed, it is not possible to have both enqueue and deleteMin in constant time: if that were so then we could sort in linear time! Anyway, since there was no fair way to grade this we decided to toss the problem, every student was scored 10pts on it. Still, read on! In what follows we give a solution for just findMin . Moreover, we give it for stacks, not for queues, but it is straightforward to adapt it to queues. DISCUSSION (not required): A naive implementation would use a regular stack implementation S and a reference theMin main- tained to always give the smallest element currently in S : | | | 3 | | 5 | ---------- | 1 |<------| theMin | | 4 | ---------- ----- S 9
Every time we push an element e onto S , we compare it with the element referred to by theMin and if it is smaller we update theMin to refer to e .

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

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern