自从步入码农的行列,这种基础性的东西就敲得少了,今天和经理讨论算法的时候发现自己有些忘得差不多了,今天特地回来复习复习。于是费了自己一个小时时间,从度娘复习了原理,然后自己摸索着重新实现了一下这个算法。
邻接矩阵:
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 +
'}';
}
}