Monday, October 17, 2016

#codingexercise
Detect number of three-node cycles in graph
We use depth first search to traverse the graph and generate paths.
DFS ( V, E, paths)
For each vertex v in V
       V.color=white
       V.d = nil
  Time = 0
 For each vertex v in V:
       If v.color == white:
           path = new Path();  
           DFS-Visit (V, E, v, path, paths)
     
 DFS-VISIT (V,E, u, path, ref paths)
  time = time + 1
   u.d = time
   u.color = gray
   path.Add(u);
   foreach  vertex v adjacent to u
        If v.color == white
           DFS-VISIT(V,E,v)
         Else
               If v.d  <= u.d < u.f <= v.f  :
                        paths.Add(path); 
u.color = black
time = time + 1
 u.f = time


filter(paths):
var d = new Dictionary<string, int>();
foreach path in paths:
     foreach vertex  v in path:
           if v.next and v.next.next and v.next.next.next and v.next.next.next == v:
               pathidentifier = stringify(v,3)
                if d.contains(pathidentifier) == false:
                    d.Add(pathidentifier, 1);


Find the greatest number we can get by rearranging the digits of a number n involving k shifts only as shown below:
Take n=43592169 and k=5
1st swap: 43952169
2nd swap: 49352169
3rd swap: 94352169
4th swap: 94532169
5th swap: 95432169 :- final number.

int GetMaxWithKShifts(int n, int k)
{
var digits = n.todigits();
int rem = k;
for (int i = 0; i < digits.Length && rem > 0; i++)
{
int index = GetMaxDigitIndexNext(digits, i);
if (index == i || digits[i] == digits[index]) continue;
if (index-i <= rem){
   Shift(digits, i, index);
   rem -= index - i;
}
}
return digits.ToInt();
}

Alternatively the maximum number may be obtained recursively by considering next higher element. The result for the above and below could vary based on which rule to follow:
void GetMaxWithKSwaps(int k, ref digits, ref candidate)
{
if (k == 0)
   return;
for (int i = 0; i < digits.length - 1; i++)
{
   for (int j = i+1; j < digits.length; j++)
   {
     if (digits[i] < digits[j])
     {
        Swap(digits, i, j);
        if (digits.compare(candidate) > 0)
           candidate = digits;
        GetMaxWithKSwaps(int k, ref digits, ref candidate);
        Swap(digits, i, j);
     }
   }
}
}

Or we could enumerate all arrangements

Void Permute (int k, String digits, StringBuilder candidate, bool[] used)
{
  If ( b.Length == a.Length)  { 
  if (digits.compare(candidate) > 0 && CountSwaps(digits, candidate) == k) 
      Console.Write(candidate.ToString(); 
  return;
   }
  For (int I = 0; I < digits.Length ; i++)
  {
    If (used[i]) continue;
     used[i] = true;
     candidate += digits[i];
     Permute(digits, candidate, used);
     candidate [candidate.Length – 1] = ‘/0’;
     used[i] = false;
  }
}

Also given an original sequence and a final sequence, it is possible to say how many swaps are required.

For example, we can say 43592169 and 95432169 has five swaps because

  • There are four positions out of order and we can get two right with two swaps

No comments:

Post a Comment