ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer...

14
ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444, USA Fall 2012

Transcript of ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer...

Page 1: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

ECE 643- Design and Analysis of Computer Networks

K Shortest Paths

Dept. of Electrical and Computer Eng. George Mason University

Fairfax, VA 22030-4444, USA

Fall 2012

Page 2: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Why KSP?

• Sometimes, it is necessary to consider additional constraints that are additive to the original routing problems, such as maximum delay requirement.

• Optimization of all the additive constraints also is NP-hard.

• Additive constraints only need to be satisfied rather than optimized.

2

Page 3: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

How to KSP

• The calculation of KSP is based on the shortest path algorithm, which can be achieved through Dijkstra’s algorithm.

• Two different algorithms for KSP will be introduced here:– Recursive Enumeration Algorithm (REA) – YEN’s Algorithm

3

Page 4: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Recursive Enumeration Algorithm (REA) (1)

• Define a digraph G=<V, E>, where V is the node set and E is the edge set.

• Define N(t) as the neighbor nodes set of node v.• Define πk(v) is the kth shortest path from s to v.• It is easy to find the shortest path from node s to v

and other node, and denote them as π1(u), u V.∈

4

1 4 5

3

20 0

3 0

1 2

2

0

V={1 ,2, 3, 4, 5}E={12, 14, 13, 25, 24, 34, 45}N(4)={1, 2, 3} (5 is not 4’s neighbor)

Page 5: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Recursive Enumeration Algorithm (REA) (2)

• NextPath(v, k)1. If k=2, then initialize a set of candidates to the next shortest path

from s to v by setting C[v] ←{π1(u)v: u N(v) and π∈ 1(v)≠π1(u)v}2. If v=s and k=2, then go to 63. Let u and k’ be the node and index, respectively, such that πk-

1(v)=πk’(u)v4. If πk’+1(u) has not been computed yet, compute it by calling

NextPath(u,k’+1).5. If πk’+1(u) exists, then insert πk’+1(u)v in C[v]6. If C[v]≠empty, then select and delete the path with minimum length

from C[v] and assign it to π1(v), otherwise πk(v) does not exist.

5

Page 6: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

YEN’s Algorithm

6

1 4 5

3

20 0

3 0

1 2

2

0

1. Initial solution tree with the shortest path, i<-1 and initial a heap, cache the source node.

2. Traverse the tree for nodes has not been cached. If all nodes has been cached, go to step 8.

3. Block the corresponding nodes and edges.4. Find the shortest path from the point to the

destination. If no path can be found, goto step 6.

5. Push the found path into the heap, unblock the corresponding nodes and edges.

6. Cache the node.7. Goto step 28. If the heap is empty, exit. Otherwise, output

the shortest path in the heap, add the path into the solution tree, i<-i+1, un-cache the corresponding deviation node.

9. If i=k, exit. Otherwise, goto step 2

Page 7: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

A Sample Solution Tree

7

Page 8: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Sample Digraph

8

1 4 5

3

20 0

3 0

1 2

2

0

512,1,2,013,1,3,114,1,4,324,2,4,225,2,5,034,3,4,035,3,5,245,4,5,0

Page 9: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Project Schedule (2012)

• Part I - Generate and output the network graph (20%) (November 12th)– We need to have a program which can generate a network graph from

a input file. You also need to demonstrate the graph by printing the list of the nodes and edges.

• Part II - Shortest Path (20%) (November 19th)– Implement Dijkstra’s algorithm yourself

• Part III – K shortest Path (60%) (November 26th)– Find the K shortest paths in a network, where

• K can be any integer greater than 0. If all possible paths are exhausted before K is reached, the program should output “path exhausted” (20%)

• The path should be output in the order of length (20%)• All paths should be loopless (20%)

• Presentation on December 3rd9

Page 10: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Some Hints About How to Read File in Java

BufferedReader br = null;FileReader fr = new FileReader(fileName);br = new BufferedReader(fr);System.out.println("Reading from file.");String line = br.readLine();MyGraph g = new MyGraph(Integer.parseInt(line));line = br.readLine();while (line != null) {

String[] list = line.split(",");g.addEdge(Integer.parseInt(list[0]), Integer.parseInt(list[1]) - 1,

Integer.parseInt(list[2]) - 1, Integer.parseInt(list[3]));line = br.readLine();

}br.close();

10

Page 11: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Some Hints About How to Implement YEN’s algorithm in Java (1)

public LinkedList<Path> myKSPImpl(int src, int des, int k) {LinkedList<Path> myKSP = new LinkedList<Path>();// myKSP is the list of final solutionLinkedList<Path> heap = new LinkedList<Path>();// heap is used to store the midway resultsPath spath = myDijkstraImpl(src,des);myKSP.add(spath);// find the shortest path, and save it in myKSPTree<Integer> solTree = subTreeFromPath(spath,0);// initiate the tree structure using the shortest pathint iCount = 1;

11

Page 12: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Some Hints About How to Implement YEN’s algorithm in Java (2)

while (iCount < k) {List<Node<Integer>> l = solTree.toList();for (Node<Integer> node : l) {// go through the solution tree

if(node.data==des)continue;

if (!node.isCached) { // if the node in the tree hasn't been checked, // block its parent, grandparent, grand-grandparent and so on. // Also block the links between itself with its children.// find the shortest path from the node to the destination// under the blocking condition

Path temp = myDijkstraImpl(node.data, des);if (temp != null) { // if can find a path, then reconstruct the path from the source to

// the destination as well as the cost.Node<Integer> parent = node.parent;Node<Integer> child = node;

12

Page 13: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

Some Hints About How to Implement YEN’s algorithm in Java (3)

while (parent != null) {temp.path.addFirst(parent.data);

temp.cost += findEdge(parent.data,child.data).cost;child = parent;parent = parent.parent; }

heap.add(temp);// put the path into the heapnode.isCached = true;// note the node has been checked}// undo all the blockingsundoKSPreparation(node);}}int min = INF;Path mPath = null;// find the minimum cost of the paths in the heap

13

Page 14: ECE 643- Design and Analysis of Computer Networks K Shortest Paths Dept. of Electrical and Computer Eng. George Mason University Fairfax, VA 22030-4444,

for (Path myPath : heap) {if(myPath.cost<min) {min = myPath.cost;mPath = myPath;}}// if the heap is not empty, then put the minimum path in myKSPif(min<INF){

myKSP.add(mPath);heap.remove(mPath);addPath2Tree(mPath, solTree);iCount++;}

// otherwise all the paths have been found, returnelse

break;}return myKSP;

14