O'Reilly logo

C# Data Structures and Algorithms by Marcin Jamro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example – directed and weighted edges

The next example involves the directed and weighted graph, as follows:

The implementation is very similar to the one described in the previous section. However, some modifications are necessary. To start with, different values of the parameters of the constructor are used, namely true instead of false to indicate that a directed and weighted variant of edges is being considered. The suitable line of code is as follows:

Graph<int> graph = new Graph<int>(true, true); 

The part regarding adding nodes is exactly the same as in the previous example:

Node<int> n1 = graph.AddNode(1); Node<int> n2 = graph.AddNode(2); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required