<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sai Panyam.NET &#187; Technology</title>
	<atom:link href="http://www.saipanyam.net/topics/technology/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saipanyam.net</link>
	<description>On all things .NET &#38; Software Engineering</description>
	<lastBuildDate>Thu, 05 Jan 2012 16:42:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Gale Shapley Algorithm for Stable Matching</title>
		<link>http://www.saipanyam.net/2011/09/gale-shapley-algorithm.html</link>
		<comments>http://www.saipanyam.net/2011/09/gale-shapley-algorithm.html#comments</comments>
		<pubDate>Wed, 14 Sep 2011 02:24:15 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Stable Marriage Problem]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=651</guid>
		<description><![CDATA[Achieving Stable Matching between two sets of entities with various preferences for each other is a real world problem (a.k.a Stable Marriage Problem). Examples include dating sites, matching medical students to hospital jobs (National Resident Matching Program) etc. How can we ensure that there is a stable matching (defined as any pairings, such that no [...]]]></description>
			<content:encoded><![CDATA[<p>Achieving Stable Matching between two sets of entities with various preferences for each other is a real world problem (a.k.a Stable Marriage Problem). Examples include dating sites, matching medical students to hospital jobs (National Resident Matching Program) etc. How can we ensure that there is a stable matching (defined as any pairings, such that no entity can find any one they would rather be with, who would rather be with them)? Such a matching would be stable (or in equilibrium) because any further change would be detrimental to the entities involved in terms of their stated preferences. Some questions that arise are whether such a matching actually exists? If it exists, how do we find it? How can we be confident that the solution that we get is the &#8216;best&#8217; among all possible matchings? These issues are not trivial and when the elements of the entity sets increase, it soon becomes an intractable problem to solve by trial and error.</p>
<p>Thankfully in answer to the question of whether a stable matching exists, we have a theorem that guarantees exactly that! It states that:<br />
&#8220;For any number entities of two disjoint sets (having equal number of elements), no matter how they rank each other, there always <strong>exists</strong> at least <strong>one stable matching</strong>&#8221;</p>
<p>While the solution is stable, it is not necessarily optimal from all individuals&#8217; points of view. The traditional form of the algorithm is optimal for the initiator (suitor) of the proposals and the stable, suitor-optimal solution may or may not be optimal for the acceptor of the proposals. It will be acceptor pessimal.</p>
<p>To find the solution, we have the Gale-Shapley Algorithm. In 1962,&nbsp;&nbsp;<a title="David Gale" href="http://en.wikipedia.org/wiki/David_Gale">David Gale</a>&nbsp;and&nbsp;<a title="Lloyd Shapley" href="http://en.wikipedia.org/wiki/Lloyd_Shapley">Lloyd Shapley</a>&nbsp;proved that, for any equal number of suitors and acceptors, it is always possible to solve the Stable Marriage Problem.</p>
<p>&nbsp;</p>
<pre>The Gale-Shapely Algorithm:
Each suitor proposes to his highest preferred acceptor. If the acceptor is not already matched, we automatically match suitor to acceptor. If the acceptor is previously matched; acceptor picks the most preferred suitor. The rejected suitor moves on to his next preferred acceptor. When each suitor is matched the problem is solved.</pre>
<h3>Download</h3>
<p>Download&nbsp;<a href="http://www.saipanyam.net/wp-content/downloads/StableMatching.zip" target="_new">source code</a>. It is an implementation of the algorithm in .NET 4 using Visual Studio 2010 and C#.</p>
<pre>
/Here Optimal Entity is the Suitor and Pessimal Entity is the acceptor
//The function that implements the Gale-Shapley algorithm in .NET is shown
public static SortedDictionary&lt;T,U&gt; Match(HashSet&lt;SuitorEntity&lt;T, U&gt;&gt; optimalEntities, HashSet&lt;AcceptorEntity&lt;U, T&gt;&gt; pessimalEntities)
        {
            Dictionary&lt;AcceptorEntity&lt;U, T&gt;, SuitorEntity&lt;T, U&gt;&gt; matching = new Dictionary&lt;AcceptorEntity&lt;U, T&gt;, SuitorEntity&lt;T, U&gt;&gt;();
            Stack&lt;SuitorEntity&lt;T,U&gt;&gt; processStack = new Stack&lt;SuitorEntity&lt;T, U&gt;&gt;(optimalEntities);
            while(processStack.Count&gt;0)
            {

                //Store each suitor in a Stack.
                SuitorEntity&lt;T, U&gt; suitorEntity = processStack.Pop();
                // Match the optimal entity to its first preference
                //If its first preference is not previously matched then automatically match and dequeue
                SuitorEntity&lt;T, U&gt; matchedEntity;
                AcceptorEntity&lt;U, T&gt; acceptorEntity = suitorEntity.Preferences.Dequeue();
                if(!matching.TryGetValue(acceptorEntity,out matchedEntity))
                {
                    matching.Add(acceptorEntity, suitorEntity);
                }
                else//Else
                {
                    foreach (SuitorEntity&lt;T,U&gt; entity in acceptorEntity.Preferences)
                    {
                        if(matchedEntity == entity)
                        {
                            //The Pessimal Entity makes a choice: If it already has a better preference then it keeps it
                            processStack.Push(suitorEntity);
                            break;
                        }
                        // Replace the matching of the acceptor with the most preferred new suitor
                        if (suitorEntity != entity) continue;
                        matching[acceptorEntity] = suitorEntity;
                        //Push the previously matched suitor who is now rejected on to the stack for further processing
                        processStack.Push(matchedEntity);
                        break;
                    }

                }

            }
            SortedDictionary&lt;T,U&gt; result = new SortedDictionary&lt;T, U&gt;();
            foreach (KeyValuePair&lt;AcceptorEntity&lt;U, T&gt;, SuitorEntity&lt;T, U&gt;&gt; keyValuePair in matching)
            {
                result.Add(keyValuePair.Value.Entity,keyValuePair.Key.Entity);
            }
            return result;
        }</pre>
<h3>References</h3>
<p><a title="Stable Marriage Problem" href="http://mathsite.math.berkeley.edu/smp/smp.html" target="_blank">Interactive Flash Demonstration of Stable Marriage Problem</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/09/gale-shapley-algorithm.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shape of Contextual Streams</title>
		<link>http://www.saipanyam.net/2011/07/shape-of-contextual-streams.html</link>
		<comments>http://www.saipanyam.net/2011/07/shape-of-contextual-streams.html#comments</comments>
		<pubDate>Wed, 20 Jul 2011 01:39:25 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Evolution]]></category>
		<category><![CDATA[g+]]></category>
		<category><![CDATA[Models]]></category>
		<category><![CDATA[Stream]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=562</guid>
		<description><![CDATA[In a previous post I talked about stream evolution. In it I alluded to an evolutionary phase for streams: Contextual Streams. Since then I searched the blogosphere to see if someone had a solution or even a semblance of a model for describing this phase. Unfortunately I only found a list of wants, people are just [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post I talked about <a title="Solution to the Scoble Problem – A Study in Stream Evolution" href="http://www.saipanyam.net/2011/07/solution-to-the-scoble-problem-a-study-in-stream-evolution.html">stream evolution</a>. In it I alluded to an evolutionary phase for streams: Contextual Streams. Since then I searched the blogosphere to see if someone had a solution or even a semblance of a model for describing this phase. Unfortunately I only found a list of wants, people are just airing what they want to see in social networks. Typically this is how it goes: They describe what facebook did or did not do till now, they will layout how circles from Google + works and then conclude with what they want to see in Google +. This is all fine, as it provides the social networking leaders with free product suggestions.  But the essential problem is, <strong>no one</strong> has <strong>described or </strong><strong>modeled</strong> a solution. The users rightly feel it is the job of these companies and the companies are so inundated with feature requests and executive mandated deadlines that they have no time or will to think of a &#8216;pure&#8217; solution. Since no one has a model (as far as I know) I took it upon myself to start on this ginormous task.</p>
<p>First let me start with basics (more advanced (jaded <img src='http://www.saipanyam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) readers can skip this paragraph). A social network has two facets: <em>Structure and Behavior</em><strong><em>. </em></strong>When we talk about a network, we typically talk about how we are connected to others in the network and also how &#8216;connected&#8217; we are at the interaction level.<strong> Graph Theory</strong> is the language of structure, whereas <strong>Game Theory</strong> is the language of behavior for social networks. A structure is a snapshot of a social network at a particular instance of time. Behaviors define how the structure gets transformed over a period of time. Behavior is the dynamic part of social networks. One might think, that behavior is the cause and structure the effect. But at scale, structure also influences behavior and behavior in turn influences structure, forming a virtuous (or destructive) cycle. A structural model has big implications on the underlying data structures that store and manipulate data. So getting this model right is a fundamental step.</p>
<h3>Current Models</h3>
<p>In this post, I would like to propose a model for the structural aspects of a contextual stream. Model is a representation of a system or phenomenon. In computer science, a model is used to simulate a process, concept or operation of the system. My premise is that the existing models are no longer sufficient to explain or operate the <em>system</em>. A system is only as good as the model. History abounds with examples where our models were wrong. We moved from &#8220;earth is flat&#8221; to &#8220;earth centric model of the universe&#8221; to &#8220;Helio centric model of solar system&#8221;. Each revision in our models brought about great progress.</p>
<p>The design constraints faced by us today in social networking is similar, because we are limited by our existing models. Before I proceed with a revamped model for the structure of a social network, let me go over what we have today.</p>
<p>A social network is modeled as a graph of &#8216;friends&#8217;. An extension to this in recent years is an affiliation network super imposed on a traditional network of friends.</p>
<div id="attachment_574" class="wp-caption aligncenter" style="width: 587px"><img class="size-full wp-image-574 " title="Friends Model" src="http://www.saipanyam.net/wp-content/uploads/2011/07/Friends-Model.png" alt="Friends Model" width="577" height="298" /><p class="wp-caption-text">Traditional way of modelling friendships in social networks</p></div>
<p>&nbsp;</p>
<p>The rise of groups, lists, circles are an example of this. An affiliation network is modeled as a <a title="Bipartite Graph" href="http://en.wikipedia.org/wiki/Bipartite_graph" target="_blank">bipartite graph</a>.  A bipartite graph by definition connects two disjoint and independent sets. I took some of my groups from Linked In for illustration purposes.</p>
<div id="attachment_577" class="wp-caption aligncenter" style="width: 352px"><img class="size-full wp-image-577 " title="Affiliation Model" src="http://www.saipanyam.net/wp-content/uploads/2011/07/Affiliation-Model.png" alt="Affiliation Model" width="342" height="527" /><p class="wp-caption-text">Groups, Lists, Circles model of social networks</p></div>
<p>These two structural models served our purposes till now. The affiliate model came about to serve the need to form communities around shared interests. Accessibility is the only difference between a group on Linked In and a circle in Google +. A circle is a private classification , whereas a group on Linked In is public. (Note: I removed the connections among &#8216;friends&#8217; to  make the model uncluttered)</p>
<h3>Contextual Model</h3>
<p>A contextual model is a network of &#8216;contexts&#8217;. Here we model our &#8216;friends&#8217; as<em> contexts too </em>!!!. We can imagine that circles, lists and groups are an attempt to set context. So in effect all circles, lists and groups are contexts in which our social posts reside. By definition <em>public </em>is an over encompassing context. It is a set of all possible contexts. Now the key insight is to <em>model </em>our friend connections as contexts. If you think about it, when we post to our friends, we are implying a context. Ergo it is true of lists and circles. A <em>friend</em> is a circle (list or group) which contains one member. Once we model a friend as a context, then we can use the same language of graph theory to divine better and more information about our <em>connections</em> and the <em>context</em> in which these play out. Historically affiliations are modeled right after friends, in a typical product iteration cycle. So engineers instead of going back to the drawing board, tack on the affiliations. What I am proposing is to revisit our current models and make <em>context the fundamental unit</em> of a social network.</p>
<div id="attachment_586" class="wp-caption aligncenter" style="width: 608px"><img class="size-full wp-image-586 " title="Contextual Model" src="http://www.saipanyam.net/wp-content/uploads/2011/07/Contextual-Model.png" alt="Contextual Model" width="598" height="510" /><p class="wp-caption-text">Modelling contexts as a fundamental unit of social networks</p></div>
<p>This is just not a jumbled &#8216;affiliation&#8217; network. There are two key differences, each &#8216;friend&#8217; is a context (on par with the groups) and the presence of links between groups (Circles) &#8211; shown as dashed lines.</p>
<p>Till now, we have viewed these &#8216;affiliations&#8217; as existing &#8216;outside&#8217; the network. With the conceptual model we put affiliations, friends and any other &#8216;contexts&#8217; in the network as nodes. Using such a formulation, we get additional insights by observing the <em>co-evolution</em> of contexts and friendships by treating them through a common framework. Theoretically a context can be anything. For e.g. it can be location (check in features), interests, causes, deals, alumni groups, affiliations (as shown above) etc, all unified in one. (To be totally facetious, I am calling it &#8220;The General Theory of Social Networks&#8221; <img src='http://www.saipanyam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   )</p>
<p>Why this is important and powerful, needs a little bit of explanation and theoretical exploration. It has got to do with <em>Closures and Hypergraphs</em>.</p>
<h3>Closures</h3>
<p>Closure is the &#8220;act of closing&#8221;. It is the primary mechanism through which PYMK (People you may know) feature is implemented. Closures imbue social networks with the ability to grow organically. In literature there are three types of closures which have been extensively studied: Triadic Closure, Focal Closure and Membership Closure.</p>
<div id="attachment_603" class="wp-caption aligncenter" style="width: 696px"><img class="size-full wp-image-603 " title="Closures" src="http://www.saipanyam.net/wp-content/uploads/2011/07/Closures.png" alt="Closures" width="686" height="195" /><p class="wp-caption-text">Each closure above corresponds to the closing of a triangle in social-affiliation networks</p></div>
<p>Here focus corresponds to a group, interests, lists or circles.</p>
<h4>Triadic Closure</h4>
<p>If two people in a social network have a friend in common, then there is an increased likelihood of they becoming friends themselves at some point in the future. If A and B have edges to C, then there is a high probability that an edge will form between A and B. PYMK feature is based on this principle of triadic closure.</p>
<h4>Focal Closure</h4>
<p>By introducing affiliations in to the network, focal closure comes in to play. If two people have an affiliation (focus) in common, then there is an increased likelihood of they becoming friends. If A and B are members of  C, then there is high probability that an edge will form between A and B. This has intuitive appeal, as by their membership of C, they have indicated that they share common interests and passions. So it is advantageous for them to be friends themselves.</p>
<h4>Membership Closure</h4>
<p>If two people are friends and one of them is a member of a particular affiliation, then it is quite likely that the member would introduce his friend to the group and in turn becomes its member. That is membership gets &#8216;closed&#8217; as result. More formally, if A is a friend of B and B is a member of C, then there is high probability that A will also become a member of C in future.</p>
<h4>Contextual Closure</h4>
<p>We do see all of the above closures happening today on social-affiliation networks. But there is one more closure, that is yet to happen. This is not a <em>new type of closure</em>, but an artifact of modelling contexts as <em>fundamental units</em> of social networks. This type of closure is the &#8216;general&#8217; solution to all of the above. If all <em>three contexts are people</em>, then it becomes triadic closure. If one of the contexts is a focus (or group/circle/list), then it can become either a focal or membership closure (See above).</p>
<p>The interesting thing that happens above and beyond this is the case when we operate on <em>three contexts. </em>Either <em>two of them are groups and one person</em> OR <em>all three are groups</em>.  In the case of two groups, nothing can be gained by an individual instance of a person belonging to two groups. Interesting things happen, when we see the results in aggregate. If a multitude of people contexts are members of two separate group contexts, then we can safely predict that either these group contexts will merge in to one or will have a &#8216;connection&#8217; to each other. If all three are group contexts and one group context is linked to two other groups, there is a high likelihood of triadic closure happening on group contexts.  Also there is an increased likelihood of focal and membership closures happening on people contexts contained within the group contexts.</p>
<div id="attachment_610" class="wp-caption aligncenter" style="width: 558px"><img class="size-full wp-image-610" title="Contextual Closure" src="http://www.saipanyam.net/wp-content/uploads/2011/07/Contextual-Closure.png" alt="Contextual Closure" width="548" height="244" /><p class="wp-caption-text">An artifact of modelling everything as a context</p></div>
<p>&nbsp;</p>
<p>This brings us to the point, where we understand how contextual modelling helps us achieve greater and more organic growth of social networks. Since it is more contextual it is more valuable to the end user as well as the social network. More information can be mined and more &#8216;intents&#8217; can be readily perceived. The forcing functions of selection and social influence are amplified to a great extent. I can only wonder, if we pull this off at scale, it would be a gold mine of hidden motivations and real social behaviors. From a computational stand point, we only need one common language to handle all the nodes and edges in the network. We can co-locate &#8216;disparate&#8217; data in one storage model, since they are all contexts. This greatly simplifies data mining and querying capabilities. Each new node and edge adds more context, like pieces of a puzzle, the later pieces provide &#8216;global context&#8217; for solving the whole puzzle.</p>
<p>Our existing 2 planar network will not suffice. With the explosion of contexts and the fact that a context can belong to other contexts, it would soon become just data and no information. We would need a mathematical structure that underpins this form and help us navigate the contextual social network. The traditional two vertices connected by an edge will not be enough. So what can replace this? What kind of data structures can support this? It should be robust and computationally solid. Also the new solution, keeping the central theme going, should also support current investments as well as provide a solution to n-planar networks. What this means is that 2-planar network model should be a an instance of a more general solution. Enter Hypergraphs!!!</p>
<h3>Hypergraphs</h3>
<p>In mathematics, a <em>hypergraph </em>is a generalization of a graph, where the <em>edges</em> can connect any number of <em>vertices</em>. Formally, a hypergraph H is a pair (X,E), where X is <em>a set of vertices</em> (nodes) and E is a <em>non empty set of subsets</em> of X, called <em>hyperedges</em>. So E is a subset of P(X), where P(X) is a <a title="Power Set" href="http://en.wikipedia.org/wiki/Power_set" target="_blank">power set</a> of X.</p>
<p>A <em>hypergraph</em> with all <em>hyperedges</em> of cardinality 2 is a graph as we know it. More generally we can define a k-uniform  hypergraph as a hypergraph, such that all its <em>hyperedges</em> have size <em><strong>k. </strong></em>So a graph is just a 2-uniform hypergraph, satisfying our requirement of fitting current models, while enabling us to extend.</p>
<p>Hypergraph traversals are very important in the fields of machine learning, data mining, game theory etc. It is too vast a subject to explore in this post.</p>
<h3>Conclusion</h3>
<p>In conclusion, we can see how moving to a contextual model, helps us immensely, both from a conceptual as well as computational stand point. Better models and underlying mathematical structures will force streams in to the next evolutionary phase. Of course there is a lot to do on the UI/UX side to make all of this intuitive to the end user. But I suspect that it would be easier than the contortions that the users go  through now. Modelling behavior is another beast that we need to tackle sooner than later. Current behavioral models are quite complex and I would imagine a contextual behavioral model would be even more so.</p>
<p>As always, please let me know if there is any work in progress in this direction somewhere. My limited search abilities did not uncover anything of substance. If there is anything at all, I am sure it is spread out in different fields, areas and silos. The need of the hour is to bring all of this to the main stream.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/07/shape-of-contextual-streams.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solution to the Scoble Problem &#8211; A Study in Stream Evolution</title>
		<link>http://www.saipanyam.net/2011/07/solution-to-the-scoble-problem-a-study-in-stream-evolution.html</link>
		<comments>http://www.saipanyam.net/2011/07/solution-to-the-scoble-problem-a-study-in-stream-evolution.html#comments</comments>
		<pubDate>Tue, 12 Jul 2011 23:25:26 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Diversity]]></category>
		<category><![CDATA[Evolution]]></category>
		<category><![CDATA[g+]]></category>
		<category><![CDATA[Relevance]]></category>
		<category><![CDATA[Stream]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=523</guid>
		<description><![CDATA[I read Rocky Agrawal&#8217;s post on the Scoble Problem and the rejoinder to it &#8220;It&#8217;s not Scoble&#8217;s fault&#8220;. It is and will be an ongoing problem on any &#8216;stream&#8217; (Facebook, twitter, g+ etc). Before laying out a possible solution to this, I would like to give a bit of a background. Please indulge me. I have some [...]]]></description>
			<content:encoded><![CDATA[<p>I read Rocky Agrawal&#8217;s post on the<a title="Solve Scoble Problem" href="http://techcrunch.com/2011/07/09/scoble-problem-social-networks/" target="_blank"> Scoble Problem</a> and the rejoinder to it &#8220;<a title="Not Scoble's Fault" href="http://techcrunch.com/2011/07/11/google-algorithm-scoble/" target="_blank">It&#8217;s not Scoble&#8217;s fault</a>&#8220;. It is and will be an ongoing problem on any &#8216;stream&#8217; (Facebook, twitter, g+ etc). Before laying out a possible solution to this, I would like to give a bit of a background. Please indulge me.</p>
<p>I have some experience in dealing with this problem. Till recently I was an architect on the stream team @ myspace. What was lost in all the noise of myspace&#8217;s fall from grace, is the cool stuff that was done by engineers there. Quite early in the design phase of &#8216;futura&#8217; (redesign of MySpace to my___ )  , we were tasked with solving two major problems: Relevance and Stream clogging (now known as Scoble effect!). These are still unsolved in the sense that there is no universal or elegant solution. There are solutions which achieve the results to varied degrees. It is still an open problem.</p>
<div id="attachment_531" class="wp-caption aligncenter" style="width: 310px"><img class="size-full wp-image-531" title="evolution" src="http://www.saipanyam.net/wp-content/uploads/2011/07/evolution.png" alt="evolution" width="300" height="117" /><p class="wp-caption-text">Evolution of Man</p></div>
<p>Before I layout a proposed solution, let me talk a bit about <strong>Stream Evolution</strong>. Each evolutionary phase <strong>builds</strong> on top of the previous. Like evolution, each phase was born out of a need to survive the changing landscape of user needs and adoption. The frustration of users is valid in the sense that we want our machines to anticipate and mimic our real life. Our online world should reflect our offline world. Circles of g+,  Groups of facebook, follow model of twitter are all different facets of human behavior (among others).  From my perspective (Origin of Streams ! <img src='http://www.saipanyam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) every stream product follows an evolutionary path:</p>
<h4>Chronological Stream (CS)</h4>
<p>This is the classic stream solution, where we show the activities based on a reverse chronological order. Newest ones first. As activities occur, we store them. Then based on a user pull, we display a paged view of the activities. This worked for a while as the amount of data was manageable. We could fit all the information in a few &#8216;pages&#8217; which could be traversed easily. This was a single dimension solution</p>
<h4>Categorized Chronological Stream (CCS)</h4>
<p>CCS came about when the number of activities increased by an order of magnitude, since humans have a limited capacity to consume at one go, CCS attempted to divide it in to smaller pieces. It was an answer to the question: &#8220;How do you eat an elephant?&#8221; Answer: In small pieces. But the need was to be categorized rather than partitioned based on date time. So a solution was devised to categorized based on an extra dimension of &#8216;friend lists/groups/circles&#8217;.  The user is given an option to filter the stream by groups of friends. This gave them the ability to consume only those parts, which interested them at that particular moment in time.</p>
<h4>Bucketed Chronological Stream (BCS)</h4>
<p>BCS was built on top of the previous two, by providing the ability to consume by &#8216;type&#8217;. It evolved to satisfy the need to consume in three dimensions: Date, Groups and Buckets (In this context buckets refer to media types). For e.g. a user wanted to see all Videos shared by friends (all or a select few), or see only photos, events etc. The need and the forcing function here was, the user knew what type of content to consume, but was not sure when and who provided it in the first place. It enabled discovery for the first time.</p>
<h4>Real Time Stream (RTS)</h4>
<p>RTS was the answer to the problem of immediacy. Users wanted their data now! It was push based model, instead of  pull. Yes this evolutionary phase incorporated all the advantages of the previous phases. Now the stream was a fast flowing stream, a flood!!!. The innovation here was only in how to scale the infrastructure. Each solution in this phase had variations on solving scaling for more and more data. Twitter&#8217;s scaling problems come to mind. But more importantly user&#8217;s were not satisfied. They had this nagging feeling that they might have missed something interesting. So the solution joined the advantages all the above phases.</p>
<h4>Aggregated Stream (AS)</h4>
<p>AS was an answer to RTS&#8217;s inherent weakness of missing something important. The solution was to <strong>aggregate </strong>activities by <strong>type </strong>so that we can fit more data in a small space. The evolution of thumbnails, profile pics are a manifestation of this. This enabled one to increase the amount of data per screen item. The solution was limited only by the amount of data that we could aggregate in a specific time window. If a user uploaded 20 photos, we don&#8217;t need to show 20 single items, but as one item. The item itself contained jump off points for the more interested consumer. Aggregation is not trivial as it is often confused with <em>collection. </em>A collection is a special case of aggregation. Aggregation can span across multiple dimensions for e.g. Multiple friends + Same Type, Same User + multiple types etc. Adding the dimension of date adds more complexity to it.</p>
<h4>Asynchronous Interaction Activated Stream (AIAS)</h4>
<p>AIAS can be considered as parallel evolution. AIAS solutions are based on the premise that, any interaction that occurs on a piece of content in the stream needs to be resurfaced to the top as it is fresh. More the interaction, more it can be thought of as new content. Here the content that is added, is the interaction event itself. It is asynchronous as interaction can occur out of band. Different users will see the same piece of content at varying levels of sort order. The premise is also that if it is interacted most, then it must be <em>relevant</em> to <strong><em>all</em></strong> users. AIAS works only when there is a fast flowing stream, otherwise you would see duplication in the same view.</p>
<p>&nbsp;</p>
<p>Till now we have see how we moved from <strong>Caveman phase</strong> (Chronological) to <strong>Agricultural phase</strong> (Categorized, Bucketed), to <strong>Industrial phase </strong>(Real Time), to<strong> Information phase </strong>(Aggregated, Asynchronous Interaction).  We now examine the <strong><em>Digital phase </em></strong>of Relevant Stream and Diversity Infused Relevant Stream.</p>
<h4>Relevant Stream (RS)</h4>
<p>Relevant stream was an answer to the exponential data rates that we are encountering. None of the previous evolutionary adaptations could handle this. Even with better infrastructure, better  and more hardware, it was realized that there is no chance of survival. As more and more companies were competing, the need arose to differentiate. My contribution was to come up with a relevance algorithm for real time streams. Since it is patented (Inventor: Me, Owned By: Myspace), I can only discuss the high level heuristics of any solutions that belong to this family.</p>
<p>Essentially any relevance solution comprises of tracking a bunch of signals, normalizing them and folding the resulting values in to a relevance metric. This relevance measure can then be used to sort the list of activities. Then we take the <em>top n</em> of the list and display to the user. We can combine the features of the previous evolutionary phases to improve the throughput.  There are as many implementations of this approach as are companies. One more key heuristic is the use of <a title="Serendipity" href="http://en.wikipedia.org/wiki/Serendipity" target="_blank">serendipity</a>. We introduce a random element to promote discovery. Otherwise, we will have the case of &#8220;rich getting richer and the poor getting poorer&#8221;.  We would never discover new items. The same old people keeping showing up. Scoble effect is a prime example of this. Virality comes in to play because of serendipity. In fact nature by and large is a successful system, as it includes serendipity as a fundamental design principle. Relevance at first blush is a great solution. You would find an initial improvement in user satisfaction and then as more prolific/enterprising users &#8216;discover&#8217; or &#8216;uncover&#8217; the signals/algorithms, it is prone to gaming. If we can determine the signals, we can simulate behavior that games the relevance algorithm. We see examples of these in nature too. Insects having &#8216;big eyes&#8217; on their backs to scare potential predators, camouflage etc. So no system is impenetrable or opaque. At best we can have a first movers advantage. If one doesn&#8217;t evolve it will soon be extinct.</p>
<p><span class="Apple-style-span" style="font-weight: bold;">Diversity Infused Relevant Stream (DIRS)</span></p>
<p>DIRS came about to make relevance and serendipity more effective. I might want to see at least some of my less frequent friend activities, than more of a prolific friend. I would trade the excess of something with what I have less of. In case of Scoble effect (Stream clogging), I neither want to turn Scoble off or keep him fully on. I want just enough!!!! Now just enough is not something that computers understand. We need to map something that is fuzzy to binary. There are of course many variations on this theme. There could be a facility to <em>mute or volume control. </em>But it soon becomes a pain to manage. We don&#8217;t have any visual cues to help us. It is always trial and error and that makes a lot of people uncomfortable. I went with a simpler guiding principle. Like previous (regarding relevance patent) I cannot discuss the entire solution, but I can share the guiding principle behind diversity algorithms, which are in the public domain.</p>
<p><strong>Definitions:</strong></p>
<p>If there are two users u<sub>i</sub>  and u<sub>j</sub>  connected to user C. Define Candidate(U, p) as the count of number of items from user U in pool p.  Define Count(U) as the number of items from user U displayed in C’s  stream.</p>
<p><strong>Guiding Principle:</strong></p>
<p>Stream Diversity: if Candidate(u<sub>i</sub> , p)&gt;0, and Count(u<sub>i</sub> )= 0, then Count(u<sub>j</sub>)≥ k  until Count(u<sub>i</sub> )≥1.</p>
<p><strong>Outcome:</strong></p>
<p>If there is an item from u<sub>i</sub> then we  should show not more than k items from u<sub>j</sub> unless we also show at least one item from u<sub>i</sub>.</p>
<p>This diversity has been shown to increases  &#8217;perceived&#8217; relevance. Diversity underpins system level robustness, allowing for multiple responses to external shocks and internal adaptations;  it provides the seeds for large events by creating outliers that fuel tipping points; it drives novelty and innovation.</p>
<p>We use an algorithmic way to simulate the real world. Like all simulations, it is only an approximation of reality. If we look hard we can perceive cracks where reality seeps in. The famous <a title="Red Pill Or Blue Pill" href="http://youtu.be/te6qG4yn-Ps" target="_blank">Red Pill or Blue Pill</a> scene from Matrix is a good metaphor.</p>
<p>Improvements in these areas are possible by increasing diversity. Here too we would reach an asymptote soon. We also need to watch out for diversity turning in to randomness and chaos. The good news is we haven&#8217;t reached the plateau yet. More can be done in this area. My small contribution not withstanding, I believe we would see more innovation in this direction.</p>
<p><span class="Apple-style-span" style="font-weight: bold;">Situation as it stands today</span></p>
<p>Most, if not all streams are at this point in evolution. But I could be wrong. I am not privy to the inner workings of companies. My observation is based on what I see and guesstimate the evolutionary phase. So what else can we do to trigger the next evolutionary phase? Is there a theoretical limit to what can be achieved? Can a truly relevant/diverse system be designed?Can the holy grail of absolute relevance be achieved?</p>
<p>These are the questions that I struggled with. Then inspiration hit me! Nature abounds with solutions to hard problems. How can small insects like ants and bees be successful species? They perform complex tasks which are disproportionate with their brain capacity. Of course this is nothing new. There is research going on Nature inspired Computing, Artificial Intelligence, Computational Intelligence, Bio Mimicry, Neural nets etc.</p>
<p>What we need is to apply the insights from these fields on to human consumption behavior. The nearest system I can think of is the foraging behavior of ants. If we feel too uncomfortable with that, let me say we are like bees: social, intelligent and actually do a dance to communicate!!!! (waggle dance).</p>
<p>If you want a taste of these algorithms, you can start reading on my blog <a title="Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/clever-algorithms-python.html">Clever Algorithms in Python</a> .</p>
<p>Though we have not seen the next evolutionary phase, we can at least describe the <em>shape </em>of what it would be. I would hasten to add that like biological evolution, extrapolation would be widely inaccurate. For e.g. if we as an observer went back in time to dinosaur age, would be predict the evolution of humans or more grotesque creatures straight out of a Alien movie?</p>
<p>All is not lost though. I can safely venture to say, that we can predict the next nearest evolutionary step with a fair degree of accuracy than the shape of evolution many steps away&#8230;.</p>
<p>So let us start. Please note this is only my opinion and like I said could be totally wrong.</p>
<h4>Context Based Adaptive Stream (Contextual Stream)(CBAS, COS)</h4>
<p>I would like to propose that the next evolutionary step would be the rise of <em><strong>Contextual Streams.</strong></em> Some key characteristics would be natural serendipity, redundant systems, scale invariant, adaptive and sense making. The idea is to provide a non deterministic view to every user. Each user sees a highly customized view specific to him. Even more, this view is not deterministically customized by the user, but organically grown. Think snowflakes, than manufacturing toasters. Each action of the user simultaneously determines the future as well as the &#8216;past&#8217;. You might say, how can the past change? Keep in mind, &#8216;past&#8217; is only a representation made by us. Individual facts cannot be contravened, but the way we combine them could produce new meaning, in light of the present facts!!!!</p>
<p>Without degenerating this in to an Issac Asimov narrative, I would like to conclude that a lot of emergent systems theory, complex adaptive systems and nature inspired algorithms would definitely be a part of the solution in some form.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/07/solution-to-the-scoble-problem-a-study-in-stream-evolution.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stochastic Algorithms : Part 2 &#8211; Clever Algorithms in Python</title>
		<link>http://www.saipanyam.net/2011/06/stochastic-algorithms-2.html</link>
		<comments>http://www.saipanyam.net/2011/06/stochastic-algorithms-2.html#comments</comments>
		<pubDate>Wed, 29 Jun 2011 02:35:55 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Clever Algorithms]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Stochastic Algorithms]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=474</guid>
		<description><![CDATA[This is a multi part series on implementing Clever Algorithms by Jason Brownlee in Python. See overview, Part 1. In part 2 we look at the following Stochastic Algorithms: Iterated Local Search, Guided Local Search, Variable Neighborhood Search, Greedy Randomized Adaptive Search, Tabu Search and Reactive Tabu Search. This completes Stochastic Algorithms. We apply the above algorithms [...]]]></description>
			<content:encoded><![CDATA[<p>This is a multi part series on implementing <a title="Clever Algorithms" href="http://www.cleveralgorithms.com/" target="_blank">Clever Algorithms</a> by Jason Brownlee in Python. See <a title="Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/clever-algorithms-python.html">overview</a>, <a title="Stochastic Algorithms – Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/stochastic-algorithms-1.html">Part 1</a>.</p>
<p>In part 2 we look at the following Stochastic Algorithms: Iterated Local Search, Guided Local Search, Variable Neighborhood Search, Greedy Randomized Adaptive Search, Tabu Search and Reactive Tabu Search. This completes Stochastic Algorithms.</p>
<p>We apply the above algorithms to the <a title="Berlin52 TSP" href="http://www-neos.mcs.anl.gov/neos/solvers/co:concorde/Berlin52_TSP.txt" target="_blank">Berlin52</a> instance of the <a title="Travelling Salesman Problem" href="http://en.wikipedia.org/wiki/Travelling_salesman_problem" target="_blank">Travelling Salesman Problem</a>. The solution attempts to find a permutation of the cities (called a tour) order that minimizes the total distance traveled. The optimal distance for Berlin52 TSP is at 7542 units.</p>
<h3>Download</h3>
<p>Download <a href="http://www.saipanyam.net/wp-content/downloads/CleverAlgorithms.rar" target="_new">source code</a>. Unzip and import the attached folder in to your Eclipse workspace. It includes a test suite for exercising the algorithms</p>
<h3>Iterated Local Search</h3>
<p>We use Stochastic 2-opt move for doing local search and the double bridge move for creating a &#8216;perturbation&#8217;. The idea is to escape the local minima by going to neighborhoods which are beyond the local search method.  Advanced usage of ILS takes in to account search history for perturbation and acceptance criteria. I tried to keep the code as near to the pseudo code as possible. This would enable one to understand the algorithm and incorporate search history. Search History can be used to either &#8216;intensify&#8217; or &#8216;diversify&#8217;  (or a blend) the candidates in the search space. The way I have used is to intensify the search using  a &#8216;search history&#8217; of ONE.</p>
<h3>Variable Neighborhood Search</h3>
<p>Variable Neighborhood Search explores larger and larger neighborhoods of a given local optima until an improvement is found. After an improvement is found the search is repeated on expanding neighborhoods. This strategy is based on the following three principles:</p>
<p>A local minimum for one neighborhood may not be the local minimum for for a different neighborhood.</p>
<p>A global minimum is a local minimum for <strong>all </strong>neighborhoods.</p>
<p>Local minima are assumed to be clustered for many problem domains.</p>
<p>One can see these three principles in action, by repeatedly applying the stochastic 2 opt move in a given neighborhood, doing the same at a larger frequency for the embedded local search heuristic and expanding the search to other neighborhoods when we reach a local optimum.</p>
<h3>Greedy Randomized Adaptive Search</h3>
<p>GRASP is related to ILS in terms of identifying candidate solutions which are &#8216;greedy&#8217;. This is achieved by constructing a Restricted Candidate List (RCL) which are used to penalize solutions. We start of by constructing a greedy candidate solution and then use a local embedded search to refine the result.  The greedy solution uses a factor &#8216;alpha&#8217; to control how greedy we need the solution to be. By increasing alpha [0,1] our solution becomes generalized, whereas alpha near zero are more greedy. The approach is to select each &#8216;feature&#8217; (here a feature is the cost of adding a point to the solution), is within a certain distance from the minimum cost. The code listing that is provided contains extensive comments, which explain in more detail.</p>
<h3>Guided Local Search</h3>
<p>GLS uses a scaling factor to calculated an &#8216;augmented cost function&#8217; which is used to restart a search. The augmented cost function is specific to the embedded local search. The main search procedure is oblivious to this function. The aim is to diversify, so that we can escape local optima.  See code listing for more details.</p>
<h3>Tabu Search</h3>
<p>Tabu Search maintains a tabu list of edges that have been rewired in stochastic 2-opt move. This is used to prevent the generation of candidate solutions from revisiting already visited paths (called cycles). By preventing cycles we search a broader candidate space, there by converging to the global optima faster.</p>
<h3>Reactive Tabu Search</h3>
<p>Reactive Tabu Search is an extension of Tabu Search. Here we monitor the search and react to occurrence of cycles, by increasing or decreasing our candidate space. This algorithm is the most involved in terms of use of certain heuristics for prohibition period, repetition interval etc. The intent is to diversify the search once a threshold of cycle repetitions is reached. The aim here is to &#8216;react&#8217; to the search direction and change it.</p>
<p>&nbsp;</p>
<p><strong>Note: </strong>The descriptions are purposely kept short, to encourage delving in to the code itself. I would recommend reading the pseudo code provided in Clever Algorithms and follow my code along. I went out-of-the-way to keep my code similar to pseudo code.</p>
<p><span class="Apple-style-span" style="font-weight: bold;">Visualization of Stochastic 2-opt and Double Bridge Moves</span></p>
<div style="width: 620;">
<div style="float: left;">
<div id="attachment_477" class="wp-caption alignleft" style="width: 332px"><img class="size-full wp-image-477   " title="Stochastic2opt" src="http://www.saipanyam.net/wp-content/uploads/2011/06/Stochastic2opt.png" alt="2 opt move" width="322" height="170" /><p class="wp-caption-text">Stochastic 2-opt Move : Original tour on the left and resulting tour on the right</p></div>
</div>
<div style="float: right;">
<div id="attachment_482" class="wp-caption alignright" style="width: 236px"><img class="size-full wp-image-482  " title="DoubleBridgeMove" src="http://www.saipanyam.net/wp-content/uploads/2011/06/DoubleBridgeMove1.png" alt="Schematic Representation of Double Bridge Move" width="226" height="197" /><p class="wp-caption-text">Double Bridge Move : The four dotted edges are removed and the remaining parts A, B, C, D are reconnected by the dashed edges.</p></div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/06/stochastic-algorithms-2.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Stochastic Algorithms &#8211; Clever Algorithms in Python</title>
		<link>http://www.saipanyam.net/2011/06/stochastic-algorithms-1.html</link>
		<comments>http://www.saipanyam.net/2011/06/stochastic-algorithms-1.html#comments</comments>
		<pubDate>Sat, 25 Jun 2011 04:50:50 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Clever Algorithms]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Stochastic Algorithms]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=416</guid>
		<description><![CDATA[This is a multi part series on implementing Clever Algorithms by Jason Brownlee in Python. See overview, Part 2. Stochastic Algorithms are primarily global optimization algorithms. A stochastic process is one whose behavior is non-deterministic. The system&#8217;s subsequent state is determined both by the process&#8217; predictable actions and by a random element. The main strategy (with some exceptions) are [...]]]></description>
			<content:encoded><![CDATA[<p>This is a multi part series on implementing <a title="Clever Algorithms" href="http://www.cleveralgorithms.com/" target="_blank">Clever Algorithms</a> by Jason Brownlee in Python. See <a title="Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/clever-algorithms-python.html">overview</a>, <a title="Stochastic Algorithms : Part 2 – Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/stochastic-algorithms-2.html">Part 2</a>.</p>
<p>Stochastic Algorithms are primarily global optimization algorithms. A <strong><a title="Stochastic process" href="http://en.wikipedia.org/wiki/Stochastic_process" target="_blank">stochastic process</a></strong> is one whose behavior is non-deterministic. The system&#8217;s subsequent state is determined both by the process&#8217; predictable actions and by a random element. The main strategy (with some exceptions) are based on sampling the search space, calculating the &#8216;cost&#8217; and using that to improve on the result. This can be either through a random process, changing search direction or step size. There is no &#8216;inspiring&#8217; system, either natural or biological on which these are based.  The first part details the implementation in python of four algorithms: Random Search (RS), Adaptive Random Search (ARS), Stochastic Hill Climbing (SHC) and Scatter Search (SC).  Out of these except for Stochastic Hill Climbing , the remaining three use the same objective function for calculating the cost.</p>
<p>The objective function I chose for RS, ARS and SC was a variation on the one provided by Jason. He used a function that was a &#8216;sum of squares&#8221; , I used a variation that would provide a non trivial solution value, while keeping the structure of the function the same.</p>
<div id="attachment_427" class="wp-caption alignnone" style="width: 621px"><img class="size-full wp-image-427  " title="Original Basin Function" src="http://www.saipanyam.net/wp-content/uploads/2011/06/SA-Original.png" alt="Basin Function" width="611" height="263" /><p class="wp-caption-text">Objective function used in Clever Algorithms</p></div>
<p>&nbsp;</p>
<div id="attachment_428" class="wp-caption alignnone" style="width: 623px"><img class="size-full wp-image-428 " title="My Basin Function" src="http://www.saipanyam.net/wp-content/uploads/2011/06/SA-Variation.png" alt="My Basin Function" width="613" height="236" /><p class="wp-caption-text">Objective Function that I used</p></div>
<p>The optimal solution (minimum) for  my basin function can be found at the value -10. The input value would be 2.</p>
<p>For SHC I used the same &#8216;<a title="One Max" href="http://tracer.lcc.uma.es/problems/onemax/onemax.html" target="_blank">One Max</a>&#8216; Problem from the original Clever Algorithms book.</p>
<h3>Download</h3>
<p>Download <a href="http://www.saipanyam.net/wp-content/downloads/CleverAlgorithms.rar" target="_new">source code</a>. Unzip and import the attached folder in to your Eclipse workspace. It includes a test suite for exercising the algorithms</p>
<h3><strong>Random Search (RS)</strong></h3>
<p>Random search has no memory of previous search candidates. So it might revisit previously considered candidate solutions. The advantage is, it is a minimal solution. Its worst case performance is worse than a straight linear approach. But it has its uses in a domain where the values are continuous. Most commonly it is used as an embedded heuristic in other search algorithms for refining a local optima. It is one of the building blocks for other more advanced search techniques.</p>
<h3><strong><strong>Adaptive Random Search (ARS)</strong></strong></h3>
<p>Adaptive Random Search seeks to avoid the problems of localized random search by sampling with larger or smaller step sizes. We keep trying larger step sizes as long as we have improvement in the result. We go back to smaller step sizes if we don&#8217;t see an improvement in the result after a preset max count. The initial starting point is selected similar to that of RS.</p>
<h3><strong>Scatter Search (SC)</strong></h3>
<p>Scatter search is a global optimization algorithm, which has some Evolutionary computation features. The premise of SC is that the desired result is present in some combination of a diverse set of high quality candidate sets. By combining and recombining the values from a reference set of elite solutions to create &#8216;offsprings&#8217;. We then test these offsprings for best values by applying the objective function to them. We use an embedded heuristic of RS for refining a local candidate solution.</p>
<h3><strong>Stochastic Hill Climbing (SHC)</strong></h3>
<p>Stochastic Hill Climbing iterates the process by randomly selecting a neighbor of a local candidate solution and evaluating it. It is an improvement on RS, where we don&#8217;t sample randomly, but near neighbors of randomly selected candidate solution. I noticed an error in the code listing provided by Jason Brownlee. Specifically on page 40 last line. ( mutant[pos] = (mutant[pos]==&#8217;1&#8242;) ? &#8217;0&#8242; : &#8217;1&#8242; ) . It should instead check for &#8217;0&#8242; and replace it with &#8217;1&#8242;. I believe it is a typo in the book. I also tried to evaluate the search efficacy for the OneMax problem. Since the idea is to replace all zeros in the bitstring with 1. A search efficacy metric can be defined as (FinalOneMaxCount &#8211; InitialOneMaxCount)/finalIteration. If we set our max iterations to the length of the initial bitstring, we get a nice metric for measuring the search efficacy.</p>
<h4><strong>Visualization of the two objective functions: </strong></h4>
<div style="width: 620;">
<div style="float: left;">
<div id="attachment_430" class="wp-caption alignleft" style="width: 252px"><img class="size-full wp-image-430 " title="Original Basin Function Graph" src="http://www.saipanyam.net/wp-content/uploads/2011/06/SA-OriginalFunction.png" alt="Original Basin Function Graph" width="242" height="342" /><span style="font-size: 11px; line-height: 17px;">Visualizing the original basin function</span></dt>
</dl>
</div>
<div style="float: right;">
<dl id="attachment_432" class="wp-caption alignright" style="width: 323px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-432 " title="My Basin Function Graph" src="http://www.saipanyam.net/wp-content/uploads/2011/06/SA-Function1.png" alt="My Basin Function Graph" width="313" height="643" /><p class="wp-caption-text">Visualizing my basin function</p></div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/06/stochastic-algorithms-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clever Algorithms in Python</title>
		<link>http://www.saipanyam.net/2011/06/clever-algorithms-python.html</link>
		<comments>http://www.saipanyam.net/2011/06/clever-algorithms-python.html#comments</comments>
		<pubDate>Sat, 25 Jun 2011 03:11:41 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Clever Algorithms]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=408</guid>
		<description><![CDATA[Ever since I got hold of Clever Algorithms &#8211; Nature Inspired Programming Recipes by Jason Brownlee, I had a desire to assimilate it. Jason has done us a great service by researching, collating and proposing a unified template for understanding &#8220;Nature inspired algorithms&#8221;.  The nature of the subject itself spans varied fields, is relatively new, [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I got hold of <a title="Clever Algorithms" href="http://www.cleveralgorithms.com/" target="_blank">Clever Algorithms</a> &#8211; Nature Inspired Programming Recipes by Jason Brownlee, I had a desire to assimilate it. Jason has done us a great service by researching, collating and proposing a unified template for understanding &#8220;Nature inspired algorithms&#8221;.  The nature of the subject itself spans varied fields, is relatively new, fragmented, incomplete, spread across various papers and is not amenable to understand as a whole. So I was naturally (no pun intended) excited when I saw this book.</p>
<p>My first attempt at reading through it was frustrating to say the least. It was not Jason&#8217;s writing style per-se, but the nature of the subject itself. It is not amenable for reading as a programming book. Jason himself cautions in the first paragraph: &#8220;rather than being read cover to cover&#8221;, it is a handbook and a technique guide-book. In-spite of that warning I plunged in trying to gather as much as possible. Only later I realized that to make this knowledge my own, I needed to go slow and experiment with each algorithm. But blindly copying what he had done, would be of no avail.</p>
<p>So I set myself up with the following goals:</p>
<p><strong>Goal 1:</strong> I will try to implement the algorithms in a language that I have no familiarity with.</p>
<p><strong>Goal 2:</strong> I will try to stick to the original pseudo code as far as possible to increase my understanding of the technique</p>
<p><strong>Goal 3:</strong> I will try to question and verify every line in the code listing provided by him, so that my implementation would be from understanding, rather than duplicating, or worse copying.</p>
<p><strong>Goal 4:</strong> Have an alternate (or accompanying, if you will) source for people who come across this book, with more contextual information on implementing these algorithms. (sort of standing on the shoulders of giants!)</p>
<p><strong>Goal 5:</strong> Provide a narrative of my experience, implementing these excellent algorithmic techniques.</p>
<p>To realize <strong>goal 1</strong>, I chose python as the language to implement the algorithms. As you can see I am a .NET guy throughout my career. So trying to implement the algorithms in python would force me to re-evaluate every line of code that I write. The problem is compounded by the fact that Jason&#8217;s original code listing is in Ruby. I had to learn just enough Ruby to understand <strong>his </strong>code and translate that in to python!. This has a dual advantage: one i get to learn a new language while applying it to a non trivial problem, two it forced me to look at each line of code from an algorithmic stand point.</p>
<p>To realize <strong>goal 2</strong>, I changed my implementation functions and structure to reflect the pseudo code listing that he provided. This makes it easy to follow how the pseudo code translates to implementation. In all fairness to Jason, he did a great job of it. But I wanted to take it one step further and make it more consumable. The subject matter is hard enough, I wanted to remove any further obstacles to assimilating this seminal work from Jason.</p>
<p>To realize <strong>goal 3</strong>, I changed some of the objective functions that he provided to others where possible. I also made some changes to some data structures and input variables to further kick the tires. I also included a test suite that exercises the algorithms. So anyone who just want to see how the algorithms behave for different inputs, can do so easily. I also put in a lot more comments in places where I ran in to difficulties. I hope it would make it more valuable to someone trying to follow along.</p>
<p>To realize <strong>goal 4</strong>, I want people to see how the algorithms play out in a different language. By writing this in a language that I am unfamiliar with, I hope it gives one confidence to attempt them in your language of choice. Each .py file also has a header which lays out the Name, Taxonomy, Strategy and Heuristics of the algorithm. (This is mostly from Jason&#8217;s Clever Algorithms book and algorithm template that he designed). The reason I chose to do so was, while understanding one algorithm, I wanted the code and the algorithm context at the same place. I found that very helpful in understanding and assimilating each algorithm.</p>
<p>To realize<strong> goal 5</strong>, I would be blogging about this. His 45 algorithms are divided in to <a title="Stochastic Algorithms – Clever Algorithms in Python" href="http://www.saipanyam.net/2011/06/stochastic-algorithms-1.html">Stochastic Algorithms</a>, Evolutionary Algorithms, Physical Algorithms, Probabilistic Algorithms, Swarm Algorithms, Immune Algorithms and Neural Algorithms. As I finish implementing each category of algorithms, I will write a blog describing it.</p>
<p>I would like to sincerely thank Jason Brownlee for making available his Clever Algorithms. (By the way, Jason Brownlee did his Phd from the same university that I went to: Swinburne University Melbourne (I am from class of 2000)</p>
<p>Note: I tried as far as possible to write the code in a &#8216;pythonic&#8217; way. Since I was learning python for this, I would like to beg forgiveness in advance from expert python developers. Any suggestions are always welcome to improve my code.</p>
<p>I am using <a title="Python 2.7.2" href="http://python.org/download/releases/2.7.2/" target="_blank">Python 2.7.2</a> with <a title="Eclipse IDE" href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/indigor" target="_blank">Eclipse IDE for Java Developers</a> Version: Helios Service Release 2. I have the <a title="PyDev Plugin for Eclipse" href="http://pydev.org/updates" target="_blank">PyDev</a> ver 2.1.0xxx plugin for eclipse to work in python.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2011/06/clever-algorithms-python.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Algorithms in Computational Geometry – Part 2</title>
		<link>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html</link>
		<comments>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html#comments</comments>
		<pubDate>Fri, 09 Apr 2010 05:01:58 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Expression Design]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=277</guid>
		<description><![CDATA[In Part 1 we discussed the base technique for determining relative orientation. We used that to answer the first two questions: To find relative orientation of two points and two directed segments with a common end point. In Part 2 we answer the next two questions: Given two line segments p0p1 and p1p2, if we [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a> we discussed the base technique for determining relative orientation. We used that to answer the first two questions: To find relative orientation of two points and two directed segments with a common end point. In Part 2 we answer the next two questions:</p>
<ul>
<li>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub>, if we traverse from p<sub>0</sub> to p<sub>2</sub>, did we make a left, right or no turn at p<sub>1</sub>?</li>
<li>Do line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> intersect?</li>
</ul>
<p>We build on what we learned in <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a>, the <strong>cross product</strong>. </p>
<p>Download the demo application <a href="http://www.saipanyam.net/code-center/computational-geometry">here</a></p>
<p><strong>Determining whether consecutive segments turn left or right</strong></p>
<p>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub> we simply check whether the directed segment p<sub>0</sub>p<sub>2</sub> is clockwise or counterclockwise relative to directed segment p<sub>0</sub>p<sub>1</sub>. This is similar to the solution to question 2 in <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-1.html">Part 1</a>. If the cross product is negative, then p<sub>0</sub>p<sub>2</sub> is counterclockwise to p<sub>0</sub>p<sub>1</sub>. Hence we should turn <strong>left</strong> when we reach p<sub>1</sub> to reach p<sub>2</sub> while traversing p<sub>0</sub>, p<sub>1</sub> and p<sub>2</sub>. If the cross product is <strong>0</strong> (boundary condition) then it means all the three points are <strong>collinear</strong> (on the same line). The image below shows the three possible scenarios<br />
<div id="attachment_287" class="wp-caption alignnone" style="width: 610px"><img src="http://www.saipanyam.net/wp-content/uploads/2010/04/Consecutive-Segments1-e1270768161354.png" alt="Consecutive Segments Cross Product" title="Consecutive Segments" width="600" height="375" class="size-full wp-image-287" /><p class="wp-caption-text">Using the cross product to determine how p0p1 and p1p2 turn at p1</p></div></p>
<p><strong>Determining whether two line segments intersect</strong></p>
<p>Two line segments intersect if and only if either or both of the following conditions are true:</p>
<ul>
<li>Each line segment straddles the other line segment.</li>
<li>An end point of one line segment is on the other line segment</li>
</ul>
<p>A line segment p<sub>1</sub>p<sub>2</sub> <strong>straddles</strong> another line if p<sub>1</sub> and p<sub>2</sub> are on opposite sides of the line. A boundary condition occurs when one of the end points is <strong>on</strong> the other line.</p>
<p><strong>Algorithm</strong></p>
<p>Calculate the orientation (using cross product!) d<sub>i</sub> for each point of one segment w.r.t the end points of the other segment. For line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> we will get d<sub>1</sub>, d<sub>2</sub>, d<sub>3</sub> and d<sub>4</sub>.</p>
<p>if none of the d<sub>i</sub> are collinear (i.e. d<sub>i</sub> !=0) we check if d<sub>1</sub> and d<sub>2</sub> <strong>and</strong> d<sub>3</sub> and d<sub>4</sub> have opposite orientations (if one is clockwise,other should be counterclockwise). If this condition is true then we know that the line segments <strong>straddle</strong> each other and hence <strong>intersect</strong>. If it is false we can straight away say they <strong>don&#8217;t intersect</strong>.</p>
<p>If any d<sub>i</sub> is collinear (boundary condition) and the corresponding p<sub>i</sub> is <strong>on</strong> the other line segment, the lines <strong>intersect</strong>.<br />
<div id="attachment_306" class="wp-caption alignnone" style="width: 610px"><img src="http://www.saipanyam.net/wp-content/uploads/2010/04/Intersects-e1270789093105.png" alt="Intersect cases" title="Intersects" width="600" height="375" class="size-full wp-image-306" /><p class="wp-caption-text">Shows the different cases that we encounter visually</p></div></p>
<pre>
bool Intersects(p<sub>1</sub>, p<sub>2</sub>, p<sub>3</sub>, p<sub>4</sub>)
{
    d<sub>1</sub> = CrossProduct (p<sub>1</sub>p<sub>3</sub>, p<sub>1</sub>p<sub>2</sub>)
    d<sub>2</sub> = CrossProduct (p<sub>1</sub>p<sub>4</sub>, p<sub>1</sub>p<sub>2</sub>)
    d<sub>3</sub> = CrossProduct (p<sub>3</sub>p<sub>1</sub>, p<sub>3</sub>p<sub>4</sub>)
    d<sub>4</sub> = CrossProduct (p<sub>3</sub>p<sub>2</sub>, p<sub>3</sub>p<sub>4</sub>)

    if( ((d<sub>1</sub> > 0 and d<sub>2</sub><0) or (d<sub>2</sub> > 0 and d<sub>1</sub><0)) <strong>and </strong> ((d<sub>3</sub> > 0 and d<sub>4</sub><0) or (d<sub>4</sub> > 0 and d<sub>3</sub><0)))
    {
      then return <strong>true</strong>
    }
    else if (d<sub>1</sub> = 0 <strong>and</strong> PointOnLine (p<sub>3</sub>, p<sub>4</sub>, p<sub>1</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>2</sub> = 0 <strong>and</strong> PointOnLine (p<sub>3</sub>, p<sub>4</sub>, p<sub>2</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>3</sub> = 0 <strong>and</strong> PointOnLine (p<sub>1</sub>, p<sub>2</sub>, p<sub>3</sub>))
    {
       then return <strong>true</strong>
    }
    else if (d<sub>4</sub> = 0 <strong>and</strong> PointOnLine (p<sub>1</sub>, p<sub>2</sub>, p<sub>4</sub>))
    {
       then return <strong>true</strong>
    }
    else
    {
       return <strong>false</strong>
    }
}

bool PointOnLine (p<sub>i</sub>, p<sub>j</sub>, p<sub>k</sub>)
{
    if ( Min (x<sub>i</sub>, x<sub>j</sub>)<= x<sub>k</sub> <= Max (x<sub>i</sub>, x<sub>j</sub>) <strong>and</strong> Min (y<sub>i</sub>, y<sub>j</sub>)<= y<sub>k</sub> <= Max (y<sub>i</sub>, y<sub>j</sub>) )
    {
        return <strong>true</strong>
    }
    else
    {
        return <strong>false</strong>
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/04/computational-geometry-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Algorithms in Computational Geometry &#8211; Part 1</title>
		<link>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html</link>
		<comments>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html#comments</comments>
		<pubDate>Thu, 08 Apr 2010 18:52:32 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computational Geometry]]></category>
		<category><![CDATA[Expression Design]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=219</guid>
		<description><![CDATA[Computational Geometry (CG) deals with algorithms for solving geometric problems. It is used in computer graphics, robotics, computer aided design,statistics among others. A typical CG problem takes as input a set of geometric objects like a set of points, a set of line segments etc. The output is most commonly finding certain attributes of the [...]]]></description>
			<content:encoded><![CDATA[<p>Computational Geometry (CG) deals with algorithms for solving geometric problems. It is used in computer graphics, robotics, computer aided design,statistics among others. A typical CG problem takes as input a set of geometric objects like a set of points, a set of line segments etc. The output is most commonly finding certain attributes of the geometric objects. For e.g. we are interested in answering the following questions in two dimensions (in a plane):</p>
<ol>
<li>Given two points p<sub>1</sub> &amp; p<sub>2</sub> in a co-ordinate plane, is the directed segment p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to directed segment p<sub>0</sub>p<sub>2</sub>? Where p<sub>0</sub> is the <em>origin</em> of the co-ordinate plane</li>
<li>Given two directed segments p<sub>0</sub>p<sub>1</sub> and p<sub>0</sub>p<sub>2</sub>, is p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to p<sub>0</sub>p<sub>2</sub> with respect to the common end point p<sub>0</sub>?</li>
<li>Given two line segments p<sub>0</sub>p<sub>1</sub> and p<sub>1</sub>p<sub>2</sub>, if we traverse from p<sub>0</sub> to p<sub>2</sub>, did we make a left, right or no turn at p<sub>1</sub>?</li>
<li>Do line segments p<sub>1</sub>p<sub>2</sub> and p<sub>3</sub>p<sub>4</sub> intersect?</li>
</ol>
<p>Download the demo application <a href="http://www.saipanyam.net/code-center/computational-geometry">here</a></p>
<p>Before we proceed any further a few definitions. <strong>Clockwise </strong>and <strong>Counterclockwise </strong>are intuitively easy to understand. They have the usual colloquial meaning. There is one thing to note though. A co-ordinate plane is not a <em>watch face</em>! Here we are determining the <em>relative orientations</em> of one segment w.r.t the other. Instead of trying to explain it in words, I will show you a picture (should replace a thousand words!).</p>
<div style="float: left;">
<div id="attachment_225" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-225" title="Orientation" src="http://www.saipanyam.net/wp-content/uploads/2010/04/Orientation-300x225.png" alt="Orientation" width="300" height="225" /><p class="wp-caption-text">The darkly shaded orientation region contains vectors counterclockwise relative to p. The lightly shaded region contains vectors clockwise to p </p></div>
</div>
<p>Two vectors are <strong>colinear</strong> if they are pointing in the same or opposite direction. In the picture all vectors that lie along the diagonal will be colinear to p.</p>
<p>With definitions out of the way, let us proceed to answer the initial queries that we posed. A brute force method of computing whether two line segments intersect, would be to find the line equations of the form y=mx + c where m is the slope and c the y-intercept and then compute the point of intersection, and verify whether this point lies on both lines. This involves a lot of expensive computational resources. Further this involves division which is sensitive to round-off errors depending upon the precision of the division operation. For e.g. almost parallel line segments will give incorrect results.</p>
<p>The algorithms given here will only use additions, multiplications, subtractions and comparisons ! No trigonometry also !<br />
We do this by computing the <strong>cross product</strong>. This is a fundamental technique that is at the base of most CG algorithms. Let me say that again. <strong>Cross Product</strong> is a most fundamental and important technique for solving CG problems. A <strong>cross product</strong> p<sub>1</sub> X p<sub>2</sub> is defined as the determinant of the matrix comprising of x and y values of the points.</p>
<div style="float: left;">
<div id="attachment_255" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-255" title="Determinant of Matrix" src="http://www.saipanyam.net/wp-content/uploads/2010/04/Determinant1-300x225.png" alt="Determinant" width="300" height="225" /><p class="wp-caption-text">Determinant of the above matrix will be equal to (x1y2-x2y1)</p></div>
</div>
<p>If the <strong>cross product </strong>is positive then p<sub>1</sub> is clockwise to p<sub>2</sub>. A boundary condition occurs if the cross product is <strong>0</strong>. In that case p<sub>1</sub> and p<sub>2</sub> are <strong>colinear</strong>.</p>
<p><strong>Proof</strong>: We use trigonometric function <strong>tan</strong> on the angle formed by the point (x,y), origin (0,0) and (x,0) for both the points. If the ratio of one tangent to other determines orientation depending upon whether the value is greater than 1 or not.</p>
<pre>//Clockwise:
x1y2-x2y1&gt;0

//So
x1y2&gt;x2y1

//alternatively
(x1y2)/(x2y1)&gt;1

//rearranging the terms
(y2/x2)/(y1/x1)&gt;1

//Notice that tan θ is the ratio of opposite side by adjacent side
//in a right-angled triangle
//so the numerator of the above fraction is tan θ<sub>2</sub> and
//denominator tan θ<sub>1</sub>

(tan θ<sub>2</sub>)/(tan θ<sub>1</sub>)&gt;1

<strong>If the angle θ<sub>2</sub> is greater than θ<sub>1</sub>
then we have proved that p<sub>1</sub> is clockwise to p<sub>2</sub></strong></pre>
<p>The above technique of using cross product makes for an easy and efficient way to answer <strong>Question 1</strong>.<br />
To answer <strong>Question 2</strong> just involves an extra pre-processing step before doing the cross product. To determine whether two directed segments p<sub>0</sub>p<sub>1</sub> and p<sub>0</sub>p<sub>2</sub>, is p<sub>0</sub>p<sub>1</sub> clockwise, counterclockwise or colinear to p<sub>0</sub>p<sub>2</sub> with respect to the common end point p<sub>0</sub> we just translate the axis to use p<sub>0</sub> as the origin and use the cross product as above.</p>
<pre>// To translate the origin to the common end point p<sub>0</sub>
//We let p<sub>1</sub>' (p prime) be the point where 

x<sub>1</sub>' =x<sub>1</sub> - x<sub>0</sub> and
y<sub>1</sub>' =y<sub>1</sub> - y<sub>0</sub>

//We do the above for  p<sub>2</sub> and calculate its prime: p<sub>2</sub>' 

x<sub>2</sub>' =x<sub>2</sub> - x<sub>0</sub> and
y<sub>2</sub>' =y<sub>2</sub> - y<sub>0</sub>

//Then we calculate the cross product of p<sub>1</sub>' x p<sub>2</sub>' 

<strong>If it is positive then the directed segment p<sub>0</sub>p<sub>1</sub> is clockwise to
directed segment p<sub>0</sub>p<sub>2</sub></strong></pre>
<p>In <a href="http://www.saipanyam.net/2010/04/computational-geometry-part-2.html">Part 2</a> we will answer <strong>questions 3 &amp; 4</strong> using the techniques and principles learned here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/04/computational-geometry-part-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple way to encrypt query strings</title>
		<link>http://www.saipanyam.net/2010/03/encrypt-query-strings.html</link>
		<comments>http://www.saipanyam.net/2010/03/encrypt-query-strings.html#comments</comments>
		<pubDate>Fri, 26 Mar 2010 23:12:06 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[encryption]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=190</guid>
		<description><![CDATA[Query strings are used to carry information. We might need to obfuscate them and provide some basic security without writing some elaborate encryption mechanism. Some standard ways of obfuscating, you will find is to Base64Encode the query string(remember to Url encode the resulting string as it is going to be on a url!): string data="query [...]]]></description>
			<content:encoded><![CDATA[<p>Query strings are used to carry information. We might need to obfuscate them and provide some basic security without writing some elaborate encryption mechanism. Some standard ways of obfuscating, you will find is to Base64Encode the query string(remember to Url encode the resulting string as it is going to be on a url!):</p>
<pre>
     string data="query string";
     string encodedData = String.Empty;
       try
       {
           byte[] data_byte = Encoding.UTF8.GetBytes(data);
           encodedData = HttpUtility.UrlEncode(Convert.ToBase64String(data_byte));
        }
        catch (Exception exception)
        {
           //Log exception
        }
        return encodedData;
</pre>
<p>While this works, it provides only <em>obfuscation</em>, but no <em>security</em>. Anyone can do the reverse:</p>
<pre>
        string data="encoded data";
        string decodedData = String.Empty;
        try
        {
             byte[] data_byte = Convert.FromBase64String(HttpUtiltity.UrlDecode(data));
             decodedData = Encoding.UTF8.GetString(data_byte);
        }
        catch (Exception exception)
        {
             //Log exception
         }
         return decodedData;
</pre>
<p>A little more advanced method is to use  <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.passwordderivebytes.aspx">PasswordDeriveBytes </a> class in .NET to do proper encryption. This has since been replaced with  <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx">Rfc2898DeriveBytes</a> in .NET 2.0. The reason you would want to use Rfc2898DeriveBytes is because it meets the PKCS #5 standard, which PasswordDerrivedBytes, does not. Both of them are present in<em> System.Security.Cryptography</em> namespace.</p>
<p>What we need to do is:</p>
<ul>
<li>Generate the encryption key and IV(Initialization Vector) from the password and salt.</li>
<li>Create a new instance of the encryptor with the key and IV.</li>
<li>Encrypt the query string.</li>
</ul>
<p>We are not done yet. Improper use of Rfc2898DeriveBytes will result in a performance impact on large sets of data. The cause is the call to GetBytes(int32) method on Rfc2898DeriveBytes class. It uses a pseudo-random number generator based on HMACSHA1. GetBytes initializes a new instance of HMAC is the killer in terms of performance. But the redeeming factor is, subsequent calls to GetBytes does not need to do this initialization.</p>
<p>So we change our implementation to do this expensive call once. Since there is no need to create a new instance of Rfc2898DeriveBytes for each call to encrypt, we will use the same key but change the IV for each message. So remove Rfc2898DeriveBytes from the Encrypt method and pass along the key and IV instead of the password and salt.</p>
<p>Putting all this together here is the final code (fast and efficient):</p>
<pre>
      public static class MyHelper
      {

          #region Private variables and Constants
          private const string ENCRYPTION_KEY = "Wx0Te1rc0pA";
          // Generated from Pass Phrase  "xxxxxxxx12012010" or some pass phrase that you can remember from
          // https://www.bigbiz.com/genkey.html Key Generator Site

          private const string QUERY_PARTS_DELIMITOR = "&amp;";
          private const string QUERY_PARAMS_DELIMITOR = "=";

          ///
          /// The salt value used to strengthen the encryption.
          ///
          private readonly static byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY);
          private readonly static byte[] key;
          private readonly static byte[] iv;
          private static readonly Rfc2898DeriveBytes keyGenerator;

          #endregion

         #region Constructor

         static MyHelper()
         {
            //We create the Rfc2898DerveBytes once as
            //Rfc2898DeriveBytes uses a pseudo-random number generator based on HMACSHA1.
            //When calling GetBytes it initializes a new instance of HMAC which takes some time.
            //Subsequent calls to GetBytes does not need to do this initialization.
            keyGenerator =new Rfc2898DeriveBytes(ENCRYPTION_KEY,SALT);
            key = keyGenerator.GetBytes(32);
            //Generate Initialization Vector - This will be less expensive as we have already intialized Rfc2898DeriveBytes
            iv = keyGenerator.GetBytes(16);
        }

       #endregion

       #region Encrypt/Decrypt Methods

        ///
<summary>
        /// Encrypts any string using the Rijndael algorithm.
        /// </summary>

        ///
<param name="inputText">The string to encrypt.</param>
        ///
<param name="queryStringParam">The name of the query string paramater</param>
        /// <returns>A Base64 encrypted string.</returns>
        public static string Encrypt(string inputText, string queryStringParam)
        {
            //Create a new RijndaelManaged cipher for the symmetric algorithm from the key and iv
            RijndaelManaged rijndaelCipher = new RijndaelManaged {Key = key, IV = iv};

            byte[] plainText = Encoding.Unicode.GetBytes(inputText);

            using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor())
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(plainText, 0, plainText.Length);
                        cryptoStream.FlushFinalBlock();
                        return "?" + queryStringParam + "=" + Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
        }

        ///
<summary>
        /// Decrypts a previously encrypted string.
        /// </summary>

        ///
<param name="inputText">The encrypted string to decrypt.</param>
        /// <returns>A decrypted string.</returns>
        public static string Decrypt(string inputText)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            byte[] encryptedData = Convert.FromBase64String(inputText);

            using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(key, iv))
            {
                using (MemoryStream memoryStream = new MemoryStream(encryptedData))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        byte[] plainText = new byte[encryptedData.Length];
                        int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                        return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                    }
                }
            }
        }

        #endregion

       }
</pre>
<p>The only draw back is this doesn&#8217;t give you full security.  This method will provide basic security in terms of stopping the hacker from deterministically manipulating the encrypted string. So it is a more a deterrence than a verification strategy. We can tell if someone tampers with the encrypted string only if the decryption &#8216;fails&#8217; to decrypt to something you recognize or expect.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/03/encrypt-query-strings.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;God Mode&#8221; in Windows 7</title>
		<link>http://www.saipanyam.net/2010/01/god-mode-in-windows-7.html</link>
		<comments>http://www.saipanyam.net/2010/01/god-mode-in-windows-7.html#comments</comments>
		<pubDate>Thu, 07 Jan 2010 00:28:05 +0000</pubDate>
		<dc:creator>Sai Panyam</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[interesting]]></category>
		<category><![CDATA[Windows7]]></category>

		<guid isPermaLink="false">http://www.saipanyam.net/?p=165</guid>
		<description><![CDATA[If you haven&#8217;t heard about &#8220;GodMode&#8221; in Windows 7, then read on. Windows 7 has a &#8220;GodMode&#8221; feature, which isn&#8217;t as grandiose as it sounds. Actually GodMode is not even the name that Microsoft has given it. In fact Microsoft doesn&#8217;t have an official name for it, as the feature itself is undocumented. GodMode is [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t heard about &#8220;GodMode&#8221; in Windows 7, then read on. Windows 7 has a &#8220;GodMode&#8221; feature, which isn&#8217;t as grandiose as it sounds. Actually GodMode is not even the name that Microsoft has given it. In fact Microsoft doesn&#8217;t have an official name for it, as the feature itself is undocumented. GodMode is the name given by bloggers who found it accidentally. I cannot imagine how they could have found it aside from getting some inside information from Microsoft developers themselves. I will attempt to give it a definition.</p>
<p><strong>GodMode:</strong> A feature that transforms a folder to a shortcut pointing to some windows tasks, just by appending a folder name with a period (&#8216;.&#8217;), followed by a special string (&#8216;{GUID}&#8217;).</p>
<p>It includes direct access to all kinds of settings, from choosing a location to managing power settings to identifying biometric sensors, to name a few.</p>
<p>To keep them all grouped together and &#8216;know&#8217; what they do with a glance, I followed this pattern-<br />
                                                    [folder name].{GUID}<br />
In my case, I chose &#8216;GodMode&#8217; as the folder name. Once the shortcut is made, I change the name to the following format before moving on to the next God Mode- [folder name].[feature name] For e.g. with the Control Panel God Mode, I would call it something like &#8216;GodMode.ControlPanel&#8217;.</p>
<p><strong>This is how it looks on my computer:</strong></p>
<div id="attachment_172" class="wp-caption alignnone" style="width: 430px"><img class="size-full wp-image-172" title="GodModeCapture" src="http://www.saipanyam.net/wp-content/uploads/2010/01/GodModeCapture.JPG" alt="God Mode Folders" width="420" height="341" /><p class="wp-caption-text">God Mode Folders</p></div>
<p>Here is a list of God Modes that I have found so far with a short description:</p>
<p><strong>Control Panel :</strong> {ED7BA470-8E54-465E-825C-99712043E01C}<br />
<strong>Description:</strong> A shortcut to all Control Panel functions</p>
<p><strong>Location Info :</strong> {00C6D95F-329C-409a-81D7-C46C66EA7F33}<br />
<strong>Description:</strong> A shortcut to set up default location for programs to use when a location sensor, such as GPS receiver, is unavailable. On can also enter Geographic location (latitude and longitude) too.</p>
<p><strong>Biometric Info :</strong> {0142e4d0-fb7a-11dc-ba4a-000ffe7ab428}<br />
<strong>Description:</strong> A shortcut showing the information of any biometric device like fingerprint reader etc, if installed.</p>
<p><strong>Power Plan :</strong> {025A5937-A6BE-4686-A844-36FE4BEC8B6D}<br />
<strong>Description:</strong> A shortcut to select the power consumption settings for your computer.</p>
<p><strong>System Tray :</strong> {05d7b0f4-2121-4eff-bf6b-ed3f69b894d9}<br />
<strong>Description:</strong> A shortcut which opens the icon and notifications settings, where you can set whether you want to show/hide icons in system tray and whether to show only notifications etc.</p>
<p><strong>Windows Vault :</strong> {1206F5F1-0569-412C-8FEC-3204630DFB70}<br />
<strong>Description:</strong> A shortcut which points to Windows Vault. We can store credentials for automatic logon here.</p>
<p><strong> Program Install:</strong> {15eae92e-f17a-4431-9f28-805e482dafd4}<br />
<strong>Description:</strong> A shortcut to install a program from the network.</p>
<p><strong> Default Programs:</strong> {17cd9488-1228-4b2f-88ce-4298e93e0966}<br />
<strong>Description:</strong> A shortcut to choose programs that Windows uses by default.</p>
<p><strong>Manage Wireless:</strong> {1FA9085F-25A2-489B-85D4-86326EEDCD87}<br />
<strong>Description:</strong> A shortcut to manage wireless networks.</p>
<p><strong>Manage Network:</strong> {208D2C60-3AEA-1069-A2D7-08002B30309D}<br />
<strong>Description:</strong> A shortcut to network computers, websites, FTP sites etc.<br />
<strong>Remarks</strong>: This God Mode doesn&#8217;t allow us to rename the folder to anything else. It keeps changing back to &#8216;Network (MS)&#8217;</p>
<p><strong>Manage Network:</strong> {1D2680C9-0E2A-469d-B787-065558BC7D43}<br />
<strong>Description:</strong> A shortcut to GAC (Global Assembly Cache).<br />
<strong>Remarks</strong>: This God Mode doesn&#8217;t change its folder icon or hide its GUID string like others. But it correctly points to the GAC on the computer.</p>
<p><strong>Computer Drives:</strong> {20D04FE0-3AEA-1069-A2D8-08002B30309D}<br />
<strong>Description:</strong> A shortcut which shows the current drives on the computer.</p>
<p><strong>Printers:</strong> {2227A280-3AEA-1069-A2DE-08002B30309D}<br />
<strong>Description:</strong> A shortcut to the printers added to the computer.</p>
<p><strong>Remote Connections:</strong> {241D7C96-F8BF-4F85-B01F-E2B043341A4B}<br />
<strong>Description:</strong> A shortcut to manage your RemoteApp and Desktop Connections.</p>
<p><strong>Windows Firewall:</strong> {4026492F-2F69-46B8-B9BF-5654FC07E423}<br />
<strong>Description:</strong> A shortcut to setup Windows Firewall settings to prevent malicious hackers from getting access to your computer.</p>
<p><strong>Computer Performance:</strong> {78F3955E-3B90-4184-BD14-5397C15F1EFC}<br />
<strong>Description:</strong> A shortcut to get your computer&#8217;s speed and performance information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.saipanyam.net/2010/01/god-mode-in-windows-7.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

