<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Peli's Farm - Pex, Stubs, Moles, QuickGraph, MbUnit, Reflector Addins - QuickGraph</title>
    <link>http://blog.dotnetwiki.org/</link>
    <description>TouchDevelop, Pex4Fun, Rise4Fun, Pex, Moles, QuickGraph, MbUnit, Reflector Addins</description>
    <language>en-us</language>
    <copyright>Jonathan 'Peli' de Halleux</copyright>
    <lastBuildDate>Sat, 19 Nov 2011 15:09:04 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jonathan.dehalleux@gmail.com</managingEditor>
    <webMaster>jonathan.dehalleux@gmail.com</webMaster>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=0e5d0c65-baab-4d26-90d2-20b190d5d6b9</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,0e5d0c65-baab-4d26-90d2-20b190d5d6b9.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,0e5d0c65-baab-4d26-90d2-20b190d5d6b9.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=0e5d0c65-baab-4d26-90d2-20b190d5d6b9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
QuickGraph is now a portable library. Get it while it is hot at <a href="http://quickgraph.codeplex.com">http://quickgraph.codeplex.com</a>.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=0e5d0c65-baab-4d26-90d2-20b190d5d6b9" />
      </body>
      <title>QuickGraph as a portable class library</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,0e5d0c65-baab-4d26-90d2-20b190d5d6b9.aspx</guid>
      <link>http://blog.dotnetwiki.org/2011/11/19/QuickGraphAsAPortableClassLibrary.aspx</link>
      <pubDate>Sat, 19 Nov 2011 15:09:04 GMT</pubDate>
      <description>&lt;p&gt;
QuickGraph is now a portable library. Get it while it is hot at &lt;a href="http://quickgraph.codeplex.com"&gt;http://quickgraph.codeplex.com&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=0e5d0c65-baab-4d26-90d2-20b190d5d6b9" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,0e5d0c65-baab-4d26-90d2-20b190d5d6b9.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=f93c462b-6837-415e-80d9-a62c8eb0d873</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,f93c462b-6837-415e-80d9-a62c8eb0d873.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,f93c462b-6837-415e-80d9-a62c8eb0d873.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=f93c462b-6837-415e-80d9-a62c8eb0d873</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://rise4fun.com/">http://rise4fun.com/</a> is a web front end to a number
of tools produced by the RiSE group. It also exposes<strong></strong><a href="http://rise4fun.com/Services.svc/help"><strong>REST
services</strong></a> that allow you to play with the tools from your favorite environment.
</p>
        <p>
          <strong>Rendering DOT graphs to SVG</strong>
        </p>
        <p>
          <a href="http://graphviz.org/">DOT</a> is a popular language to describe graphs. It
can be rendered into SVG using the MSAGL tool on <a href="http://rise4fun.com">http://rise4fun.com</a> .
To do so, you simply need to do a <strong>POST</strong> query to <a href="http://rise4fun.com/services.svc/ask/agl">http://rise4fun.com/services.svc/ask/agl</a> where
the dot code is passed in the request body. rise4fun returns SVG markup that can be
viewed in browsers that support it.
</p>
        <p>
Wondering what graph SVG look like? Check out <a href="http://rise4fun.com/agl/cilreader">http://rise4fun.com/agl/cilreader</a> to
see this beautiful graph. Make sure to zoom out as the graph is rather laaaaaarge.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ConvertingDOTgraphstoSVGusingtherise4fun_A860/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ConvertingDOTgraphstoSVGusingtherise4fun_A860/image_thumb.png" width="604" height="452" />
          </a>
        </p>
        <p>
Cheers, Peli
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f93c462b-6837-415e-80d9-a62c8eb0d873" />
      </body>
      <title>Converting DOT graphs to SVG using the rise4fun REST services</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,f93c462b-6837-415e-80d9-a62c8eb0d873.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/11/07/ConvertingDOTGraphsToSVGUsingTheRise4funRESTServices.aspx</link>
      <pubDate>Sun, 07 Nov 2010 20:09:47 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://rise4fun.com/"&gt;http://rise4fun.com/&lt;/a&gt; is a web front end to a number
of tools produced by the RiSE group. It also exposes&lt;strong&gt; &lt;/strong&gt;&lt;a href="http://rise4fun.com/Services.svc/help"&gt;&lt;strong&gt;REST
services&lt;/strong&gt;&lt;/a&gt; that allow you to play with the tools from your favorite environment.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Rendering DOT graphs to SVG&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://graphviz.org/"&gt;DOT&lt;/a&gt; is a popular language to describe graphs. It
can be rendered into SVG using the MSAGL tool on &lt;a href="http://rise4fun.com"&gt;http://rise4fun.com&lt;/a&gt; .
To do so, you simply need to do a &lt;strong&gt;POST&lt;/strong&gt; query to &lt;a href="http://rise4fun.com/services.svc/ask/agl"&gt;http://rise4fun.com/services.svc/ask/agl&lt;/a&gt; where
the dot code is passed in the request body. rise4fun returns SVG markup that can be
viewed in browsers that support it.
&lt;/p&gt;
&lt;p&gt;
Wondering what graph SVG look like? Check out &lt;a href="http://rise4fun.com/agl/cilreader"&gt;http://rise4fun.com/agl/cilreader&lt;/a&gt; to
see this beautiful graph. Make sure to zoom out as the graph is rather laaaaaarge.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ConvertingDOTgraphstoSVGusingtherise4fun_A860/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/ConvertingDOTgraphstoSVGusingtherise4fun_A860/image_thumb.png" width="604" height="452"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Cheers, Peli
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f93c462b-6837-415e-80d9-a62c8eb0d873" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,f93c462b-6837-415e-80d9-a62c8eb0d873.aspx</comments>
      <category>Fun with graphs</category>
      <category>QuickGraph</category>
      <category>RiSE</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=db310025-eb84-4138-845d-f744057c7da0</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,db310025-eb84-4138-845d-f744057c7da0.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,db310025-eb84-4138-845d-f744057c7da0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=db310025-eb84-4138-845d-f744057c7da0</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Render beautiful dot graphs to SVG at <a href="http://rise4fun.com/agl">http://rise4fun.com/agl</a> –
yes you can render any graph there. If your graph is big enough, it will trigger the
new <strong>edge bundling feature</strong> that looks AMAZING! Try loading this url
with modern browser with SVG support…. click, zoom out and wait for the magic.
</p>
        <blockquote>
          <p>
            <a title="http://www.rise4fun.com/Agl/cilreader" href="http://www.rise4fun.com/Agl/cilreader">
              <strong>http://www.rise4fun.com/Agl/cilreader</strong>
            </a>
          </p>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=db310025-eb84-4138-845d-f744057c7da0" />
      </body>
      <title>Rendering beautiful graphs at http://rise4fun.com/agl</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,db310025-eb84-4138-845d-f744057c7da0.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/10/30/RenderingBeautifulGraphsAtHttprise4funcomagl.aspx</link>
      <pubDate>Sat, 30 Oct 2010 05:59:00 GMT</pubDate>
      <description>&lt;p&gt;
Render beautiful dot graphs to SVG at &lt;a href="http://rise4fun.com/agl"&gt;http://rise4fun.com/agl&lt;/a&gt; –
yes you can render any graph there. If your graph is big enough, it will trigger the
new &lt;strong&gt;edge bundling feature&lt;/strong&gt; that looks AMAZING! Try loading this url
with modern browser with SVG support…. click, zoom out and wait for the magic.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a title="http://www.rise4fun.com/Agl/cilreader" href="http://www.rise4fun.com/Agl/cilreader"&gt;&lt;strong&gt;http://www.rise4fun.com/Agl/cilreader&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=db310025-eb84-4138-845d-f744057c7da0" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,db310025-eb84-4138-845d-f744057c7da0.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=fca14632-5606-41ce-b562-088f5e70c5da</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,fca14632-5606-41ce-b562-088f5e70c5da.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,fca14632-5606-41ce-b562-088f5e70c5da.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=fca14632-5606-41ce-b562-088f5e70c5da</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just uploaded a refresh of the <a href="http://quickgraph.codeplex.com" target="_blank">QuickGraph</a> binaries
with <a href="http://research.microsoft.com/contracts">Code Contracts</a> references
and documentation instrumented with the Contracts.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGraphrefreshwithCodeContracts_14540/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGraphrefreshwithCodeContracts_14540/image_thumb.png" width="239" height="244" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=fca14632-5606-41ce-b562-088f5e70c5da" />
      </body>
      <title>QuickGraph refresh with Code Contracts</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,fca14632-5606-41ce-b562-088f5e70c5da.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/06/04/QuickGraphRefreshWithCodeContracts.aspx</link>
      <pubDate>Fri, 04 Jun 2010 14:34:36 GMT</pubDate>
      <description>&lt;p&gt;
