Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applying Recursion Routing in Computer Networks. Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve.

Similar presentations


Presentation on theme: "Applying Recursion Routing in Computer Networks. Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve."— Presentation transcript:

1 Applying Recursion Routing in Computer Networks

2 Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve certain problems. A recursive solution consists of: – a base case: an instance of the problem that is trivial to solve; and –an induction step: a means of solving non-trivial instances using “smaller” solutions to the problem.

3 Problem A computer network is a collection of computers, that communicate via links. Links may represent –phone lines –coaxial cable –infared transmissions –microwave relays –satellite communication links –... 2 0 5 3 4 6 1

4 Network Operations If we could represent a network in software, some of the useful operations might be: Initialization (presumably from a file).Initialization (presumably from a file). Is there a computer with id i in the network?Is there a computer with id i in the network? Is there a link from computer i to computer j ?Is there a link from computer i to computer j ? Is there a path from computer i to computer j ?Is there a path from computer i to computer j ?

5 Design We can represent the computers in the network by assigning each one a unique integer value (i.e., its network id). One way to represent the links in a network is to use an adjacency matrix -- a boolean matrix in which entry [i][j] is if and only if there is a link from i to j. One way to represent the links in a network is to use an adjacency matrix -- a boolean matrix in which entry [i][j] is true if and only if there is a link from i to j. 2 0 5 3 4 6 1 FTFFFFT TFTFFTF FTFTFFF FFTFTFF FFFTFTT FTFFTFF TFFFTFF [0][1][2][3][4][5][6] [0] [1] [2] [3] [4] [5] [6]

6 Class Members We might thus begin a Network class as follows: // Network.h //... // Directives omitted class Network { public: Network(const string & fileName); int Size() const; bool HasComputer(int id) const; bool HasLink(int i, int j) const; vector RouteFrom(int i, int j) const; private: vector PathFrom(int i, int j, vector visited) const; int mySize; // number of computers typedef vector Row; vector myLinks; // adjacency matrix };

7 Initialization For convenience, we will initialize our network using information from a file, whose format will be: –the first line of the file provides the number of computers in the network; and –each subsequent line is a pair i j indicating a bidirectional link between computers i and j. 7 0 1 1 2 2 3 3 4 4 5 4 6 0 6 1 5 2 0 5 3 4 6 1 

8 Constructor We can define the class constructor as follows: // Network.cpp //... #include “Network.h” Network::Network(const string & fileName) { ifstream in(fileName.data()); // open stream to file assert(in.is_open()); // verify int n; // for convenience in >> n; // read number of comps assert(n > 0); // check validity mySize = n; // save //...

9 Constructor //... Network constructor (ct’d) myLinks = vector (n, // n Rows Row(n, false)); // of n values // all ‘F’ int i, j; // computers for (;;) // loop: { in >> i >> j; // read i, j if (in.eof()) break; // if done, quit assert(i >= 0 && i < mySize); // check i assert(j >= 0 && j < mySize); // check j myLinks[i][j] = myLinks[j][i] // set links i,j = true; // & j,i to ‘T’ } in.close(); // close stream }

10 Declaration We can now declare a Network object: Network net(inputFile); If inputFile contains the values shown earlier, will be constructed as follows: If inputFile contains the values shown earlier, net will be constructed as follows: FTFFFFT TFTFFTF FTFTFFF FFTFTFF FFFTFTT FTFFTFF TFFFTFF [0][1][2][3][4][5][6] [0] [1] [2] [3] [4] [5] [6] 7 mySize myLinks net

11 The Size() Operation The Size() operation is a simple extractor: //... inline int Network::Size() const { return mySize; }

12 The HasComputer() Operation The HasComputer() operation is also simple: //... inline bool Network::HasComputer(int id) const { return id >= 0 && id < mySize; }

13 The HasLink() Operation Thanks to our adjacency matrix, the HasLink() operation is also quite simple: //... inline bool Network::HasLink(int i, int j) const { return myLinks[i][j]; }

14 The RouteFrom() Operation The RouteFrom() operation is more complicated, since in some networks, it may be necessary to check many links in order to find a path. Example: How do we find the path from 5 to 4 in the following network? 2 0 5 3 46 1

15 RouteFrom() RouteFrom(i,j) can be implemented recursively. However, we must keep track of those nodes we have already visited on a route... If we pass this information via a parameter, we should define a recursive utility function PathFrom(i, j, visited) to do the actual work.

16 Defining RouteFrom() PathFrom() makes RouteFrom() easy to define: //... inline vector Network::RouteFrom(int i, int j) const { vector visited; return PathFrom(i, j, visited); } We could just make PathFrom() a public operation, but RouteFrom() provides a more convenient interface by not requiring the user to pass visited.

17 Designing PathFrom() PathFrom(i, j, visited): Base case: HasLink(i,j) is true. Return a vector containing i and j. Induction Step: HasLink(i,j) is false. For each computer c: If HasLink(i,c) && c has not been visited: If there is a PathFrom(c,j) Add i to and return that path. Add i to and return that path.

18 Defining PathFrom() Here is one way PathFrom() can be defined: //... vector Network::PathFrom(int i, int j, vector beenTo) const { vector result; result.push_back(i); beenTo.push_back(i); if (myLinks[i][j]) // base case { result.push_back(j); beenTo.push_back(j); } //...

19 Defining PathFrom() (Ct’d) //... else // induction step { for (int v = 0; v < mySize; v++) { if (myLinks[i][v] && find(beenTo.begin(), beenTo.end(), v) == beenTo.end()) { vector temp = PathFrom(v, j, beenTo); if (temp.size() > 1) { for (int k = 0; k < temp.size(); k++) result.push_back(temp[k]); break; } return result; }

20 Class Members Our final declaration of Network is thus as follows: // Network.h //... // Directives omitted class Network { public: Network(const string & fileName); int Size() const; bool HasComputer(int id) const; bool HasLink(int i, int j) const; vector RouteFrom(int i, int j) const; private: vector PathFrom(int i, int j, vector visited) const; int mySize; // number of computers typedef vector Row; vector myLinks; // adjacency matrix };

21 Discussion PathFrom() is not very efficient, and the path it returns may not be the shortest path from i to j. The graph (a set of nodes and a set of edges connecting them) is often used to model networks. Efficient solutions have been devised for many graph- theory problems (e.g., the shortest path problem), providing ready-made solutions to the corresponding networking problems.

22 Summary We have barely scratched the surface of C++! C++ is an incredibly rich programming language, that lets the programmer specifiy precisely what he or she wishes the computer to do. C++ provides many other libraries, containing more ready-made solutions to common problems. Continue on to the next course and learn about more features of the C++ programming language!


Download ppt "Applying Recursion Routing in Computer Networks. Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve."

Similar presentations


Ads by Google