Page 115 - Algorithms Notes for Professionals
P. 115

{
             System.out.print("ReachSet = ");
             for (int i = 0; i < Reached.length; i++ )
                if ( Reached[i] )
                  System.out.print( i + " ");
             //System.out.println();
          }
        public static void main(String[] args)
          {
             int[][] conn = {{0,3,0,2,0,0,0,0,4},  // 0
                             {3,0,0,0,0,0,0,4,0},  // 1
                             {0,0,0,6,0,1,0,2,0},  // 2
                             {2,0,6,0,1,0,0,0,0},  // 3
                             {0,0,0,1,0,0,0,0,8},  // 4
                             {0,0,1,0,0,0,8,0,0},  // 5
                             {0,0,0,0,0,8,0,0,0},  // 6
                             {0,4,2,0,0,0,0,0,0},  // 7
                             {4,0,0,0,8,0,0,0,0}   // 8
                            };
             Graph G = new Graph(conn);
             G.Prim();
          }
       }

       Compile the above code using javac Graph.java

       Output:


       $ java Graph
        *  3  *  2  *  *  *  *  4
        3  *  *  *  *  *  *  4  *
        *  *  *  6  *  1  *  2  *
        2  *  6  *  1  *  *  *  *
        *  *  *  1  *  *  *  *  8
        *  *  1  *  *  *  8  *  *
        *  *  *  *  *  8  *  *  *
        *  4  2  *  *  *  *  *  *
        4  *  *  *  8  *  *  *  *
       ReachSet = 0 Min cost edge: (0,3)cost = 2
       ReachSet = 0 3
       Min cost edge: (3,4)cost = 1
       ReachSet = 0 3 4
       Min cost edge: (0,1)cost = 3
       ReachSet = 0 1 3 4
       Min cost edge: (0,8)cost = 4
       ReachSet = 0 1 3 4 8
       Min cost edge: (1,7)cost = 4
       ReachSet = 0 1 3 4 7 8
       Min cost edge: (7,2)cost = 2
       ReachSet = 0 1 2 3 4 7 8
       Min cost edge: (2,5)cost = 1
       ReachSet = 0 1 2 3 4 5 7 8
       Min cost edge: (5,6)cost = 8
       ReachSet = 0 1 2 3 4 5 6 7 8
       0 --> 0
       0 --> 1
       7 --> 2
       0 --> 3
       3 --> 4
       2 --> 5
       5 --> 6


       colegiohispanomexicano.net – Algorithms Notes                                                            111
   110   111   112   113   114   115   116   117   118   119   120