I just uploaded a refresh of the &lt;a href="http://quickgraph.codeplex.com" target="_blank"&gt;QuickGraph&lt;/a&gt; binaries
with &lt;a href="http://research.microsoft.com/contracts"&gt;Code Contracts&lt;/a&gt; references
and documentation instrumented with the Contracts.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGraphrefreshwithCodeContracts_14540/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGraphrefreshwithCodeContracts_14540/image_thumb.png" width="239" height="244"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=fca14632-5606-41ce-b562-088f5e70c5da" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,fca14632-5606-41ce-b562-088f5e70c5da.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=a95df4bb-aa7b-4bea-a6c1-f1df65ee8035</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,a95df4bb-aa7b-4bea-a6c1-f1df65ee8035.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,a95df4bb-aa7b-4bea-a6c1-f1df65ee8035.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=a95df4bb-aa7b-4bea-a6c1-f1df65ee8035</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While on parental leave, I could sneak a couple hours to read a paper on <a href="http://portal.acm.org/citation.cfm?id=1089023.1089024" target="_blank">Frontier
Search</a> by Korf and al. Frontier search is a kind of <a href="http://en.wikipedia.org/wiki/Best-first_search" target="_blank">best-first
search</a> algorithm that reduces the needs for memory. A very interesting and relaxing
read… I add <a href="http://quickgraph.codeplex.com/SourceControl/changeset/view/36531#430731" target="_blank">an
implementation to QuickGraph</a> that follows the idea.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=a95df4bb-aa7b-4bea-a6c1-f1df65ee8035" />
      </body>
      <title>Best First Frontier Search in QuickGraph</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,a95df4bb-aa7b-4bea-a6c1-f1df65ee8035.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/09/07/BestFirstFrontierSearchInQuickGraph.aspx</link>
      <pubDate>Mon, 07 Sep 2009 05:17:05 GMT</pubDate>
      <description>&lt;p&gt;
While on parental leave, I could sneak a couple hours to read a paper on &lt;a href="http://portal.acm.org/citation.cfm?id=1089023.1089024" target="_blank"&gt;Frontier
Search&lt;/a&gt; by Korf and al. Frontier search is a kind of &lt;a href="http://en.wikipedia.org/wiki/Best-first_search" target="_blank"&gt;best-first
search&lt;/a&gt; algorithm that reduces the needs for memory. A very interesting and relaxing
read… I add &lt;a href="http://quickgraph.codeplex.com/SourceControl/changeset/view/36531#430731" target="_blank"&gt;an
implementation to QuickGraph&lt;/a&gt; that follows the idea.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=a95df4bb-aa7b-4bea-a6c1-f1df65ee8035" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,a95df4bb-aa7b-4bea-a6c1-f1df65ee8035.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=3f76f5dd-5139-4205-a716-d9c48af1ff6f</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,3f76f5dd-5139-4205-a716-d9c48af1ff6f.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,3f76f5dd-5139-4205-a716-d9c48af1ff6f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=3f76f5dd-5139-4205-a716-d9c48af1ff6f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve just made a <a href="http://quickgraph.codeplex.com/Release/ProjectReleases.aspx" target="_blank">release</a> of <a href="http://quickgraph.codeplex.com" target="_blank">QuickGraph</a> 3.3
on CodePlex. Along with the usual bug fixes, this version brings a set of <strong>new
graph data structures based on delegates</strong>. Instead of taking care of the storage,
these graphs simply ‘query’ delegates for vertices and out-edges. You can use them
to bridge any existing graph data structure in your code with QuickGraph – with almost
no performance/memory penalty. (Delegate graphs can also be used to deal with graphs
that won’t hold in memory but that’s another story).
</p>
        <p>
Let’s see this with an example. Suppose, we have a simple graph representation using
a jagged array, <em>int[][]</em><em>graph</em>, where each edge is defined as <em>(i,
graph[i][j])</em> :
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb.png" width="360" height="133" />
          </a>
        </p>
        <p>
In order to use this graph with QuickGraph, <strong>we ‘wrap’ it up into a delegate
graph</strong>. There are a number of extension methods in GraphExtensions that take
care of the common cases. Delegate graphs rely on 2 delegates: one that returns the
vertices, the other one out-edges: 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_4.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb_1.png" width="587" height="107" />
          </a>
        </p>
        <p>
At this point, we can apply any of the algorithms that QuickGraph provides. For example,
computing a topological sort of the vertices:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_6.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb_2.png" width="322" height="67" />
          </a> 
</p>
        <p>
The new delegate graphs are ready to be downloaded at <a href="http://quickgraph.codeplex.com/Release/ProjectReleases.aspx">http://quickgraph.codeplex.com/Release/ProjectReleases.aspx</a> .
</p>
        <p>
Happy programming!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3f76f5dd-5139-4205-a716-d9c48af1ff6f" />
      </body>
      <title>QuickGraph 3.3: Easy interop with Delegate Graphs</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,3f76f5dd-5139-4205-a716-d9c48af1ff6f.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/08/26/QuickGraph33EasyInteropWithDelegateGraphs.aspx</link>
      <pubDate>Wed, 26 Aug 2009 15:34:18 GMT</pubDate>
      <description>&lt;p&gt;
I’ve just made a &lt;a href="http://quickgraph.codeplex.com/Release/ProjectReleases.aspx" target="_blank"&gt;release&lt;/a&gt; of &lt;a href="http://quickgraph.codeplex.com" target="_blank"&gt;QuickGraph&lt;/a&gt; 3.3
on CodePlex. Along with the usual bug fixes, this version brings a set of &lt;strong&gt;new
graph data structures based on delegates&lt;/strong&gt;. Instead of taking care of the storage,
these graphs simply ‘query’ delegates for vertices and out-edges. You can use them
to bridge any existing graph data structure in your code with QuickGraph – with almost
no performance/memory penalty. (Delegate graphs can also be used to deal with graphs
that won’t hold in memory but that’s another story).
&lt;/p&gt;
&lt;p&gt;
Let’s see this with an example. Suppose, we have a simple graph representation using
a jagged array, &lt;em&gt;int[][]&lt;/em&gt; &lt;em&gt;graph&lt;/em&gt;, where each edge is defined as &lt;em&gt;(i,
graph[i][j])&lt;/em&gt; :
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb.png" width="360" height="133"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
In order to use this graph with QuickGraph, &lt;strong&gt;we ‘wrap’ it up into a delegate
graph&lt;/strong&gt;. There are a number of extension methods in GraphExtensions that take
care of the common cases. Delegate graphs rely on 2 delegates: one that returns the
vertices, the other one out-edges: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb_1.png" width="587" height="107"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
At this point, we can apply any of the algorithms that QuickGraph provides. For example,
computing a topological sort of the vertices:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/QuickGrap.3EasyinteropwithDelegateGraphs_787C/image_thumb_2.png" width="322" height="67"&gt;&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
The new delegate graphs are ready to be downloaded at &lt;a href="http://quickgraph.codeplex.com/Release/ProjectReleases.aspx"&gt;http://quickgraph.codeplex.com/Release/ProjectReleases.aspx&lt;/a&gt; .
&lt;/p&gt;
&lt;p&gt;
Happy programming!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3f76f5dd-5139-4205-a716-d9c48af1ff6f" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,3f76f5dd-5139-4205-a716-d9c48af1ff6f.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=5a6044a6-c289-44a7-b921-f06e5b7bafb6</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,5a6044a6-c289-44a7-b921-f06e5b7bafb6.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,5a6044a6-c289-44a7-b921-f06e5b7bafb6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=5a6044a6-c289-44a7-b921-f06e5b7bafb6</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just released a new version of <strong>QuickGraph for Silverlight</strong> on <a href="http://quickgraph.codeplex.com">http://quickgraph.codeplex.com</a> .
You can grad it to run your favorite graph algorithm in the silverlight browser!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=5a6044a6-c289-44a7-b921-f06e5b7bafb6" />
      </body>
      <title>QuickGraph for Silverlight (new release)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,5a6044a6-c289-44a7-b921-f06e5b7bafb6.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/04/09/QuickGraphForSilverlightNewRelease.aspx</link>
      <pubDate>Thu, 09 Apr 2009 12:29:58 GMT</pubDate>
      <description>&lt;p&gt;
I just released a new version of &lt;strong&gt;QuickGraph for Silverlight&lt;/strong&gt; on &lt;a href="http://quickgraph.codeplex.com"&gt;http://quickgraph.codeplex.com&lt;/a&gt; .
You can grad it to run your favorite graph algorithm in the silverlight browser!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=5a6044a6-c289-44a7-b921-f06e5b7bafb6" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,5a6044a6-c289-44a7-b921-f06e5b7bafb6.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=35666880-fe92-4d7e-8e37-aa74796f87a6</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,35666880-fe92-4d7e-8e37-aa74796f87a6.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,35666880-fe92-4d7e-8e37-aa74796f87a6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=35666880-fe92-4d7e-8e37-aa74796f87a6</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_thumb.png" width="244" height="101" />
          </a>
        </p>
        <p>
