自從步入碼農的行列,這種基礎性的東西就敲得少了,今天和經理討論算法的時候發現自己有些忘得差不多了,今天特地回來複習複習。於是費了自己一個小時時間,從度娘複習了原理,然後自己摸索着重新實現了一下這個算法。
鄰接矩陣:
package com.zhangyanujie.graph.adjacency_matrix;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Author zyj
* @Date 2020/3/27 21:58
**/
public class BFS {
private int[][] matrix;
private char[] vertex;
public BFS(int[][] matrix,char[] vertex){
this.matrix = matrix;
this.vertex = vertex;
}
public List<Character> doBFS(char begin){
List<Character> result = new LinkedList<>();
// 找到給定字符的位置
int beginPosition = -1;
for(int i=0;i<vertex.length;i++){
if(vertex[i] == begin){
beginPosition = i;
break;
}
}
// 用於記錄結點是否被訪問過
boolean[] visited = new boolean[vertex.length];
// 首結點入隊
Queue<Integer> queue = new LinkedList<>();
queue.add(beginPosition);
result.add(vertex[beginPosition]);
visited[beginPosition] = true;
// 廣度優先遍歷,當隊列不爲空時,表示沒有遍歷完畢
int currentRow;
while (!queue.isEmpty()){
currentRow = queue.poll();
for(int i=0;i<vertex.length;i++){
if(matrix[currentRow][i] == 1 && !visited[i]){
queue.add(i);
result.add(vertex[i]);
visited[i] = true;
}
}
}
return result;
}
public void print(){
System.err.println(Arrays.deepToString(matrix));
System.err.println(Arrays.toString(vertex));
}
public static void main(String[] args){
int[][] matrix = {
{0,1,1,0,0,0,0,0},
{1,0,0,1,1,0,0,0},
{1,0,0,0,0,1,1,0},
{0,1,0,0,0,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,1,0,0,0,1,0},
{0,0,1,0,0,1,0,0},
{0,0,0,1,1,0,0,0},
};
char[] vertex = {'A','B','C','D','E','F','G','H'};
BFS bfs = new BFS(matrix,vertex);
//bfs.print();
System.err.println(bfs.doBFS('C'));
}
}
鄰接表:
package com.zhangyanujie.graph.adjacency_list;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Author zyj
* @Date 2020/3/27 22:36
**/
public class BFS {
Node[] list;
public BFS(Node[] list){
this.list = list;
}
public List<Character> doBFS(char begin){
List<Character> result = new LinkedList<>();
// 找到給定字符的位置
int beginPosition = findIndexByValueFromList(begin);
// 用於記錄結點是否被訪問過
boolean[] visited = new boolean[list.length];
// 首結點入隊
Queue<Integer> queue = new LinkedList<>();
queue.add(beginPosition);
result.add(list[beginPosition].value);
visited[beginPosition] = true;
// 廣度優先遍歷,當隊列不爲空時,表示沒有遍歷完畢
Node currentNode;
int nodePosition;
while (!queue.isEmpty()){
currentNode = list[queue.poll()];
while (currentNode.next != null){
nodePosition = findIndexByValueFromList(currentNode.next.value);
if(!visited[nodePosition]){
queue.add(nodePosition);
result.add(list[nodePosition].value);
visited[nodePosition] = true;
}
currentNode = currentNode.next;
}
}
return result;
}
private Integer findIndexByValueFromList(Character value){
int location = -1;
for(int i=0;i<list.length;i++){
if(list[i].value == value){
location = i;
break;
}
}
return location;
}
public void print(){
System.err.println(Arrays.toString(list));
}
public static void main(String[] args){
Node[] list = new Node[]{
new Node('A',new Node('B',new Node('C',null))),
new Node('B',new Node('A',new Node('D',new Node('E',null)))),
new Node('C',new Node('A',new Node('F',new Node('G',null)))),
new Node('D',new Node('B',new Node('H',null))),
new Node('E',new Node('B',new Node('H',null))),
new Node('F',new Node('C',new Node('G',null))),
new Node('G',new Node('C',new Node('F',null))),
new Node('H',new Node('D',new Node('E',null))),
};
BFS bfs = new BFS(list);
System.err.println(bfs.doBFS('C'));
//bfs.print();
}
}
class Node{
public Character value;
public Node next;
public Node(Character value, Node next) {
this.value = value;
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + next +
'}';
}
}