import java io BufferedReader; import java io File; import java io FileReader; import java io IOException; import java util ArrayList; import java util Arrays; import java util Collections; import jav.
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; public abstract class Graphs { int numvext; int[][] maxtrix; int v; public Graphs(int numvext, int[][] maxtrix) { super(); this.numvext = numvext; this.maxtrix = maxtrix; } public int getNumvext() { return numvext; } public void setNumvext(int numvext) { this.numvext = numvext; } public int[][] getMaxtrix() { return maxtrix; } public Graphs(String file) { load(file); } public void load(String pATH) { // TODO Auto-generated method stub File file = new File(pATH); FileReader rd; try { rd = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(rd); numvext = Integer.parseInt(bufferedReader.readLine()); maxtrix = new int[numvext][numvext]; int rows = 0; String line = ""; while ((line = bufferedReader.readLine()) != null) { String[] strings = line.split("\\s"); for (int i = 0; i < strings.length; i++) { maxtrix[rows][i] = Integer.valueOf(strings[i]); } rows++; } } catch (NumberFormatException | IOException e) { // TODO: handle exception System.out.println("error!!"); } } public void printmatrix(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } System.out.println("\n"); } } public abstract int numVertexs(); public abstract int numEdges(); // KIEM TRA DO THI DO CO HOP LI KHONG public boolean checkValid(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if (matrix[i][j] == && i == j) { return true; } else { return false; } } } return false; } // KIEM TRA DO THI DO VO HUONG HAY CO HUONG public boolean checkUnGraph(int[][] matrix) { for (int i = 0; i < numvext; i++) { for (int j = 0; j < numvext; j++) { if (matrix[i][j] == matrix[j][i]) { return true; } } } return false; } // tong bac cua moi dinh public abstract int deg(int v); // tinh tong bac thi public abstract int sumDeg(int v); // implement code // xet inh lien thong cua thi public void diTimcacDinhLienThong(int[][] matrixAdj, int[] arr, int v) { for (int i = 0; i < numvext; i++) { if (matrixAdj[v][i] != && arr[v] != arr[i]) { arr[i] = arr[v]; diTimcacDinhLienThong(matrixAdj, arr, i); } } } // xet tinh lien thong cua thi public int soThanphanlienthong() { int[] index = new int[numvext]; int stplt = 0; for (int i = 0; i < numvext; i++) { index[i] = 0; } for (int i = 0; i < numvext; i++) { if (index[i] == 0) { stplt++; index[i] = stplt; } if (index[i] != 0) { diTimcacDinhLienThong(maxtrix, index, i); } } return stplt; } // xet inh lien thong cua thi public void xetTinhLienThong(int[][] matrixAdj) { // implement code int[] index = new int[numvext];// tao mang luu tru cac dinh int stplt = 0; for (int i = 0; i < numvext; i++) {// xet het tat ca dinh index[i] = 0;// gan voi chon dinh bat ki vieng tham } for (int i = 0; i < numvext; i++) { if (index[i] == 0) { stplt++;// so phan no se tang len index[i] = stplt;// da nh dau diem da dc vieng tham } if (index[i] != 0) { diTimcacDinhLienThong(matrixAdj, index, i); } System.out.print("So phan lien thong :" + "\t" + stplt + "\t"); for (int j = 1; j = 0; i ) { if (matrix[tem][i] != && visited[i] == false) { stack.push(i); } } } System.out.println(Arrays.toString(result)); return result; } // implement code public abstract void addEdge(int[][] matrix, int v1, int v2); // DUYET DO THI BANG DFS public int[] BFSGraph(int[][] matrix, int v) { // implement int[] result = new int[numvext]; boolean[] visited = new boolean[numvext]; } @Override public boolean isUlerGraph() { // TODO Auto-generated method stub int sodinbacle = 0; boolean result = true; for (int i = 0; i < maxtrix.length; i++) { if (deg(i) % == && soThanphanlienthong() == 1) { return true; } else { return false; } } return result; } @Override public int count() { // TODO Auto-generated method stub int dem = 0; for (int i = 0; i < numvext; i++) { if (deg(i) % == 0) { dem++; } } return dem; } // dem tong bac = public int count1(int[][] matrix) { int count1 = 0; for (int i = 0; i < numvext; i++) { if (deg(i) == 1) { count1++; } } return count1; } // dem tong ba bang public int count2(int[][] matrix) { int count2 = 0; for (int i = 0; i < numvext; i++) { if (deg(i) == 2) { count2++; } } return count2; } // dem bac nho hon hoac bang canh /2 public int countless(int[][] matrix) { int countless = 0; for (int i = 0; i < numvext; i++) { if (deg(i) numvext) { if (stop == false) { System.out.println(" co chu trinh am"); return; } stop = true; } } for (int i = 0; i < numvext; i++) { printpath(P, L, startV, i); } } private void printpath(int[] path, int[] l, int i, int v) { // TODO Auto-generated method stub int j = v; System.out.print("Dinh " + j + ", Khoang cach" + l[j] + "\n" + j); while (j != i) { System.out.print("< -" + path[j]); j = path[j]; } System.out.println(); } @Override public void algoFloyd1(int[][] matrix, int startV) { // TODO Auto-generated method stub } @Override public int deg(int v) { // TODO Auto-generated method stub int count = 0; // implement code for (int i = 0; i < numvext; i++) { if (maxtrix[v][i] > 0) { count += maxtrix[v][i]; } } return count; } @Override public int sumDeg(int v) { // TODO Auto-generated method stub int count = 0; for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { if (maxtrix[i][j] > 0) { count += maxtrix[i][j]; } } } return count; } @Override public void algoWarShall(int[][] matrix, int startV) { // TODO Auto-generated method stub } @Override public boolean isHalfEulerGraph(int[][] matrix) { // TODO Auto-generated method stub return false; } } ... return false; } // TIM CAY BAO TRUM NHO NHAT KRUSKAL đồ thị có hướng public int[][] SpanningTreeByKruskal(int[][] matrix) { // implement code int total = 0; int nofedg = 0; int[][] tree = new... removeEdge(int[][] matrix, int v1, int v2); // implement code // KIEM TRA DO THI CO LIEN THONG public boolean isConnected(int[][] matrix) { // implement code int[] result = new int[numvext]; boolean[]... @Override public String toString() { return "Edges [x=" + x + ", y=" + y + ", w=" + w + "]"; } } ĐỒ THỊ CÓ HƯỚNG import java.util.ArrayList; import java.util.Iterator; import java.util.Stack; public