Advertisement
FahimFaisal

Prims

Feb 18th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 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 LAB4_Prims;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.HashSet;
  11. import java.util.PriorityQueue;
  12. import java.util.Scanner;
  13.  
  14.  
  15. public class prims {
  16.     public static int node;
  17.     public static int [][] matrix;
  18.     public static int [] color;
  19.     public static int [] parent;
  20.     public static int [] vertices= {0,1,2,3,4,5,6,7,8};
  21.     public static int [] d;
  22.    
  23.     public static PriorityQueue<Integer> q = new PriorityQueue<Integer>();
  24.    
  25.    
  26.  
  27.     public static void main(String [] args) {
  28.      try{
  29.             Scanner scn=new Scanner(new File("E://Ins.txt"));
  30.             String[] splitA;
  31.            
  32.             String temp=scn.nextLine();
  33.             node=Integer.parseInt(temp);
  34.            
  35.            
  36.          String temp1=scn.nextLine();
  37.             int edge=Integer.parseInt(temp1);
  38.            
  39.            
  40.          //   out.println(v);
  41.             matrix=new int[node][node];
  42.             color=new int[node];
  43.             parent=new int[node];
  44.             d=new int[node];
  45.             while(scn.hasNext())
  46.             {
  47.             String line=scn.nextLine();
  48.             splitA=line.split(",");
  49.             int x=Integer.parseInt(splitA[0]);
  50.             int y=Integer.parseInt(splitA[1]);
  51.             int w=Integer.parseInt(splitA[2]);
  52.             matrix[x][y]=w;
  53.             matrix[y][x]=w;
  54.             }
  55.          for(int i=0;i<node;i++) {
  56.                 for(int j=0;j<node;j++) {
  57.                     System.out.print(matrix[i][j]+",");
  58.                 }
  59.                 System.out.println();
  60.             }
  61.        
  62.          color=new int[node];
  63.          HashSet<Integer> Set=new <Integer>HashSet();
  64.          
  65.          int s=0;
  66.          
  67.          for(int i=0;i<node;i++){
  68.              if(i!=s){
  69.                  color[i]=0;
  70.            d[i]=999;
  71.            parent[i]=-1;
  72.          
  73.          }
  74.          }
  75.          
  76.          for(int i=0;i<node;i++){
  77.          
  78.            q.add(d[i]);
  79.          }
  80.          for(int i:d){
  81.              System.out.print(i+" ");
  82.          }
  83.          
  84.          d[s]=0;
  85.          parent[s]=-1;
  86.          color[s]=1;
  87.          //q.add(d[s]);
  88.          while(!(q.isEmpty())){
  89.              //System.out.println("sssssssssssss");
  90.              //System.out.println(q.peek()+"");
  91.              
  92.              int u=findIndex(q.poll(),d);
  93.              //System.out.println(u+"<<<>>>");
  94.              for(int v=0;v<node;v++){
  95.                  if(matrix[u][v]!=0){
  96.                      
  97.                      if(matrix[u][v]<d[v]){
  98.                          parent[v]=u;
  99.                          d[v]=matrix[u][v];
  100.                      }
  101.                  }
  102.              }
  103.              color[u]=1;//black
  104.                 queueSort();
  105.                 if(q.peek()!=null){
  106.                 System.out.print(u+"->");
  107.                 }else{
  108.                     System.out.print(u);
  109.                 }
  110.              
  111.          }
  112.          for(int i:d){
  113.              System.out.print(i+" ");
  114.          }
  115.          
  116.          
  117.          
  118.          }
  119.      catch(Exception e){
  120.          e.printStackTrace();
  121.      }
  122.     }
  123.      public static int findIndex(int value,int [] arr) {
  124.          int index=-1;
  125.          for(int i=0;i<arr.length;i++){
  126.              if(value==arr[i] ){
  127.                  index=i;
  128.              }
  129.          }
  130.         return index;
  131.     }
  132.      
  133.       public static void queueSort(){
  134.         q.clear();
  135.         for(int i=0;i<node;i++){
  136.             if(color[i]==0){
  137.             q.add(d[i]);
  138.                
  139.         }
  140.        }
  141.        
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement