Link-state routing algorithm simulation

课设要求实现使用dijkstra的路由算法。 


1. Dijkstra.java


package com.liu.hw3;

import java.util.ArrayList;

public class Dijkstra {
	int NumOfNodes;
	int routeTable[];
	int shortestPath[];
	int preTable[];
	public Dijkstra(int [][]matrix) {
		this.NumOfNodes = matrix.length;
		this.routeTable  = new int [matrix.length];
		this.shortestPath = new int[matrix.length];
		this.preTable = new int[matrix.length];
	}

	public int[] getshortestDistance(int [][] matrix, int src){
		
		//init 
		int[] visited = new int[NumOfNodes];
		routeTable[src] = -2;
		shortestPath[src] = 0;
		visited[src] = 1;
		preTable[src] = -1;
		
		//if adjacent with src node, update pre table info
		for(int i=0;i<NumOfNodes;i++){
			if(matrix[src][i] != 99999){
				preTable[i] = src;
			}
		}
		
		//for loop   循环的次数??
		for(int count = 1;count <= NumOfNodes-1;count++){
			int k = -1;
			int dmin = Integer.MAX_VALUE;
			for(int i=0;i<NumOfNodes;i++){
				if(visited[i] ==0 && matrix[src][i]<dmin){
					dmin = matrix[src][i];
					k = i;
				}
			}
			
			shortestPath[k] = dmin;
			visited[k] = 1;
			
			//update
			for(int i=0;i<NumOfNodes;i++){
				if(visited[i] ==0 && matrix[src][k]+matrix[k][i]<matrix[src][i]){
					matrix[src][i] = matrix[src][k]+matrix[k][i];
					//update pre info
					preTable[i] = k;
				}
			}
		}
		
//		System.out.println("print preTable:");
//		for(int i=0;i< NumOfNodes;i++){
//			//System.out.println(preTable[i]+1);
//		}
		return shortestPath;
	}
	
	public int[] generateRoutingTable(int src){
		for(int i=0;i< NumOfNodes;i++){
			if(i == src){
				routeTable[src] = -1;
				//System.out.println("start node");
			}else
			 if(preTable[i] == src){
				routeTable[i] = i;
			}else{
				int pointer = preTable[i];
				while(preTable[pointer]!=src){
					pointer = preTable[pointer];
				}
				routeTable[i] = pointer;
				//System.out.println("pre is "+ pointer);
				
				
			}
		}
		return routeTable;
	}
	public int[] getPreNodes(int src,int dest){
		//reverse sequence
		ArrayList<Integer> res = new ArrayList<Integer>();
		res.add((dest-1));
		int point = preTable[dest-1];
		while(point != (src-1)){
			res.add(point);
			point = preTable[point];
		}
		res.add((src-1));
		int size = res.size();
		int tmp[] = new int[size];
		int count = 1;
//		System.out.println("arrayList:");
		for(int item : res){
			tmp[size-count] = (item+1); // add one because of index start at 0
			//System.out.print(" "+item);
			count++;
		}
//		System.out.println("int tmp:");
//		for(int i=0;i<tmp.length;i++){
//			System.out.print(" "+tmp[i]);
//		}
		return tmp;
		
		
	}

}



2. Menu.java

