前言
在日常的业务性开发中,用到算法,记录一下
一、需求
有一张表,存储两个节点之间的关系数据,比如有三个字段,id、source、target
一条记录代表两个节点之间存在的直接关系。在这样一个场景下,要求给出一个节点,能找出所有关系中与该节点有直接或间接关系的节点。
例如,所有的节点关系可构造成下图一样的图结构,给出节点Node 0 , 可找出所有与Node 0有直接或间接关系的节点(所有蓝色节点)。找不到 Node 11、Node 12(绿色节点)。
二、代码实现
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* 根据数据构造一个图,遍历图中所有节点.
*
* @author linzp
* @date 2021/11/12
*/
@Slf4j
public class MapSearchDemo {
private static AtomicInteger count = new AtomicInteger(0);
public static void main(String[] args) {
testMap();
}
/**
* 扫描算法
*/
private static void testMap() {
// 定义中心节点
List<Integer> centerNodeList = new ArrayList<>();
centerNodeList.add(1);
centerNodeList.add(9);
centerNodeList.add(5);
// 所有已访问模型
List<Integer> visited = new ArrayList<>();
// 构造数据
List<MapNodeRelation> relations = createData();
// 找出所有与 1 有直接或间接关系的节点
for (int i = 0; i < centerNodeList.size(); i++) {
findNode(centerNodeList.get(i), visited, relations);
}
log.warn("已访问的节点为:{}", visited);
log.warn("总遍历次数:{}", count.get());
}
private static void findNode(Integer currentNode, List<Integer> visited, List<MapNodeRelation> relations) {
count.incrementAndGet();
// 如果已访问过,停止
if (visited.contains(currentNode)) {
return;
}
visited.add(currentNode);
// 找出所有与当前节点有直接或间接关系的节点
List<Integer> nextNodes = relations.stream()
.filter(item -> item.getSource().equals(currentNode) || item.getTarget().equals(currentNode))
.map(item -> {
if (item.getSource().equals(currentNode)) {
return item.getTarget();
}
return item.getSource();
})
.distinct()
.filter(item -> !visited.contains(item))
.collect(Collectors.toList());
// 找不到了,停止
if (CollectionUtils.isEmpty(nextNodes)) {
return;
}
// 继续寻找下一批节点
nextNodes.forEach(item -> findNode(item, visited, relations));
}
/**
* 场景1
* 1~7均为同级模型,其中,1为中心模型,要求找出所有与中心模型有直接或间接关系的模型,修改其业务状态
*
* @return
*/
private static List<MapNodeRelation> createData() {
// 构造待处理的所有节点关系
List<MapNodeRelation> allData = new ArrayList<>();
allData.add(new MapNodeRelation(8, 9));
allData.add(new MapNodeRelation(8, 13));
allData.add(new MapNodeRelation(9, 10));
allData.add(new MapNodeRelation(10, 11));
allData.add(new MapNodeRelation(11, 9));
allData.add(new MapNodeRelation(11, 12));
allData.add(new MapNodeRelation(1, 2));
allData.add(new MapNodeRelation(5, 2));
allData.add(new MapNodeRelation(2, 3));
allData.add(new MapNodeRelation(2, 4));
allData.add(new MapNodeRelation(6, 7));
allData.add(new MapNodeRelation(4, 8));
return allData;
}
}
class MapNodeRelation {
private Integer source;
private Integer target;
public MapNodeRelation() {
}
public MapNodeRelation(Integer source, Integer target) {
this.source = source;
this.target = target;
}
public Integer getSource() {
return source;
}
public void setSource(Integer source) {
this.source = source;
}
public Integer getTarget() {
return target;
}
public void setTarget(Integer target) {
this.target = target;
}
}
三、总结
码字不易,有帮助的话点个赞啪~