<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Puzzler: Find four-connected component to element 1 of 2-d matrix</title>
	<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/</link>
	<description>Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.</description>
	<pubDate>Mon, 23 Nov 2009 01:39:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Dmitry</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1487</link>
		<dc:creator>Dmitry</dc:creator>
		<pubDate>Mon, 22 Jun 2009 12:57:33 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1487</guid>
		<description>Yi, Matt, thanks for your comment. Tim, I like your method very much. It is beautifull.</description>
		<content:encoded><![CDATA[<p>Yi, Matt, thanks for your comment. Tim, I like your method very much. It is beautifull.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt fig</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1486</link>
		<dc:creator>matt fig</dc:creator>
		<pubDate>Sun, 21 Jun 2009 21:59:13 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1486</guid>
		<description>Tim,

I always forget how to add code in blocks correctly, and the above didn't work for me either.  But if you look at the instructions here:

http://blogs.mathworks.com/videos/2008/08/18/puzzler-find-largest-connected-island/

Those seem to work (See post number 17 in the current blog comments.)</description>
		<content:encoded><![CDATA[<p>Tim,</p>
<p>I always forget how to add code in blocks correctly, and the above didn&#8217;t work for me either.  But if you look at the instructions here:</p>
<p><a href="http://blogs.mathworks.com/videos/2008/08/18/puzzler-find-largest-connected-island/" rel="nofollow">http://blogs.mathworks.com/videos/2008/08/18/puzzler-find-largest-connected-island/</a></p>
<p>Those seem to work (See post number 17 in the current blog comments.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1485</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Sun, 21 Jun 2009 15:04:28 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1485</guid>
		<description>Argh.  Posting code in a comment is nasty.  The "&#62;" didn't come through correctly.  There needs to be a better method at posting code snippets in a comment in a blog.  You can see the solution here:

http://www.cise.ufl.edu/~davis/puzzle.c</description>
		<content:encoded><![CDATA[<p>Argh.  Posting code in a comment is nasty.  The &#8220;&gt;&#8221; didn&#8217;t come through correctly.  There needs to be a better method at posting code snippets in a comment in a blog.  You can see the solution here:</p>
<p><a href="http://www.cise.ufl.edu/~davis/puzzle.c" rel="nofollow">http://www.cise.ufl.edu/~davis/puzzle.c</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1484</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Sun, 21 Jun 2009 15:00:26 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1484</guid>
		<description>What you will find as the board size grows, is that searching the entire board at each iteration takes far too much time.  The asymptotic complexity is way to high.

Here's my mexFunction solution.  Note that the result is returned as a row vector (as Doug requested).  It is not sorted (which was not in Doug's specifications).

&lt;pre&gt;
#include "mex.h"

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    double *board, color, *result ;
    char *visited ;
    mwSignedIndex *queue, m, n, mn, i, k, head = 0, tail = 0, east, west,
        north, south, col ;

    if (nargin != 1 &#124;&#124; nargout &#62; 1)
    {
        mexErrMsgTxt ("Usage: s = puzzle(board)\n") ;
    }


    board = mxGetPr (pargin [0]) ;
    m = mxGetM (pargin [0]) ;
    n = mxGetN (pargin [0]) ;
    mn = m*n ;
    if (n == 0 &#124;&#124; m == 0)
    {
        mexErrMsgTxt ("board cannot be empty") ;
    }

    visited = mxCalloc (m*n, sizeof (char)) ;
    queue = mxMalloc (m*n * sizeof (mwSignedIndex)) ;

    /* place the corner element in the breadth-first-search queue */
    head = 0 ;
    tail = 0 ;
    queue [tail++] = 0 ;
    visited [0] = 1 ;
    color = board [0] ;

    while (head = 0 &#38;&#38; !visited [east] &#38;&#38; board [east] == color)
        {
            queue [tail++] = east ;
            visited [east] = 1 ;
        }

        /* West neighbor */
        west = i+m ;
        if (west = 0 &#38;&#38; north/m == col &#38;&#38;
            !visited [north] &#38;&#38; board [north] == color)
        {
            queue [tail++] = north ;
            visited [north] = 1 ;
        }

        /* South neighbor */
        south = i+1 ;
        if (south/m == col &#38;&#38; !visited [south] &#38;&#38; board [south] == color)
        {
            queue [tail++] = south ;
            visited [south] = 1 ;
        }
    }

    /* return the result */
    pargout [0] = mxCreateDoubleMatrix (1, tail, mxREAL) ;
    result = mxGetPr (pargout [0]) ;
    for (k = 0 ; k &#60; tail ; k++)
    {
        result [k] = queue [k] + 1 ;
    }

    mxFree (visited) ;
    mxFree (queue) ;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>What you will find as the board size grows, is that searching the entire board at each iteration takes far too much time.  The asymptotic complexity is way to high.</p>
<p>Here&#8217;s my mexFunction solution.  Note that the result is returned as a row vector (as Doug requested).  It is not sorted (which was not in Doug&#8217;s specifications).</p>
<pre>
#include "mex.h"

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{
    double *board, color, *result ;
    char *visited ;
    mwSignedIndex *queue, m, n, mn, i, k, head = 0, tail = 0, east, west,
        north, south, col ;

    if (nargin != 1 || nargout &gt; 1)
    {
        mexErrMsgTxt ("Usage: s = puzzle(board)\n") ;
    }

    board = mxGetPr (pargin [0]) ;
    m = mxGetM (pargin [0]) ;
    n = mxGetN (pargin [0]) ;
    mn = m*n ;
    if (n == 0 || m == 0)
    {
        mexErrMsgTxt ("board cannot be empty") ;
    }

    visited = mxCalloc (m*n, sizeof (char)) ;
    queue = mxMalloc (m*n * sizeof (mwSignedIndex)) ;

    /* place the corner element in the breadth-first-search queue */
    head = 0 ;
    tail = 0 ;
    queue [tail++] = 0 ;
    visited [0] = 1 ;
    color = board [0] ;

    while (head = 0 &amp;&amp; !visited [east] &amp;&amp; board [east] == color)
        {
            queue [tail++] = east ;
            visited [east] = 1 ;
        }

        /* West neighbor */
        west = i+m ;
        if (west = 0 &amp;&amp; north/m == col &amp;&amp;
            !visited [north] &amp;&amp; board [north] == color)
        {
            queue [tail++] = north ;
            visited [north] = 1 ;
        }

        /* South neighbor */
        south = i+1 ;
        if (south/m == col &amp;&amp; !visited [south] &amp;&amp; board [south] == color)
        {
            queue [tail++] = south ;
            visited [south] = 1 ;
        }
    }

    /* return the result */
    pargout [0] = mxCreateDoubleMatrix (1, tail, mxREAL) ;
    result = mxGetPr (pargout [0]) ;
    for (k = 0 ; k &lt; tail ; k++)
    {
        result [k] = queue [k] + 1 ;
    }

    mxFree (visited) ;
    mxFree (queue) ;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Davis</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1483</link>
		<dc:creator>Tim Davis</dc:creator>
		<pubDate>Sun, 21 Jun 2009 14:52:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1483</guid>
		<description>The MATLAB code beats a mexFunction because the JIT handles just pure MATLAB, and because the matrix is so small.  With a dummy mexFunction that does nothing at all (just returning immediately), the total time is about the same as a mexFunction that computes the complete result.

So with a larger matrix, the loop overhead in M (which is not JIT-ed when using a mexFunction) becomes neglible.

With a 100-by-100 board, Yi's solution takes 22 seconds whereas a simple mexFunction implementation takes 14 seconds.  Of that total time, the rand statement takes all the time.  The mexFunction itself takes just 0.16 seconds.


So you really have to be careful when looking at performance comparisons.  I suggest you try a larger board, and time just the puzzle solution, not the time to construct the board.</description>
		<content:encoded><![CDATA[<p>The MATLAB code beats a mexFunction because the JIT handles just pure MATLAB, and because the matrix is so small.  With a dummy mexFunction that does nothing at all (just returning immediately), the total time is about the same as a mexFunction that computes the complete result.</p>
<p>So with a larger matrix, the loop overhead in M (which is not JIT-ed when using a mexFunction) becomes neglible.</p>
<p>With a 100-by-100 board, Yi&#8217;s solution takes 22 seconds whereas a simple mexFunction implementation takes 14 seconds.  Of that total time, the rand statement takes all the time.  The mexFunction itself takes just 0.16 seconds.</p>
<p>So you really have to be careful when looking at performance comparisons.  I suggest you try a larger board, and time just the puzzle solution, not the time to construct the board.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt fig</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1482</link>
		<dc:creator>matt fig</dc:creator>
		<pubDate>Fri, 19 Jun 2009 22:47:26 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1482</guid>
		<description>Dmitry,

JIT is also different for different versions of MATLAB, which is probably one reason it is not well documented.  TMW doesn't want you writing code specifically for the JIT which is subject to change.

For example, I get completely different results for the relative timings of the solutions to this current puzzle than does Doug.  I suspect he is using the latest version of MATLAB, while I am using 2007a.</description>
		<content:encoded><![CDATA[<p>Dmitry,</p>
<p>JIT is also different for different versions of MATLAB, which is probably one reason it is not well documented.  TMW doesn&#8217;t want you writing code specifically for the JIT which is subject to change.</p>
<p>For example, I get completely different results for the relative timings of the solutions to this current puzzle than does Doug.  I suspect he is using the latest version of MATLAB, while I am using 2007a.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1481</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 19 Jun 2009 22:38:43 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1481</guid>
		<description>Dmitry

JIT is one of things which are not well documented. You may search support website to get something to read.

Yi</description>
		<content:encoded><![CDATA[<p>Dmitry</p>
<p>JIT is one of things which are not well documented. You may search support website to get something to read.</p>
<p>Yi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1480</link>
		<dc:creator>Dmitry</dc:creator>
		<pubDate>Fri, 19 Jun 2009 19:28:59 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1480</guid>
		<description>Yi, I didn't know that matlab has a JIT compilation. Is that true? Are there some guidelines available to better make use of it?</description>
		<content:encoded><![CDATA[<p>Yi, I didn&#8217;t know that matlab has a JIT compilation. Is that true? Are there some guidelines available to better make use of it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dhull</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1479</link>
		<dc:creator>dhull</dc:creator>
		<pubDate>Fri, 19 Jun 2009 17:40:39 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1479</guid>
		<description>Wow, a guy steps away for lunch and finds a dozen new comments.  I think I am testing the latest and greatest code here:

Doug
Elapsed time is 2.849295 seconds.

Kenneth Eaton
Elapsed time is 1.535686 seconds.

Matt Fig
Elapsed time is 0.191490 seconds.

Sven final
Elapsed time is 0.506205 seconds.

Dimitry
Elapsed time is 0.492957 seconds.

Jos
Elapsed time is 0.272301 seconds.

Dimitry 2
Elapsed time is 0.187197 seconds.

Yi
Elapsed time is 0.305517 seconds.

Dimitri final
Elapsed time is 0.125054 seconds.

Yi final
Elapsed time is 0.147474 seconds.

Though, at this point the code seems to be "community" owned.  This is impressive.  I was just telling someone in the office that no matter how efficient you think your code is, there is someone out there that can make it a little faster!

-Doug</description>
		<content:encoded><![CDATA[<p>Wow, a guy steps away for lunch and finds a dozen new comments.  I think I am testing the latest and greatest code here:</p>
<p>Doug<br />
Elapsed time is 2.849295 seconds.</p>
<p>Kenneth Eaton<br />
Elapsed time is 1.535686 seconds.</p>
<p>Matt Fig<br />
Elapsed time is 0.191490 seconds.</p>
<p>Sven final<br />
Elapsed time is 0.506205 seconds.</p>
<p>Dimitry<br />
Elapsed time is 0.492957 seconds.</p>
<p>Jos<br />
Elapsed time is 0.272301 seconds.</p>
<p>Dimitry 2<br />
Elapsed time is 0.187197 seconds.</p>
<p>Yi<br />
Elapsed time is 0.305517 seconds.</p>
<p>Dimitri final<br />
Elapsed time is 0.125054 seconds.</p>
<p>Yi final<br />
Elapsed time is 0.147474 seconds.</p>
<p>Though, at this point the code seems to be &#8220;community&#8221; owned.  This is impressive.  I was just telling someone in the office that no matter how efficient you think your code is, there is someone out there that can make it a little faster!</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yi Cao</title>
		<link>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1478</link>
		<dc:creator>Yi Cao</dc:creator>
		<pubDate>Fri, 19 Jun 2009 16:52:33 +0000</pubDate>
		<guid>http://blogs.mathworks.com/videos/2009/06/17/puzzler-find-four-connected-component-to-element-1-of-2-d-matrix/#comment-1478</guid>
		<description>Dmitry,

I think the reason for your code to be successful is due to the JIT acceleration. To achieve this, the code need to be as simple as possible. Any vectorization to get rid of the inner loop may deteriorate the performance.

Yi</description>
		<content:encoded><![CDATA[<p>Dmitry,</p>
<p>I think the reason for your code to be successful is due to the JIT acceleration. To achieve this, the code need to be as simple as possible. Any vectorization to get rid of the inner loop may deteriorate the performance.</p>
<p>Yi</p>
]]></content:encoded>
	</item>
</channel>
</rss>