Ealier this week, we released the <a href="http://research.microsoft.com/contracts" target="_blank">Code
Contracts</a> library for .NET. Since then, I’ve implemented a lot for contracts for
the data structures in <a href="http://www.codeplex.com/quickgraph" target="_blank">QuickGraph</a> .
In this post, I’ll talk about my experience with the contracts…
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_3.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_thumb_3.png" width="311" height="569" />
          </a>
        </p>
        <p>
          <strong>Walking through some contracts.</strong>
        </p>
        <p>
Let’s take a look at the contracts of AddEdge, a method that adds an edge to a graph.
Adding an edge to a graph is certainly a fun adventure, when you write contracts for
it. Let’s take a look:
</p>
        <pre class="code">
          <span style="color: blue">public interface </span>
          <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt; : ... <span style="color: blue">where </span>TEdge : <span style="color: #2b91af">IEdge</span>&lt;TVertex&gt;
{ <span style="color: gray">/// &lt;summary&gt; /// </span><span style="color: green">Adds
the edge to the graph </span><span style="color: gray">/// &lt;/summary&gt; /// &lt;param
name="edge"&gt;&lt;/param&gt; /// &lt;returns&gt;</span><span style="color: green">true
if the edge was added, otherwise false.</span><span style="color: gray">&lt;/returns&gt; </span><span style="color: blue">bool </span>AddEdge(TEdge
edge);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>Interface contracts</strong>
        </p>
        <p>
Since IMutableEdgeListGraph is an interface, we need to store the contracts in separate
class. To do so,  we ‘bind’ the interface and the contract class to each other
using the ContractClassForAttribute/ContractClassAttribute.
</p>
        <pre class="code">    [<span style="color: #2b91af">ContractClass</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">IMutableEdgeListGraphContract</span>&lt;,&gt;))] <span style="color: blue"> public
interface </span><span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt; ...</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code">    [<span style="color: #2b91af">ContractClassFor</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;,&gt;))] <span style="color: blue"> sealed
class </span><span style="color: #2b91af">IMutableEdgeListGraphContract</span>&lt;TVertex,
TEdge&gt; : <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt; <span style="color: blue">where </span>TEdge : <span style="color: #2b91af">IEdge</span>&lt;TVertex&gt;</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Once both types are bound, you can start implementing the contracts of the interface
in the contract class. Note that the contract class must use <i>explicit</i> interface
implementations for all methods.
</p>
        <p>
          <strong>Basic null checks</strong>
        </p>
        <p>
If you care about null references, the first contract will probably to ensure the
edge is not null. To make it quick and painless, make sure you use the <strong><em>crn</em></strong> snippet.
Note that since the method body of the contracts does not matter, we simply return
the default value.
</p>
        <pre class="code">
          <span style="color: blue">bool </span>
          <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt;.AddEdge(TEdge e) { <strong><span style="color: #2b91af">Contract</span>.Requires(e
!= <span style="color: blue">null</span>); <span style="color: blue">return default</span>(<span style="color: blue">bool</span>); </strong>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
        </p>
        <p>
          <strong>More pre-conditions</strong>
        </p>
        <p>
One of the <em>implicit</em> requirement of AddEdge is that both vertices should already
belong to the graph. We want to make this <em>explicit</em> as a pre-condition as
well:
</p>
        <pre class="code">
          <span style="color: blue">bool </span>
          <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt;.AddEdge(TEdge e) { <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt; ithis = <span style="color: blue">this</span>; <span style="color: #2b91af">Contract</span>.Requires(e
!= <span style="color: blue">null</span>); <strong><span style="color: #2b91af">Contract</span>.Requires(ithis.ContainsVertex(e.Source)); <span style="color: #2b91af">Contract</span>.Requires(ithis.ContainsVertex(e.Target));</strong></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
There are two things to notice here: (1) we had to cast the “this” pointer to the
interface we are writing contracts for, IMutableEdgeListGraph&lt;,&gt;. Because the
methods in a contract class must be explicit interface implementations, we do not
have access to the members of this interface from the “this” pointer, (2) ContainsVertex
had to be annotated with [Pure], as any method called from a contract must be pure: 
</p>
        <pre class="code">[<span style="color: #2b91af">Pure</span>] <span style="color: blue">bool </span>ContainsVertex(TVertex
vertex);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>What about post-conditions</strong>
        </p>
        <p>
One of my favorite feature of Code Contracts is to be able to state post-conditions.
This is done by calling the Contracts.Ensures method. For example, we start by expressing
that the edge must belong to the graph when we leave AddEdge:
</p>
        <pre class="code">
          <span style="color: blue">bool </span>
          <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt;.AddEdge(TEdge e) { <span style="color: #2b91af">IMutableEdgeListGraph</span>&lt;TVertex,
TEdge&gt; ithis = <span style="color: blue">this</span>;<br />
... 
<br /><span style="color: #2b91af">Contract</span>.Ensures(ithis.ContainsEdge(e));</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>Result and Old</strong>
        </p>
        <p>
Since the method returns a boolean, we should also state something about the result
value. To refer to the result, one has to use the Contract.Result&lt;T&gt;() method.
In this case,  the method returns true if the edge was new, false if it was already
in the graph. We can refer to a pre-state, i.e. the value of this.Contains(e) at the
beginning of the method***:
</p>
        <pre class="code">
          <span style="color: #2b91af">Contract</span>.Ensures(<span style="color: #2b91af">Contract</span>.Result&lt;<span style="color: blue">bool</span>&gt;() 
<br />
== <span style="color: #2b91af">Contract</span>.OldValue(!ithis.ContainsEdge(e)));</pre>
        <p>
Lastly, we can also make sure the edge count has been incremented, if an edge was
actually added (as you can see, we could use an implication operator in C#):
</p>
        <p>
          <span style="color: #2b91af">Contract</span>.Ensures(ithis.EdgeCount 
<br />
== <span style="color: #2b91af">Contract</span>.OldValue(ithis.EdgeCount) + (<span style="color: #2b91af">Contract</span>.Result&lt;<span style="color: blue">bool</span>&gt;()
? 1 : 0));
</p>
        <p>
*** The OldState value is evaluated after the preconditions.
</p>
        <p>
          <strong>Where to go next?</strong>
        </p>
        <p>
In the next post, I’ll show how to leverage these Contracts with <a href="http://research.microsoft.com/pex" target="_blank">Pex</a>…
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=35666880-fe92-4d7e-8e37-aa74796f87a6" />
      </body>
      <title>QuickGraph on Code Contracts</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,35666880-fe92-4d7e-8e37-aa74796f87a6.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/02/28/QuickGraphOnCodeContracts.aspx</link>
      <pubDate>Sat, 28 Feb 2009 16:33:15 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_thumb.png" width="244" height="101" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Ealier this week, we released the &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Code
Contracts&lt;/a&gt; library for .NET. Since then, I’ve implemented a lot for contracts for
the data structures in &lt;a href="http://www.codeplex.com/quickgraph" target="_blank"&gt;QuickGraph&lt;/a&gt; .
In this post, I’ll talk about my experience with the contracts…
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_3.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/QuickGraphonCodeContracts_6BA/image_thumb_3.png" width="311" height="569" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Walking through some contracts.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Let’s take a look at the contracts of AddEdge, a method that adds an edge to a graph.
Adding an edge to a graph is certainly a fun adventure, when you write contracts for
it. Let’s take a look:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; : ... &lt;span style="color: blue"&gt;where &lt;/span&gt;TEdge : &lt;span style="color: #2b91af"&gt;IEdge&lt;/span&gt;&amp;lt;TVertex&amp;gt;
{ &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt; /// &lt;/span&gt;&lt;span style="color: green"&gt;Adds
the edge to the graph &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt; /// &amp;lt;param
name=&amp;quot;edge&amp;quot;&amp;gt;&amp;lt;/param&amp;gt; /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;true
if the edge was added, otherwise false.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;AddEdge(TEdge
edge);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Interface contracts&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Since IMutableEdgeListGraph is an interface, we need to store the contracts in separate
class. To do so,&amp;#160; we ‘bind’ the interface and the contract class to each other
using the ContractClassForAttribute/ContractClassAttribute.
&lt;/p&gt;
&lt;pre class="code"&gt;    [&lt;span style="color: #2b91af"&gt;ContractClass&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraphContract&lt;/span&gt;&amp;lt;,&amp;gt;))] &lt;span style="color: blue"&gt; public
interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; ...&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; &lt;pre class="code"&gt;    [&lt;span style="color: #2b91af"&gt;ContractClassFor&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;,&amp;gt;))] &lt;span style="color: blue"&gt; sealed
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraphContract&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; : &lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; &lt;span style="color: blue"&gt;where &lt;/span&gt;TEdge : &lt;span style="color: #2b91af"&gt;IEdge&lt;/span&gt;&amp;lt;TVertex&amp;gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Once both types are bound, you can start implementing the contracts of the interface
in the contract class. Note that the contract class must use &lt;i&gt;explicit&lt;/i&gt; interface
implementations for all methods.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Basic null checks&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
If you care about null references, the first contract will probably to ensure the
edge is not null. To make it quick and painless, make sure you use the &lt;strong&gt;&lt;em&gt;crn&lt;/em&gt;&lt;/strong&gt; snippet.
Note that since the method body of the contracts does not matter, we simply return
the default value.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt;.AddEdge(TEdge e) { &lt;strong&gt; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(e
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: blue"&gt;return default&lt;/span&gt;(&lt;span style="color: blue"&gt;bool&lt;/span&gt;); &lt;/strong&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;More pre-conditions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
One of the &lt;em&gt;implicit&lt;/em&gt; requirement of AddEdge is that both vertices should already
belong to the graph. We want to make this &lt;em&gt;explicit&lt;/em&gt; as a pre-condition as
well:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt;.AddEdge(TEdge e) { &lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; ithis = &lt;span style="color: blue"&gt;this&lt;/span&gt;; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(e
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;strong&gt; &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(ithis.ContainsVertex(e.Source)); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(ithis.ContainsVertex(e.Target));&lt;/strong&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
There are two things to notice here: (1) we had to cast the “this” pointer to the
interface we are writing contracts for, IMutableEdgeListGraph&amp;lt;,&amp;gt;. Because the
methods in a contract class must be explicit interface implementations, we do not
have access to the members of this interface from the “this” pointer, (2) ContainsVertex
had to be annotated with [Pure], as any method called from a contract must be pure: 
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Pure&lt;/span&gt;] &lt;span style="color: blue"&gt;bool &lt;/span&gt;ContainsVertex(TVertex
vertex);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;What about post-conditions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
One of my favorite feature of Code Contracts is to be able to state post-conditions.
This is done by calling the Contracts.Ensures method. For example, we start by expressing
that the edge must belong to the graph when we leave AddEdge:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt;.AddEdge(TEdge e) { &lt;span style="color: #2b91af"&gt;IMutableEdgeListGraph&lt;/span&gt;&amp;lt;TVertex,
TEdge&amp;gt; ithis = &lt;span style="color: blue"&gt;this&lt;/span&gt;;&lt;br /&gt;
... 
&lt;br /&gt;
&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(ithis.ContainsEdge(e));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Result and Old&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Since the method returns a boolean, we should also state something about the result
value. To refer to the result, one has to use the Contract.Result&amp;lt;T&amp;gt;() method.
In this case,&amp;#160; the method returns true if the edge was new, false if it was already
in the graph. We can refer to a pre-state, i.e. the value of this.Contains(e) at the
beginning of the method***:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;() 
&lt;br /&gt;
== &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(!ithis.ContainsEdge(e)));&lt;/pre&gt;
&lt;p&gt;
Lastly, we can also make sure the edge count has been incremented, if an edge was
actually added (as you can see, we could use an implication operator in C#):
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(ithis.EdgeCount 
&lt;br /&gt;
== &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(ithis.EdgeCount) + (&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;()
? 1 : 0));
&lt;/p&gt;
&lt;p&gt;
*** The OldState value is evaluated after the preconditions.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Where to go next?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In the next post, I’ll show how to leverage these Contracts with &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt;…
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=35666880-fe92-4d7e-8e37-aa74796f87a6" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,35666880-fe92-4d7e-8e37-aa74796f87a6.aspx</comments>
      <category>Code Contracts</category>
      <category>Pex</category>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f</wfw:commentRss>
      <title>Tarjan Offline Least Common Ancestor in C# (with QuickGraph)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/08/TarjanOfflineLeastCommonAncestorInCWithQuickGraph.aspx</link>
      <pubDate>Thu, 08 Jan 2009 08:32:54 GMT</pubDate>
      <description>&lt;p&gt;
Following the implementation of the &lt;a target="_blank" href="http://blog.dotnetwiki.org/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx"&gt;disjointset&lt;/a&gt;,
another fun algorithm to implement was the offline &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Lowest_common_ancestor"&gt;least
common ancestor problem&lt;/a&gt;. &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Tarjan%27s_off-line_least_common_ancestors_algorithm"&gt;Tarjan
proposed a algorithm&lt;/a&gt; based on the disjoint-set and a DFS traversal. Given that
I now have all those ingredients in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;,
it was just a matter of hooking together the implementation with the right events.
The beef of the implementation looks as follows (and looks quite elegant to me):
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;gpair = &lt;span style="color: #2b91af"&gt;GraphExtensions&lt;/span&gt;.ToAdjacencyGraph(&lt;span style="color: blue"&gt;this&lt;/span&gt;.pairs);
// for fast lookup &lt;span style="color: blue"&gt;var &lt;/span&gt;disjointSet = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ForestDisjointSet&lt;/span&gt;
&lt;TVertex&gt;
(); &lt;span style="color: blue"&gt;var &lt;/span&gt;vancestors = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&lt;TVertex, TVertex&gt;(); &lt;span style="color: blue"&gt;var &lt;/span&gt;dfs
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DepthFirstSearchAlgorithm&lt;/span&gt;&lt;TVertex, TEdge&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: blue"&gt;this&lt;/span&gt;.VisitedGraph, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&lt;TVertex, &lt;span style="color: #2b91af"&gt;GraphColor&gt;&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.VisitedGraph.VertexCount));
dfs.InitializeVertex += (sender, args) =&gt; disjointSet.MakeSet(args.Vertex); dfs.DiscoverVertex
+= (sender, args) =&gt; vancestors[args.Vertex] = args.Vertex; dfs.TreeEdge += (sender,
args) =&gt; { &lt;span style="color: blue"&gt;var &lt;/span&gt;edge = args.Edge; disjointSet.Union(edge.Source,
edge.Target); vancestors[disjointSet.FindSet(edge.Source)] = edge.Source; }; dfs.FinishVertex
+= (sender, args) =&gt; { &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;e &lt;span style="color: blue"&gt;in &lt;/span&gt;gpair.OutEdges(args.Vertex)) &lt;span style="color: blue"&gt;if &lt;/span&gt;(dfs.VertexColors[e.Target]
== &lt;span style="color: #2b91af"&gt;GraphColor&lt;/span&gt;.Black) &lt;span style="color: blue"&gt;this&lt;/span&gt;.ancestors[&lt;span style="color: #2b91af"&gt;EdgeExtensions&lt;/span&gt;.ToVertexPair(e)]
= vancestors[disjointSet.FindSet(e.Target)]; }; &lt;span style="color: green"&gt;// go! &lt;/span&gt;dfs.Compute(root);
&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Enjoy!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,0970233e-8ce3-435a-b2e5-2fc6ccd0bb8f.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=e52cfaba-ca74-4841-8067-9fd229873685</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=e52cfaba-ca74-4841-8067-9fd229873685</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The <a target="_blank" href="http://www.nist.gov/dads/HTML/kthShortestPath.html"><strong>k-shortest
paths</strong></a> computes the <em>k</em> first shortest path between two vertices
in a directed graph. If you put this in the context of route planning, it gives you <em>k</em> alternative
routes in case the shortest path is blocked by snow :) While the <a target="_blank" href="http://en.wikipedia.org/wiki/Shortest_path_problem">single
source shortest</a> path is well-known and implemented in many languages, there are
not many implementations available for this problem, although it has been extensively
studied. After looking around, I stumbled on a <a target="_blank" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.3286">nice
article comparing various approaches</a>. The authors pointed out <a target="_blank" href="http://citeseerx.ist.psu.edu/showciting?cid=463224">an
algorithm from 1959 from Hoffman and Pavley</a> that solved this problem (there are
actually many others). This algorithm looked like a good fit:
</p>
        <ul>
          <li>
it requires a single call to a single-source shortest path algorithm. Other approaches
will require as much as <em>kn</em> calls to a shortest path algorithm.</li>
          <li>
it sounded simple and did not require new specialized data structures.</li>
        </ul>
        <p>
          <strong>I want to try it!</strong>
        </p>
        <p align="left">
The algorithm is available in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a> 3.1.40104.00
and up. You can take a look at it at <a title="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982">http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982</a>.
To use it on a <em>BidirectionalGraph</em>, 
</p>
        <blockquote>
          <p>
IBidirectionalGraph&lt;TVertex, TEdge&gt; g = …;<br />
foreach(IEnumerable&lt;TEdge&gt; path in g.RankedShortestPathHoffmanPavley(weights,
source, goal, 4))<br />
     …
</p>
        </blockquote>
        <p>
          <strong>A glimpse at the Hoffmann-Pavley algorithm</strong>
        </p>
        <p>
The algorithm works in 2 phases. 
</p>
        <p>
On the first phase, we build a minimum ‘successor’ tree towards the goal vertex. This
tree can be used to build a shortest path from any (reachable) vertex to the goal
vertex. To build this tree, we can simply apply the Dijkstra shortest path algorithm
on the reversed graph. This can be done in a couple lines with QuickGraph.
</p>
        <p>
As for many other <em>k</em>-shortest path algorithm, phase works by building deviation
path and picking the best one. In the case of the Hoffman-Pavley algorithm, it works
as follows: pick the latest shortest path, for each vertex of this path, build a deviation
path (more later) for each out-edge and add it to a priority queue. Then start again:
</p>
        <blockquote>
          <p>
var queue = new PriorityQueue&lt;Path&gt;();<br />
queue.Enqueue(shortestpath);<br />
while(queue.Count &gt; 0) {<br />
    var path = queue.Dequeue();<br />
    foreach(var vertex in path)<br />
        foreach(var edge in graph.OutEdges(vertex))<br />
             queue.Enqueue(CreateDeviation(path,
vertex, edge));<br />
}
</p>
        </blockquote>
        <p>
A deviation path is composed of three parts: 
</p>
        <ol>
          <li>
the initial part of the ‘seeding’ path, i.e. the edges before the deviation edge,</li>
          <li>
the deviation edge</li>
          <li>
the remaining shortest path to the goal. When we build the deviation path, we also
compute it’s weight. A nice property of the deviation paths is that they can be ‘built’
when needed. This saves a lot of space and computation as most deviation paths will
probably not end up in the winning set of paths – instead of storing a full path,
we store a path index and an edge index.</li>
        </ol>
        <p>
That’s it! The details contain more code to deal with self-edges and loops but the
main idea is there. This is definitely a very elegant algorithm!
</p>
        <p>
          <strong>What about testing!</strong>
        </p>
        <p>
Algorithm authors usually illustrate their approach with an example. This is a good
way to get started on a small graph example and ensure that the algorithm works as
the original author expected. This is the kind of unit test I get started with.
</p>
        <p>
The next step is to apply the algorithm to a large number of graph instances. Unfortunately,
I do not have any other k-shortest path algorithm, so the oracle is harder to build
here. Nevertheless, the result of the algorithm, i.e. the shortest path collection,
has a couple of properties that should always be true:
</p>
        <ul>
          <li>
the algorithm produces loopless paths,</li>
          <li>
path <em>k</em> is lower or equal to path <em>k</em> + 1.</li>
        </ul>
        <p>
The problem with this test is that it does not guarantee that some shortest path have
been missed. At this point, I’m a bit puzzled on how to test that.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e52cfaba-ca74-4841-8067-9fd229873685" />
      </body>
      <title>A K-Shortest Path implementation in .Net</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/05/AKShortestPathImplementationInNet.aspx</link>
      <pubDate>Mon, 05 Jan 2009 07:05:14 GMT</pubDate>
      <description>&lt;p&gt;
The &lt;a target="_blank" href="http://www.nist.gov/dads/HTML/kthShortestPath.html"&gt;&lt;strong&gt;k-shortest
paths&lt;/strong&gt;&lt;/a&gt; computes the &lt;em&gt;k&lt;/em&gt; first shortest path between two vertices
in a directed graph. If you put this in the context of route planning, it gives you &lt;em&gt;k&lt;/em&gt; alternative
routes in case the shortest path is blocked by snow :) While the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Shortest_path_problem"&gt;single
source shortest&lt;/a&gt; path is well-known and implemented in many languages, there are
not many implementations available for this problem, although it has been extensively
studied. After looking around, I stumbled on a &lt;a target="_blank" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.3286"&gt;nice
article comparing various approaches&lt;/a&gt;. The authors pointed out &lt;a target="_blank" href="http://citeseerx.ist.psu.edu/showciting?cid=463224"&gt;an
algorithm from 1959 from Hoffman and Pavley&lt;/a&gt; that solved this problem (there are
actually many others). This algorithm looked like a good fit:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
it requires a single call to a single-source shortest path algorithm. Other approaches
will require as much as &lt;em&gt;kn&lt;/em&gt; calls to a shortest path algorithm.&lt;/li&gt;
&lt;li&gt;
it sounded simple and did not require new specialized data structures.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;I want to try it!&lt;/strong&gt;
&lt;/p&gt;
&lt;p align="left"&gt;
The algorithm is available in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt; 3.1.40104.00
and up. You can take a look at it at &lt;a title="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982"&gt;http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982&lt;/a&gt;.
To use it on a &lt;em&gt;BidirectionalGraph&lt;/em&gt;, 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
IBidirectionalGraph&amp;lt;TVertex, TEdge&amp;gt; g = …;&lt;br&gt;
foreach(IEnumerable&amp;lt;TEdge&amp;gt; path in g.RankedShortestPathHoffmanPavley(weights,
source, goal, 4))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; …
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;A glimpse at the Hoffmann-Pavley algorithm&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The algorithm works in 2 phases. 
&lt;/p&gt;
&lt;p&gt;
On the first phase, we build a minimum ‘successor’ tree towards the goal vertex. This
tree can be used to build a shortest path from any (reachable) vertex to the goal
vertex. To build this tree, we can simply apply the Dijkstra shortest path algorithm
on the reversed graph. This can be done in a couple lines with QuickGraph.
&lt;/p&gt;
&lt;p&gt;
As for many other &lt;em&gt;k&lt;/em&gt;-shortest path algorithm, phase works by building deviation
path and picking the best one. In the case of the Hoffman-Pavley algorithm, it works
as follows: pick the latest shortest path, for each vertex of this path, build a deviation
path (more later) for each out-edge and add it to a priority queue. Then start again:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
var queue = new PriorityQueue&amp;lt;Path&amp;gt;();&lt;br&gt;
queue.Enqueue(shortestpath);&lt;br&gt;
while(queue.Count &amp;gt; 0) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var path = queue.Dequeue();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(var vertex in path)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(var edge in graph.OutEdges(vertex))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; queue.Enqueue(CreateDeviation(path,
vertex, edge));&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
A deviation path is composed of three parts: 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
the initial part of the ‘seeding’ path, i.e. the edges before the deviation edge,&lt;/li&gt;
&lt;li&gt;
the deviation edge&lt;/li&gt;
&lt;li&gt;
the remaining shortest path to the goal. When we build the deviation path, we also
compute it’s weight. A nice property of the deviation paths is that they can be ‘built’
when needed. This saves a lot of space and computation as most deviation paths will
probably not end up in the winning set of paths – instead of storing a full path,
we store a path index and an edge index.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
That’s it! The details contain more code to deal with self-edges and loops but the
main idea is there. This is definitely a very elegant algorithm!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;What about testing!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Algorithm authors usually illustrate their approach with an example. This is a good
way to get started on a small graph example and ensure that the algorithm works as
the original author expected. This is the kind of unit test I get started with.
&lt;/p&gt;
&lt;p&gt;
The next step is to apply the algorithm to a large number of graph instances. Unfortunately,
I do not have any other k-shortest path algorithm, so the oracle is harder to build
here. Nevertheless, the result of the algorithm, i.e. the shortest path collection,
has a couple of properties that should always be true:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
the algorithm produces loopless paths,&lt;/li&gt;
&lt;li&gt;
path &lt;em&gt;k&lt;/em&gt; is lower or equal to path &lt;em&gt;k&lt;/em&gt; + 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The problem with this test is that it does not guarantee that some shortest path have
been missed. At this point, I’m a bit puzzled on how to test that.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e52cfaba-ca74-4841-8067-9fd229873685" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</comments>
      <category>Fun with graphs</category>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
When you implement an algorithm that computes an optimal solution (i.e. minimum/maximum
with respect to a cost function), how do you test that the solution is actually optimal? 
</p>
        </blockquote>
        <p>
This is the kind of question I face when implementing algorithms in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a>.
For example, I was recently looking at the <a target="_blank" href="http://en.wikipedia.org/wiki/Minimum_spanning_tree">minimum
spanning tree of a graph</a> (MST)? While checking that the result tree is a spanning
tree is easy, checking that it is <em>minimum</em> is not obvious: nobody has written <em>Assert.IsMinimum</em> yet
:). Here are a couple techniques that I found useful along the way: 
</p>
        <p>
          <strong>Input-Output table</strong>
        </p>
        <p>
The most obvious approach is to pre-compute the result for a number of problems and
assert the solution of the algorithm matches. In this MST case, use a small set of
graphs for which the MST is well known, and check that the computed MST has the correct
weight. This approach will take you only so far and requires a lot of manual work
since you need to solve the problem (or find a known solution) for a number of representative
cases.
</p>
        <p>
          <strong>Solution Perturbation</strong>
        </p>
        <p>
If the algorithm computes a solution that is minimal with respect to a cost function,
one can try to perturbate the solution to see if there’s a smaller one. If so, it
clearly violates the fact that the solution should be minimal, thus you just found
a bug. In the case of MST, this would mean randomly picking edges that are not in
the minimum spanning tree, swapping them and evaluate if the result tree is smaller.
</p>
        <p>
This is kind of approach is actually used in optimization where the search space might
have local minima (see <a target="_blank" href="http://en.wikipedia.org/wiki/Simulated_annealing">simulated
annealing</a>).
</p>
        <p>
          <strong>Multiple Implementations</strong>
        </p>
        <p>
A nice thing about graph problems is that there are usually many (vastly) different
ways to solve them. If multiple implementations are available, we can simply compare
the result of each algorithm against each other and make sure that they match each
other. Each algorithm might have bugs but it is unlikely that they share common ones.
Since we have now a good oracle, we can apply this approach that a large number of
inputs to increase our coverage.
</p>
        <p>
In the MST case, two popular algorithms are <a target="_blank" href="http://en.wikipedia.org/wiki/Prim's_algorithm">Prim’s</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal's_algorithm">Kruskal’s</a>.
Those are 2 different approaches: Prim is built on top of <a target="_blank" href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm">Dijkstra
single source shortest path</a>, while Kruskal is built on top of the <a target="_blank" href="http://blog.dotnetwiki.org/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx">disjoint-set</a> data
structure. By carefully picking the weights of the edges, we can assert different
things:
</p>
        <ul>
          <li>
if the edge weights are all equals, any spanning tree is minimal. So we can compare
the result to a depth-first-search algorithm (which can easily compute a spanning
tree). 
</li>
          <li>
if some edge weights are different, there may be many minimum spanning tree. In this
case, we can still assert that weight of the tree is minimum. 
</li>
          <li>
if all the edge weights are different, then the MST is unique. This fact can be used
to precisely pinpoint the differences in solution during testing.</li>
        </ul>
        <p>
There is a corner case that needs to be checked: if all algorithm are no-op, i.e.
they don’t do anything. There solution will always match!
</p>
        <p>
Happy New Year!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d" />
      </body>
      <title>Testing Graph Algorithms With Optimal Solutions</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/01/TestingGraphAlgorithmsWithOptimalSolutions.aspx</link>
      <pubDate>Thu, 01 Jan 2009 17:03:14 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
When you implement an algorithm that computes an optimal solution (i.e. minimum/maximum
with respect to a cost function), how do you test that the solution is actually optimal? 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is the kind of question I face when implementing algorithms in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;.
For example, I was recently looking at the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Minimum_spanning_tree"&gt;minimum
spanning tree of a graph&lt;/a&gt; (MST)? While checking that the result tree is a spanning
tree is easy, checking that it is &lt;em&gt;minimum&lt;/em&gt; is not obvious: nobody has written &lt;em&gt;Assert.IsMinimum&lt;/em&gt; yet
:). Here are a couple techniques that I found useful along the way: 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Input-Output table&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The most obvious approach is to pre-compute the result for a number of problems and
assert the solution of the algorithm matches. In this MST case, use a small set of
graphs for which the MST is well known, and check that the computed MST has the correct
weight. This approach will take you only so far and requires a lot of manual work
since you need to solve the problem (or find a known solution) for a number of representative
cases.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution Perturbation&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
If the algorithm computes a solution that is minimal with respect to a cost function,
one can try to perturbate the solution to see if there’s a smaller one. If so, it
clearly violates the fact that the solution should be minimal, thus you just found
a bug. In the case of MST, this would mean randomly picking edges that are not in
the minimum spanning tree, swapping them and evaluate if the result tree is smaller.
&lt;/p&gt;
&lt;p&gt;
This is kind of approach is actually used in optimization where the search space might
have local minima (see &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Simulated_annealing"&gt;simulated
annealing&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Multiple Implementations&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A nice thing about graph problems is that there are usually many (vastly) different
ways to solve them. If multiple implementations are available, we can simply compare
the result of each algorithm against each other and make sure that they match each
other. Each algorithm might have bugs but it is unlikely that they share common ones.
Since we have now a good oracle, we can apply this approach that a large number of
inputs to increase our coverage.
&lt;/p&gt;
&lt;p&gt;
In the MST case, two popular algorithms are &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Prim's_algorithm"&gt;Prim’s&lt;/a&gt; and &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal's_algorithm"&gt;Kruskal’s&lt;/a&gt;.
Those are 2 different approaches: Prim is built on top of &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm"&gt;Dijkstra
single source shortest path&lt;/a&gt;, while Kruskal is built on top of the &lt;a target="_blank" href="http://blog.dotnetwiki.org/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx"&gt;disjoint-set&lt;/a&gt; data
structure. By carefully picking the weights of the edges, we can assert different
things:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
if the edge weights are all equals, any spanning tree is minimal. So we can compare
the result to a depth-first-search algorithm (which can easily compute a spanning
tree). 
&lt;li&gt;
if some edge weights are different, there may be many minimum spanning tree. In this
case, we can still assert that weight of the tree is minimum. 
&lt;li&gt;
if all the edge weights are different, then the MST is unique. This fact can be used
to precisely pinpoint the differences in solution during testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
There is a corner case that needs to be checked: if all algorithm are no-op, i.e.
they don’t do anything. There solution will always match!
&lt;/p&gt;
&lt;p&gt;
Happy New Year!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</comments>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=06a6b1c8-6cc3-4728-9791-c129619219eb</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=06a6b1c8-6cc3-4728-9791-c129619219eb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently implemented a <a target="_blank" href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint-Set</a> data
structure in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a>,
which is the main building block of the <a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm">Kruskal’s
minimum spanning tree algorithm</a>. This is a fun data-structure to look at, as it
purpose is quite different from the ‘main’ BCL collections. The disjoint-set is useful
to partition elements into sets, and defines 2 main operation to that purpose: <em>Find</em>,
finds the set an element belongs to (can be used to check if 2 elements are in the
same set), <em>Union</em> merges two sets.
</p>
        <p>
The <a target="_blank" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29499#343829">source
of the disjoint-set</a> is in the QuickGraph source, if you are curious about the
details.
</p>
        <p>
          <strong>Testing the disjoint-set</strong>
        </p>
        <p>
There is not much left to TDD when it comes to write data structure. Such data structure
are usually described in details in an article, and you ‘just’ have to follow the
authors instruction to implement. Nevertheless, unit testing is really critical to
ensure that the implementation is correct – but there is a risk of having to re-implement
the algorithm to test it. 
</p>
        <p>
For the disjoint-set, I used 2 tools developed at <a target="_blank" href="http://research.microsoft.com/rise">RiSE</a>: <a target="_blank" href="http://research.microsoft.com/contracts">Code
Contracts</a> and… <a target="_blank" href="http://research.microsoft.com/pex">Pex</a>.
When implementing data structure from the literature, I usually start ‘dumping’ the
code and the contracts. As much as possible, any invariant or property that the author
describes should be translated to contracts. It will give more opportunities for Pex
to find bugs in my code.
</p>
        <p>
          <strong>Contracts First</strong>
        </p>
        <p>
For example, the contracts for the <strong>Union</strong> method look as follows:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: blue">private bool </span>Union(<span style="color: #2b91af">Element </span>left, <span style="color: #2b91af">Element </span>right)
{ <span style="color: #2b91af">Contract</span>.Requires(left != <span style="color: blue">null</span>); <span style="color: #2b91af">Contract</span>.Requires(right
!= <span style="color: blue">null</span>); <span style="color: #2b91af">Contract</span>.Ensures(<span style="color: #2b91af">Contract</span>.Result&lt;<span style="color: blue">bool</span>&gt;()
? <span style="color: #2b91af">Contract</span>.OldValue(<span style="color: blue">this</span>.SetCount)
- 1 == <span style="color: blue">this</span>.SetCount : <span style="color: #2b91af">Contract</span>.OldValue(<span style="color: blue">this</span>.SetCount)
== <span style="color: blue">this</span>.SetCount ); <span style="color: #2b91af">Contract</span>.Ensures(<span style="color: blue">this</span>.FindNoCompression(left)
== <span style="color: blue">this</span>.FindNoCompression(right)); </pre>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The two first requires clauses do the usual null checks. The first ensures clause
checks that if <em>Union</em> returns true, a merge has been done and the number of
sets has decreased by 1. The last ensures checks that left and right belong to the
same set at the end of the union. 
</p>
        <p>
Note that so far, I did not have to provide any kind of implementation details. The
other methods in the implementation receive the same treatment.
</p>
        <p>
          <strong>A Parameterized Unit Test</strong>
        </p>
        <p>
I wrote a single parameterized unit test while writing/debugging the implementation
of the forest. It could probably have been re-factored into many smaller tests, but
for the sake of laziness, I used a single one.
</p>
        <p>
The parameterized unit test implement a common scenario: add elements to the disjoint-set,
then apply a bunch of union operations. Along the way, we can rely on test assertion
and code contracts to check the correctness of our implementation.
</p>
        <p>
Let’s start with the test method signature:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
void </span>Unions(<span style="color: blue">int </span>elementCount, [<span style="color: #2b91af">PexAssumeNotNull</span>]<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">int</span>,<span style="color: blue">int</span>&gt;[]
unions) {</pre>
        </blockquote>
        <p>
The test takes a number of element to add and a sequence of unions to apply. The input
data as is needs to be refined to be useful. In that sense, we add <strong>assumptions</strong>,
under the form of calls to <em>PexAssume</em>, to tell Pex ‘how it should shape’ the
input data. In this case, we want to ensure that <em>elementCount</em> is positive
and relatively small; and that the values in unions are within the <em>[0…elementCount)</em> range.
</p>
        <blockquote>
          <pre class="code">
            <span style="color: #2b91af">
              <font color="#000000">
              </font>PexAssume</span>.IsTrue(0
&lt; elementCount); <span style="color: #2b91af">PexSymbolicValue</span>.Minimize(elementCount); <span style="color: #2b91af">PexAssume</span>.TrueForAll(
unions, u =&gt; 0 &lt;= u.Key &amp;&amp; u.Key &lt; elementCount &amp;&amp; 0 &lt;=
u.Value &amp;&amp; u.Value &lt; elementCount );</pre>
        </blockquote>
        <p>
Now that we have some data, we can start writing the first part of the scenario: filling
the disjoint-set. To do so, we simply add the integers from <em>[0..elementCount)</em>.
Along the way, we check that the <em>Contains,</em><em>ElementCount</em>, <em>SetCount</em> all
behave as expected:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: blue">
              <font color="#000000">
              </font>var </span>target
= <span style="color: blue">new </span><span style="color: #2b91af">ForestDisjointSet</span>&lt;<span style="color: blue">int</span>&gt;(); <span style="color: green">//
fill up with 0..elementCount - 1 </span><span style="color: blue">for </span>(<span style="color: blue">int </span>i
= 0; i &lt; elementCount; i++) { target.MakeSet(i); <span style="color: #2b91af">Assert</span>.IsTrue(target.Contains(i)); <span style="color: #2b91af">Assert</span>.AreEqual(i
+ 1, target.ElementCount); <span style="color: #2b91af">Assert</span>.AreEqual(i +
1, target.SetCount); }</pre>
        </blockquote>
        <p>
The second part gets more interesting. Each element of the unions array is a ‘union’
action between 2 elements:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: green">
              <font color="#000000">
              </font>//
apply Union for pairs unions[i], unions[i+1] </span>
            <span style="color: blue">for </span>(<span style="color: blue">int </span>i
= 0; i &lt; unions.Length; i++) { <span style="color: blue">var </span>left = unions[i].Key; <span style="color: blue">var </span>right=
unions[i].Value; <span style="color: blue">var </span>setCount = target.SetCount; <span style="color: blue">bool </span>unioned
= target.Union(left, right);</pre>
        </blockquote>
        <p>
There is 2 things we want to assert here: first, then left and right now belong to
the same set. Secondly, that the <em>SetCount</em> has been updated correctly: if <em>unioned</em> is
true, it should have decreased by one.
</p>
        <blockquote>
          <p>
        <span style="color: green">// should be
in the same set now<br />
       </span><span style="color: #2b91af">Assert</span>.IsTrue(target.AreInSameSet(left,
right));<br />
        <span style="color: green">// if unioned,
the count decreased by 1<br />
       </span><span style="color: #2b91af">PexAssert</span>.ImpliesIsTrue(unioned,
() =&gt; setCount - 1 == target.SetCount);<br />
    }<br />
}
</p>
        </blockquote>
        <p>
From this parameterized unit test, I could work on the implementation, and refine
again and again until it had all passing tests (I did not write <strong>any</strong> other
test code)<strong>.</strong> 
</p>
        <p>
          <strong>Happy Ending</strong>
        </p>
        <p>
The resulting test suite generated by Pex is summarized in the screenshot below: the
number of elements does not really matter, what is interesting is the sequence of
unions performed. This test suite achieves 100% coverage of the methods under test
:).
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb.png" width="458" height="287" />
          </a>
        </p>
        <p>
In fact, the <em>Union</em> method involved some tricky branches to cover, due to
some optimization occurring in the disjoint-set. Pex managed to generate unit tests
for each of the branches. 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_3.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb_3.png" width="369" height="273" />
          </a>
        </p>
        <p>
        </p>
        <p>
          <strong>Thanks to the contracts, the test assertions, and the high code coverage of
the generated test suite, I have now a good confidence that my code is properly tested. </strong>The
End!
</p>
        <p>
(Next time, we will talk about testing minimum spanning tree implementations…)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=06a6b1c8-6cc3-4728-9791-c129619219eb" />
      </body>
      <title>Writing a DisjointSet in C# with the help of Code Contracts and Pex.</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/12/30/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx</link>
      <pubDate>Tue, 30 Dec 2008 19:41:45 GMT</pubDate>
      <description>&lt;p&gt;
I recently implemented a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure"&gt;Disjoint-Set&lt;/a&gt; data
structure in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;,
which is the main building block of the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm"&gt;Kruskal’s
minimum spanning tree algorithm&lt;/a&gt;. This is a fun data-structure to look at, as it
purpose is quite different from the ‘main’ BCL collections. The disjoint-set is useful
to partition elements into sets, and defines 2 main operation to that purpose: &lt;em&gt;Find&lt;/em&gt;,
finds the set an element belongs to (can be used to check if 2 elements are in the
same set), &lt;em&gt;Union&lt;/em&gt; merges two sets.
&lt;/p&gt;
&lt;p&gt;
The &lt;a target="_blank" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29499#343829"&gt;source
of the disjoint-set&lt;/a&gt; is in the QuickGraph source, if you are curious about the
details.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Testing the disjoint-set&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
There is not much left to TDD when it comes to write data structure. Such data structure
are usually described in details in an article, and you ‘just’ have to follow the
authors instruction to implement. Nevertheless, unit testing is really critical to
ensure that the implementation is correct – but there is a risk of having to re-implement
the algorithm to test it. 
&lt;/p&gt;
&lt;p&gt;
For the disjoint-set, I used 2 tools developed at &lt;a target="_blank" href="http://research.microsoft.com/rise"&gt;RiSE&lt;/a&gt;: &lt;a target="_blank" href="http://research.microsoft.com/contracts"&gt;Code
Contracts&lt;/a&gt; and… &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;.
When implementing data structure from the literature, I usually start ‘dumping’ the
code and the contracts. As much as possible, any invariant or property that the author
describes should be translated to contracts. It will give more opportunities for Pex
to find bugs in my code.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Contracts First&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
For example, the contracts for the &lt;strong&gt;Union&lt;/strong&gt; method look as follows:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private bool &lt;/span&gt;Union(&lt;span style="color: #2b91af"&gt;Element &lt;/span&gt;left, &lt;span style="color: #2b91af"&gt;Element &lt;/span&gt;right)
{ &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(left != &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(right
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;()
? &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(&lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount)
- 1 == &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount : &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(&lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount)
== &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount ); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: blue"&gt;this&lt;/span&gt;.FindNoCompression(left)
== &lt;span style="color: blue"&gt;this&lt;/span&gt;.FindNoCompression(right)); &lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The two first requires clauses do the usual null checks. The first ensures clause
checks that if &lt;em&gt;Union&lt;/em&gt; returns true, a merge has been done and the number of
sets has decreased by 1. The last ensures checks that left and right belong to the
same set at the end of the union. 
&lt;/p&gt;
&lt;p&gt;
Note that so far, I did not have to provide any kind of implementation details. The
other methods in the implementation receive the same treatment.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;A Parameterized Unit Test&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I wrote a single parameterized unit test while writing/debugging the implementation
of the forest. It could probably have been re-factored into many smaller tests, but
for the sake of laziness, I used a single one.
&lt;/p&gt;
&lt;p&gt;
The parameterized unit test implement a common scenario: add elements to the disjoint-set,
then apply a bunch of union operations. Along the way, we can rely on test assertion
and code contracts to check the correctness of our implementation.
&lt;/p&gt;
&lt;p&gt;
Let’s start with the test method signature:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Unions(&lt;span style="color: blue"&gt;int &lt;/span&gt;elementCount, [&lt;span style="color: #2b91af"&gt;PexAssumeNotNull&lt;/span&gt;]&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;[]
unions) {&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
The test takes a number of element to add and a sequence of unions to apply. The input
data as is needs to be refined to be useful. In that sense, we add &lt;strong&gt;assumptions&lt;/strong&gt;,
under the form of calls to &lt;em&gt;PexAssume&lt;/em&gt;, to tell Pex ‘how it should shape’ the
input data. In this case, we want to ensure that &lt;em&gt;elementCount&lt;/em&gt; is positive
and relatively small; and that the values in unions are within the &lt;em&gt;[0…elementCount)&lt;/em&gt; range.
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;PexAssume&lt;/span&gt;.IsTrue(0
&amp;lt; elementCount); &lt;span style="color: #2b91af"&gt;PexSymbolicValue&lt;/span&gt;.Minimize(elementCount); &lt;span style="color: #2b91af"&gt;PexAssume&lt;/span&gt;.TrueForAll(
unions, u =&amp;gt; 0 &amp;lt;= u.Key &amp;amp;&amp;amp; u.Key &amp;lt; elementCount &amp;amp;&amp;amp; 0 &amp;lt;=
u.Value &amp;amp;&amp;amp; u.Value &amp;lt; elementCount );&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Now that we have some data, we can start writing the first part of the scenario: filling
the disjoint-set. To do so, we simply add the integers from &lt;em&gt;[0..elementCount)&lt;/em&gt;.
Along the way, we check that the &lt;em&gt;Contains,&lt;/em&gt; &lt;em&gt;ElementCount&lt;/em&gt;, &lt;em&gt;SetCount&lt;/em&gt; all
behave as expected:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;var &lt;/span&gt;target
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ForestDisjointSet&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(); &lt;span style="color: green"&gt;//
fill up with 0..elementCount - 1 &lt;/span&gt;&lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i
= 0; i &amp;lt; elementCount; i++) { target.MakeSet(i); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(target.Contains(i)); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(i
+ 1, target.ElementCount); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(i +
1, target.SetCount); }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
The second part gets more interesting. Each element of the unions array is a ‘union’
action between 2 elements:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;//
apply Union for pairs unions[i], unions[i+1] &lt;/span&gt;&lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i
= 0; i &amp;lt; unions.Length; i++) { &lt;span style="color: blue"&gt;var &lt;/span&gt;left = unions[i].Key; &lt;span style="color: blue"&gt;var &lt;/span&gt;right=
unions[i].Value; &lt;span style="color: blue"&gt;var &lt;/span&gt;setCount = target.SetCount; &lt;span style="color: blue"&gt;bool &lt;/span&gt;unioned
= target.Union(left, right);&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
There is 2 things we want to assert here: first, then left and right now belong to
the same set. Secondly, that the &lt;em&gt;SetCount&lt;/em&gt; has been updated correctly: if &lt;em&gt;unioned&lt;/em&gt; is
true, it should have decreased by one.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// should be
in the same set now&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(target.AreInSameSet(left,
right));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// if unioned,
the count decreased by 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PexAssert&lt;/span&gt;.ImpliesIsTrue(unioned,
() =&amp;gt; setCount - 1 == target.SetCount);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
From this parameterized unit test, I could work on the implementation, and refine
again and again until it had all passing tests (I did not write &lt;strong&gt;any&lt;/strong&gt; other
test code)&lt;strong&gt;.&lt;/strong&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Happy Ending&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The resulting test suite generated by Pex is summarized in the screenshot below: the
number of elements does not really matter, what is interesting is the sequence of
unions performed. This test suite achieves 100% coverage of the methods under test
:).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb.png" width="458" height="287"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In fact, the &lt;em&gt;Union&lt;/em&gt; method involved some tricky branches to cover, due to
some optimization occurring in the disjoint-set. Pex managed to generate unit tests
for each of the branches. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_3.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb_3.png" width="369" height="273"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Thanks to the contracts, the test assertions, and the high code coverage of
the generated test suite, I have now a good confidence that my code is properly tested. &lt;/strong&gt;The
End!
&lt;/p&gt;
&lt;p&gt;
(Next time, we will talk about testing minimum spanning tree implementations…)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=06a6b1c8-6cc3-4728-9791-c129619219eb" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</comments>
      <category>Pex</category>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=23babb8f-9c26-41d1-ae6c-b8349e214fcb</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,23babb8f-9c26-41d1-ae6c-b8349e214fcb.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,23babb8f-9c26-41d1-ae6c-b8349e214fcb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=23babb8f-9c26-41d1-ae6c-b8349e214fcb</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just <a target="_blank" href="http://www.codeplex.com/quickgraph/Release/ProjectReleases.aspx?ReleaseId=20160">released
a new version</a> of <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a>.
Among many small improvements here and there, the big news are:
</p>
        <ul>
          <li>
.net 2.0 support is back! QuickGraph now builds both for .net 2.0 and .net 3.5 (with
extension methods).</li>
          <li>
The Dijkstra algorithm use a Fibonacci Heap under the hood, which is way more efficient.
Courtesy of Roy Williams!</li>
          <li>
better serialization support and other bug fixes.</li>
        </ul>
        <p>
Enjoy, Peli.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=23babb8f-9c26-41d1-ae6c-b8349e214fcb" />
      </body>
      <title>QuickGraph 3.1 &amp;ndash; .net 2.0 support &amp;ndash; Fibonacci Heap</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,23babb8f-9c26-41d1-ae6c-b8349e214fcb.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/12/04/QuickGraph31NdashNet20SupportNdashFibonacciHeap.aspx</link>
      <pubDate>Thu, 04 Dec 2008 08:28:30 GMT</pubDate>
      <description>&lt;p&gt;
I just &lt;a target="_blank" href="http://www.codeplex.com/quickgraph/Release/ProjectReleases.aspx?ReleaseId=20160"&gt;released
a new version&lt;/a&gt; of &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;.
Among many small improvements here and there, the big news are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
.net 2.0 support is back! QuickGraph now builds both for .net 2.0 and .net 3.5 (with
extension methods).&lt;/li&gt;
&lt;li&gt;
The Dijkstra algorithm use a Fibonacci Heap under the hood, which is way more efficient.
Courtesy of Roy Williams!&lt;/li&gt;
&lt;li&gt;
better serialization support and other bug fixes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Enjoy, Peli.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=23babb8f-9c26-41d1-ae6c-b8349e214fcb" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,23babb8f-9c26-41d1-ae6c-b8349e214fcb.aspx</comments>
      <category>QuickGraph</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=3a53ab1f-b7f3-4582-9e55-427062cfe0c1</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,3a53ab1f-b7f3-4582-9e55-427062cfe0c1.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,3a53ab1f-b7f3-4582-9e55-427062cfe0c1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=3a53ab1f-b7f3-4582-9e55-427062cfe0c1</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve upgraded <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a> to
C# 3.0, and it’s extension methods and lambdas <strong>(requires .net 3.5)</strong>.
The result is fairly nice, specially the extension methods who make algorithms more
discoverable.
</p>
        <p>
For example, the following snippet shows how to get the <a target="_blank" href="http://en.wikipedia.org/wiki/Topological_sorting">topological
sort</a> of tables out of a DataSet (useful when you need to fill/delete tables):
</p>
        <blockquote>
          <p>
DateSet ds = …; // get some DataSet<br />
// ToGraph() is an extension method on DataSet, which builds 
<br />
// the Table/Relation graph from the data set schema<br />
var g = ds.<strong>ToGraph(); </strong><br />
// TopologicalSort() is an extension method on IVertexAndEdgeListGraph&lt;T,E&gt;<br />
// which returns the graph topological sort 
<br />
var tables = g.<strong>TopologicalSort();</strong><br />
foreach(DataTable table in tables)<br />
    Console.WriteLine(table.TableName);
</p>
        </blockquote>
        <p>
More changes were made on the API to take advantage of delegates (i.e. since it’s
easier to write them): no more predicate interfaces, dropping dictionaries for delegates
in shortest path algorithm etc…
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3a53ab1f-b7f3-4582-9e55-427062cfe0c1" />
      </body>
      <title>QuickGraph 3.0</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,3a53ab1f-b7f3-4582-9e55-427062cfe0c1.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/09/21/QuickGraph30.aspx</link>
      <pubDate>Sun, 21 Sep 2008 08:00:51 GMT</pubDate>
      <description>&lt;p&gt;
I’ve upgraded &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt; to
C# 3.0, and it’s extension methods and lambdas &lt;strong&gt;(requires .net 3.5)&lt;/strong&gt;.
The result is fairly nice, specially the extension methods who make algorithms more
discoverable.
&lt;/p&gt;
&lt;p&gt;
For example, the following snippet shows how to get the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Topological_sorting"&gt;topological
sort&lt;/a&gt; of tables out of a DataSet (useful when you need to fill/delete tables):
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
DateSet ds = …; // get some DataSet&lt;br&gt;
// ToGraph() is an extension method on DataSet, which builds 
&lt;br&gt;
// the Table/Relation graph from the data set schema&lt;br&gt;
var g = ds.&lt;strong&gt;ToGraph(); &lt;/strong&gt;
&lt;br&gt;
// TopologicalSort() is an extension method on IVertexAndEdgeListGraph&amp;lt;T,E&amp;gt;&lt;br&gt;
// which returns the graph topological sort 
&lt;br&gt;
var tables = g.&lt;strong&gt;TopologicalSort();&lt;/strong&gt; 
&lt;br&gt;
foreach(DataTable table in tables)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(table.TableName);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
More changes were made on the API to take advantage of delegates (i.e. since it’s
easier to write them): no more predicate interfaces, dropping dictionaries for delegates
in shortest path algorithm etc…
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3a53ab1f-b7f3-4582-9e55-427062cfe0c1" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,3a53ab1f-b7f3-4582-9e55-427062cfe0c1.aspx</comments>
      <category>QuickGraph</category>
    </item>
  </channel>
</rss>