Friday, August 19, 2016

We were discussing the flow networks and the Ford Fulkerson method. It introduced us to residual network, augmenting paths and cuts. These are used in the max-flow min-cut theorem which explains the maximum flow in terms of cuts
Residual network consists of edges  with capacities that represent how we can change the flow on the edges of G. This is typically seen as the amount of flow that an edge can admit  and is equal to the edge's capacity minus the flow on that edge. If that value is positive, then it is included into the residual network and are excluded otherwise.  But it may also contain edges that are not in G. For example an edge whose flow has been decreased by admitting a counter flow from the opposite direction that can at most cancel out the flow is also part of residual network.
These reverse edges in the residual network allow an algorithm to send back flow that it has already sent on an edge.
The residual capacity cf(u,v) is therefore defined as
cf(u,v) = c(u,v) - f(u,v) if (u,v) belongs to E
            = f(u,v)              if (v,u) belongs to E
             = 0                    otherwise
 The above equation covers all the three cases of admitting a positive, negative or unchanged flow in terms of capacity.
Each edge of a residual network is a residual edge.
Augmentation of a flow by f to f' is defined as the previous flow f + the increase in the flow in the forward direction - the decreased flow in the reverse direction.  If the augmentation does not happen, the flow is unchanged.
Some of the lemmas associated with the maximum flow algorithm include the following:
Let G = (V,E)  be a flow network with source s and sink t, and let f  be a flow in G. Let Gf be the residual network of G induced by f and let f' be a flow in Gf.  Then the augmentation is defined as a flow in G with value equal to the individual magnitude of the two flows.
Another lemma is that if f is a flow in G and and p is an augmenting path
then the flow along the path between u and v in the residual network is the residual capacity of the path if (u,v) is on the path or zero otherwise. This binary definition of the flow along the path suggests that either and edge is counted or it isn't and when it is, the residual capacity of the path and not just between (u,v) determines the flow in the residual network.
#puzzles
Two children - a boy and a girl play in the mud. Their mother states at least one of them has a muddy head. Then she asks them if they know whether they have a muddy head. What will the children answer if they speak the truth.
The children will say No first and Yes afterwards collectively.
Let s be the statement that the son has a muddy head
Let d be the statement that the daughter has a muddy head
The mother statement is true but the kids don't see their own head  so they don't know if their head is muddy and answer no.
subsequently, each one has an additional information from their response given earlier and therefore know their state.  They would have known their respective state even if both of them answered yes in the first place.
#codingquestion
 Given n ropes of different length, combine them into a single rope,such that total cost is minimum. You can tie two ropes at a time,and cost of tying is sum of length of ropes.
Int getmin (list <int> Len)
{
Int cost = 0
Heap h = build_heap(len)
While (h.size () >1)
{
First = extract_min (h)
Second = extract_min (h)
Cost += first + second
H.insert (first+second );
}
Return cost;
}
bool  hasPythagoreanSum(List<int> sorted)
{
    assert(sorted.all(x=>x> 0));
    sorted.sort();
    Int len = sorted.count;
    For (int i=0; i<len; i++)
    {
          Int start = I + 1, end = len-1;
          While (start<end)
          {
            int sum = sorted[i] ^ 2 + sorted[start] ^2;
            int target = sorted[end] ^ 2;
            If (sum  ==  target)
            {
                return true;
            }
            If (sum  < target)
            {
                     start++;
            }
           Else
           {
               end--;
           }
      }      
  }
   Return false;
}

Find excel column name from given number for example
26 Z
52 AZ
702 ZZ
String getColName(int n)
{
Stringbuilder ret;
Int i = 0;

While(n)
{
Int rem = n%26;
If (rem==0)
{
Ret[i++] = 'Z';
N = n/26 -1;
}
Else{
Ret[i++] = 'A' +(rem-1);
N = n/26;
}
}
Ret += '/0';
Ret.reverse();
Return ret.toString();
}
This could also be done as pre compitations from powers of 26
For example 26 power 1 is Z
26 power 2 exceeds 52.
So 52 must be some multiple of 26 and offset from the previous computation which is Z. Since (52 -26) / 26 = 1, we have AZ and so on.

No comments:

Post a Comment