<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Guy Alster’s Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/galstertechblog/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/galstertechblog/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 04:00:56 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Implementing a Skip List in C</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/implementing-a-skiplist-in-c</link>
      <pubDate>Wed, 25 Jan 2017 20:21:22 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/?p=345</guid>
      <description><![CDATA[I'm dedicating this post to a new data structure. That is the SkipList. This is an intriguing data...]]></description>
      <content:encoded><![CDATA[<h3>I'm dedicating this post to a new data structure. That is the SkipList. This is an intriguing data structure with many benefits. In addition, I've decided to implement this data structure in C. Before I go about explaining the data structure itself I want to explain what drove me to implement it in C. I've been programming for the majority of my career in C++. In this blog, I've written most of my posts in C++ and have praised the language as being the best tool for almost any task. However, I decided that in this instance I want to go back to the basics. I wanted to prove to myself that I can still design a C library with the proper abstractions while leveraging the best software engineering methodologies. I wanted also to remember how it feels to program without the comfort of the C++ type system, C++ Templates, Object Oriented Programming etc... Hence, this post is going to be divided into two parts: The first will discuss Skip Lists, and the second will dwell into how to design a library in C. I hope you enjoy this post as much as I enjoyed writing this code.</h3>
<h3>The entire code can be found on my Github: <a href="https://github.com/guyalster/SkipList">https://github.com/guyalster/SkipList</a></h3>
<h2><span style="text-decoration: underline">SKIP LIST, so what is it all about?</span></h2>
<h3>A skip list is a dynamic and randomized data structure that allows storing keys in a sorted fashion. The skip list is in essence, a linked list with multiple levels. Each level is itself a linked list and with pointers from the upper levels to the lower levels. The thing that is special about this data structure is that its expected runtime for all operations (insertion, deletion, searching) is O(Log(N)). So how is this runtime achieved? Well, that's a result of the randomness of the skip list and how it is built. But before we talk about how we achieve this runtime lets first answer a more fundamental question. Why do we even need it? We already have binary search trees which are great data structures for storing sorted keys (and values) and achieving a logarithmic runtime when they are balanced. In fact, they are deterministic in behavior and have a guaranteed logarithmic runtime (rather than an expected one). The answer to this question will motivate us to continue this quest.
First, implementing a balanced binary search tree can be a very tricky business. Anyone who has tried to implement an AVL/RB tree has seen the complications. On the other hand, as I'll show later in this post, implementing a skip list is much easier. But there is one more important advantage to skip lists over their tree counterparts. That is synchronization. Locking in skip lists can be done in a much more efficient way than in trees, but most importantly, synchronization in skip lists can be implemented without any locking at all while guaranteeing consistency. However, this is a subject that I will not get into in this post as it is also very complicated and requires much more insight and depth. However, mentioning this is crucial to justify why this data structure is so important.</h3>
<h2><span style="text-decoration: underline">How to build a skip list:</span></h2>
<h3>So now we come to the juicy part. How to build a skip list. Well, the best analogy to a skip list I've heard came from an MIT Data Structures &amp; Algorithms class. There the lecturer explained skip lists in terms of the NYC subway system. The NYC subways have multiple lines which go in the same direction. However, one of them passes through every station on its way to the destination, while others pass through some of the stations. The express line passes through the least amount of stations. If you want to get from station A to M, the best strategy is to take the express train from station A directly to station F. Then take the next train from F to J that also stop at stations H and I. Then take the slowest train from I to M that stops at every station. This, of course, is a better strategy than taking the slowest train from A to M that stops in all stations on its way. This is the same idea of how a skip list works and where it derives its efficiency from.  So let's examine how we build the skip list from the ground up. We start with the lowest level (that is the slowest train lane), we then toss a coin, and if its "heads" we insert the element in the next upper level. We continue to do this until we draw "tail". The actual way the skip list will be implemented is slightly different but in essence, is the same as I just described. The idea is that when we want to insert a new node to the skip list, we first generate a number between 0 to MAX_SKIP_LEVELS (which is a constant defined by the user). This number will determine in how many levels this new node will appear. Inserting the new node starts from the topmost level that is equal to the random level we generated (the express lane) and we move to the right in that lane until we reach a node that has a key that is greater than our new node. That will be our insertion point. From that point, we continuously insert our node while adjusting the "next" pointers until and including the last level (e.g. level 0). Searching of an existing key will be done in a similar fashion: We'll start from the topmost level of the skip list, going right (using the next pointer) until we hit a node with a key &gt;= our key. We then go down to the next level until we reach the topmost level that contains our node. As you can see from these, the runtime of insertion, searching, and deletion is proportional to the number of nodes we have in each level. What is the expected number of nodes we have in the level i? It is easy to see based on the random process that it will be about half of the nodes that appear in the previous (bottom) layer. This should sound familiar at this point. We notice that in essence. a skip list resembles very much a balanced binary search tree. The main difference is that a key appears in the parent as well as in the child nodes all the way down to the leafs.  Using this intuition, we can now see why those operations of the skip list are expected to be O(Log(N)) where N is the number of elements stored in the list.</h3>
<h2><span style="text-decoration: underline">Skip List Data Structure and APIs:</span></h2>
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/skpinode.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/skpinode.png" alt="skpinode" width="694" height="231" class="alignnone size-full wp-image-355" /></a>

<a href="https://msdnshared.blob.core.windows.net/media/2017/01/SkipList.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/SkipList.png" alt="skiplist" width="660" height="239" class="alignnone size-full wp-image-365" /></a>
<h4><a href="https://msdnshared.blob.core.windows.net/media/2017/01/Initialization.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/Initialization.png" alt="initialization" width="640" height="137" class="alignnone size-full wp-image-375" /></a></h4>
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/Comparators.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/Comparators.png" alt="comparators" width="625" height="469" class="alignnone size-full wp-image-385" /></a>
<h3>In GitHub all of the above are located in the SkipList.h
At the top, we have the definition of a SkipNode. The SkipNode has only two fields: the current level of that node and a pointer to pointers to other SkipNodes. The pointer to pointers can be viewed as a dynamic array of lists. This array represents all the levels that this node participates in. This seems strange at first. How many different pointers will the array contain? This of it like this: Image Node N1 with key K1, appears in 10 Levels. Imagine that Node N2 with key k2 is greater than k1 and appears in 5 levels while Node N3 with key k3 which is also greater than k1 appears in 11 levels. N1's array will have the first 5 pointers in its array pointing to N2 and the next 5 pointers pointing to N3.
Let's assume that we're looking for N2 in the SkipList. When we reach level 10 we'll get to N1 which is less than N2. we will see that at level 10 through 6 it points to N3 which is greater than N2, so we'll go down in the levels until we reach level  5, where we'll find N2 as the next pointer of N1 and return it.</h3>
<h3>The next definition is of a SkipList structure. Notice that this structure contains only three function pointers. This structure is the C equivalent of a C++ interface with pure virtual functions. Who will implement this interface?  That will be a structure that is defined in SkipList.c. The reason it is defined in the .c file rather than in the .h file is for encapsulation purposes. How does the user of the library get a pointer to the SkipList interface? This is done using the initialization function defined just below the structure. But there is something interesting about that function (InitializeSkipList). Notice that it accepts three parameters: size_t - This is the maximal number of levels. SkipCompare - This is a type of a function pointer defined above and which receives two skip lists and returns the larger of them. ValCompare - a function pointer which accepts a pointer to a key (void*) and a skipNode* and returns if that key is greater than the key in that SkipNode.
So, it is the responsibility of the user to tell the library how to compare the values in the SkipNode by providing the comparators to the library upon initialization. But wait a minute! that's weird! Shouldn't the library know itself how to compare these Nodes ? after all, the nodes are defined here. But if you'll take a closer look, you'll notice how none of the structures defined above contain keys and values. In fact, they are very stripped down. So where are the keys and the values that are supposed to be stored inside each SkipNode? That is the trick here. This is a design decision I made when I wrote this library. If I were to write this code in C++, I would use templates to define the SkipNode in a generic way that is independent of the key type it operates on.  But in C I don't have that luxury. In C the only way for me to create generic types is by defining them as void *. Instead of doing that, I decided to take a different approach. I decided to adopt the approach that is often used in operating system kernels, which is to have the data structure be embedded inside the main user structure. That is, instead of having the  SkipNode contain the user's types, the user will hold a SkipNode inside his/hers structures.</h3>
<h3>For example:</h3>
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/TestStructure.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/TestStructure.png" alt="teststructure" width="349" height="219" class="alignnone size-full wp-image-386" /></a>
<h3>But how will this work? Since the library only operates on SkipNodes, how will we be able to get the containing structure?
This is a critical piece of the puzzle. in the SkipList.h file you will find a macro defined as:
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/Macro.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/Macro.png" alt="macro" width="829" height="171" class="alignnone size-full wp-image-395" />
</a>This Macro is not simple to understand so I suggest examining it carefully to see what it does. However, I will mention that it uses the relative location of the SkipNode variable inside the enclosing structure to get the address of the enclosing structure. This is a worthwhile trick to master by itself.</h3>
<h3>So far we have seen the structures that are exposed to the users of the library. But as you probably noticed none of them contain the metadata to manage the skiplist. Furthermore, ideally, we would like to have a sentinel node which does not contain any keys or values but is used to get access to the rest of the skip list. This sentinel structure is defined in the .C file (again for the purposes of encapsulation).</h3>
<a href="https://msdnshared.blob.core.windows.net/media/2017/01/Sentinal.png"><img src="https://msdnshared.blob.core.windows.net/media/2017/01/Sentinal.png" alt="sentinal" width="306" height="199" class="alignnone size-full wp-image-405" /></a>
<h3>Are you seeing the object-orientedness here? The first variable is the SkipList interface pointer. When the user calls the initialization function: InitializeSkipList(...) he expects to get a pointer to a SkipList structure. We will return to the user the address of the Sentinel object cast to be a SkipList pointer. This is classic C style polymorphism.
Notice that the interface function pointers inside the SkipList structure all accept a pointer to a SkipList structure as well. The user will pass back to each one of the API functions the pointer to the SkipList. The implementation will cast that pointer back to be SkipNodeSentinal and will have the access it needs to all the metadata of the skip list. Again, classic C style polymorphism. The rest of the variables inside the SkipNodeSentinal are not worth emphasizing as they are all part of the metadata of the skip list.</h3>
<h3>I highly recommend going over the code in my GitHub. I really tried to put a lot of emphasis on good software engineering and to make the interface as clean as possible. Enjoy.</h3>]]></content:encoded>
    </item>
    <item>
      <title>KMP Algorithm and its less obvious applications</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/kmp-algorithm-and-its-less-obvious-applications</link>
      <pubDate>Sun, 25 Dec 2016 05:02:02 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/?p=276</guid>
      <description><![CDATA[Today I'd like to talk about an algorithm that I believe should be in any  Software Engineer's...]]></description>
      <content:encoded><![CDATA[<h2>Today I'd like to talk about an algorithm that I believe should be in any  Software Engineer's toolkit. That is, the KMP algorithm named after its three inventors: Knuth-Morris-Pratt. The algorithm was designed to solve efficiently a very common problem. That is the problem of finding whether or not one string is a substring of a different string. While I will definitely dwell on this original problem, I will also show an example that is less obvious of how this algorithm can be used to solve string problems efficiently.</h2>
<h2>Let's start first with the original problem. We would like to implement the function:<strong> int  strstr</strong>(<strong>const string &amp;Str, const string&amp; Pattern); </strong></h2>
<h2>Most of us have had the chance of implementing this function at some point or another during our careers. The brute force solution of this function entails two loops, one over Str and one over pattern. Let's see it:</h2>
<a href="https://msdnshared.blob.core.windows.net/media/2016/12/strstr.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/strstr.png" alt="strstr" width="527" height="440" class="size-full wp-image-285 aligncenter" /></a>

&nbsp;

&nbsp;
<h2>What is the runtime complexity of this algorithm? Since we are running in two loops and for each i we are trying all j's, we have a worst-case complexity of O(N*M) where N is the size of Str and M is the size of Pat. Can you spot where the inefficiency comes from?  The problem is that if we can't find a match we have to go back (with i) and start over from where we left off when we started the match. Why are we doing this? let's look at two examples:</h2>
<h2>* str = "abcbcefg" and pat = "bce".  In this case we start the match at i = 1 and j = 0, we continue to i = 2 and j = 1, but then at i = 3 and j = 2 we have a mismatch. Our algorithm will detect this mismatch and will set "i" to be 2. But what's the point? for i = 2 we already know that there will be no match. In fact, based on strwe should not be setting "i" to a previous index, but we could just as well continue advancing i.</h2>
<h2>* str = "abababc" pat = "ababc". In this case, we will start matching at i = 0 and will reach i = 4 when we detect a mismatch. We will then return to i = 1 and start again. When we reach i = 2 we will detect that the strings match. The observation is, that we should not have even bothered returning to i = 1 we could have set "i"to 2. But, if we look even closer we may notice that after we matched str = "abab" and pat = "abab" we already have one pair of "ab" in pat which we know that can be used for future matches.</h2>
<h2>The two examples are a great segway to explain how KMP works and why it is so efficient. KMP starts off by building a prefix table for pat. This prefix table will be used to indicate for each index "i" how much we have matched thus far and use this information to jump back only by the amount needed.</h2>
<h2>Lets see how the prefix table will look like for different patterns:</h2>
<h2>pat = "ababc" will result in the table: [0,0,1,2,0]</h2>
<h2>pat = "ississi" will result in the table: [0,0,0,1,2,3,4]</h2>
<h2>pat = "abcd" will result in the table: [0,0,0,0]</h2>
<h2>Notice what is happening here ? when we detect a prefix that we have seen (such as the second "ab" in "ababc") the numbers in the table indicate the size of that prefix. So if we start matching "abababc" with "ababc" for i = 4 and j = 4 we see that 'a' is different from 'c', and hence we have a mismatch. Using the table above we see that the table at index j - 1 = 3 has the value 2. We can have "j" jump back two indexes from 4 to 2, and continue our match from there. We don't have to touch "i" anymore, we just modify "j" based on the maximal size of the prefix that we have seen thus far.</h2>
<h2>Lets see the code to generate the Prefix table:</h2>
&nbsp;

<a href="https://msdnshared.blob.core.windows.net/media/2016/12/GeneratePrefix-e1482568351662.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/GeneratePrefix-e1482568351662.png" alt="generateprefix" width="525" height="446" class="size-full wp-image-315 aligncenter" /></a>
<h2>We allocate the prefix table to be the size of "Pat". We use two indexes to fill the table. The first is "i", it will indicate the end of the current prefix. The second is "j" which will be used to find the next occurance of the prefix. Notice that "j" starts from index 1. As we are traversing we check to see whether Pat[i] == Pat[j]. If they are, then we know that there is a prefix of size i + 1 that we have encountered at index "j". We update the table at index "j" to indicate this prefix, and update "i" as well. If they are not the same, then we have two cases: First, if i == 0, it means that we have not found any prefix thus far and we just increment "j". However, if  i &gt; 0, we keep "j" where it is and make "i" go back to the index where the prefix  has started and compare it again with "j" . To better explain this scenario I will show an example:</h2>
<h2>Pat = "aabaabaaaab". The prefix table for this will be: [0,1,0,1,2,3,4,5,2,2,3]. When j = 1, we have our first match and i is increment to 1. However, for j = 2 we have a mismatch Pat[i = 1] != Pat[j = 2] and "i" goes back to 0. When j = 3, things start becoming more interesting. We set PrefixT[3] = 1. For j = 4 we have a prefix of size 2 matching, "i" becomes 2 and PrefixT[4] = 2. For j = 5 we have a prefix of size 3 matching (e.g. "aab") we update "i" and "j again. In fact we continue to have more matches up to j = 8 when Pat[j = 8] = 'a' and Pat[i = 5] = 'b'. At that point since i &gt; 0 we don't advance j but we move i to be i = PrefixT[i - 1] = 2. Pat[i = 2] = 'b'  while Pat[j = 8] = 'a' which is again a mismatch. We set i = Prefix[i - 1] = 1. In this case we have Pat[i = 1] = 'a', which is equal to Pat[j = 8], and so PrefixT[8] = i + 1= 2. We increase i by 1 to i = 2 and increase j by 1 to j = 9. Since Pat[i = 2] == Pat[j == 9] we set PrefixT[9] = i + 1 = 3.  What does it tell us ? It tells us that at index 9 (the end of Pat) we have been able to match a prefix of size 3 of Pat (e.g. "aab"). So even though we had a slight mismatch at index j = 8 and i = 5, we used this prefix table to jump back as much as needed based on the previous prefix match so that we can continue matching afterwards.</h2>
<h2>We are now ready to see the full algorithm and how it uses the prefix table.</h2>
<a href="https://msdnshared.blob.core.windows.net/media/2016/12/strstrlast.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/strstrlast.png" alt="strstrlast" width="521" height="774" class="size-full wp-image-325 aligncenter" /></a>
<h2>Notice how we use the Prefix Table only when a mismatch occurs and if "j" which in this case points to pat is greater than 0. The runtime complexity of this new version in the worst case will be O(N) the size of str.</h2>
<h2>Now we have reached the second part. In this part we will show how to solve an interesting problems using KMP algorithm.</h2>
<h2><em><strong>The problem asks us to convert a string to a palindrome by appending at its beginning the minimum number of letters</strong></em>. For example: "<span>aacecaaa" can be turned into a palindrome by appending a single "a" at its beginning. Furthermore, consider the following string: "abcd" to turn it into a palindrome we need to append "dcb" at its beginning. What about the string "abba"? Since it is already a palindrome, we needn't append anything at its beginning. The problem asks us to return the palindrome string after we appended the letters to it.</span></h2>
<h2>With a brute force approach, we may try to find the longest palindrome from the beginning. Everything after that consists of letters that we need to append (in reverse order) to the beginning.  Finding the longest palindrome is a task that takes O(N^2) time, unless you are familiar with Manacher's algorithm in which case you can turn it into a O(N) time algorithm. Manacher's algorithm is very complicated and so we will instead use a different approach that will leverage KMP and will take us o(N) time as well.</h2>
<h2>An important observation about palindromes is that their prefix (up to the center) is equal to the reverse of their suffix (from the center until the end). It would be nice given a string Str to know how much are the prefix and suffix (from the center) of Str alike. For example, "aacecaaa", we notice that this is almost a palindrome. The last "a" of the string is making it a non-palindrome. All we need to do is to append that "a" to the beginning of the string and it becomes a palindrome. In the case of  "abcd" there is no suffix that is equal to a prefix. Hence, we need to append "dcb" to the beginning. Whenever you need to deal with prefixes KMP should be one of the things that come to mind immediately. In this case, we will use it in a sophisticated way.</h2>
<h2>Given a string W will we generate a new string W#Wr (where Wr is the reverse of W and # is a symbol that is not in the alphabet of W). The reason we need the special character # will be clear later. By reversing W and appending it W# we can create the KMP prefix table and find out if Wr contains at its some shared characters with the prefix of W. If it does, we know that all the letters before it (e.g. from # to the value at the end of the prefix table) are letters we need to add at the beginning of W to turn it into a palindrome. Lets look again at "aacecaaa".</h2>
<h2>We will create a new string as explained above: "aacecaaa#aaacecaa" the corresponding prefix table for it is: [0,1,0,0,0,1,2,2,0,1,2,2,3,4,5,6,7]. Notice that the value at the last entry in the table is 7. This means that we have a match of size 7 with the prefix of our original string, and you can actually verify it since we have already seen that "aacecaa" is indeed of size 7. to find the letters we need to append we can either go from the # to the index of string length - 7, or we can only look at the original string and take the substring starting at index 7. In our algorithm, we will use the latter. Now we need to address the special character "#" that we added. The reason for it is not obvious. Consider the string "aaaaa". If we don't add the "#" the value at the end of the prefix table will be much larger than the size of the original string (due to the fact that the string is composed of the same character). By adding the delimiter "#" we are separating the two parts of the string (e.g. W and Wr) making sure only to seek the prefix we are interested in Wr.</h2>
<h2>We are now ready to see the code in action:</h2>
<a href="https://msdnshared.blob.core.windows.net/media/2016/12/ShortestPalindrome.png"><img src="https://msdnshared.blob.core.windows.net/media/2016/12/ShortestPalindrome.png" alt="shortestpalindrome" width="425" height="375" class="size-full wp-image-335 aligncenter" /></a>

&nbsp;
<h2>Notice how short and concise this code is. I urge you to try and implement it by seeking the longest palindrome and comparing the code. That is it for this post. It was long, but I hope it will help you internalize this algorithm and use it when you need it.</h2>
&nbsp;]]></content:encoded>
    </item>
    <item>
      <title>Reservoir Sampling - Once and for all</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/reservoir-sampling-once-and-for-all</link>
      <pubDate>Sat, 02 Apr 2016 22:32:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2016/04/02/reservoir-sampling-once-and-for-all/</guid>
      <description><![CDATA[Today I&#39;ll talk about a topic which I consider very important - Reservoir Sampling.
Imagine...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:large;">Today I&#39;ll talk about a topic which I consider very important - Reservoir Sampling.</span></p>
<p><span style="font-size:large;">Imagine that you have a set of N numbers. You would like to generate a random subset of size K, such that any such subset has equal probability to be generated. In other words, every element of the set has equal probability to end up in that subset. The algorithm to do this is actually very straight forward, however, the proof of why it works is not so trivial. Hence I will describe the algorithm first show the code and then I&#39;ll add the formal mathematical proof for it which I wrote in LaTex due to the need for mathematical notation.<br /></span></p>
<p><span style="font-size:large;">First thing is first: The Algorithm. The algorithm works as follows: we start&nbsp;by taking the first K elements from the set. We then iterate from indexes K+1 to N and generate a random number between {0 - i +1} where i is the current index we are traversing. If the random number we get is between 0 to k (say index j) we replace the number in that index with the number at i. Otherwise, we continue to the next number.<br /></span></p>
<p><strong><span style="text-decoration:underline;"><span style="font-size:large;">Code:</span></span></strong></p>
<p><strong><span style="text-decoration:underline;"><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/ReservoirCode.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/ReservoirCode.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/ReservoirCode.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/ReservoirCode.PNG" border="0" alt=" " /></a><br /></span></span></strong></p>
<p><span style="font-size:large;">The resulting subset will be in the first K elements of the vector V. I&#39;ve attached to this post a pdf file with the proof I wrote for the correctness of the method. I think that reading it and understanding it is worth your while.&nbsp;</span></p>
<p><span style="font-size:large;">&nbsp;</span></p>
<p>Reservoir Sampling - Once and for all</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/telligent.evolution.components.attachments/01/10160/00/00/03/66/15/62/ReservoirSampling1.pdf" original-url="https://blogs.technet.com/cfs-filesystemfile.ashx/__key/telligent-evolution-components-attachments/01-10160-00-00-03-66-15-62/ReservoirSampling1.pdf">ReservoirSampling1.pdf</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Burst Balloons</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/burst-balloons</link>
      <pubDate>Thu, 31 Mar 2016 15:46:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2016/03/31/burst-balloons/</guid>
      <description><![CDATA[I&#39;d like to discuss today a new problem called the &quot;Burst Balloons&quot; problem.
This is...]]></description>
      <content:encoded><![CDATA[<p style="margin-left:30px;"><span style="font-size:medium;">I&#39;d like to discuss today a new problem called the &quot;Burst Balloons&quot; problem.</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">This is an interesting problem because it challenges us to rethink our hypothesis and strengthen it.</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">In this problem, we are given a set of balloons with numbers on them. The numbers represent the cost of each balloon. If we pop a balloon i, we get coins by the following formula: &nbsp; &nbsp;B[i - 1]*B[i]*B[i + 1].</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">Where B[i] represents the cost of balloon i and B[i + 1], B[i - 1] the corresponding costs of the right and left balloons to the ith balloon. </span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">For the sake of this problem, we may assume that B[-1] and B[N + 1] (N = the last balloon) are both 1.&nbsp;</span><span style="font-size:medium;">The problem asks you to maximize your profit by popping the balloons.&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">Notice however, that once you pop a balloon i, balloons i - 1 and i + 1 become neighbors of each other.&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">Let&#39;s try to solve this problem with induction:</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;"><strong>P(n &lt; N)</strong> - we know how to solve the problem for n &lt; N balloons and get the max profit for them.</span></p>
<p style="margin-left:30px;"><strong><span style="font-size:medium;">P(1) - &nbsp;</span></strong><span style="font-size:medium;">The base case deals with only a single balloon, in which case the answer is straightforward - we just pop it and collect the coins for it.<br /></span></p>
<p style="margin-left:30px;"><strong><span style="font-size:medium;">P(N) -&nbsp;</span></strong><span style="font-size:medium;">Assuming we have N balloons, how do we reduce the problem to smaller subproblems ? We have to pop one or more of the balloons to reduce the problem size. However, which balloon should we pop first ? This is tricky. When we pop a balloon we modify the rest of the system. Also,which one maximizes our profit ? At this point, we notice that we need to change our perspective.</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">What if instead of choosing a balloon to pop first, we actually choose a balloon to pop last ? so we reduce P(N) to P(N - 1) based on the last balloon to pop. This way, our subproblems are not affected by our decision. But which balloon should we save for last ? anyone of them may end up with a different cost. In such a case we are left with no choice but to try every one of them as the last balloon to pop and pick the one that maximizes our profit.</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">We&#39;ll solve this problem using dynamic programming. The recurrence relation is as follows:</span></p>
<p style="margin-left:30px;"><strong><span style="font-size:medium;">D[i,j] = max{over all k: D[i,k - 1] + D[k + 1,j] &nbsp;+ (B[i - 1] * B[k] * B[j + 1])}</span></strong></p>
<p style="margin-left:30px;"><span style="font-size:medium;">notice how this recurrence relation maps to our hypothesis: D[i,j] represents P(N) and D[i,k -1],D[k + 1,j] represent our hypothesis: P(n &lt; N). We multiply B[k] (the last balloon to pop) with B[i - 1] and B[j + 1] because we assume that the rest of the balloons betwee i to k and between k to j have been popped already.&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">So now, writing the code should be straight forward:</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;">&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:medium;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Burst1.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Burst1.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Burst1.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Burst1.PNG" border="0" alt=" " width="645" height="617" /></a></span></p>]]></content:encoded>
    </item>
    <item>
      <title>Text Justification problem</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/text-justification-problem</link>
      <pubDate>Thu, 10 Mar 2016 10:33:10 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2016/03/10/text-justification-problem/</guid>
      <description><![CDATA[I&#39;ve heard of the text justification problem along time ago. But, I&#39;ve never taken the time...]]></description>
      <content:encoded><![CDATA[<p style="margin-left:30px;"><span style="font-size:large;">I&#39;ve heard of the text justification problem along time ago. But, I&#39;ve never taken the time to actually try to solve it, until&nbsp;recently when I decided to look it up again. In this post, I&#39;ll show how to solve the problem.</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">Let&#39;s first describe the problem: Given a series of words and a page width (line length), left justify the words to fit them in the lines based on the page width. &nbsp;More formally: given S = { Wi } for i &gt;= 0 and Pw &gt;= 0 the page width we need to return: R = { {Wi} } i&gt; 0 such that &nbsp;Sum(|Wi|) + Sum(Wi) &nbsp;&lt;= Pw.</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">As usual,&nbsp;we&#39;ll turn to our inductive approach for&nbsp;solving the problem. Let&#39;s define Cost(i,j) - the cost of putting Wi ... Wj in a single line. The cost function is what we need to actually minimize. We&#39;ll define it more specifically later on, so for now, let&#39;s assume that we are given this cost function and that if Wi... Wj can&#39;t be put on the same line it will return infinity, otherwise, it will return a score such that 0 &lt;= score &lt; infinity. </span></p>
<p style="margin-left:30px;"><span style="font-size:large;"><span style="text-decoration:underline;"><strong>Hypothesis:</strong></span> P(n &lt; N) - We know how to get the minimum score for arranging words Wi ... Wn (inclusive) in multiple lines in a justified manner (e.g. with the minimal accumulative score).<br /></span></p>
<p style="margin-left:30px;"><span style="font-size:large;"><strong><span style="text-decoration:underline;">Base case</span></strong> P(1) &nbsp;= Score(1,1).</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">How do we reduce P(N) to P(n &lt; N)? well, let&#39;s start by fitting some K words in the last line. The score for those words is Score(<span style="font-size:x-large;">W<span style="font-size:large;">N - k + 1,&nbsp;</span></span></span><span style="font-size:x-large;">W</span><span style="font-size:large;">N). Once we have determined the cost of these words we are left with N - K &lt; N words to fit in the rest of the lines. based on our hypothesis we know how to score them!</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">So the algorithm works as follows:</span></p>
<p style="margin-left:30px;"><em><strong><span style="font-size:large;">Initialization: P(-1) = INFINITY</span></strong></em></p>
<p style="margin-left:30px;"><em><strong><span style="font-size:large;">1. For each &nbsp;0 &lt;= i &lt;= N:</span></strong></em></p>
<p style="margin-left:30px;"><em><strong><span style="font-size:large;">&nbsp; &nbsp; 2. For each 0 &lt;= j &lt;= i:</span></strong></em></p>
<p style="margin-left:30px;"><em><strong><span style="font-size:large;">&nbsp; &nbsp; &nbsp; &nbsp; 3. P(i) = min(P(i),P(j - 1) + Score(j,i))</span></strong></em></p>
<p style="margin-left:30px;"><span style="font-size:large;">You may have already noticed that the code is arranged in a dynamic programming fashion. In fact, TeX uses this exact approach to justify text .The only thing left to be shown is how the Score(i,j) function works. The truth is, that the score function is very straightforward. It takes the difference between the page width and the sum of word length i...j (including spaces) and raises it to some power. We&#39;ll use the third power in our scoring function. Furthermore, we would like to be able to keep track of the words we put in each line. After all, the point of this exercise is to justify the text, and not just hand out scores. To do so, we&#39;ll use an additional data structure Parent(i) that will save back pointers to the previous line. As we determine the minimum j (from above) we also update Parent(i) = j - 1. this way we can retrieve the words to be arranged in each line &nbsp;(in reverse order). Now let&#39;s see some actual code:</span></p>
<p style="margin-left:30px;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/8750.Justify1.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/8750.Justify1.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/8750.Justify1.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/8750.Justify1.PNG" border="0" alt=" " /></a></p>
<p style="margin-left:30px;"><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify2.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify2.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify2.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify2.PNG" border="0" alt=" " /></a><br /></span></p>
<p style="margin-left:30px;"><span style="font-size:large;">Let&#39;s look on how the main function uses Justify to print a set of words in a justified manner:</span></p>
<p style="margin-left:30px;"><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify3.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify3.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify3.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify3.PNG" border="0" alt=" " /></a></span></p>
<p style="margin-left:30px;"><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify4.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify4.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Justify4.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Justify4.PNG" border="0" alt=" " /></a><br /></span></p>
<p style="margin-left:30px;"><span style="font-size:large;">As a final comment I will just note that you can take a slightly different (but equivalent) approach, and instead of filling the last line and calculate the score of the first lines, you first try to fill the first line and calculate the score for the rest of the line. I personally find dynamic programming to be easier to grasp when you look at it from end to beginning rather than the other way.&nbsp;<br /></span></p>]]></content:encoded>
    </item>
    <item>
      <title>Non-Recursive Postorder Tree Traversal Without Additional Data Structures.</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/non-recursive-postorder-tree-traversal-without-additional-data-structures</link>
      <pubDate>Thu, 18 Feb 2016 23:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2016/02/18/non-recursive-postorder-tree-traversal-without-additional-data-structures/</guid>
      <description><![CDATA[I would like to show today code to a problem that seems to be trivial at first, but turns out to be...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:medium;">I would like to show today code to a problem that seems to be trivial at first, but turns out to be not trivial at all. In fact,&nbsp;</span><span style="font-size:medium;">I haven&#39;t been able to find any equivalent solution anywhere yet (but I&#39;m still looking). I would like to present code that prints the values of a binary tree in a postorder fashion, without recursion.... and most interestingly, without any additional data structures. If you look in the internet on solutions to this problem you will find that all the solutions use a stack to keep track of the visited nodes. However, in the solution I will present to you I won&#39;t use a stack at all.<br /></span></p>
<p><span style="font-size:medium;">Before we talk about how to do this, lets remind ourselves of what a postorder traversal is. A postorder traversal traverses the tree in the following fashion:&nbsp;</span></p>
<p><span style="font-size:medium;">Left subtree -&gt; Right subtree -&gt; Root.&nbsp;</span></p>
<p><span style="font-size:medium;">Now, before we go about devising the algorithm for a postorder traversal without recursion and without any data structures, lets think how we can solve a simpler problem. Lets try to do the same but with an inorder traversal (e.g. Left subtree-&gt;Root-&gt;Right subtree).</span></p>
<p><span style="font-size:medium;">Since we can&#39;t use any additional data structures, we must use the tree&#39;s internal structure to keep track of where we are and how we visit each node. For this problem there is a very elegant solution that I will present now, and that you can easily find on the internet. Notice that once we are done traversing the Left subtree we need to jump back to the Root, but since we are not using any additional data structures we need to make sure that we have a way to get back to the Root node. I will first present the code, and then explain it.&nbsp;</span></p>
<p><span style="font-size:medium;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/7853.Inorder.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/7853.Inorder.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/7853.Inorder.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/7853.Inorder.PNG" border="0" alt=" " /></a>&nbsp;</span></p>
<p><span style="font-size:medium;">The algorithm runs in a loop so long as Root is not NULL. The first thing we do is get a pointer to the left child of Root. if that child is NULL, we need to print Root&#39;s value and move to the right child of Root (by making Root = Root-&gt;right). Left is the the left child of Root. Now comes the interesting part. We look at the right subtree of Left and we take the path all the way to the right. The node that will be used as the right most node of Left is R. In line 31 we run in a loop so long as R-&gt;right is not NULL (hence we need to go more to the right) or R is not equal to Root. At first the second part of this condition seems weird, but it will soon be all clear. At first, you may assume that R will not be equal to Root and so the loop in line 31 ends when R-&gt;right is equal to NULL. When that loops end, we check again to see on which of the conditions we stopped. The first time we stop because R-&gt;right is NULL. what we do is that we make R-&gt;right point back to Root. This is the most critical part of the algorithm, because this is how we are able to get back to Root after we finished traversing the left subtree. Once R-&gt;right is set to point to Root, we set Root to be equal to Left and continue the loop. Since we are going to be doing this continuously&nbsp;on every iteration, at some point we will reach back that node that was pointed to by R. Root will be equal to that node at that point. We will then realize that Root-&gt;left is NULL, print that node and then take the right path (as in line 26). Where will we end up when we take that right path ? well, obviously because we pre-set the right path of that node to point back up to the Root of the tree, we will end up there. Root will point once again to the original root of the tree. Now here is where things get interesting again. Left will again point to the left subtree of the original Root of the tree and R will now traverse the right subtree of Left as before. However, this time the condition in line 31 will stop due to the fact that R-&gt;right will be equal to Root. We will end up in else clause at line 41. In this case what do we know? We know that we have just completed traversing the left subtree of the original tree, and that we need to print the Root and move to the right subtree. That is exactly what lines 43 - 45 do. However, in addition, they also clean up the &quot;mess&quot; we created, by setting R-&gt;right to be NULL again. This is fine, because after this point we will no longer be traversing this subtree again. Root will be set to Root-&gt;right and we will do everything we did all over again, but this time to the right subtree. Notice that the algorithm will end after Root points to the right most node in the right most subtree. If you find this to be a little tricky, I suggest going over the code, and playing on a paper with a &quot;tree&quot; of your own, so that you can convince yourself that this works.&nbsp;<br /></span></p>
<p><span style="font-size:medium;">We are now ready to see how to do a postorder traversal in a similar fashion. However, I must warn you that the postorder traversal, although similar in principle, is still more complicated. We&#39;ll understand why soon. Before that, lets ask ourselves what was it about the above solution that helped us reach our goal ? The answer is that by setting the rightmost node of the left subtree to point to the Root we achieved a way&nbsp;to get back to the root of the original tree. This also served as a &quot;marker&quot; for us that we completed traversing the left subtree and that it is time to print Root&#39;s value and move on to the right subtree. A key observation is that once we set Root to point to Root-&gt;right we no longer need to traverse Root again and we only focus on the right subtree. In other words, we forget about Root and move on.</span></p>
<p><span style="font-size:medium;">When we do postorder traversal however, things are much more complicated. This is because Root gets printed last. We can&#39;t just forget about it and move to another subtree. Once we are done traversing the left subtree we need to traverse the right subtree and only at the end we print Roots value. This fact is what causes the solution above to break if we try to apply it in similar fashion to the postorder case. However, let&#39;s not despair. We can still use the above solution, with some modifications. But, the devil is in the details, so pay close attention. Again, I will present first the code, and explain it in detail afterward.</span></p>
<p><span style="font-size:medium;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/8562.Postorder.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/8562.Postorder.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/8562.Postorder.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/8562.Postorder.PNG" border="0" alt=" " /></a><br /></span></p>
<p><span style="font-size:medium;">Ok, this code is much longer. But the structure should look familiar. You will immediately notice that in the first lines I create a dummy node named &quot;Dummy&quot; and make Root be its left subtree. The reason I do this is because without it I will not have a way to get back to Root (the original root of the tree) after I complete traversing the right subtree. By adding this dummy node, I&#39;m turning&nbsp;Root into a left subtree of another tree, and based on our previous solution we already saw that getting from a left subtree back to the root of a tree is simple, by using a back pointer. Once we have set Root to be the left subtree of the dummy node, we set Root to point to the dummy node (in line 21). At this point we start our loop. We have Left point again to the left subtree of Root. If Left is NULL, we need to move to the right subtree (without printing Root). Otherwise, we do the traversal of the right subtree of Left in lines 35 - 39 just as we did in the inorder traversal. Notice that we also count the levels we traverse on the right path. This count will be used later on when we revisit this subtree again. On the first time and just as in the inorder case R-&gt;right will be NULL at some point (no back pointer). We will end up in lines 43-44. In those lines we will set the back pointer up to Root and set Root to be Left. So far, nothing fancy at all. The fun part comes after we complete traversing the entire left subtree and we end up with Root pointing to what was pointed to by R (with the back pointer). Root-&gt;left will be NULL and so we&#39;ll set Root to be Root-&gt;right. However, Root-&gt;right is our back pointer that will lead us back to our original Root of the original tree (in this case it will be the dummy node). So now, we have Root pointing to the dummy node again. Again, we have Left pointing to the left subtree (which is in this case the original tree we were supposed to traverse) and we start taking the right path using R in lines 35-39. However, this time the loop will terminate because R-&gt;right will be equal to Root (or Dummy if you will). Lets stop for a second and think what does it mean ? what it means is that we completed traversing the Left subtree (of the left subtree of Dummy) and we still haven&#39;t completed the right subtree (of the left subtree of dummy). But this is where things really get tricky. We have to print the right subtree in a bottom-up fashion, starting from R going up all the way to Left. This is where&nbsp;&quot;Count&quot; from lines 33 and 37 comes to play. When we reach the point that we realize that R-&gt;right is equal to Root (line 46) we loop so long as Count &gt;= 0. For each Count we continuously&nbsp;go down the right path from Left but each time stopping one level above the level we were in the previous loop. When we stop we print the value of that node and continue looping. Once we are done with this loop we can clean up the &quot;mess&quot; just as we did in the inorder case, and move to the right. Notice that since Dummy has only a left subtree, moving to the right means we essentially end the traversal and break from the main loop in line 23. The last thing we do just before exiting is to delete Dummy.<br /></span></p>
<p><span style="font-size:medium;">That&#39;s it. A little long, and slightly tedious, but hopefully you were able to keep up and get the gist of it.&nbsp;</span></p>]]></content:encoded>
    </item>
    <item>
      <title>Playing with Palindromes - Part 1</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/playing-with-palindromes-part-1</link>
      <pubDate>Thu, 15 Oct 2015 13:53:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2015/10/15/playing-with-palindromes-part-1/</guid>
      <description><![CDATA[Today I&#39;ll show some problems involving palindromes:
The first problem is stated as followed:...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:large;">Today I&#39;ll show some problems involving palindromes:</span></p>
<p><span style="font-size:large;">The first problem is stated as followed: &quot;Given a string S, return the minimum number of characters required to be deleted from S, in order to turn it into a Palindrome&quot;.</span></p>
<p><span style="font-size:large;">By definition, the minimum characters solution will give us also the maximal size palindrome, but lets focus first on the minimum. </span></p>
<p><span style="font-size:large;">Lets define the problem as P(N) - the minimum number of characters required to be deleted to create a palindrome from string S of size N. if S is a palindrome, then the answer should be 0. if S has all unique characters, then the answer should be N - 1 (since a string of size 1 is a palindrome). Lets assume that we know how to solve this problem for any string n &lt; N. How can we solve P(N + 1) ? there are two options here: the first is to think how we can extend the solution from n &lt; N to N + 1. The second is to think how we can reduce N + 1 to n &lt; N. in this case the second option will be easier. consider the N+1&#39;th character of S. it either participates in the palindrome or it doesn&#39;t. It can participate in the palindrome in two cases: the first, it is equal to the first character of S, in which case we need to see what P(N - 2) is equal to. In the second case, it can participate in a palindrome that starts at the second character of S for example S = BS&#39;A &nbsp;|S| = N + 1, and S&#39; = AS&#39;&#39; ,&nbsp;<span>|S&#39;| = N - 2. In this case we need to check P(N - 1) and add 1 to its result (since we decided to delete the first character of S). The last option is that the N+1&#39;th character is not present in the solution, so we need to delete it. In this case we need to check again P(N - 1) and add 1 to it (since we are about to delete the N + 1&#39;th character of S).&nbsp;</span></span></p>
<p><span style="font-size:large;"><span>Notice that our problem definition in terms of just N is not very robust. Instead of defining the problem in terms of just N,let adds an additional parameter: the beginning of the string.</span></span></p>
<p><strong><span style="text-decoration:underline;"><span style="font-size:large;">P(i,n &lt; N) - we know how to find the minimal number of character&nbsp;deletions for &nbsp;any sub string of S starting&nbsp;at index i and of size n &lt; N.</span></span></strong></p>
<p><strong><span style="text-decoration:underline;"><span style="font-size:large;">Base case: P(i,1) &nbsp;= 0</span></span></strong></p>
<p><strong><span style="text-decoration:underline;"><span style="font-size:large;">&nbsp;</span></span></strong></p>
<p><em><span style="font-size:large;">P(i,N) = MIN{S[i] == &nbsp;S[i + N - 1] ? P(i + 1, N - 2) : INFINITY ,P(i + 1,N - 1) + 1, P(i,N - 1) + 1}</span></em></p>
<p><span style="font-size:large;"><strong></strong>The above equation is merely a formalization of what we spoke about in the paragraph above (using the ternary operator).</span></p>
<p><span style="text-decoration:underline;"><span style="font-size:large;">How does the code look like for this:</span></span></p>
<p><span style="text-decoration:underline;"><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/6663.MinDelsToPal.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/6663.MinDelsToPal.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/6663.MinDelsToPal.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x550/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/6663.MinDelsToPal.PNG" border="0" alt=" " /></a><br /></span></span></p>
<p><span style="font-size:large;">In such a problem in which there is the optimal substructure property, dynamic programming is called for.</span></p>
<p><span style="font-size:large;">The run time complexity of this code is O(N^2).&nbsp;</span></p>
<p><span style="font-size:large;">I&#39;ll come back with some additional problems related palindromes soon enough.</span></p>
<p><em><span style="font-size:large;">&nbsp;</span></em></p>
<p><span style="font-size:large;">&nbsp;</span></p>
<p><span style="font-size:large;">&nbsp;</span></p>
<p><span style="font-size:large;">&nbsp;</span></p>
<p><span style="font-size:large;"><span>&nbsp;</span></span></p>
<p><span style="font-size:large;"><span>&nbsp;</span></span></p>
<p><span style="font-size:large;">&nbsp;</span></p>
<p></p>]]></content:encoded>
    </item>
    <item>
      <title>If you wanna get sh** done ... Python is there for your rescue.</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/if-you-wanna-get-sh-done-python-is-there-for-your-rescue</link>
      <pubDate>Wed, 16 Sep 2015 11:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2015/09/16/if-you-wanna-get-sh-done-python-is-there-for-your-rescue/</guid>
      <description><![CDATA[How I wish life could be automated. Well, at least the boring parts of it. I recently realized that...]]></description>
      <content:encoded><![CDATA[<p style="margin-left:30px;"><span style="font-size:large;">How I wish life could be automated. Well, at least the boring parts of it. I recently realized that if every aspect of life could be automated, Python would have been my language of choice to do so.</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">No, you won&#39;t hear from me in this post how Python is the best language I&#39;ve encountered.You won&#39;t write a line of code in Python that will make you shed a tear of happiness because you just wrote the most amazing line of code in your life - this is saved for C++. But if you want to get things done quickly and neatly, then Python is there for you. The abundance of libraries and the ease at which you can do things in the language makes me smile. The language has been created for lazy programmers as myself. I skimmed through a python book once to the point where the syntax made sense to me. From that point on, anything I needed I found on line. I would like to share in this post some of the nice things I was able to achieve with Python that made my life much happier.</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">In my previous group at Microsoft, we worked for a long time on a highly&nbsp;impact-full&nbsp;feature. The design and&nbsp;development&nbsp;of this feature took over a year. It was an amazing ride, and for such an amazing ride you take with you only the best tools. So my obvious choice was C++. By the end of the project I had written tens of thousands of &nbsp;C++ lines of code. the feature itself was shipped as a DLL, and was intended to be called by another layer of the stack. Our biggest concern was testing. How do you verify the correctness of so many scenarios supported by this huge DLL. More specifically, we wanted to find a way to test our DLL in isolation (without taking dependencies on other layers of the stack). We realized that a testing harness was called for, and my initial approach (given that it was a native C++ DLL) to write this testing harness in C++ as well. &nbsp;However, call it luck or call it coincidence, it so happened that I got my hands on a Python book. As I mentioned before, I skimmed through this book, learning all the necessary stuff I wanted to learn about the language. I&nbsp;immediately&nbsp;suspected that since the&nbsp;interpreter was written in C, the language might have a good way of interfacing with native code. Indeed, my suspicions were correct. Python in fact has a great way of interacting with native code (the ctypes library), and specifically interacting with native DLLs. After a small conversation with my manager, I decided to give it a shot.&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">After a month worth of Python coding I was finally ready to demo my testing harness. It was in fact a great success. It was even more&nbsp;successful&nbsp;than I originally anticipated for it to be. Using this new harness I was able to write many scenario (integration) tests that usually required a full fledged system with all the layers of the stack available. The testing harness allowed myself and others working on this project to write python scripts that would call the DLL (as a blackbox) and test the specific scenario, enabling to validate the correctness of the system. Not only that, the testing harness proved itself extremely useful for finding systematic bugs that would otherwise take a very long time to reproduce or detect. It was only then that I realized the power of a great scripting language. It was only then that I realized the importance of using the right tool for the right task. As much as I love C++, Python was the best tool to use at that point, and I&#39;m happy I chose it.</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">Since then, I&#39;ve adopted Python as a step brother to C++. If I need to get *** done, I don&#39;t think about it twice. I go for Python. A nice IDE that I found to be extremely useful is the PyCharm IDE, I highly recommend it. As a last story about how I find Python to be useful, I&#39;ll tell about a manual task that my new manager (at my new team at Bing) asked me to do. My manager asked me to go through pairs of URLs (page url and image url) and figure out some properties about them.&nbsp;Unfortunately, that task could not be automated, and required that I open each pair of URLs in a browser and look at them to detect some&nbsp;anomalies. &nbsp;However, given that I&#39;m a lazy programmer (yea, yea I already said that), I figured that the fact that the task can&#39;t be automated does not mean that the process of reviewing them can&#39;t be automated. So I&nbsp;immediately wrote a python script that read the hundreds of URL pairs from a file, and opened the browser with each pair for me, awaiting a keyboard stroke to move on to the next pair. Yep, in less than an hour of work, I was able to make my life so much easier and so much more comfortable.&nbsp;</span></p>
<p style="margin-left:30px;"><span style="font-size:large;">To conclude. I highly recommend picking up python. It is a great tool for most of the stuff that you want to automate. For additional resources on the language I also recommend reading the book: <strong>&quot;<em>Automate the boring stuff with Python: Practical &nbsp;Programming for Total Beginner&quot;</em></strong> by <strong>Al Sweigart</strong>. Enjoy.</span></p>]]></content:encoded>
    </item>
    <item>
      <title>Permutation Sequence (LintCode)</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/permutation-sequence-lintcode</link>
      <pubDate>Thu, 10 Sep 2015 12:37:00 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2015/09/10/permutation-sequence-lintcode/</guid>
      <description><![CDATA[Today I&#39;ll show a solution to another nice problem from LintCode: Permutation...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:medium;">Today I&#39;ll show a solution to another nice problem from LintCode: <em>Permutation Sequence</em>.&nbsp;</span></p>
<p><span style="font-size:medium;">The description of this problem is as followed:</span></p>
<p></p>
<p><span style="font-size:medium;">&nbsp;</span></p>
<p><span style="font-size:medium;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/LIntCode1.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/LIntCode1.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/LIntCode1.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/LIntCode1.PNG" border="0" alt=" " /></a><br /></span></p>
<p><span style="font-size:medium;">As the description says, the most intuitive way to solve this problem is to start enumerating all permutation until we reach the K&#39;th one. However, this would make the problem not very interesting. We would like to solve it differently. We&#39;ll start by observing all the enumerations for N = 3 (above). We see that the numbers 1,2,3 appear as the leading digits two times each. For each instance in which 1 is the leading digit, the digits 2 and 3 appear as the second digits only once, and as the third (and last) digits only once. What if we would have had N = 4 ? &nbsp;In this case 1,2,3,4 would appear as the leading digits six times each (4*6 = 4!). For each instance that they appear as leading digits, we have the rest of the digits appearing 2 times each as the next one in line (just like the case of N = 3) and the leftover digits appearing as second to last 1 time each yet again ....</span></p>
<p><span style="font-size:medium;">This observation should hint us to a potential solution. We can find the next leading digit based on K and N. If N = 3 and K = 4 it means that the permutation that we are looking for starts with 2. Why ? Because we have observed that each leading digit appears two times in the total permutations of the numbers 1 - 3. In other words since we have buckets of size 2 for each leading digit, we can find the leading digit by (K-1/Bucket Size) Notice that we need to scale K from 1 - K to 0 - (K-1) scale. So we can find the first leading digit... what about the next leading digit ? lets continue with our example of N = 3 K = 4. Once we found the bucket that the K&#39;th number falls into, we can get rid of the leading digit as we will not need it anymore. We are left with digits 1 and 3 in this order. Since we have two digits we can be in either the permutation 2 1 3 or the permutation 2 3 1. Which one is it ? luckily we can apply the same logic using K to find which of these two we need to choose. Since our buckets are of size 2, K - 1 % 2 will tell us which of the two options we need to choose. In this case we choose the second permutation. We will apply the same logic exactly to find the last digit.</span></p>
<p><span style="font-size:medium;">The logic is now clear. However, looking closely it seems that we still need to order the permutations in some way so that we can choose the right permutation based on K. Do we really need to order the permutations ? Actually we don&#39;t. We only need to enumerate the digits in their natural ordering &nbsp;- 1 2 3 4 ... N . We pick the next digit based on K and the bucket size which is of size N!/N = N-1! . Once we find our digit, we remove it from the list and leave the other digits in tact. We return to choose the next digit from the list by performing the logic described above, but this time we adjust the bucket size to be (N-1!)/N-1 = N-2! and using (K - 1)% (Old Bucket Size) as our new K. We continue this iterative process until we are left with no digits.</span></p>
<p><span style="font-size:medium;">Now that we understand the logic, lets look at the code:</span></p>
<p><span style="font-size:medium;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Perms.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Perms.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Perms.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Perms.PNG" border="0" alt=" " /></a></span></p>
<p><span style="font-size:medium;">&nbsp;</span></p>
<p><span style="font-size:medium;">&nbsp;</span></p>
<p><span style="font-size:medium;">&nbsp;</span></p>]]></content:encoded>
    </item>
    <item>
      <title>Building an Expression Tree</title>
      <link>https://docs.microsoft.com/archive/blogs/galstertechblog/building-an-expression-tree</link>
      <pubDate>Sun, 19 Jul 2015 23:31:51 GMT</pubDate>
      <dc:creator><![CDATA[Guy Alster]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.technet.microsoft.com/galstertechblog/2015/07/19/building-an-expression-tree/</guid>
      <description><![CDATA[I recently solved a problem in yet another online judge website (LintCode) which asked to build an...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size:large;">I recently solved a problem in yet another online judge website (LintCode) which asked to build an expression tree from an infix expression.&nbsp;</span><span style="font-size:large;">I found this problem to be quite challenging and not as straightforward as I originally anticipated. Hence, I&#39;m dedicating this post to it.</span></p>
<p><span><span style="font-size:large;">To start lets define an expression tree. This is a binary tree in which every inner node represents an operator and every leaf represents an operand. The input for the problem is an infix&nbsp;expression&nbsp;of the form: &quot;2&quot; &quot;*&quot; &quot;6&quot; &quot;-&quot; &quot;(&quot; &quot;23&quot; &quot;+&quot; &quot;7&quot; &quot;)&quot; &quot;/&quot; &quot;(&quot; &quot;1&quot; &quot;+&quot; &quot;2&quot; &quot;)&quot;. The quotation marks are deliberate, as each operand/operator is a&nbsp;separate&nbsp;string. The set of strings is received in a vector.&nbsp;</span></span></p>
<p><span><span style="font-size:large;">We are given the definition of the tree structure as followed:&nbsp;</span></span></p>
<p><span><span style="font-size:large;"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/Tree.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/Tree.PNG" border="0" alt=" " /></span></span></p>
<p><span><span style="font-size:large;">If we look carefully at the definition of the tree we notice that we need to transform the infix notation into a prefix notation. The reason is that the tree itself is built from the prefix notation. The problem then comes down to turning the infix expression into a corresponding prefix expression. To do so we realize that in order to transform the infix expression to its equivalent prefix notation we need to scan the infix backwards (from end to start). This way we put the operands last and the operators first with special care to operator precedence.&nbsp;</span></span></p>
<p><span><span style="font-size:large;">To implement the design we&#39;ll leverage two stacks. The first will hold only operators: &quot;+,-,*,/,(,)&quot;. The stack will hold them according to their precedence. More precisely, &quot;+&quot; or &quot;-&quot; can follow only &quot;)&quot; and other &quot;+&quot; and &quot;-&quot; already pushed to the stack. However, if the top of the stack contains &quot;*&quot; or &quot;/&quot; then we need to handle them first before pushing the &quot;+&quot; or &quot;-&quot; to the stack. Also, when we encounter &quot;(&quot; that means that we just finished processing an inner expression. In this case we have to first handle all the operators that have been pushed after the matching &quot;)&quot;. When we encounter: &quot;)&quot;,&quot;*&quot;,&quot;/&quot; we needn&#39;t do anything special and we can just push them into the stack.&nbsp;</span></span></p>
<p><span><span style="font-size:large;">The second stack that we&#39;ll use is an expression stack. this stack will contain all of our (Tree Node) expressions as we build them. Since we are building a binary tree, we have to make sure that every operator (from the operator stack) that is being handled, will consume two expressions from the expression stack.</span></span></p>
<p><span><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/0511.Tree1.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/0511.Tree1.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/0511.Tree1.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/0511.Tree1.PNG" border="0" alt=" " /></a></span></span><span style="font-size:large;">&nbsp;&nbsp;</span></p>
<p style="margin-left:30px;"><span><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/7853.Tree2.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/7853.Tree2.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/7853.Tree2.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/410x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/7853.Tree2.PNG" border="0" alt=" " /></a></span></span></p>
<p><span><span style="font-size:large;"><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/6165.Tree2.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/6165.Tree2.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/6165.Tree2.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/410x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/6165.Tree2.PNG" border="0" alt=" " /></a></span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/3108.Tree3.PNG" original-url="https://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/3108.Tree3.PNG"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/01/60/3108.Tree3.PNG" original-url="https://blogs.technet.com/resized-image.ashx/__size/420x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-01-60/3108.Tree3.PNG" border="0" alt=" " /></a></p>
<p></p>
<p><span style="font-size:large;">The run time of this algorithm is O(N), for N - the size of the input vector.&nbsp;</span></p>]]></content:encoded>
    </item>
  </channel>
</rss>