c# - Return the Boss in the hierarchy -- Tried to apply Depth First Search -


XYZ is a company in which the CEO is a hierarchy of bills and employees. Employees' reports can be a list of other employees who can report themselves, and so on. The employee with at least one report is called Manager Apply the closest Command Manager method to find the nearest manager (CEO from now) to find two employees. You can assume that all employees eventually report to the CEO. Sample data: There are 3 employees reported in the CEO bills: {Dome, Sameer, Michael}

Three reports in the Dome (Peter, Bob, Porter)

Sameer has no reports {} Michael has no report {}

Peter has two reports (Milton, Nina) Bob has no report {}

Porter has no report {} Milton has no report {}

There is no report of Nina {}

Sample Call: Closest Commanding Manager (min) The nearest commanding manager (Nina, Sameer) = Bill

The closest commanding officer to the nearest commanding officer (Nina, Porter) = << p>

(Peter, Nina) = Peter

Now, to solve this problem I have contacted this way - but I have not found the solution. I have tried to use simple DFS algorithm, but the solution is not complete.

  Public Communications Communications Communications Commander (Staff CEO, First Employee Employee, Employee Second Employee) {Visit Var = New Hashet & lt; Employee & gt; (); Bool firstFound = false, secondFound = false; Stack & LT; Employee & gt; Stack = new stack & lt; Employee & gt; (); // DFS stack. Push (CEO); While (stack.kount! = 0) {employee current = stack.pop (); IList & LT; Employee & gt; EmployeeList = CurrentGetres (); If (first empori.getid () == present.getit ()) {firstFound = true; } Else if (secondEmployee.getId () == Current .getId ()) {secondFound = true; } If (First Found & Secondfound) Current Return; // Should I return the last time? How do I keep the track of // node found in the hierarchy first ??? Console.WriteLine (current.getName ()); Foreign exchange (various employees in the employee's list) {if (visited, additional (employee)) {stack.Push (employee); }}} Return tap; }  

This looks for a request. For this, crafty algorithms usually do some pre-processing on the tree. One simple way is to mark each element with roots in the tree with its distance. Then, to find the nearest common ancestor of two nodes, first take a pointer up from the lower part so that they are both in the same depth, and then take both pointers together to the pointers touch. If you do not pre-process, you can move from both nodes at once, by adding all the nodes seen in a set, and when you add the nodes to the set of nodes, which already exists If so, check. Either in any case, for the first time when you face a node from both sides, that node is the least common ancestor.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -