<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=TSP_programmi_Java_l%C3%A4htekood</id>
	<title>TSP programmi Java lähtekood - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=TSP_programmi_Java_l%C3%A4htekood"/>
	<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=TSP_programmi_Java_l%C3%A4htekood&amp;action=history"/>
	<updated>2026-10-28T10:55:02Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://www.lambda.ee/w/index.php?title=TSP_programmi_Java_l%C3%A4htekood&amp;diff=8488&amp;oldid=prev</id>
		<title>Tammet – 23. november 2010, kell 03:56</title>
		<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=TSP_programmi_Java_l%C3%A4htekood&amp;diff=8488&amp;oldid=prev"/>
		<updated>2010-11-23T03:56:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;et&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←Vanem redaktsioon&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Redaktsioon: 23. november 2010, kell 05:56&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;1. rida:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;1. rida:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;// Travelling Salesman Problem&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;// Travelling Salesman Problem&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;// Depth first search&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;// Depth first search&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l90&quot;&gt;90. rida:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;91. rida:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;    &lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;    &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}   // end of class&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}   // end of class&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key lambdadb:diff:1.41:old-4915:rev-8488:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Tammet</name></author>
	</entry>
	<entry>
		<id>http://www.lambda.ee/w/index.php?title=TSP_programmi_Java_l%C3%A4htekood&amp;diff=4915&amp;oldid=prev</id>
		<title>Tammet – 23. november 2010, kell 03:55</title>
		<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=TSP_programmi_Java_l%C3%A4htekood&amp;diff=4915&amp;oldid=prev"/>
		<updated>2010-11-23T03:55:37Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;// Travelling Salesman Problem&lt;br /&gt;
// Depth first search&lt;br /&gt;
// Fourth attempt: improved algorithm&lt;br /&gt;
//  we will not continue searching paths&lt;br /&gt;
//  which are ALREADY worse than the best one&lt;br /&gt;
//  found so far&lt;br /&gt;
&lt;br /&gt;
public class Tsp4 {&lt;br /&gt;
                &lt;br /&gt;
  static int cities=10; // nr of cities&lt;br /&gt;
  static int nodeslooked=0; // count nodes in search&lt;br /&gt;
  static int leaveslooked=0; // count leaves in search&lt;br /&gt;
  static int best=9999999;  // global variable recording best distance  &lt;br /&gt;
  // Matrix of distances (cost to travel) between cities &lt;br /&gt;
  static int dist[][] =&lt;br /&gt;
          {{0,5,4,3,7,1,3,6,9,2},&lt;br /&gt;
           {5,0,2,9,4,1,7,2,3,4},&lt;br /&gt;
           {4,2,0,6,7,2,4,6,8,9},&lt;br /&gt;
           {3,9,6,0,6,2,5,6,9,9},&lt;br /&gt;
           {4,2,2,6,0,2,4,5,8,9},&lt;br /&gt;
           {3,9,6,6,6,0,5,6,9,4},&lt;br /&gt;
           {1,9,3,2,6,2,0,6,1,5},&lt;br /&gt;
           {3,2,0,6,2,2,4,0,2,3},&lt;br /&gt;
           {7,9,6,7,6,1,5,6,0,2},&lt;br /&gt;
           {1,8,2,7,6,1,3,6,2,0}};&lt;br /&gt;
           &lt;br /&gt;
  static int passedcities[]; // passed cities on a path&lt;br /&gt;
  static int bestpath[]; // best path so far        &lt;br /&gt;
           &lt;br /&gt;
  public static void main(String[] args) {               &lt;br /&gt;
     // create helper arrays&lt;br /&gt;
     passedcities = new int[cities];&lt;br /&gt;
     bestpath = new int[cities];&lt;br /&gt;
     // search from city 0, with 0 passed and 0 cost of travel so far      &lt;br /&gt;
     search(0,0,0);                     &lt;br /&gt;
     // print out result&lt;br /&gt;
     System.out.println(&amp;quot;examined nodes during search: &amp;quot;+nodeslooked);&lt;br /&gt;
     System.out.println(&amp;quot;examined leaves during search: &amp;quot;+leaveslooked);&lt;br /&gt;
     System.out.println(&amp;quot;best cost: &amp;quot;+best); &lt;br /&gt;
     System.out.println(&amp;quot;best path: &amp;quot;);&lt;br /&gt;
     for (int i=0; i&amp;lt;cities; i++) System.out.print(bestpath[i] + &amp;quot; &amp;quot;);&lt;br /&gt;
       &lt;br /&gt;
  }&lt;br /&gt;
     &lt;br /&gt;
  // The recursive search function&lt;br /&gt;
  //&lt;br /&gt;
  // city: nr of city just arrived into&lt;br /&gt;
  // passed: number of cities passed so far&lt;br /&gt;
  // passedlen: length (cost) travelled so far&lt;br /&gt;
  &lt;br /&gt;
  static void search(int city, int passed, int passedlen) {&lt;br /&gt;
      //System.out.println(&amp;quot;city: &amp;quot;+city+&amp;quot; passed: &amp;quot;+passed+&lt;br /&gt;
      //                   &amp;quot; passedlen: &amp;quot;+passedlen); &lt;br /&gt;
      nodeslooked++; &lt;br /&gt;
      // store the new passed city in the passed array&lt;br /&gt;
      passedcities[passed]=city;&lt;br /&gt;
      // maybe we have travelled through all cities - 1?&lt;br /&gt;
      if (passed==(cities-1)) {&lt;br /&gt;
          leaveslooked++;        &lt;br /&gt;
          // compute the final length (trip to home is left!)&lt;br /&gt;
          int length=passedlen+dist[0][city];&lt;br /&gt;
          // save the length and path if we are back home &lt;br /&gt;
          // and the length found is best so far&lt;br /&gt;
          if (length&amp;lt;best) {&lt;br /&gt;
              best=length; &lt;br /&gt;
              for (int i=0; i&amp;lt;cities; i++) bestpath[i]=passedcities[i]; &lt;br /&gt;
          }              &lt;br /&gt;
      // if not all cities have been passed, try all cities&lt;br /&gt;
      // not yet passed          &lt;br /&gt;
      } else  {&lt;br /&gt;
           // loop over all cities&lt;br /&gt;
           for(int next=0; next&amp;lt;cities; next++) {&lt;br /&gt;
             // if the distance will be less&lt;br /&gt;
             // than the best distance so far, ONLY THEN GO!&lt;br /&gt;
             if (passedlen+dist[city][next] &amp;lt; best) {&lt;br /&gt;
               // check if next is OK (not passed yet)&lt;br /&gt;
               boolean okcity=true;&lt;br /&gt;
               for (int i=0; i&amp;lt;passed+1 &amp;amp;&amp;amp; okcity; i++) &lt;br /&gt;
                  if (passedcities[i]==next) okcity=false;                                    &lt;br /&gt;
               if (okcity) {   // if ok, continue search&lt;br /&gt;
                search(next,&lt;br /&gt;
                       passed+1,&lt;br /&gt;
                       passedlen+dist[city][next]); &lt;br /&gt;
               }&lt;br /&gt;
             }  &lt;br /&gt;
           }    &lt;br /&gt;
       }    &lt;br /&gt;
               &lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
}   // end of class&lt;/div&gt;</summary>
		<author><name>Tammet</name></author>
	</entry>
</feed>