Advertisement
Foyaj128

copy Adjacency Matrix

May 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.InputMismatchException;
  7. import java.util.Scanner;
  8.  
  9. class CopyOfAdjacencyMatrix
  10. {
  11.     private final int MAX_NO_OF_VERTICES;
  12.     private int adjacency_matrix[][];
  13.  
  14.     public CopyOfAdjacencyMatrix(int number_of_vertices)
  15.     {
  16.         MAX_NO_OF_VERTICES = number_of_vertices;
  17.         adjacency_matrix = new int[MAX_NO_OF_VERTICES + 1][MAX_NO_OF_VERTICES + 1];
  18.     }
  19.  
  20.     public void setEdge(int from_vertex, int to_vertex, int edge)
  21.     {
  22.         try
  23.         {
  24.             adjacency_matrix[from_vertex][to_vertex] = edge;
  25.         } catch (ArrayIndexOutOfBoundsException indexBounce)
  26.         {
  27.             System.out.println("the vertex entered is not present");
  28.         }
  29.     }
  30.  
  31.     public int getEdge(int from_vertex, int to_vertex)
  32.     {
  33.         try
  34.         {
  35.             return adjacency_matrix[from_vertex][to_vertex];
  36.         } catch (ArrayIndexOutOfBoundsException indexBounce)
  37.         {
  38.             System.out.println("the vertex entered is not present");
  39.         }
  40.         return -1;
  41.     }
  42.  
  43.     public static void main(String args[]) throws NumberFormatException, IOException
  44.     {
  45.         int number_of_vertices, count = 1;
  46.         int source = 0, destination = 0;
  47.         Scanner scan = new Scanner(System.in);
  48.         CopyOfAdjacencyMatrix adjacencyMatrix;
  49.         File inputFile=new File("input.txt");
  50.         BufferedReader br = new BufferedReader(new FileReader(inputFile));
  51.         String thisLine =null;
  52.         number_of_vertices=Integer.parseInt(br.readLine());
  53.         int number_of_edges=Integer.parseInt(br.readLine());
  54.        
  55.         adjacencyMatrix  = new CopyOfAdjacencyMatrix(number_of_vertices);
  56.        
  57.         while ((thisLine = br.readLine()) != null) { // while loop begins here             
  58.           String [] splitted = thisLine.split("\\s+");
  59.           source = Integer.parseInt(splitted[0]);
  60.           destination = Integer.parseInt(splitted[1]);
  61.           adjacencyMatrix.setEdge(source, destination, 1);
  62.           count++;
  63.         }
  64.          
  65.         try
  66.         {            
  67.             System.out.println("The adjacency matrix for given graph is");
  68.             for (int i = 1; i <= number_of_vertices; i++)
  69.                 System.out.print(i);
  70.  
  71.             System.out.println();
  72.             for (int i = 1; i <= number_of_vertices; i++)
  73.             {
  74.                 System.out.print(i);
  75.                 for (int j = 1; j <= number_of_vertices; j++)
  76.                 {
  77.                     System.out.print(adjacencyMatrix.getEdge(i, j));
  78.                 }
  79.                 System.out.println();
  80.             }
  81.         } catch (InputMismatchException inputMisMatch)
  82.         {
  83.             System.out.println("Error in Input Format.<source index> <destination index>");
  84.         }
  85.         scan.close();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement