邻接矩阵实现图的广搜和深搜(java模板)

经常要用到,放到这里备用!!
//邻接矩阵实现图的广搜和深搜
import java.util.*;

public class Graph {

private int[][] G;//邻接矩阵
private int k;//顶点数目
private boolean[] visited;//判断顶点是否被访问过

public Graph(int k,int[][] G){
this.k=k;
this.G=G;
visited = new boolean[k];
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k;
k = sc.nextInt(); //顶点数(顶点为0,1,2,...k-1)
int m=sc.nextInt();//边数

//构建邻接矩阵
int[][] G = new int[k][k];
for (int i = 0; i <m; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
G[u][v] = 1;
G[v][u] = 1;//无向图的对称
}
Graph g=new Graph(k,G);
g.bfs(0);
// g.dfs(0);
System.out.println();
}

//广搜
private void bfs(int v) {
//队列用来保存被访问节点的分支节点
Queue<Integer> que = new LinkedList<Integer>();
que.offer(v);
while (!que.isEmpty()) {
v = que.poll();
System.out.print(v+" ");
visited[v] = true;
//将被访问节点的分支节点加入到队列中
for (int i = 0; i < k; i++) {
if (G[v][i] == 1 && !visited[i]) {
que.add(i);
visited[i] = true;
}
}
}
}

//深搜
private void dfs(int v) {
visited[v] = true;
System.out.print(v+" ");
for (int i = 0; i <k; i++) {
//递归调用搜索没有被访问过的当前节点的下一个节点(邻接点)
if (G[v][i] == 1 && !visited[i])
dfs(i);
}
}

}

[img]http://dl.iteye.com/upload/attachment/0077/7578/8f868200-af7b-3c05-8c74-edf687f169f1.gif[/img]
C:\java>java Graph
6
10
0 1
0 2
0 3
1 2
1 4
2 4
2 5
2 3
3 5
4 5
0 1 2 3 4 5

源码:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接矩阵邻接表是两种常用的的表示方法。下面是Java代码实现邻接矩阵邻接表的示例: 1. 邻接矩阵表示Java代码实现: ```java import java.util.Arrays; class Graph { private int[][] adjMatrix; private int numVertices; public Graph(int numVertices) { this.numVertices = numVertices; this.adjMatrix = new int[numVertices][numVertices]; } public void addEdge(int src, int dest) { // 无向,所以需要在两个位置都设置为1 adjMatrix[src][dest] = 1; adjMatrix[dest][src] = 1; } public void printGraph() { for (int i = 0; i < numVertices; i++) { System.out.print("Vertex " + i + " is connected to: "); for (int j = 0; j < numVertices; j++) { if (adjMatrix[i][j] == 1) { System.out.print(j + " "); } } System.out.println(); } } } public class Main { public static void main(String[] args) { Graph graph = new Graph(5); graph.addEdge(0, 1); graph.addEdge(0, 4); graph.addEdge(1, 2); graph.addEdge(1, 3); graph.addEdge(1, 4); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.printGraph(); } } ``` 2. 邻接表表示Java代码实现: ```java import java.util.LinkedList; class Graph { private int numVertices; private LinkedList<Integer>[] adjList; public Graph(int numVertices) { this.numVertices = numVertices; this.adjList = new LinkedList[numVertices]; for (int i = 0; i < numVertices; i++) { adjList[i] = new LinkedList<>(); } } public void addEdge(int src, int dest) { adjList[src].add(dest); adjList[dest].add(src); } public void printGraph() { for (int i = 0; i < numVertices; i++) { System.out.print("Vertex " + i + " is connected to: "); for (int j : adjList[i]) { System.out.print(j + " "); } System.out.println(); } } } public class Main { public static void main(String[] args) { Graph graph = new Graph(5); graph.addEdge(0, 1); graph.addEdge(0, 4); graph.addEdge(1, 2); graph.addEdge(1, 3); graph.addEdge(1, 4); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.printGraph(); } } ``` 以上代码示例分别实现邻接矩阵邻接表表示Java代码。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值