22
Applying Recursion Applying Recursion Routing in Computer Networks Routing in Computer Networks

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

Embed Size (px)

Citation preview

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

Applying RecursionApplying Recursion

Routing in Computer NetworksRouting in Computer Networks

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

ReviewReview

We’ve seen that recursion provides an We’ve seen that recursion provides an elegant, simple, and (sometimes) elegant, simple, and (sometimes) efficient way to solve certain problems.efficient way to solve certain problems.

A recursive solution consists of:A recursive solution consists of:– a a base casebase case: an instance of the problem : an instance of the problem

that is trivial to solve; andthat is trivial to solve; and

– an induction step: a means of solving non-an induction step: a means of solving non-trivial instances using “smaller” solutions to trivial instances using “smaller” solutions to the problem.the problem.

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

ProblemProblem

A A computer networkcomputer network is a collection of is a collection of computerscomputers, that communicate via , that communicate via linkslinks. .

Links may representLinks may represent– phone linesphone lines

– coaxial cablecoaxial cable

– infared transmissionsinfared transmissions

– microwave relaysmicrowave relays

– satellite communication linkssatellite communication links

– ......

2

0 5

3

4

6

1

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

Network OperationsNetwork Operations

If we could represent a network in software, If we could represent a network in software, some of the useful operations might be: some of the useful operations might be:

• Initialization (presumably from a file).Initialization (presumably from a file).

• Is there a computer with id Is there a computer with id ii in the network? in the network?

• Is there a link from computer Is there a link from computer ii to computer to computer j j ??

• Is there a path from computer Is there a path from computer ii to computer to computer j j ??

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

DesignDesign

We can represent the We can represent the computerscomputers in the network by assigning in the network by assigning each one a unique each one a unique integerinteger value (i.e., its value (i.e., its network idnetwork id).).

One way to represent the One way to represent the linkslinks in a network is to in a network is to use an use an adjacency matrixadjacency matrix -- a boolean -- a boolean matrix in which entry matrix in which entry [i][i][j][j] is is true if and only if if and only if there is a link from there is a link from ii to to jj..

2

0 5

3

4

6

1

F T F F F F T

T F T F F T F

F T F T F F F

F F T F T F F

F F F T F T T

F T F F T F F

T F F F T F F

[0] [1] [2] [3] [4] [5] [6]

[0]

[1]

[2]

[3]

[4]

[5]

[6]

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

Class MembersClass MembersWe might thus begin a Network class as follows: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<int> RouteFrom(int i, int j) const;

private: vector<int> PathFrom(int i, int j, vector<int> visited) const; int mySize; // number of computers typedef vector<bool> Row; vector<Row> myLinks; // adjacency matrix};

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

InitializationInitializationFor convenience, we will initialize our network using For convenience, we will initialize our network using

information from a information from a filefile, whose format will be:, whose format will be:– the first line of the file provides the number of the first line of the file provides the number of

computers in the network; andcomputers in the network; and

– each subsequent line is a pair each subsequent line is a pair i ji j indicating a indicating a bidirectional link between computers bidirectional link between computers ii and and jj..

70 11 22 33 44 54 60 61 5

2

0 5

3

4

6

1

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

ConstructorConstructor

We can define the class constructor as follows: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

// ...

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

ConstructorConstructor// ... Network constructor (ct’d)

myLinks = vector<Row>(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}

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

DeclarationDeclaration

We can now declare a Network object:We can now declare a Network object:

Network net(inputFile);

If inputFile contains the values shown earlier, If inputFile contains the values shown earlier, net will be constructed as follows: will be constructed as follows:

F T F F F F T

T F T F F T F

F T F T F F F

F F T F T F F

F F F T F T T

F T F F T F F

T F F F T F F

[0] [1] [2] [3] [4] [5] [6]

[0]

[1]

[2]

[3]

[4]

[5]

[6]

7mySize

myLinks

net

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

The Size() OperationThe Size() Operation

The Size() operation is a simple extractor:The Size() operation is a simple extractor:

// ...

inline int Network::Size() const{ return mySize;}

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

The HasComputer() The HasComputer() Operation Operation

The HasComputer() operation is also simple:The HasComputer() operation is also simple:

// ...

inline bool Network::HasComputer(int id) const{ return id >= 0 && id < mySize;}

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

The HasLink() OperationThe HasLink() Operation

