logo资料库

实验4 DV LS路由算法编程实验报告(含代码).pdf

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
实验 4【DV、LS 路由算法编程】 一、 实验目的 熟悉并掌握的 DV、LS 路由算法的实现过程。 二、 实验环境 Java 开发环境(Win7+Eclipse) 三、 实验要求 1. 在 500*500 的场景中,随机生成 N(可设置)个节点(节点坐标 x, y);考虑节点的信号 覆盖半径为 R(可设置);如果两个节点之间的欧氏距离小于等于 R,则认为这两个节点可 以直接通信(有一条直接相连的边),生成网络拓扑图。 2. 在生成的网络拓扑图里,设定源节点和目的节点。 3. 利用距离矢量路由算法(DV)和链路状态路由算法(LS),在源节点和目的节点之间找 到一条合适的路由并显示。 四、 实验报告内容 1、实验代码: Main.java: package com.test; import java.awt.Color; import java.awt.Label; import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.Border; @SuppressWarnings("unused") public class Main { private Random rnd = new Random();
public static final int n = 50; private static int radius=200; private static ArrayList points= new ArrayList(); private static Window window=new Window(); private static final double max=10000; private static int startNode=0; private static int endNode=36; private static ArrayListpathNode=new ArrayList(); public static void main(String[] args) { JFrame frame=new JFrame("路由算法"); initPanel(window); frame.setContentPane(window); initFrame(frame); random(); connect(); //初始化矩阵 double [][]martix=new double[n][n]; for(int i=0;i
//通过算法给出的 path 倒推得出原始路径 private static boolean findRoad(int[]path,int start,int end) { int n=end; while(path[n]!=start&&path[n]!=-1) { pathNode.add(n); n=path[n]; } if(path[n]==start) { pathNode.add(n); pathNode.add(start); return true; } return false; } private static void random() { Random random = new Random(); int x,y; boolean flag=false;; for(int i=0;i
window.add(label); window.repaint(); } } private static void connect() { Point pointI,pointJ; for(int i=0;i
import javax.swing.JPanel; import javax.swing.JScrollPane; @SuppressWarnings({ "unused", "serial" }) public class Window extends JPanel{ ArrayList lineList=new ArrayList (); public void addLine(Line line) { lineList.add(line); } public void paint(Graphics g) { super.paint(g); for(int i=0;i eages=new ArrayList(); } Line.java: package com.test; //得到线的格式 import java.awt.Color;
import java.awt.Graphics; public class Line { private Color color; private int x1, x2, y1, y2 ; public Line(int x1, int y1, int x2, int y2, Color color) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.color = color; } public void draw(Graphics g) { g.setColor(color); g.drawLine(x1, y1, x2, y2); } } Eage.java: package com.test; import com.test.Point; //边,只记录目标节点的位置和权值 public class Eage{ public Eage(Point pointJ,double weight) { targetPoint=pointJ; this.weight=weight; } Point targetPoint; double weight;//该边的权重 } DV.java: package com.test; import java.util.ArrayList; //DV 算法对周围节点进行信息交换和更新 public class DV { public static Router[]R=new Router[Main.n]; int start; int end; public DV() { } public ArrayListdv(ArrayListpoints,double[][]martix,int start,int
end) { for(int i=0;ipoints_2=new ArrayList(); int i=1; while(R[start].cost[end]<10000&&start!=end) { if(i==1) { Point p1 = new Point(R[start].x,R[start].y); Point p2 = new
Point(R[R[start].next[end]].x,R[R[start].next[end]].y); points_2.add(p1); points_2.add(p2); i++; }else { Point p3 = new Point(R[R[start].next[end]].x,R[R[start].next[end]].y); points_2.add(p3); } start=R[start].next[end]; } return points_2; } } class Router{ int index; int x; int y; boolean[] neighbour=new boolean[Main.n]; double[] cost=new double[Main.n]; int[] next=new int[Main.n]; public Router(){ for(int i=0;ithis.cost[k]+r.cost[i]){ this.cost[i]=this.cost[k]+r.cost[i]; this.next[i]=k; if(this.neighbour[i]==false){ flag=true; } } } return flag; } public boolean changeAll(Router[] router){
分享到:
收藏