Advertisement
chowdhury_riham

Prim's

Nov 3rd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package prims;
  7. import java.util.InputMismatchException;
  8. import java.util.Scanner;
  9.  
  10. public class Prims
  11. {
  12.     private boolean unsettled[];
  13.     private boolean settled[];
  14.     private int numberofvertices;
  15.     private int adjacencyMatrix[][];
  16.     private int key[];
  17.     public static final int INFINITE = 999;
  18.     private int parent[];
  19.  
  20.     public Prims(int numberofvertices)
  21.     {
  22.         this.numberofvertices = numberofvertices;
  23.         unsettled = new boolean[numberofvertices + 1];
  24.         settled = new boolean[numberofvertices + 1];
  25.         adjacencyMatrix = new int[numberofvertices + 1][numberofvertices + 1];
  26.         key = new int[numberofvertices + 1];
  27.         parent = new int[numberofvertices + 1];
  28.     }
  29.  
  30.     public int getUnsettledCount(boolean unsettled[])
  31.     {
  32.         int count = 0;
  33.         for (int index = 0; index < unsettled.length; index++)
  34.         {
  35.             if (unsettled[index])
  36.             {
  37.                 count++;
  38.             }
  39.         }
  40.         return count;
  41.     }
  42.  
  43.     public void primsAlgorithm(int adjacencyMatrix[][])
  44.     {
  45.         int evaluationVertex;
  46.         for (int source = 1; source <= numberofvertices; source++)
  47.         {
  48.             for (int destination = 1; destination <= numberofvertices; destination++)
  49.             {
  50.                 this.adjacencyMatrix[source][destination] = adjacencyMatrix[source][destination];
  51.             }
  52.         }
  53.  
  54.         for (int index = 1; index <= numberofvertices; index++)
  55.         {
  56.             key[index] = INFINITE;
  57.         }
  58.         key[1] = 0;
  59.         unsettled[1] = true;
  60.         parent[1] = 1;
  61.  
  62.         while (getUnsettledCount(unsettled) != 0)
  63.         {
  64.             evaluationVertex = getMimumKeyVertexFromUnsettled(unsettled);
  65.             unsettled[evaluationVertex] = false;
  66.             settled[evaluationVertex] = true;
  67.             evaluateNeighbours(evaluationVertex);
  68.         }
  69.     }
  70.  
  71.     private int getMimumKeyVertexFromUnsettled(boolean[] unsettled2)
  72.     {
  73.         int min = Integer.MAX_VALUE;
  74.         int node = 0;
  75.         for (int vertex = 1; vertex <= numberofvertices; vertex++)
  76.         {
  77.             if (unsettled[vertex] == true && key[vertex] < min)
  78.             {
  79.                 node = vertex;
  80.                 min = key[vertex];
  81.             }
  82.         }
  83.         return node;
  84.     }
  85.  
  86.     public void evaluateNeighbours(int evaluationVertex)
  87.     {
  88.  
  89.         for (int destinationvertex = 1; destinationvertex <= numberofvertices; destinationvertex++)
  90.         {
  91.             if (settled[destinationvertex] == false)
  92.             {
  93.                 if (adjacencyMatrix[evaluationVertex][destinationvertex] != INFINITE)
  94.                 {
  95.                     if (adjacencyMatrix[evaluationVertex][destinationvertex] < key[destinationvertex])
  96.                     {
  97.                         key[destinationvertex] = adjacencyMatrix[evaluationVertex][destinationvertex];
  98.                         parent[destinationvertex] = evaluationVertex;
  99.                     }
  100.                     unsettled[destinationvertex] = true;
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     public void printMST()
  107.     {
  108.         int sum=0;
  109.         System.out.println("SOURCE  : DESTINATION = WEIGHT");
  110.         for (int vertex = 2; vertex <= numberofvertices; vertex++)
  111.         {
  112.             System.out.println((char)(parent[vertex]+64) + "\t:\t" + (char)(vertex +64)+"\t=\t"+ adjacencyMatrix[parent[vertex]][vertex]);
  113.         }
  114.        for (int vertex = 2; vertex <= numberofvertices; vertex++) {
  115.           sum+= adjacencyMatrix[parent[vertex]][vertex];
  116.        }
  117.        System.out.println("Total cost is :"+ sum);
  118.     }
  119.  
  120.     public static void main(String... arg)
  121.     {
  122.         int adjacency_matrix[][];
  123.         int number_of_vertices;
  124.         Scanner scan = new Scanner(System.in);
  125.  
  126.         try
  127.         {
  128.             System.out.println("Enter the number of vertices");
  129.             number_of_vertices = scan.nextInt();
  130.             System.out.println("We assigened the vertices as below:");
  131.             for(int i=0;i<number_of_vertices;i++){
  132.                 System.out.print((char)(i+65)+"\t");
  133.             }
  134.             System.out.println();
  135.             adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
  136.  
  137.             System.out.println("Enter the Weighted Matrix for the graph & type 999 for no connection");
  138.             for (int i = 1; i <= number_of_vertices; i++)
  139.             {
  140.                 for (int j = 1; j <= number_of_vertices; j++)
  141.                 {  
  142.                     if(adjacency_matrix[i][j]==0 ){
  143.                     if(i!=j ){
  144.                         System.out.println("Enter the weight for the"+(char)(i+64)+"--"+(char)(j+64));
  145.                    adjacency_matrix[i][j] = scan.nextInt();
  146.                    adjacency_matrix[j][i] =adjacency_matrix[i][j];
  147.                     }
  148.                     }
  149.                     if (i == j)
  150.                     {
  151.                         adjacency_matrix[i][j] = 0;
  152.                         continue;
  153.                     }
  154.                     if (adjacency_matrix[i][j] == 0 ||adjacency_matrix[i][j] == 999)
  155.                     {
  156.                         adjacency_matrix[i][j] = INFINITE;
  157.                     }
  158.                     }
  159.                
  160.             }
  161.  
  162.             Prims prims = new Prims(number_of_vertices);
  163.             prims.primsAlgorithm(adjacency_matrix);
  164.             prims.printMST();
  165.  
  166.         } catch (InputMismatchException inputMismatch)
  167.         {
  168.             System.out.println("Wrong Input Format");
  169.         }
  170.         scan.close();
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement