思路
深度优先
1:访问初始顶点v,并设置为已访问
2:获取节点v的邻接节点 w
3:如果 w存在,则向下继续执行,否则结束算法
4:如果节点w没有访问过,对w进行DFS
5:获取节点v的下一个邻接节点,回到 步骤3
private void DFS(boolean[] isVisited, int v){
int w;
System.out.print(getVertexByIndex(v)+"->");
isVisited[v] = true;
w = getFirstNeighbor(v);
while(w != -1){
if(!isVisited[w]){
DFS(isVisited,w);
}
w = getNextNeighbor(v,w);
}
}
public void DFS(){
isVisited = new boolean[getNumOfVertexes()];
for (int i = 0; i < getNumOfVertexes(); i++) {
if(!isVisited[i]){
DFS(isVisited,i);
}
}
System.out.println();
}
广度优先
需要一个队列用来保持访问的顺序
1:访问初始节点v,并将v设置为已访问
2:将v加入队列
3:如果队列不为空,向下执行,否则结束算法
4:获取队头元素u
5:获取u邻接节点w
6:如果w为空,则返回步骤3,如果不为空则进行以下操作
6.1:访问w,并将w设置为已访问
6.2:将v入队
6.3:获取v的下一个邻接节点,返回 步骤6
private void BFS(boolean[] isVisited,int v){
int u;
int w;
LinkedList<Integer> queue = new LinkedList<>();
System.out.print(getVertexByIndex(v)+"=>\t");
isVisited[v] = true;
queue.addLast(v);
while(!queue.isEmpty()){
u = queue.removeFirst();
w = getFirstNeighbor(u);
while (w != -1){
if(!isVisited[w]){
System.out.print(getVertexByIndex(w)+"=>\t");
isVisited[w] = true;
queue.addLast(w);
w = getNextNeighbor(u,w);
}
w = getNextNeighbor(u,w);
}
}
}
public void BFS(){
isVisited = new boolean[getNumOfVertexes()];
for (int i = 0; i < getNumOfVertexes(); i++) {
if(!isVisited[i]){
BFS(isVisited,i);
}
}
System.out.println();
}
测试代码
class Graph{
private ArrayList<String> vertexes;
private int[][] edges;
private int numOfEdges;
private boolean[] isVisited ;
public Graph(int n) {
this.vertexes = new ArrayList<String>();
edges = new int[n][n];
numOfEdges = 0;
}
public void insertEdge(int v1,int v2, int weight){
edges[v1][v2] = weight;
edges[v2][v1] = weight;
numOfEdges++;
}
public void insertVertex(String vertex){
vertexes.add(vertex);
}
public int getNumOfVertexes(){
return vertexes.size();
}
public int getNumOfEdges() {
return numOfEdges;
}
public String getVertexByIndex(int index){
return vertexes.get(index);
}
public int getWeight(int v1,int v2){
return edges[v1][v2];
}
public void showGraph(){
for (int[] row: edges) {
for (int edg: row) {
System.out.printf("%d\t",edg);
}
System.out.println();
}
}
public int getFirstNeighbor(int v){
for (int i = 0; i < numOfEdges; i++) {
if(edges[v][i] != 0){
return i;
}
}
return -1;
}
public int getNextNeighbor(int v1,int v2){
for (int i = v2 + 1; i < numOfEdges; i++) {
if(edges[v1][i] != 0){
return i;
}
}
return -1;
}
private void DFS(boolean[] isVisited, int v){
int w;
System.out.print(getVertexByIndex(v)+"->");
isVisited[v] = true;
w = getFirstNeighbor(v);
while(w != -1){
if(!isVisited[w]){
DFS(isVisited,w);
}
w = getNextNeighbor(v,w);
}
}
public void DFS(){
isVisited = new boolean[getNumOfVertexes()];
for (int i = 0; i < getNumOfVertexes(); i++) {
if(!isVisited[i]){
DFS(isVisited,i);
}
}
System.out.println();
}
private void BFS(boolean[] isVisited,int v){
int u;
int w;
LinkedList<Integer> queue = new LinkedList<>();
System.out.print(getVertexByIndex(v)+"=>\t");
isVisited[v] = true;
queue.addLast(v);
while(!queue.isEmpty()){
u = queue.removeFirst();
w = getFirstNeighbor(u);
while (w != -1){
if(!isVisited[w]){
System.out.print(getVertexByIndex(w)+"=>\t");
isVisited[w] = true;
queue.addLast(w);
w = getNextNeighbor(u,w);
}
w = getNextNeighbor(u,w);
}
}
}
public void BFS(){
isVisited = new boolean[getNumOfVertexes()];
for (int i = 0; i < getNumOfVertexes(); i++) {
if(!isVisited[i]){
BFS(isVisited,i);
}
}
System.out.println();
}
}
|