Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- class CopyOfAdjacencyMatrix
- {
- private final int MAX_NO_OF_VERTICES;
- private int adjacency_matrix[][];
- public CopyOfAdjacencyMatrix(int number_of_vertices)
- {
- MAX_NO_OF_VERTICES = number_of_vertices;
- adjacency_matrix = new int[MAX_NO_OF_VERTICES + 1][MAX_NO_OF_VERTICES + 1];
- }
- public void setEdge(int from_vertex, int to_vertex, int edge)
- {
- try
- {
- adjacency_matrix[from_vertex][to_vertex] = edge;
- } catch (ArrayIndexOutOfBoundsException indexBounce)
- {
- System.out.println("the vertex entered is not present");
- }
- }
- public int getEdge(int from_vertex, int to_vertex)
- {
- try
- {
- return adjacency_matrix[from_vertex][to_vertex];
- } catch (ArrayIndexOutOfBoundsException indexBounce)
- {
- System.out.println("the vertex entered is not present");
- }
- return -1;
- }
- public static void main(String args[]) throws NumberFormatException, IOException
- {
- int number_of_vertices, count = 1;
- int source = 0, destination = 0;
- Scanner scan = new Scanner(System.in);
- CopyOfAdjacencyMatrix adjacencyMatrix;
- File inputFile=new File("input.txt");
- BufferedReader br = new BufferedReader(new FileReader(inputFile));
- String thisLine =null;
- number_of_vertices=Integer.parseInt(br.readLine());
- int number_of_edges=Integer.parseInt(br.readLine());
- adjacencyMatrix = new CopyOfAdjacencyMatrix(number_of_vertices);
- while ((thisLine = br.readLine()) != null) { // while loop begins here
- String [] splitted = thisLine.split("\\s+");
- source = Integer.parseInt(splitted[0]);
- destination = Integer.parseInt(splitted[1]);
- adjacencyMatrix.setEdge(source, destination, 1);
- count++;
- }
- try
- {
- System.out.println("The adjacency matrix for given graph is");
- for (int i = 1; i <= number_of_vertices; i++)
- System.out.print(i);
- System.out.println();
- for (int i = 1; i <= number_of_vertices; i++)
- {
- System.out.print(i);
- for (int j = 1; j <= number_of_vertices; j++)
- {
- System.out.print(adjacencyMatrix.getEdge(i, j));
- }
- System.out.println();
- }
- } catch (InputMismatchException inputMisMatch)
- {
- System.out.println("Error in Input Format.<source index> <destination index>");
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement