图的两种表示方式,邻接表(点及点对应一级节点链表),邻接矩阵(多少个点即多少数组长度,二维)
图的广度搜索(层层递进,最近距离,利用队列收集每一层的节点,遍历下一层节点),
深度搜索(递归获取直属节点,终极节点为终结条件)
public class TableGraph{
int pointCount;
java.util.ArrayList<Integer> childrenList[];
public TableGraph(int i){
pointCount = i;
this.childrenList=new java.util.ArrayList[pointCount];
for(int j=0;j<i;j++){
this.childrenList[j] = new java.util.ArrayList();
}
}
public void bfs(int s, int e){
if(s == e) return;
boolean[] visited = new boolean[this.pointCount];
visited[s] = true;
java.util.Queue<Integer> queue = new java.util.LinkedList();
queue.add(s);
int prev[] = new int[this.pointCount];
for(int i=0;i<this.pointCount;++i){
prev[i] = -1;
}
while(queue.size()!=0){
int w = queue.poll();
for(int l=0;l<this.childrenList[w].size();l++){
int q = this.childrenList[w].get(l);
if(!visited[q]){
prev[q]=w;
if(q == e){
System.out.println("找到了");
return;
}
visited[q] = true;
queue.add(q);
}
}
}
}
}
public class MatrixGraph{
int graghArray[][];
public MatrixGraph(int i){
this.graghArray = new int[i][i];
for(int j=0;j<i;j++){
for(int k=0;k<i;k++){
this.graghArray[j][i] = 0;
}
}
}
}