日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

matlab 无向拓扑图,无向图绘画树状拓扑图算法

發(fā)布時間:2023/12/2 循环神经网络 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 matlab 无向拓扑图,无向图绘画树状拓扑图算法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

基于目前Network項目需要,研究相關(guān)樹形算法

該需求難點如下:

1、目前拓撲圖是無向圖,而樹大多數(shù)都是基于有向圖來畫的,無法確定根節(jié)點

2、網(wǎng)絡(luò)拓撲中存在回環(huán)問題,導(dǎo)致鏈路可能會存在重疊問題

針對問題1,目前根據(jù)所有節(jié)點的連通度來計算得出連通度最大的點作為根節(jié)點。

問題2目前沒有完美的解決方案。

目前這邊demo的算法分為2種

1、根據(jù)每層葉子節(jié)點個數(shù)切割X軸坐標,然后平均分布在一條直線上。

效果圖如下:

代碼比較簡單,暫時就不公布了。

2、類似于一種遞歸的方式,從第一層葉子節(jié)點開始切割X軸,第二層葉子節(jié)點的范圍不能超過上層節(jié)點之間的節(jié)點間距。

直接看圖:

這種方式的弊端顯而易見,如果后面存在大量葉子節(jié)點的枝節(jié)點,那么這里的葉子會非常密集

改變一下根節(jié)點樣子或許會好一些,但是這邊會出現(xiàn)回路重合鏈路問題:

下面貼上部分代碼:

