Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3.  
  4. class Graph {
  5.     public int size;
  6.     public int[][] matrix;
  7.  
  8.     public Graph(int size, int[][] matrix) {
  9.         this.size = size;
  10.         this.matrix = matrix;
  11.     }
  12.  
  13.     public Graph(int size) {
  14.         this.size = size;
  15.         this.matrix = new int[size][size];
  16.     }
  17.  
  18.     public void addEdge(int fromIndex, int toIndex) {
  19.         matrix[fromIndex][toIndex] = 1;
  20.         matrix[toIndex][fromIndex] = 1;
  21.     }
  22.  
  23.     public void removeEdge(int fromIndex, int toIndex) {
  24.         matrix[fromIndex][toIndex] = 0;
  25.         matrix[toIndex][fromIndex] = 0;
  26.     }
  27.  
  28.     public void printGraph() {
  29.         for (int i = 0; i < size; i++) {
  30.             for (int j = 0; j < size; j++) {
  31.                 System.out.print(matrix[i][j] + " ");
  32.             }
  33.             System.out.println();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement