Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspxI said last time that I was interested in finding colourings for graphs that have lots of fully connected subgraphs, aka "cliques". For instance, I'd like to find a four-colouring for this sixteen-node graph:
Yuck. What a mess.
What this graphen-USTelligent Evolution Platform Developer Build (Build: 5.6.50428.7875)re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10159958Mon, 02 May 2011 09:08:36 GMT91d46819-8472-40ad-a661-2c78acb4018c:10159958AlexOld<p>The Status property fails (returns Solved) in degenerate case when required number of colours = 1. Some check must be added. I think Eric would do it in the most elegant way.</p>
<p>The whole job is just marvellous (breathtaking). Thank you very much, Eric.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10159958" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10071358Mon, 04 Oct 2010 20:15:18 GMT91d46819-8472-40ad-a661-2c78acb4018c:10071358EugenUS<p>@Eris - awesome post.</p>
<p>@All - anybody knows other blogs where somebody would explain other algorithms from real world with a step by step implementation. I hate those blogs with theory only. &nbsp;:) Eric rules with his posts.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10071358" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10059741Thu, 09 Sep 2010 11:49:45 GMT91d46819-8472-40ad-a661-2c78acb4018c:10059741Visitor<p>@Greg - if you claim that graph coloring is solvable by doing transitive closure with matrices please present the solution or leave a reference. Or even just for Sudoku. Not kidding - it&#39;s considered to be NP-complete and transitive closure is computable in N^3, so just in case that there might be a golden shortcut in some corner of the internet - one never knows :-)</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10059741" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10051225Tue, 17 Aug 2010 23:43:34 GMT91d46819-8472-40ad-a661-2c78acb4018c:10051225Greg<p>There is an optimized way of doing this by using a square matrix to represent transitions between nodes and then using matrix multiplication to find the transitive closure of the matrix. &nbsp;The memory footprint, upper bound on the number of multiplications and performance information is known ahead of time for this implementation.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10051225" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10046595Thu, 05 Aug 2010 15:33:42 GMT91d46819-8472-40ad-a661-2c78acb4018c:10046595Chris<p>Now what;s the chances for a solver for next weeks lotto?</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10046595" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10045928Wed, 04 Aug 2010 14:27:01 GMT91d46819-8472-40ad-a661-2c78acb4018c:10045928Micah<p>@configurator: Toget an exception for a non-unique solution you would use:</p>
<p> &nbsp; return solution.Single();</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10045928" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10045356Tue, 03 Aug 2010 13:01:43 GMT91d46819-8472-40ad-a661-2c78acb4018c:10045356tobi<p>rofl what a nice surprise. a sudoku solver is a nine-colorer of some specific graph.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10045356" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10044655Sun, 01 Aug 2010 13:35:34 GMT91d46819-8472-40ad-a661-2c78acb4018c:10044655configurator<p>@Brian: Or change the line in Solve():</p>
<p>from</p>
<p> &nbsp; &nbsp;return solutions.FirstOrDefault();</p>
<p>to</p>
<p> &nbsp; &nbsp;return solutions.First();</p>
<p>if you want an exception for non-unique solutions</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10044655" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10044021Thu, 29 Jul 2010 21:17:54 GMT91d46819-8472-40ad-a661-2c78acb4018c:10044021Brian<p>@oldnewthing: That&#39;s boring. &nbsp;All it takes to do that is to mark the first solution found as invalid, thus forcing the backtracker to search for an alternative solution.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10044021" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10043961Thu, 29 Jul 2010 19:36:55 GMT91d46819-8472-40ad-a661-2c78acb4018c:10043961Raymond Chen - MSFT<p>Exercise: Extend this program so it also reports whether the solution is unique, a detail often lost in Sudoku puzzle creation.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10043961" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10043887Thu, 29 Jul 2010 16:46:20 GMT91d46819-8472-40ad-a661-2c78acb4018c:10043887Brian<p>Funny, I was thinking in the opposite direction of Samual Jack, and wondering if a Sudoku solver could be used to achieve graph coloring. &nbsp;More precisely, I was thinking about was Donald Knuth&#39;s &quot;Dancing Links&quot; (AKA DLX) algorithm, which is a recursive backtracking algorithm which can solve instances of the exact cover problem (e.g. Sudoku).</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10043887" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10043846Thu, 29 Jul 2010 15:14:45 GMT91d46819-8472-40ad-a661-2c78acb4018c:10043846ShuggyCoUk<p>I await your next NP-Complete segue with interest :)</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10043846" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10043824Thu, 29 Jul 2010 14:32:34 GMT91d46819-8472-40ad-a661-2c78acb4018c:10043824Samuel Jack<p>Nice work Eric - though you pre-empted my idea for a blog post! </p>
<p>I sensed this conclusion coming a couple of days ago when I was mulling over in my own mind how to write a Sudoku solver and realised there was a connection was colouring graphs.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10043824" width="1" height="1">re: Graph Colouring, Part Fivehttp://blogs.msdn.com/b/ericlippert/archive/2010/07/29/graph-colouring-part-five.aspx#10043822Thu, 29 Jul 2010 14:25:07 GMT91d46819-8472-40ad-a661-2c78acb4018c:10043822configurator<p>Very sneaky, Eric.</p>
<p>Honestly though, whenever you talk about the power of linq I remember a book from 1984 - Structure and Interpretation of Computer Programs. When it speaks of Streams, it&#39;s really talking about linq. If you want a couple of video lectures from &#39;86 check out <a rel="nofollow" target="_new" href="http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/">ocw.mit.edu/.../video-lectures</a> - sessions 6a and 6b implement, use and discuss something very close to LINQ in Scheme.</p>
<p>It makes me happy that these programming techniques I used when I was a kid are now available in a mainstream language... But it&#39;s sad that it took over 20 years.</p>
<p>Have fun in your vacation!</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10043822" width="1" height="1">