图论算法的矩阵写法
BFS_m
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int start = sc.nextInt();
int matrix[][] = new int[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
matrix[i][j] = sc.nextInt();
}
}
bfs_m(matrix,start);
}
public static void bfs_m(int[][] matrix,int start){
Queue<Integer> queue = new ArrayDeque<>();
HashSet<Integer> set = new HashSet<>();
queue.add(start);
set.add(start);
while (!queue.isEmpty()){
int n = queue.poll();
System.out.println(n);
for (int i=0;i<matrix[0].length;i++){
if (matrix[n][i]>0&&!set.contains(i)){
queue.add(i);
set.add(i);
}
}
}
}```
---------------------------------------------------------------------------------------------------------------------
DFS
```java
public class DFS_m {
static HashSet<Integer> set = new HashSet<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int start = sc.nextInt();
int matrix[][] = new int[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
matrix[i][j] = sc.nextInt();
}
}
dfs_m(matrix,start);
}
public static void dfs_m(int[][] matrix,int start){
System.out.println(start);
set.add(start);
for (int i=0;i<matrix.length;i++){
if (matrix[start][i]>0&&!set.contains(i)){
dfs_m(matrix,i);
}
}
}
}```
-----------------------------------------------------------------
PRIM
```java
public class Prim_m {
public static void main(String[] args) {
int m = Integer.MAX_VALUE;
int[][] matrix = new int[][]{
{m, 5, 1, 2, m},
{5, m, 3, m, 4},
{1, 3, m, 6, 2},
{2, m, 6, m, 3},
{m, 4, 2, 3, m}
};
prim_m(matrix, 0);
}
public static void prim_m(int[][] matrix, int start) {
int[] lowcost = new int[matrix.length];
int[] visited = new int[matrix.length];
int i = 0, j = 0, k = 0, min = Integer.MAX_VALUE;
int v = start;
for (i = 0; i < matrix.length; i++) {
lowcost[i] = matrix[start][i];
visited[i] = 0;
}
visited[v] = 1;
int sum = 0;
for (i = 0; i < matrix.length - 1; i++) {//执行n-1次,每次放入一条边
min = Integer.MAX_VALUE;
//选出候选边中的最小值
for (j = 0; j < matrix.length; j++) {
// System.out.print(lowcost[j]+" ");
if (visited[j] == 0 && lowcost[j] < min) {
min = lowcost[j];//记录最小值
k = j;//记录位置
}
}
visited[k] = 1;
v = k;//v变成k,更新最短边
sum += min;
for (j = 0; j < matrix.length; j++) {
if (visited[j] == 0 && matrix[v][j] < lowcost[j]) {
lowcost[j] = matrix[v][j];
}
}
}
System.out.println(sum);
}
}
Kruskal
public class Kruskal_t {
public static void main(String[] args) {
int[][] matrix = new int[][]{
{0,1,5},
{0,2,1},
{0,3,2},
{1,2,3},
{1,4,4},
{2,3,6},
{2,4,2},
{3,4,3}
};
kruskal_t(matrix);
}
public static int[] v;
public static int getRoot(int a){
while (a!=v[a]){
a = v[a];
}
return a;
}
public static void kruskal_t(int[][] matirx){
v = new int[matirx.length];
for (int i=0;i<matirx.length;i++){
v[i] = i;
}
int i=0,a=0,b=0;
int sum = 0;
Arrays.sort(matirx, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[2]-o2[2];
}
});
for (i=0;i<matirx.length;i++){
a = getRoot(matirx[i][0]);
b = getRoot(matirx[i][1]);
if (a!=b){
v[a] = b;
sum+=matirx[i][2];
}
}
System.out.println(sum);
}
}
Floyd
public class Floyd {
//邻接矩阵的解法
public static void Floyd(int[][] dis,int[][] path){
int i,j,k;
int[][] tmp = new int[dis.length][dis.length];
for (i=0;i<dis.length;i++){
for (j=0;j<dis.length;j++){
tmp[i][j] = Integer.MAX_VALUE;
if (dis[i][j]!=0){
tmp[i][j] = dis[i][j];
}
path[i][j] = -1;
}
}
for (k=0;k<dis.length;k++){
for (i=0;i<dis.length;i++){
for (j=0;j<dis.length;j++){
if (tmp[i][j]>tmp[i][k]+tmp[k][j]){
tmp[i][j] = tmp[i][k]+tmp[k][j];
path[i][j] = k;
}
}
}
}
}
public static void printPath(int u,int v,int[][] path){
if (path[u][v]==-1){
System.out.println("直接输出");
}else {
int mid = path[u][v];
printPath(u,mid,path);
printPath(mid,v,path);
}
}
}