Floyd Shortest Path algorithm

Source: Internet
Author: User

After reading this article to write the small program, Floyd shortest path algorithm, the shortest distance from one point to another point, the middle can pass through any other Points. Three for loop, from I to J in the shortest distance through k, the outermost for loop is through the point k, the internal two loops from I (0) to J (0,1,2,3) through K (0) the shortest distance, from I (1) to J (0,1,2,3) through K (0) the shortest distance, ... From I (3) to J (0,1,2,3) through the shortest distance of K (0), on the basis of K (0) and then through K (2), from I (0) to J (0,1,2,3) through the shortest distance of K (1), from I (1) to J (0,1,2,3) through the shortest distance of K (1),

The program was written according to the above Article.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingsystem.linq;4 usingsystem.text;5 usingSystem.Threading.Tasks;6 7 namespaceFloyd Shortest Path algorithm8 {9     class programTen     { one         Static voidMain (string[] Args) a         { -             int[,] e =New int[4,4] {  {0,2,6,4 },  -{9999,0,3,9999 },  the{7,9999,0,1 },  -{5,9999, a,0 } }; -  -             int[,] e1 =New int[4,4] { {0,2,6,4 },  +{9999,0,3,9999 },  -{7,9999,0,1 },  +{5,9999, a,0 } }; a  at              for(inti =0; I <4; i++) -             { -                  for(intj =0; J <4; J + +) -                 { -Console.Write (e1[i, j] +"       "); -                 } in Console.WriteLine (); -             } to              for(intK =0; K <4; K++)//through four locations, respectively . +             { -                  for(inti =0; I <4; I++)//I was the first place the                 { *                      for(intj =0; J <4; J + +)//J is the second location . $                     {Panax Notoginseng                         //from I to K,k to J less than I to J -                         if(e1[i, k] + e1[k, j] <e1[i, j]) the                         { +e1[i, j] = e1[i, k] +e1[k, j]; a  theConsole.WriteLine ("Location { 2} from position {0} to location {1}, original path distance {3}, optimized distance {4}", i+1, j+1, K +1, e[i,j],e1[i,j]); +                         } -                     } $                 } $             } -Console.WriteLine ("----------------------------------"); -              for(inti =0; I <4; i++) the             { -                  for(intj =0; J <4; J + +)Wuyi                 { theConsole.Write (e1[i, j] +"    "); -                 } wu Console.WriteLine (); -             } about Console.readkey (); $         } -     } -}

Floyd Shortest Path algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.