免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 669 | 回复: 0
打印 上一主题 下一主题

prim最小生成树 [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-06-29 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-16 14:56 |只看该作者 |倒序浏览
  1. #! /usr/bin/perl -w

  2. use strict;

  3. my $filepath = "input3";
  4. my @matrix = ();
  5. my $count;
  6. my $max = 1000;

  7. #read file from path
  8. sub readfile{
  9.         open(FD,$filepath) or die "$filepath not exitst!";
  10.         foreach my $line (<FD>){
  11.                 my @keys = split(";",$line);
  12.                 my @path = ();
  13.                 foreach my $key (@keys){
  14.                         push(@path, $key);
  15.                 }
  16.                 push(@matrix,\@path);
  17.         }
  18. }

  19. #change file from 0 to 1000
  20. sub getpath{
  21.         readfile;
  22.         $count = $#matrix + 1;
  23.         for ( my $i = 0 ; $i < $count ; $i++){
  24.                 for ( my $j = 0; $j < $count ; $j++){
  25.                         if ( $i != $j && $matrix[$i][$j] == 0 ){
  26.                                 $matrix[$i][$j] = $max;
  27.                         }
  28.                 }
  29.         }
  30. }

  31. sub prim{
  32.         getpath;
  33.         my $startnode = shift;#select where to start
  34.         my @dist = (); #select the min path
  35.         my @mark = (); #mark whether it has been accessed
  36.         my @path = (); #store the path
  37.         my $sum = 0; #store the sum length

  38. #init the first node
  39.         for ( my $i = 0; $i < $count; $i++){
  40.                 $dist[$i] = $matrix[$startnode][$i];
  41.         }

  42.         $mark[$startnode] = 0;
  43.         push(@path, "$startnode");

  44.     #start to add node
  45.         for ( my $j = 1; $j < $count; $j++){
  46.         #select the min node
  47.                 my $min = $max;
  48.                 my $location = -1;       
  49.                 for ( my $i = 0; $i < $count; $i++){
  50.                         print "       the $j dist is $dist[$i]\n";
  51.                 }

  52.                 for ( my $i = 1; $i < $count; $i++){
  53.                         if( !defined($mark[$i]) and $dist[$i] < $min){
  54.                                 $location = $i;
  55.                                 $min = $dist[$i];
  56.                         }
  57.                 }

  58.         push(@path, " --> $location");
  59.                 $mark[$location] = 0;
  60.                 $sum = $sum + $dist[$location];

  61.                 #print " $j get location $location min $min \n";
  62.                 #print "############################################\n";

  63.         #update the dist
  64.                 for ( my $i = 1; $i < $count; $i++){
  65.                         if ( !defined($mark[$i]) and $matrix[$location][$i] < $dist[$i] ){
  66.                                 $dist[$i] = $matrix[$location][$i];
  67.                         }
  68.                 }
  69.         }

  70.         print "the shortest path sum is: $sum\n";
  71.         for ( my $i = 0; $i < $count; $i++){
  72.                 print "from $i has $path[$i] ";
  73.                 print "       the dist is $dist[$i]\n";
  74.         }
  75. }


  76. prim(1);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP