视频链接:https://www.bilibili.com/video/BV1HQ4y1d7th
视频范围P168 - P178
1.图深度优先遍历
深度优先遍历,从初始访问结点出发,初始访问结点可能有多个邻接结点,深度优先遍历的策略就是首先访问,第一个邻接结点,然后再以这个被访问的邻接结点作为初始结点,访问它的第一个邻接结点,可以这样理解每次都在访问完当前结点后首先访问当前结点的第一个邻接结点。 图类:
package graph;
import java.util.ArrayList;
import java.util.Arrays;
public class Graph {
private ArrayList<String> vertexList;
private int[][] edges;
private int edgsNum;
private boolean[] isSelectd;
public Graph(int len) {
this.vertexList = new ArrayList<String>(len);
this.edges = new int[len][len];
this.isSelectd = new boolean[len];
}
public void insertVertex(String vertex){
this.vertexList.add(vertex);
}
public void insertEdges(int x, int y,int w){
edges[x][y] = w;
edges[y][x] = w;
edgsNum++;
}
public int getVertexSize(){
return this.vertexList.size();
}
public int getEdgesSize(){
return this.edgsNum;
}
public int getWeight(int x,int y){
return this.edges[x][y];
}
public void showList(){
for(int[] arr : edges){
System.out.println(Arrays.toString(arr));
}
}
public int getFirstVertex(int index){
for (int i = 0; i < vertexList.size(); i++) {
if (edges[index][i] > 0){
return i;
}
}
return -1;
}
public int getNextVertex(int x,int y){
for (int i = y + 1; i < vertexList.size(); i++) {
if (edges[x][i] > 0){
return i;
}
}
return -1;
}
public String getValueIndex(int i){
return this.vertexList.get(i);
}
public void dfs(boolean[] isSelectd,int i){
System.out.println(getValueIndex(i));
isSelectd[i] = true;
int w = getFirstVertex(i);
while (w != -1){
if (!isSelectd[w]){
dfs(isSelectd,w);
}
w = getNextVertex(i,w);
}
}
public void dfs(){
for (int i = 0; i < getVertexSize(); i++) {
if (! isSelectd[i]){
dfs(isSelectd,i);
}
}
}
}
测试类:
package graph;
public class Test {
public static void main(String[] args) {
Graph graph = new Graph(5);
String[] vertexs = {"A","B","C","D","E"};
for (String v : vertexs){
graph.insertVertex(v);
}
graph.insertEdges(0,1,1);
graph.insertEdges(0,2,1);
graph.insertEdges(1,2,1);
graph.insertEdges(1,3,1);
graph.insertEdges(1,4,1);
graph.insertEdges(0,2,1);
graph.showList();
graph.dfs();
}
}
运行结果:
2.图广度优先遍历
类似于一个分层搜素的过程,广度优先遍历(Breadth-First-Search)需要使用一个队列以保持访问过的结点的顺序,以便按这个顺序来访问这些结点的邻接结点
图类:
package graph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
public class Graph {
private ArrayList<String> vertexList;
private int[][] edges;
private int edgsNum;
private boolean[] isSelectd;
public Graph(int len) {
this.vertexList = new ArrayList<String>(len);
this.edges = new int[len][len];
this.isSelectd = new boolean[len];
}
public void insertVertex(String vertex){
this.vertexList.add(vertex);
}
public void insertEdges(int x, int y,int w){
edges[x][y] = w;
edges[y][x] = w;
edgsNum++;
}
public int getVertexSize(){
return this.vertexList.size();
}
public int getEdgesSize(){
return this.edgsNum;
}
public int getWeight(int x,int y){
return this.edges[x][y];
}
public void showList(){
for(int[] arr : edges){
System.out.println(Arrays.toString(arr));
}
}
public int getFirstVertex(int index){
for (int i = 0; i < vertexList.size(); i++) {
if (edges[index][i] > 0){
return i;
}
}
return -1;
}
public int getNextVertex(int x,int y){
for (int i = y + 1; i < vertexList.size(); i++) {
if (edges[x][i] > 0){
return i;
}
}
return -1;
}
public String getValueIndex(int i){
return this.vertexList.get(i);
}
public void bfs(boolean[] isSelectd,int i){
int u;
int w;
LinkedList queue = new LinkedList();
System.out.println(getValueIndex(i));
isSelectd[i] = true;
queue.addLast(i);
while (!queue.isEmpty()){
u = (Integer)queue.removeFirst();
w = getFirstVertex(u);
while (w != -1){
if (!isSelectd[w]){
System.out.println(getValueIndex(w));
isSelectd[w] = true;
queue.addLast(w);
}
w = getNextVertex(u,w);
}
}
}
public void bfs(){
for (int i = 0; i < getVertexSize(); i++) {
if (! isSelectd[i]){
bfs(isSelectd,i);
}
}
}
}
测试类:
package graph;
public class Test {
public static void main(String[] args) {
Graph graph = new Graph(5);
String[] vertexs = {"A","B","C","D","E"};
for (String v : vertexs){
graph.insertVertex(v);
}
graph.insertEdges(0,1,1);
graph.insertEdges(0,2,1);
graph.insertEdges(1,2,1);
graph.insertEdges(1,3,1);
graph.insertEdges(1,4,1);
graph.insertEdges(0,2,1);
graph.showList();
graph.bfs();
}
}
运行结果:
|