Thanks to our adjacency matrix, the Thanks to our adjacency matrix, the HasLink() operation is also quite simple:HasLink() operation is also quite simple:

// ...

inline bool Network::HasLink(int i, int j) const{ return myLinks[i][j];}

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

The RouteFrom() The RouteFrom() OperationOperation

The RouteFrom() operation is more complicated, since in some networks, it The RouteFrom() operation is more complicated, since in some networks, it may be necessary to check many links in order to find a path.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?Example: How do we find the path from 5 to 4 in the following network?

2

0 5

3

46

1

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

RouteFrom()RouteFrom()

RouteFrom(i,j) can be implemented RouteFrom(i,j) can be implemented recursively.recursively.

However, we must keep track of those However, we must keep track of those nodes we have already visited on a nodes we have already visited on a route...route...

If we pass this information via a If we pass this information via a parameter, we should define a parameter, we should define a recursive utility function PathFrom(i, j, recursive utility function PathFrom(i, j, visited) to do the actual work.visited) to do the actual work.

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

Defining RouteFrom()Defining RouteFrom()

PathFrom() makes RouteFrom() easy to define:PathFrom() makes RouteFrom() easy to define:// ...

inline vector<int> Network::RouteFrom(int i, int j) const{ vector<int> visited; return PathFrom(i, j, visited);}

We could just make PathFrom() a public operation, but RouteFrom() provides a We could just make PathFrom() a public operation, but RouteFrom() provides a more convenient interface by not requiring the user to pass more convenient interface by not requiring the user to pass visitedvisited..

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

Designing PathFrom()Designing PathFrom()PathFrom(i, j, visited):PathFrom(i, j, visited):

Base caseBase case: HasLink(i,j) is true.: HasLink(i,j) is true.Return a vector containing i and j.Return a vector containing i and j.

Induction StepInduction Step: HasLink(i,j) is false.: HasLink(i,j) is false.For each computer c:For each computer c:

If HasLink(i,c) && c has not been visited:If HasLink(i,c) && c has not been visited:If there is a PathFrom(c,j)If there is a PathFrom(c,j) Add i to and return that path.Add i to and return that path.

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

Defining PathFrom()Defining PathFrom()

Here is one way PathFrom() can be defined:Here is one way PathFrom() can be defined:// ...

vector<int> Network::PathFrom(int i, int j, vector<int> beenTo) const{

vector<int> result;result.push_back(i);beenTo.push_back(i);

if (myLinks[i][j]) // base case{

result.push_back(j);beenTo.push_back(j);

} // ...

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

Defining PathFrom() (Defining PathFrom() (Ct’dCt’d)) // ...

else // induction step{

for (int v = 0; v < mySize; v++) { if (myLinks[i][v] && find(beenTo.begin(), beenTo.end(), v) == beenTo.end()) { vector<int> 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;}

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

Class MembersClass MembersOur final declaration of Network is thus as follows: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<int> RouteFrom(int i, int j) const;

private: vector<int> PathFrom(int i, int j, vector<int> visited) const; int mySize; // number of computers typedef vector<bool> Row; vector<Row> myLinks; // adjacency matrix};

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

DiscussionDiscussion

PathFrom() is not very efficient, and the path it PathFrom() is not very efficient, and the path it returns may not be the returns may not be the shortest pathshortest path from i to from i to j.j.

The The graphgraph (a set of nodes and a set of edges (a set of nodes and a set of edges connecting them) is often used to model connecting them) is often used to model networks.networks.

Efficient solutions have been devised for many Efficient solutions have been devised for many graph-theory problems (e.g., graph-theory problems (e.g., the shortest path the shortest path problemproblem), providing ready-made solutions to ), providing ready-made solutions to the corresponding networking problems.the corresponding networking problems.

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

SummarySummary

We have barely scratched the surface of C++!We have barely scratched the surface of C++!

C++ is an incredibly rich programming language, C++ is an incredibly rich programming language, that lets the programmer specifiy precisely what that lets the programmer specifiy precisely what he or she wishes the computer to do.he or she wishes the computer to do.

C++ provides many C++ provides many otherother libraries, containing libraries, containing more ready-made solutions to common more ready-made solutions to common problems.problems.

Continue on to the next course and learn about Continue on to the next course and learn about more features of the C++ programming more features of the C++ programming language!language!