public ResponseEntity getTreeTopo() {

NodeList nodeList = new NodeList();

HashMap nodeCountMap = new HashMap<>();

//統(tǒng)計所有節(jié)點的連通度

for (int i = 0; i < lineTempList.size(); i++) {

if (nodeCountMap.containsKey(lineTempList.get(i).getUplinkNodeId().toString())) {

nodeCountMap.get(lineTempList.get(i).getUplinkNodeId().toString())

.setCount(nodeCountMap.get(lineTempList.get(i).getUplinkNodeId().toString()).getCount() + 1);

} else {

Connectivity connectivity = new Connectivity();

connectivity.setCount(1);

nodeCountMap.put(lineTempList.get(i).getUplinkNodeId().toString(), connectivity);

}

if (nodeCountMap.containsKey(lineTempList.get(i).getNodeId().toString())) {

nodeCountMap.get(lineTempList.get(i).getNodeId().toString())

.setCount(nodeCountMap.get(lineTempList.get(i).getNodeId().toString()).getCount() + 1);

} else {

Connectivity connectivity = new Connectivity();

connectivity.setCount(1);

nodeCountMap.put(lineTempList.get(i).getNodeId().toString(), connectivity);

}

}

//找到最大連通度的節(jié)點

int maxConnectivity = 0;

String rootNodeId = "";

for(String nodeId : nodeCountMap.keySet()) {

if(nodeCountMap.get(nodeId).getCount()>maxConnectivity) {

maxConnectivity = nodeCountMap.get(nodeId).getCount();

rootNodeId = nodeId;

}

}

int treeLevel = 1; //樹高度

Set nodeSet = new HashSet<>();//記錄所有已經(jīng)分配過坐標的節(jié)點,用于查重

Map nodePositionMap = new HashMap<>();//記錄所有節(jié)點坐標

Map subNodesCountMap = new HashMap<>();

rootNodeId="35";//手工設(shè)置根節(jié)點

nodeSet.add(rootNodeId);

NodeListInner rootNode = new NodeListInner();

rootNode.setNodeId(rootNodeId);

rootNode.setX("2000");

rootNode.setY("400");//假設(shè)畫布為4000*4000

nodeList.add(rootNode);

nodePositionMap.put(rootNodeId, rootNode);

//根節(jié)點放在(2000,400)位置

List subPoint = getSubPoint(rootNodeId, nodeSet, subNodesCountMap);

subNodesCountMap.get(rootNodeId).setSpace(3800);//兩邊各留100空間

List parentPoint = new ArrayList<>();//需要保留父節(jié)點的信息

Point rootPoint = new Point();

rootPoint.setNodeId(rootNodeId);

rootPoint.setParentId(null);

parentPoint.add(rootPoint);

while (subPoint.size() != 0) {// 如果遍歷到樹的最高一層,則結(jié)束循環(huán)

//根據(jù)父節(jié)點的位置來分配葉子節(jié)點的位置

for(int j=0;j

if(subNodesCountMap.get(parentPoint.get(j).getNodeId()).getCount()==0) {

continue;

}

int gap = subNodesCountMap.get(parentPoint.get(j).getNodeId()).getSpace() / subNodesCountMap.get(parentPoint.get(j).getNodeId()).getCount();//子節(jié)點得到點與點之間的間距距離

int rightDeviation = 0;//單數(shù)往右偏移

int leftDeviation = - gap;//雙數(shù)往左偏移

//獲得子節(jié)點數(shù)據(jù)

List point = new ArrayList<>();

point = getSubPoint(parentPoint.get(j).getNodeId(), nodeSet, subNodesCountMap);

//遍歷節(jié)點然后賦予坐標值

for(int i=0;i

if(!subNodesCountMap.containsKey(point.get(i).getNodeId())) {

NodeCount nodeCount = new NodeCount();

nodeCount.setSpace(gap);

subNodesCountMap.put(point.get(i).getNodeId(), nodeCount);

} else {

subNodesCountMap.get(point.get(i).getNodeId()).setSpace(gap);//這一個迭代的子節(jié)點是下一個迭代的父節(jié)點

}

NodeListInner node = new NodeListInner();

node.setNodeId(point.get(i).getNodeId());

if((i+1)%2==1) {//單數(shù)往右偏移

node.setX((rightDeviation+Integer.parseInt(nodePositionMap.get(parentPoint.get(j).getNodeId()).getX()))+"");

node.setY(400+treeLevel*400+"");

rightDeviation += gap;

} else {//雙數(shù)往左偏移

node.setX((leftDeviation+Integer.parseInt(nodePositionMap.get(parentPoint.get(j).getNodeId()).getX()))+"");

node.setY(400+treeLevel*400+"");

leftDeviation -= gap;

}

nodePositionMap.put(point.get(i).getNodeId(),node);

nodeList.add(node);

nodeSet.add(node.getNodeId());

}

}

parentPoint = new ArrayList<>(subPoint);

subPoint.clear();

for (int i = 0; i < parentPoint.size(); i++) {//統(tǒng)計還有沒有下一層葉子節(jié)點

subPoint.addAll(getSubPoint(parentPoint.get(i).getNodeId(), nodeSet, subNodesCountMap));

}

treeLevel+=1;

}

for (int j = 0; j < nodeList.size(); j++) {//補上節(jié)點之間的線

List lines = new ArrayList<>();

for (int i = 0; i < lineTempList.size(); i++) {

if (nodeList.get(j).getNodeId().equals(lineTempList.get(i).getUplinkNodeId().toString())) {

Line line = new Line();

line.setDest(lineTempList.get(i).getNodeId().toString());

line.setDestX(nodePositionMap.get(lineTempList.get(i).getNodeId().toString()).getX());

line.setDestY(nodePositionMap.get(lineTempList.get(i).getNodeId().toString()).getY());

lines.add(line);

}

}

nodeList.get(j).setLine(lines);

}

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

headers.add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");

headers.add("Access-Control-Allow-Origin", "*");

headers.add("Access-Control-Allow-Headers", "x-requested-with,content-type");

return new ResponseEntity(nodeList, headers, HttpStatus.OK);

}

private List getSubPoint(String nodeId, Set nodeSet, Map subNodesCountMap) {

int count = 0;

List result = new ArrayList<>();

for (int i = 0; i < lineTempList.size(); i++) {

if (lineTempList.get(i).getUplinkNodeId().toString().equals(nodeId)) {

if (!nodeSet.contains(lineTempList.get(i).getNodeId().toString())) {

// 存儲對端節(jié)點

Point point = new Point();

point.setNodeId(lineTempList.get(i).getNodeId().toString());

point.setParentId(nodeId);

result.add(point);

count++;

}

}

if (lineTempList.get(i).getNodeId().toString().equals(nodeId)) {

if (!nodeSet.contains(lineTempList.get(i).getUplinkNodeId().toString())) {

// 存儲對端節(jié)點

Point point = new Point();

point.setNodeId(lineTempList.get(i).getUplinkNodeId().toString());

point.setParentId(nodeId);

result.add(point);

count++;

}

}

}

if(!subNodesCountMap.containsKey(nodeId)) {

NodeCount nodeCount = new NodeCount();

nodeCount.setCount(count);

subNodesCountMap.put(nodeId, nodeCount);

} else {

subNodesCountMap.get(nodeId).setCount(count);

}

return result;

}

總結(jié)

以上是生活随笔為你收集整理的matlab 无向拓扑图,无向图绘画树状拓扑图算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。