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

<channel>
	<title>מחשבות, מחשבים, ושאר דברי בלע &#187; אלגוריתמים</title>
	<atom:link href="http://firefang.net/blog/category/computers/%d7%90%d7%9c%d7%92%d7%95%d7%a8%d7%99%d7%aa%d7%9e%d7%99%d7%9d/feed" rel="self" type="application/rss+xml" />
	<link>http://firefang.net/blog</link>
	<description>May your sockets never timeout</description>
	<lastBuildDate>Fri, 30 Jul 2010 15:14:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>יש דברים נשברים</title>
		<link>http://firefang.net/blog/1680</link>
		<comments>http://firefang.net/blog/1680#comments</comments>
		<pubDate>Sat, 17 Jul 2010 10:43:11 +0000</pubDate>
		<dc:creator>עמרי</dc:creator>
				<category><![CDATA[אלגוריתמים]]></category>
		<category><![CDATA[מחשבים]]></category>

		<guid isPermaLink="false">http://firefang.net/blog/?p=1680</guid>
		<description><![CDATA[חוקרים מאוניברסיטת קורנל פיתחו אלגוריתמים לסינתוז קול של &#8230; דברים מתנפצים.
האלגוריתמים מבוססים על התכונות הפיסיקליות של האובייקטים הנשברים ושל (אני מניח) הסביבה שלהם.
בדיוק כמו שאפשר לרנדר תמונות תלת מימדיות על בסיס מודל תלת מימדי, אפשר לסנתז גם צלילים שמתאימים לפיזיקה של מה שקורה במודל.
למעשה שאלתי את עצמו כבר לפני שנים מתי נראה סינתוז פיזיקלי מלא [...]]]></description>
			<content:encoded><![CDATA[<p>חוקרים מאוניברסיטת קורנל <a href="http://www.cs.cornell.edu/projects/FractureSound/">פיתחו</a> אלגוריתמים לסינתוז קול של &#8230; דברים מתנפצים.<br />
האלגוריתמים מבוססים על התכונות הפיסיקליות של האובייקטים הנשברים ושל (אני מניח) הסביבה שלהם.<br />
בדיוק כמו שאפשר לרנדר תמונות תלת מימדיות על בסיס מודל תלת מימדי, אפשר לסנתז גם צלילים שמתאימים לפיזיקה של מה שקורה במודל.<br />
למעשה שאלתי את עצמו כבר לפני שנים מתי נראה סינתוז פיזיקלי מלא של צלילים במשחקים (ובסימולציות אחרות) במקום שימוש בצלילים מוקלטים. האלגוריתם הזה הוא לא הפתרון, אבל הוא בהחלט יכול להיות חלק מהפתרון.<br />
כמה מגניב זה יכול להיות? תראו בעצמכם:<br />
<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/nHH8N_lNZzI&amp;hl=en_US&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/nHH8N_lNZzI&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://firefang.net/blog/1680/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>הקלות הבלתי נסבלת של הפרת הפטנט</title>
		<link>http://firefang.net/blog/1671</link>
		<comments>http://firefang.net/blog/1671#comments</comments>
		<pubDate>Fri, 09 Jul 2010 16:27:09 +0000</pubDate>
		<dc:creator>עמרי</dc:creator>
				<category><![CDATA[אלגוריתמים]]></category>
		<category><![CDATA[קוד פתוח]]></category>

		<guid isPermaLink="false">http://firefang.net/blog/?p=1671</guid>
		<description><![CDATA[מפתח הולנדי חובב פיתח בזמנו הפנוי במהלך סוף שבוע תוכנה דמוית שזאם למציאת מוסיקה שמבוססת על ניתוח תדר של הקול, יצירת חתימה פשוטה של חלונות בזמן וחיפוש של התאמות.
הוא פירסם את זה בבלוג שלו, וזכה לאיומים מרומזים מהחברה שפיתחה את שזאם בטענות שהוא מפר את הפטנט שלהם ובדרישה שלא יפרסם קוד ואפילו &#8211; תחזיקו &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>מפתח הולנדי חובב פיתח בזמנו הפנוי במהלך סוף שבוע תוכנה דמוית <a href="http://www.shazam.com/">שזאם</a> למציאת מוסיקה שמבוססת על ניתוח תדר של הקול, יצירת חתימה פשוטה של חלונות בזמן וחיפוש של התאמות.<br />
הוא <a href="http://sites.google.com/site/redcodenl/creating-shazam-in-java-1">פירסם</a> את זה בבלוג שלו, <a href="http://sites.google.com/site/redcodenl/patent-infringement">וזכה</a> לאיומים מרומזים מהחברה שפיתחה את שזאם בטענות שהוא מפר את הפטנט שלהם ובדרישה שלא יפרסם קוד ואפילו &#8211; תחזיקו &#8211; ימחק את הפוסט כי הוא &#8220;זמין למשתמשים בין לאומיים ועלול לשמש להפרת הפטנט&#8221;.<br />
ספציפית, אני מניח שהם מתכוונים שהוא זמין לאמריקאים (יש להם פטנט בארצות הברית).</p>
<p>הבנתם?<br />
הבחור פיתח אב טיפוס בסוף שבוע, והבהיל את החברה כל כך עד שהם החליטו להפחיד אותו ולמנוע ממנו לפרסם את הקוד.</p>
<p>דעתכם?</p>
]]></content:encoded>
			<wfw:commentRss>http://firefang.net/blog/1671/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>חלון מהיר</title>
		<link>http://firefang.net/blog/1574</link>
		<comments>http://firefang.net/blog/1574#comments</comments>
		<pubDate>Fri, 19 Feb 2010 22:44:07 +0000</pubDate>
		<dc:creator>עמרי</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[אלגוריתמים]]></category>
		<category><![CDATA[קוד פתוח]]></category>

		<guid isPermaLink="false">http://firefang.net/blog/?p=1574</guid>
		<description><![CDATA[לפני כמה פוסטים שאלתי את השאלה הבאה:
נתון מערך בגודל NxN של פיקסלים כאשר כל הפיקסלים שחורים חוץ מאחוז מסויים מהם שאינו שחור ומפוזר באופן אקראי,
בהינתן קואורדינטה (x,y) מסויימת בתמונה, מצא במהירות את כל הנקודות שנמצאות בתוך חלון בגודל K על K (כאשר K קבוע בבעיה) שמרכזו (x,y), משהו כזה:

פעולת המציאה עצמה צריכה להיות מהירה מאוד, [...]]]></description>
			<content:encoded><![CDATA[<p>לפני כמה פוסטים <a href="http://firefang.net/blog/1550">שאלתי</a> את השאלה הבאה:</p>
<p>נתון מערך בגודל NxN של פיקסלים כאשר כל הפיקסלים שחורים חוץ מאחוז מסויים מהם שאינו שחור ומפוזר באופן אקראי,<br />
בהינתן קואורדינטה (x,y) מסויימת בתמונה, מצא במהירות את כל הנקודות שנמצאות בתוך חלון בגודל K על K (כאשר K קבוע בבעיה) שמרכזו (x,y), משהו כזה:<br />
<a href="http://firefang.net/blog/wp-content/uploads/2010/01/dots2.png"><img class="alignnone" src="http://firefang.net/blog/wp-content/uploads/2010/01/dots2.png" alt="" width="198" height="199" /></a></p>
<p>פעולת המציאה עצמה צריכה להיות מהירה מאוד, אבל מותר לעבד מראש את התמונה פעם אחת (בזמן סביר).</p>
<p>אז הנה הפתרון שלי:<br />
נחלק את התמונה למשבצות בגודל KxK, ונשמור רשימה של כל הנקודות בכל משבצת.<br />
כאשר מבקשים מאיתנו לתת את רשימת הנקודות בחלון מסויים, נסתכל בארבעת החלונות שהחלון הזה חותך ונסנן את כל הנקודות בארבעת החלונות שלא נמצאות בתוך החלון המבוקש.<br />
זה הכל בעצם.<br />
הביצועים של הפתרון הזה מעולים, כדי להכין את הגריד (Grid) נעבור על כל הפיקסלים בתמונה, ולכל נקודה נמצא בזמן קבוע את המשבצת שאליה היא שייכת.<br />
כשממש מחפשים את הנקודות בחלון מסויים, נבדוק במקרה הגרוע 2K*2K פיקסלים (אם כל הנקודות לבנות) אבל במקרה הרגיל נבדוק הרבה פחות (ואם החלון לא מכיל נקודות כלל נדע את זה מהר מאוד).<br />
זה הקוד של העיבוד המקדים:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">private</span> <span class="kw4">static</span> <span class="kw4">int</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> buildgrid<span class="br0">&#40;</span><span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> image, <span class="kw4">int</span> width, <span class="kw4">int</span> height, <span class="kw4">int</span> gridSize<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> grid<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> <span class="kw4">int</span><span class="br0">&#91;</span>width / gridSize<span class="br0">&#93;</span><span class="br0">&#91;</span>height / gridSize<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="co1">// count the number of points in each grid cell</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> numpoints<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="kw2">new</span> <span class="kw4">int</span><span class="br0">&#91;</span>width / gridSize<span class="br0">&#93;</span><span class="br0">&#91;</span>height / gridSize<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> x=<span class="nu0">0</span>;x&lt;width;x++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> y=<span class="nu0">0</span>;y&lt;height;y++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>image<span class="br0">&#91;</span>x<span class="br0">&#93;</span><span class="br0">&#91;</span>y<span class="br0">&#93;</span> &gt; <a href="http://www.google.com/search?q=allinurl%3AByte+java.sun.com&#038;bntl=1"><span class="kw3">Byte</span></a>.<span class="me1">MIN_VALUE</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numpoints<span class="br0">&#91;</span>x/gridSize<span class="br0">&#93;</span><span class="br0">&#91;</span>y/gridSize<span class="br0">&#93;</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> x=<span class="nu0">0</span>;x&lt;width;x++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> y=<span class="nu0">0</span>;y&lt;height;y++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> c = image<span class="br0">&#91;</span>x<span class="br0">&#93;</span><span class="br0">&#91;</span>y<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>c &gt; <a href="http://www.google.com/search?q=allinurl%3AByte+java.sun.com&#038;bntl=1"><span class="kw3">Byte</span></a>.<span class="me1">MIN_VALUE</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> gy = y/gridSize;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> gx = x/gridSize;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> list<span class="br0">&#91;</span><span class="br0">&#93;</span> = grid<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>list == <span class="kw2">null</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> np = numpoints<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = <span class="kw2">new</span> <span class="kw4">int</span><span class="br0">&#91;</span>np * <span class="nu0">2</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span> = list;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numpoints<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list<span class="br0">&#91;</span>numpoints<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span>++<span class="br0">&#93;</span> = x;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list<span class="br0">&#91;</span>numpoints<span class="br0">&#91;</span>gx<span class="br0">&#93;</span><span class="br0">&#91;</span>gy<span class="br0">&#93;</span>++<span class="br0">&#93;</span> = y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw2">return</span> grid;<br />
<span class="br0">&#125;</span></div>
</div>
<p>הקוד הבא סופר את המספר הנקודות בתוך החלון שמרכזו (x,y)&#8217; שימו לב שהלולאות עוברות על ארבעה ריבועים בגריד בלבד:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">// window boundaries:</span><br />
<span class="kw4">int</span> rx1 = x &#8211; grid_size/<span class="nu0">2</span>;<br />
<span class="kw4">int</span> ry1 = y &#8211; grid_size/<span class="nu0">2</span>;<br />
<span class="kw4">int</span> rx2 = rx1 + grid_size;<br />
<span class="kw4">int</span> ry2 = ry1 + grid_size;</p>
<p><span class="co1">// top left grid cell (out of 2&#215;2 group to be checked)</span><br />
<span class="kw4">int</span> gx = rx1 / grid_size;<br />
<span class="kw4">int</span> gy = ry1 / grid_size;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> grid_x=gx;grid_x&lt;gx+<span class="nu0">2</span> &amp;&amp; grid_x &gt;= <span class="nu0">0</span> &amp;&amp; grid_x &lt; gwidth;grid_x++<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> grid_y=gy;grid_y&lt;gy+<span class="nu0">2</span> &amp;&amp;&nbsp; grid_y &gt;= <span class="nu0">0</span> &amp;&amp; grid_y &lt; gheight;grid_y++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> list<span class="br0">&#91;</span><span class="br0">&#93;</span> = grid<span class="br0">&#91;</span>grid_x<span class="br0">&#93;</span><span class="br0">&#91;</span>grid_y<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>list == <span class="kw2">null</span><span class="br0">&#41;</span> continue;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>;i&lt;list.<span class="me1">length</span>/<span class="nu0">2</span>;i++<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> jj = i*<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> dx = list<span class="br0">&#91;</span>jj<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> dy = list<span class="br0">&#91;</span>jj+<span class="nu0">1</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>dx &gt;= rx1 &amp;&amp; dx &lt; rx2 &amp;&amp; dy &gt;= ry1 &amp;&amp; dy &lt; ry2<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>יתכן שאפשר לשפר את הביצועים אם נחלק את הגריד לריבועים יותר קטנים ונכלול ריבועים שלמים שנופלים בתוך החלון בלי לבדוק כל נקודה באופן פרטני, אבל לא ניסיתי.<br />
הביצועים של הפתרון הזה די מטורפים. על Q6600 במהירות 2.4GHZ, עם תמונה בגודל 500X500 פיקסלים וחלון של 20X20, בדיקה של כמה זמן לוקח לספור את הפיקסלים בכל 250,000 החלונות האפשריים לקחה כ160 מילישניות, מה שנותן מהירות בדיקה של מעל 1500 חלונות במילישניה (או יותר ממליון וחצי חלונות בשניה), בלי תלות בגודל התמונה שנבדקת (על תמונה של 1000X1000 המהירות היתה דומה).</p>
<p>האפלט הבאה מדגימה את האלגוריתם. אפשר להוריד אותה <a href="http://firefang.net/files/fast_window.jar">מפה</a>. הקוד כלול בתוך הJAR (אפשר לפתוח את הJAR עם תוכנה לפתיחת קבצי ZIP).<br />
בנוסף, האפלט יכולה לרוץ כתוכנית רגילה : java -jar fast_window.jar<br />
אפשר ללחוץ בתוך האפלט כדי לסמן את הנקודות בחלון.</p>
<p><applet code="Test" width=500 height=500 archive="http://firefang.net/files/fast_window.jar"><br />
הדפדפן שלך לא תומך בג&#8217;אווה, ככה האפלט ניראית (רק שהיא אינטראקטיבית) : <BR/><br />
<a href="http://firefang.net/blog/wp-content/uploads/2010/02/fast_window.png"><img src="http://firefang.net/blog/wp-content/uploads/2010/02/fast_window.png" alt="" title="fast_window" width="297" height="249" class="alignnone size-full wp-image-1571" /></a><br />
</applet></p>
]]></content:encoded>
			<wfw:commentRss>http://firefang.net/blog/1574/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>בעיית החלוקה ההוגנת (*למדיי)</title>
		<link>http://firefang.net/blog/1550</link>
		<comments>http://firefang.net/blog/1550#comments</comments>
		<pubDate>Wed, 20 Jan 2010 20:49:19 +0000</pubDate>
		<dc:creator>עמרי</dc:creator>
				<category><![CDATA[אלגוריתמים]]></category>

		<guid isPermaLink="false">http://firefang.net/blog/?p=1550</guid>
		<description><![CDATA[אז הזכרתי באחר הפוסטים האחרונים את הבעיה הבאה:
בהנתן קבוצת S של n מספרים שלמים, איך נחלק אותה לk קבוצות כך שסכום המספרים בכל קבוצה הוא קרוב לסכום המספרים בקבוצות האחרות.
למשל:
אם
S = {2,2,3,2,3}
k=2
split(S,k)={{2,2,2},{3,3}}
S1={1,5,11,2,10}
k1=3
split(S1,k1)={{5,2,1},{10},{11}}
עבור k=2 הבעיה היא בעצם בעיית תיק הגב (Knapsack)
.
בבעיה זו יש לנו תיק גב בגודל מוגבל, וערמת חפצים במשקלים שונים ובערכים שונים ועלינו לבחור [...]]]></description>
			<content:encoded><![CDATA[<p>אז הזכרתי <a title="באחר הפוסטים" href="http://firefang.net/blog/1525">באחר הפוסטים</a> האחרונים את הבעיה הבאה:</p>
<p>בהנתן קבוצת S של n מספרים שלמים, איך נחלק אותה לk קבוצות כך שסכום המספרים בכל קבוצה הוא קרוב לסכום המספרים בקבוצות האחרות.<br />
למשל:<br />
אם</p>
<div class="codesnip-container" >S = {2,2,3,2,3}<br />
k=2<br />
split(S,k)={{2,2,2},{3,3}}</p>
<p>S1={1,5,11,2,10}<br />
k1=3<br />
split(S1,k1)={{5,2,1},{10},{11}}</p></div>
<p>עבור k=2 הבעיה היא בעצם <a title="בעיית תיק הגב" href="http://en.wikipedia.org/wiki/Knapsack_problem">בעיית תיק הגב</a> (Knapsack)<br />
<img class="alignnone" src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Knapsack.svg/250px-Knapsack.svg.png" alt="" width="184" height="159" />.<br />
בבעיה זו יש לנו תיק גב בגודל מוגבל, וערמת חפצים במשקלים שונים ובערכים שונים ועלינו לבחור תת קבוצה של החפצים שנכנסים כולם לתיק וערכם מקסימלי.<br />
אם הערך של כל חפץ שווה למשקל שלו (נניח כל החפצים עשויים מזהב טהור), ונפח התיק הוא חצי מסכום המשקל של כל החפצים אז הבעיה שקולה לגמרי לבעיה שלנו עבור K=2, כי רשימת החפצים שבתיק שקולה לחלוקה של החפצים לשתי קבוצות (אלו שבתיק ואלו שמחוץ לתיק).</p>
<p>עבור k גדול מ2, הבעיה קשה יותר.<br />
הפתרון שמצאתי פותר את הבעיה בצורה קרובה לאופטימלית (החלוקה לא תמיד הטובה ביותר, אבל קרובה מאוד), והוא גם פשוט למימוש ויעיל במיוחד.<br />
בשמיעה ראשונה הוא ישמע פשוט מדי מכדי להיות נכון, ואתם יותר ממוזמנים להוכיח לי שאני טועה:<br />
1. מיינו את רשימת המספרים בסדר יורד.<br />
2. צרו k קבוצות ריקות<br />
3. לכל איבר ברשימה הממוינת (בסדר יורד) הכניסו אותו לקבוצה הריקה ביותר.</p>
<p>זה הכל.<br />
לא היה קל מדי למצוא דוגמא שבה האלגוריתם הפשוט הזה נותר תשובה שאינה אופטימלית, אבל למעשה הדוגמא הראשונה שלי למעלה היא דוגמא כזו. בחלוקה האופטימלית שתי הקבוצות בגודל שש. בחלוקה לפי האלגוריתם שלי הגדלים יהיו שבע וחמש.<br />
ובכל זאת, השגיאה פה היא השגיאה המינימלית האפשרית בדוגמא הזו (Off by one).</p>
<p>אז מה אתם אומרים, יש לכם פתרון יותר טוב?<br />
יש לכם דוגמא שמראה לי שהפתרון שלי הוא בעצם לא טוב?</p>
<p>והנה בעיה נוספת עליה אני ארחיב באחד הפוסטים הקרובים:<br />
יש לכם תמונה שחורה בגודל N על N פיקסלים ועליה מפוזרים נקודות באופן אקראי. נניח משהו דומה כזה:</p>
<p><a href="http://play2survive.files.wordpress.com/2008/09/night-sky.jpg"></a><a href="http://firefang.net/blog/wp-content/uploads/2010/01/dots.png"><img class="alignnone size-full wp-image-1551" title="dots" src="http://firefang.net/blog/wp-content/uploads/2010/01/dots.png" alt="" width="196" height="194" /></a><br />
הבעיה היא כזו. בהינתן קואורדינטה (x,y) מסויימת בתמונה, מצא במהירות את כל הנקודות שנמצאות בתוך חלון בגודל K על K (כאשר K קבוע בבעיה) שמרכזו (x,y), משהו כזה:<br />
<a href="http://firefang.net/blog/wp-content/uploads/2010/01/dots2.png"><img class="alignnone size-full wp-image-1552" title="dots2" src="http://firefang.net/blog/wp-content/uploads/2010/01/dots2.png" alt="" width="198" height="199" /></a><br />
הדרישה היחידה פה היא המהירות של הפעולה הזו. מותר לכם להשתמש בכמה זכרון שתרצו ולעבד את התמונה מראש כאוות נפשכם (כמובן, העיבוד מראש לא יכול להיות איטי מדי)<br />
כשאני אומר מהר, אני מתכוון ממש מהר, גם עבור תמונות גדולות.</p>
]]></content:encoded>
			<wfw:commentRss>http://firefang.net/blog/1550/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