package com.liu.hw3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Menu {
	private Dijkstra dij ;
	private ReadTXT readTxt = new ReadTXT();
	int [][] path;
	int curnode;
	int numberOfNodes;
	
	
	public void display(){
		//use while loop to show info
		Scanner input = new Scanner(System.in);
		System.out.println("CS542 Link State Routing Simulator");
		while(true){
			System.out.println("(1) Create a Network Topology");
			System.out.println("(2) Build a Connection Table");
			System.out.println("(3) Shortest Path to Destination Router");
			System.out.println("(4) Modify a topology");
			System.out.println("(5) Exit");
			System.out.println("(6) display all routing table");
			System.out.println();
			System.out.print("Command:");
			
			String inputStr = input.next();
			if(inputStr.equals("1")){
				//System.out.println("press 1");
				 path = readTxt.readTxtFile("/Users/liuchang/Desktop/route5.txt");
				System.out.println("Review original topology matrix:");
				numberOfNodes = path.length;
				for (int i=0;i<path.length;i++){
					for(int j=0;j<path[0].length;j++){
						System.out.print(path[i][j]+"\t ");
					}
					System.out.println();
				}
				pause();
				
			}else if(inputStr.equals("2")){
				dij = new Dijkstra(path);
				
				System.out.println(" Select a source router:");
				int routeNum = input.nextInt();
				curnode = routeNum;
				// validation of route number
				int[] shortestPath = dij.getshortestDistance(path, routeNum-1);
				
				int [] routingTalbe = dij.generateRoutingTable(routeNum-1);
				
				
				for(int i=0;i<routingTalbe.length;i++ ){
					if(routingTalbe[i] == -1){
						System.out.println((i+1)+"\t "+"-\t");
					}else
					System.out.println((i+1)+"\t "+(routingTalbe[i]+1)+"\t");
				}
				pause();
			}else if(inputStr.equals("3")){
				System.out.println("Select the destination router:");
				int dest = input.nextInt();
				//
				System.out.println("The shortest path from router "+curnode+" to router "+dest+" is :");
				int[] preNodes = dij.getPreNodes(curnode, dest);
				for(int i=0;i<preNodes.length;i++){
					System.out.print(preNodes[i]+ " ");
				}
				System.out.println();
				int[] shortestPath = dij.getshortestDistance(path, (curnode-1));
				System.out.println("the shortest distance is :"+shortestPath[dest-1]);
				pause();
				
				
				
			}else if(inputStr.equals("4")){
				System.out.println("Select a router to be removed:");
				int route = input.nextInt();
				//copy to new array
				int [][] newpath = new int [numberOfNodes-1][numberOfNodes-1];
				numberOfNodes--;
				
				
				//left up
				for(int i=0;i<(route-1);i++){
					for(int j=0;j<(route-1);j++){
						newpath[i][j] = path[i][j];
					}
					
				}
				//left down 
				for(int i=(route-1);i<newpath.length;i++){
					for(int j=0;j<(route-1);j++){
						newpath[i][j] = path[i+1][j];
					}
					
				}
				//right up
				for(int i=0;i<(route-1);i++){
					for(int j=(route-1);j<newpath.length;j++){
						newpath[i][j] = path[i][j+1];
					}
					
				}
				//right down
				for(int i=(route-1);i<newpath.length;i++){
					for(int j=(route-1);j<newpath.length;j++){
						newpath[i][j] = path[i+1][j+1];
					}
					
				}
				
				path = newpath;
				System.out.println("updated new path:");
				for (int i=0;i<path.length;i++){
					for(int j=0;j<path[0].length;j++){
						System.out.print(path[i][j]+"\t ");
					}
					System.out.println();
				}
				
				pause();
			}else if(inputStr.equals("5")){
				System.out.println("Exit CS542 project. Good Bye!");
				break;
			}else if(inputStr.equals("6")){
				System.out.println("show all routing table:");
				System.out.println("number of nodes:"+numberOfNodes);
				int [][] allroutingtable = new int[numberOfNodes][numberOfNodes];
				dij = new Dijkstra(path);
				for(int j = 0;j<numberOfNodes;j++){
					dij.getshortestDistance(path, j);
					int [] routingTalbe = dij.generateRoutingTable(j);
					for(int i=0;i<routingTalbe.length;i++ ){
						
							allroutingtable[j][i] = routingTalbe[i];
						
					}
				}
				
				//display:
				System.out.print("\t");
				for(int i=0;i<numberOfNodes;i++)
				System.out.print((i+1)+"\t");
				System.out.println();
				for(int i=0;i<allroutingtable.length;i++){
					System.out.print((i+1)+":\t");
					for(int j=0;j< allroutingtable.length;j++){
						if(allroutingtable[i][j] == -1){
							System.out.print("-\t");
						}else
						System.out.print((allroutingtable[i][j]+1)+"\t");
					}
					System.out.println();
				}
				pause();
				
			}
			else {
				System.out.println("wrong input try again");

			}
			
			
			
		}
		
	}
	public void pause(){
		System.out.println("press any botton");
		Scanner input = new Scanner(System.in);
		input.nextLine();
		
		
	}

}


3. ReadTXT.java

package com.liu.hw3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ReadTXT {
	public int  getNumOfNodes(String file_path){
		try {
			File file = new File(file_path);
			if (file.isFile() && file.exists()) {
				InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
				BufferedReader bufferreader = new BufferedReader(reader);
				String line = bufferreader.readLine();
				String [] col = line.split(" ");
				return col.length;
			} else
				System.out.println("Can't find the file!");
			    return -1;
			
			}catch(Exception e){
				System.out.println("Error");
				return -1;
			}
	}
	public int[][] readTxtFile(String file_path) {
		String line = null;
		int numberOfNodes = getNumOfNodes(file_path);
		int [][] pathMatrix = new int [numberOfNodes][numberOfNodes];
		try {
			File file = new File(file_path);
			if (file.isFile() && file.exists()) {
				InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
				BufferedReader bufferreader = new BufferedReader(reader);
				int i=0;
				while ((line = bufferreader.readLine()) != null) {
					//System.out.println(line);
					//split for each row
					String [] col = line.split(" ");
					for(int j=0;j<col.length;j++){
						if(!col[j].equals("-1")){
							pathMatrix[i][j] = Integer.parseInt(col[j]);
						}else{
							pathMatrix[i][j] = 99999;//max
						}
						
					}
					i++;
				}	
				reader.close();
				return pathMatrix;
			} else
				System.out.println("Can't find the file!");

		} catch (Exception e) {
			System.out.println("Error");
			e.printStackTrace();
		}
		return null;
	}
	


	

}

4. TestMenu.java

package com.liu.hw3;

public class TestMenu {
	public static void main(String args[]){
		Menu menu = new Menu();
		menu.display();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值