有向⽆环图的java实现(使⽤矩阵特性开发)
⼯作流如下图所⽰,要求每⼀个任务只执⾏⼀次,不重复执⾏,要求任务的所有前置任务必须完成才能往后执⾏,例如任务7必须在任务13,2,3三个任务完成之后才能执⾏,⽽任务13,2,3属于独⽴的任务,可以并发执⾏
根据多线程求得出6个路线数据
每个线程可以独⽴执⾏,所有线程相同的任务不能重复执⾏,当前任务必须在前置任务完成之后才能执⾏,
路线:[1, 2, 7, 10, 12]
路线:[1, 13, 7, 10, 12]
路线:[1, 3, 7, 10, 12]
路线:[1, 4, 8, 10, 12]
路线:[1, 4, 9, 11, 12]
路线:[1, 5, 6, 9, 11, 12]
路线不能出现回环,出现即死循环,需要提前排除
实现代码如下(JDK1.8)
数据结构类NetWork
import java.util.*;
/**
* 图解⽹络数据结构
*/
public class NetWork {
//记录两份⽹络节点,使⽤空间换时间,提升反向查询效率
//⽹络初始化,x_k,y_k
private Map<String, Map<String, String>> xMapMap;
//⽹络初始化,y_k,x_k
private Map<String, Map<String, String>> yMapMap;
//回环路径
private List<String> loopbackList;//回环地址
//节点深度路径
private List<List<String>> deepPathList;//深度路径
private Map<String, String> jobMapStatus;//map任务状态
enum TYPE {X,Y}
public NetWork() {
xMapMap = new HashMap();//X点集
yMapMap = new HashMap();//Y点集
loopbackList = new LinkedList<>();//回环地址
deepPathList = new LinkedList<>();//深度路径
}
/
**
* 初始化任务
*/
public void initJob() {
jobMapStatus = new HashMap<>();
Set<String> allPoint = getAllPoint();
allPoint.forEach(job -> jobMapStatus.put(job, "0"));//0,1 0表⽰未执⾏,1表⽰执⾏ }
/**
* 获取任务的转状态
*
* @param job
* @return
*/
public String getJobMapStatus(String job) {
String status = (job);
return status;
}
/**
* 更新任务的状态
*
* @param job 任务名称
* @param status 任务状态
*/
public void tJobMapStatus(String job, String status) {
jobMapStatus.put(job, status);
}
public List<String> getLoopbackList() {
return loopbackList;
}
public List<List<String>> getDeepPathList() {
return deepPathList;
}
/**
* ⽹络添加节点
*
* @param xPoint x位
* @param yPoint y位
* @param distance 位移
*/
*/
public void addPoint(String xPoint, String yPoint, String distance) {
if (!ainsKey(xPoint)) {//记录x的索引
xMapMap.put(xPoint, new HashMap<>());
}
<(xPoint).put(yPoint, distance);
if (!ainsKey(yPoint)) {//记录y的索引
yMapMap.put(yPoint, new HashMap<>());
}
<(yPoint).put(xPoint, distance);
}
/**
* 根据坐标获取某点
*
* @param xPoint
* @param yPoint
* @return
*/
public String getPoint(String xPoint, String yPoint) {
Map<String, String> linePoints = (xPoint);红色旅游区
String point = (yPoint);
return point;
}
public String getPoint(String point1, String point2, TYPE type) {
if (type == TYPE.X) {
Map<String, String> linePoints = (point1);
String point = (point2);
return point;
} el {
Map<String, String> linePoints = (point1);
String point = (point2);
return point;
}
}
/**
* 获取X轴的⼀列数据
*
* @param xPoint
* @return
*/
public List<Map<String, String>> getLinePoint(String xPoint) {
List<Map<String, String>> linePointList = new ArrayList<Map<String, String>>(); Map<String, String> linePoints = (xPoint);
if (linePoints != null) {
for (Map.Entry<String, String> pointUnit : Set()) {
Map<String, String> pointMap = new HashMap<String, String>();
pointMap.put("X", xPoint);
pointMap.put("Y", Key());
pointMap.put("D", Value());
linePointList.add(pointMap);
}
}
return linePointList;
}
/**
* 根据类型获取某轴的⼀列数据
*
* @param point 索引点
* @param type 类型
* @return
*/
public List<Map<String, String>> getLinePoint(String point, TYPE type) {
public List<Map<String, String>> getLinePoint(String point, TYPE type) {
List<Map<String, String>> linePointList = new ArrayList<Map<String, String>>();
if (type == TYPE.X) {
Map<String, String> linePoints = (point);
if (linePoints != null) {
for (Map.Entry<String, String> pointUnit : Set()) {
Map<String, String> pointMap = new HashMap<String, String>();
pointMap.put("X", point);
pointMap.put("Y", Key());
pointMap.put("D", Value());
菠菜烩豆腐linePointList.add(pointMap);
}
岸组词}
} el {
Map<String, String> linePoints = (point);
if (linePoints != null) {
for (Map.Entry<String, String> pointUnit : Set()) {
Map<String, String> pointMap = new HashMap<String, String>();
pointMap.put("X", Key());
pointMap.put("Y", point);
pointMap.put("D", Value());
肉包
linePointList.add(pointMap);
}
元宵和汤圆有啥区别}
}
return linePointList;
}
/**
* @param netWork ⽹络节点
* @param startPoint 起始节点
* @param pathList 当前任务的路径
*/
public void recursive(NetWork netWork, String startPoint, ArrayList<String> pathList) {
if (ains(startPoint)) {LoopbackList().String() + "->" + startPoint);return;} pathList.add(startPoint);
List<Map<String, String>> linePoint = LinePoint(startPoint, NetWork.TYPE.X);
if (linePoint.size() == 0) {
ArrayList<String> descList = new ArrayList<>(pathList.size());
pathList.forEach(path -> descList.add(path));
}
男性生殖器按摩for (Map<String, String> map : linePoint) {recursive(netWork, ("Y"), pathList);}
}
/**
* 获取所有的点,合并key
*
* @return
*/
public Set<String> getAllPoint() {
Set<String> allSet1 = xMapMap.keySet();
Set<String> allSet2 = yMapMap.keySet();
Set<String> allSet = new HashSet<>();
allSet.addAll(allSet1);
allSet.addAll(allSet2);
return allSet;下边流水怎么回事
}
/**
* 显⽰路径
*/
*/
public void show() {
int placeholder = 3;
String placeholderSrting = "";
for (int i = 0; i < placeholder; i++) {placeholderSrting = placeholderSrting + "-";}
Set<String> allSet = getAllPoint();//获取所有的点,⽤于遍历
System.out.print(String.format("%-" + placeholder + "s", ""));
System.out.print(" ");
for (String x : allSet) {
System.out.print(String.format("%-" + placeholder + "s", x));
}
System.out.println();
System.out.print(String.format("%-" + placeholder + "s", "X\\Y"));
System.out.print(" ");
for (String ignored : allSet) {System.out.print(placeholderSrting);}
System.out.println();
for (String x : allSet) {
System.out.print(String.format("%-" + placeholder + "s|", x));
for (String y : allSet) {
Map<String, String> linePoints = (x);
String point = "0";
if (linePoints != null && (y) != null) {
point = (y);
}
System.out.print(String.format("%-" + placeholder + "s", point));
}
System.out.println();
}
}
}
测试类TestMain
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import urrent.ConcurrentHashMap;
import urrent.ConcurrentMap;
/**
* ⽹络计算
*/
public class TestMain {
作文素材人物事例public static ConcurrentMap<String, List<String>> println = new ConcurrentHashMap<>(); public static List<String> strings = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
NetWork netWork = new NetWork();
netWork.addPoint("1", "2", "1");
netWork.addPoint("1", "3", "1");
netWork.addPoint("1", "4", "1");
netWork.addPoint("1", "5", "1");
netWork.addPoint("1", "13", "1");
netWork.addPoint("2", "7", "1");
netWork.addPoint("3", "7", "1");
netWork.addPoint("13", "7", "1");
netWork.addPoint("4", "8", "1");
netWork.addPoint("4", "9", "1");
netWork.addPoint("5", "6", "1");
netWork.addPoint("6", "9", "1");
netWork.addPoint("7", "10", "1");