Tuesday, September 20, 2016

#algorithm discussion continued
Today we continue reading the flow networks. We were looking at maximum flows
Maximum flows can also be calculated using "push-relabel" method.
We reviewed relabel-to-front algorithm next. This algorithm improves the running time of the maximum flow problem by choosing the order of basic operations carefully and managing the network data structure efficiently.  We saw what admissible edges are. If a vertex u is overflowing and (u,v) is an admissible edge, the push (u,v) applies and while it does not create any new admissible edge, it may cause the edge u,v to become admissible. The admissible edges also applies to relabel operation
We also saw how the discharge operation takes place and how the relabel-to-front algorithm proceeds.
Since the relabel-to-front algorithm iterates on vertices other than the source or sink, this is best done in the form of topological sort of the vertices in the admissible network.
#codingexercise
Print all nodes that are a distance k from leaf
Void GetK(Node root, int k, ref List<int> path, ref list<int> visited, ref int count)
{
if (root== null) return;
path[count] = root.data;
visited[count] = false;
count += 1;
if (root.left == null && root.right == null &&
     Count-k-1==0 && visited[count] ==false)
{
Console.write(path[count-k-1]);
visited[count] = true;
}
root.left= GetK(root.left, K, ref path, ref visited, ref count);
root.right = GetK(root.right, K, ref path, ref visited, ref count);
}

The above method assumes each element is visited in inorder

#Inorder without recursion
void inorder(node root)
{
var current = root;
var stk = new Stack<node>();

while(current != null || stk.empty() == false)
{
if (current != null)
{
stk.push(current);
current = current.left;
continue;
}
if (stk.empty() == false)
{
current = stk.pop();
Console.Write(current.data);
current = current.right;
}
}
}


No comments:

Post a Comment