Class DirectedGraph<TNodeValue, TEdgeLabel>
Represents a directed graph that consists of labeled nodes and edges.
public class DirectedGraph<TNodeValue, TEdgeLabel> : ICollection<Node<TNodeValue, TEdgeLabel>>, IReadOnlyList<Node<TNodeValue, TEdgeLabel>>, IReadOnlyCollection<Node<TNodeValue, TEdgeLabel>>, IEnumerable<Node<TNodeValue, TEdgeLabel>>, IEnumerable
Type Parameters
TNodeValue
The type of the labels associated with graph nodes.
TEdgeLabel
The type of the labels associated with graph edges.
- Inheritance
-
DirectedGraph<TNodeValue, TEdgeLabel>
- Implements
-
ICollection<Node<TNodeValue, TEdgeLabel>>IReadOnlyList<Node<TNodeValue, TEdgeLabel>>IReadOnlyCollection<Node<TNodeValue, TEdgeLabel>>IEnumerable<Node<TNodeValue, TEdgeLabel>>
- Derived
- Inherited Members
- Extension Methods
Constructors
DirectedGraph()
Initializes a new instance of the DirectedGraph<TNodeValue, TEdgeLabel> class.
public DirectedGraph()
Properties
Count
Gets the number of nodes in the directed graph.
public int Count { get; }
Property Value
this[int]
Gets the node at the specified index.
public Node<TNodeValue, TEdgeLabel> this[int index] { get; }
Parameters
index
intThe zero-based index of the node to get.
Property Value
- Node<TNodeValue, TEdgeLabel>
The node at the specified index.
Methods
Add(Node<TNodeValue, TEdgeLabel>)
Adds a node and all its successors to the directed graph.
public void Add(Node<TNodeValue, TEdgeLabel> node)
Parameters
node
Node<TNodeValue, TEdgeLabel>The node to be added to the directed graph.
Add(TNodeValue)
Creates and adds a new node with the specified value to the directed graph.
public Node<TNodeValue, TEdgeLabel> Add(TNodeValue value)
Parameters
value
TNodeValueThe value of the node label.
Returns
- Node<TNodeValue, TEdgeLabel>
The created node.
AddEdge(Node<TNodeValue, TEdgeLabel>, Edge<TNodeValue, TEdgeLabel>)
Adds a labeled outgoing edge from the specified node in the directed graph.
public void AddEdge(Node<TNodeValue, TEdgeLabel> from, Edge<TNodeValue, TEdgeLabel> edge)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edge
Edge<TNodeValue, TEdgeLabel>The labeled outgoing edge to be added to the directed graph.
AddEdge(Node<TNodeValue, TEdgeLabel>, Node<TNodeValue, TEdgeLabel>, TEdgeLabel)
Creates and adds a new labeled edge linking the specified nodes in the directed graph.
public Edge<TNodeValue, TEdgeLabel> AddEdge(Node<TNodeValue, TEdgeLabel> from, Node<TNodeValue, TEdgeLabel> to, TEdgeLabel label)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
to
Node<TNodeValue, TEdgeLabel>The node that is the target of the edge.
label
TEdgeLabelThe value of the edge label.
Returns
- Edge<TNodeValue, TEdgeLabel>
The created edge.
AddRange(IEnumerable<Node<TNodeValue, TEdgeLabel>>)
Adds the nodes in a collection to the end of the directed graph.
public void AddRange(IEnumerable<Node<TNodeValue, TEdgeLabel>> collection)
Parameters
collection
IEnumerable<Node<TNodeValue, TEdgeLabel>>The collection of nodes to insert into the directed graph.
Remarks
If any of the nodes in the collection are already in the directed graph, they will be moved into the new index position. Any successor nodes which are not in the graph will also be added in depth-first order.
Exceptions
Clear()
Removes all nodes and corresponding edges from the directed graph.
public void Clear()
Contains(Node<TNodeValue, TEdgeLabel>)
Determines whether a node is in the directed graph.
public bool Contains(Node<TNodeValue, TEdgeLabel> node)
Parameters
node
Node<TNodeValue, TEdgeLabel>The node to locate in the directed graph.
Returns
CopyTo(Node<TNodeValue, TEdgeLabel>[])
Copies all the nodes in the directed graph to a compatible one-dimensional array, starting at the beginning of the target array.
public void CopyTo(Node<TNodeValue, TEdgeLabel>[] array)
Parameters
array
Node<TNodeValue, TEdgeLabel>[]The one-dimensional array that is the destination of the nodes copied from the directed graph.
Exceptions
- ArgumentNullException
array
is null.- ArgumentException
The number of nodes in the directed graph is greater than the number of elements that the destination array can contain.
CopyTo(Node<TNodeValue, TEdgeLabel>[], int)
Copies all the nodes in the directed graph to a compatible one-dimensional array, starting at the specified index of the target array.
public void CopyTo(Node<TNodeValue, TEdgeLabel>[] array, int arrayIndex)
Parameters
array
Node<TNodeValue, TEdgeLabel>[]The one-dimensional array that is the destination of the nodes copied from the directed graph.
arrayIndex
intThe zero-based index in the array at which copying begins.
Exceptions
- ArgumentNullException
array
is null.- ArgumentOutOfRangeException
arrayIndex
is less than 0.- ArgumentException
The number of nodes in the directed graph is greater than the available space from
arrayIndex
to the end of the destination array.
GetEnumerator()
Returns an enumerator that iterates through the nodes in the directed graph.
public IEnumerator<Node<TNodeValue, TEdgeLabel>> GetEnumerator()
Returns
- IEnumerator<Node<TNodeValue, TEdgeLabel>>
An enumerator that can be used to iterate through the nodes in the directed graph.
IndexOf(Node<TNodeValue, TEdgeLabel>)
Searches for the specified node and returns its zero-based index within the collection.
public int IndexOf(Node<TNodeValue, TEdgeLabel> node)
Parameters
node
Node<TNodeValue, TEdgeLabel>The node to locate in the collection.
Returns
- int
The zero-based index of
node
in the collection, if found; otherwise, -1.
Insert(int, Node<TNodeValue, TEdgeLabel>)
Inserts a node into the directed graph at the specified index.
public void Insert(int index, Node<TNodeValue, TEdgeLabel> node)
Parameters
index
intThe zero-based index at which the node should be inserted.
node
Node<TNodeValue, TEdgeLabel>The node to insert into the directed graph.
Remarks
If the node is already in the directed graph, it will be moved into the new index position. Any successor nodes which are not in the graph will also be added in depth-first order.
Exceptions
Insert(int, TNodeValue)
Creates and inserts a new node with the specified value into the directed graph at the specified index.
public Node<TNodeValue, TEdgeLabel> Insert(int index, TNodeValue value)
Parameters
index
intThe zero-based index at which the node should be inserted.
value
TNodeValueThe value of the node label.
Returns
- Node<TNodeValue, TEdgeLabel>
The created node.
Exceptions
InsertEdge(Node<TNodeValue, TEdgeLabel>, int, Edge<TNodeValue, TEdgeLabel>)
Inserts a labeled outgoing edge of a source node at the specified index.
public void InsertEdge(Node<TNodeValue, TEdgeLabel> from, int edgeIndex, Edge<TNodeValue, TEdgeLabel> edge)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edgeIndex
intThe zero-based index at which the edge should be inserted on the successor list of the
from
node.edge
Edge<TNodeValue, TEdgeLabel>The labeled outgoing edge that is to be inserted at the specified index on the successor list of the
from
node.
InsertEdge(Node<TNodeValue, TEdgeLabel>, int, Node<TNodeValue, TEdgeLabel>, TEdgeLabel)
Creates and inserts a labeled outgoing edge of the source node at the specified index.
public Edge<TNodeValue, TEdgeLabel> InsertEdge(Node<TNodeValue, TEdgeLabel> from, int edgeIndex, Node<TNodeValue, TEdgeLabel> to, TEdgeLabel label)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edgeIndex
intThe zero-based index at which the edge should be inserted on the successor list of the
from
node.to
Node<TNodeValue, TEdgeLabel>The node that is the target of the edge.
label
TEdgeLabelThe value of the edge label.
Returns
- Edge<TNodeValue, TEdgeLabel>
The created edge.
InsertRange(int, IEnumerable<Node<TNodeValue, TEdgeLabel>>)
Inserts the nodes in a collection into the directed graph at the specified index.
public void InsertRange(int index, IEnumerable<Node<TNodeValue, TEdgeLabel>> collection)
Parameters
index
intThe zero-based index at which the node collection should be inserted.
collection
IEnumerable<Node<TNodeValue, TEdgeLabel>>The collection of nodes to insert into the directed graph.
Remarks
If any of the nodes in the collection are already in the directed graph, they will be moved into the new index position. Any successor nodes which are not in the graph will also be added in depth-first order.
Exceptions
Remove(Node<TNodeValue, TEdgeLabel>)
Removes the specified node from the directed graph.
public bool Remove(Node<TNodeValue, TEdgeLabel> node)
Parameters
node
Node<TNodeValue, TEdgeLabel>The node to remove from the directed graph.
Returns
- bool
true if
node
is successfully removed; otherwise, false. This method also returns false ifnode
was not found in the directed graph.
RemoveAt(int)
Removes the node at the specified index of the directed graph.
public void RemoveAt(int index)
Parameters
index
intThe zero-based index of the node to remove.
Exceptions
RemoveEdge(Node<TNodeValue, TEdgeLabel>, Edge<TNodeValue, TEdgeLabel>)
Removes the specified edge from the directed graph.
public bool RemoveEdge(Node<TNodeValue, TEdgeLabel> from, Edge<TNodeValue, TEdgeLabel> edge)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edge
Edge<TNodeValue, TEdgeLabel>The outgoing edge to remove from the directed graph.
Returns
- bool
true if
edge
is successfully removed; otherwise, false. This method also returns false ifedge
was not found in the directed graph.
RemoveRange(int, int)
Removes a range of nodes from the directed graph.
public void RemoveRange(int index, int count)
Parameters
index
intThe zero-based starting index of the range of nodes to remove.
count
intThe number of nodes to remove.
Exceptions
- ArgumentOutOfRangeException
The
index
andcount
were out of bounds for the node list orcount
is greater than the number of nodes fromindex
to the end of the node list.
RemoveWhere(Predicate<Node<TNodeValue, TEdgeLabel>>)
Removes all nodes that match the conditions defined by the specified predicate from the directed graph.
public int RemoveWhere(Predicate<Node<TNodeValue, TEdgeLabel>> match)
Parameters
match
Predicate<Node<TNodeValue, TEdgeLabel>>The Predicate<T> delegate that defines the conditions of the nodes to remove.
Returns
- int
The number of nodes that were removed from the directed graph.
Exceptions
SetEdge(Node<TNodeValue, TEdgeLabel>, int, Edge<TNodeValue, TEdgeLabel>)
Replaces a labeled outgoing edge of a source node at the specified index.
public void SetEdge(Node<TNodeValue, TEdgeLabel> from, int edgeIndex, Edge<TNodeValue, TEdgeLabel> edge)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edgeIndex
intThe zero-based index of the edge to replace on the successor list of the
from
node.edge
Edge<TNodeValue, TEdgeLabel>The labeled outgoing edge that is to be set at the specified index on the successor list of the
from
node.
SetEdge(Node<TNodeValue, TEdgeLabel>, int, Node<TNodeValue, TEdgeLabel>, TEdgeLabel)
Creates and replaces a labeled outgoing edge of the source node at the specified index.
public Edge<TNodeValue, TEdgeLabel> SetEdge(Node<TNodeValue, TEdgeLabel> from, int edgeIndex, Node<TNodeValue, TEdgeLabel> to, TEdgeLabel label)
Parameters
from
Node<TNodeValue, TEdgeLabel>The node that is the source of the edge.
edgeIndex
intThe zero-based index of the edge to replace on the successor list of the
from
node.to
Node<TNodeValue, TEdgeLabel>The node that is the target of the edge.
label
TEdgeLabelThe value of the edge label.
Returns
- Edge<TNodeValue, TEdgeLabel>
The created edge.