Class DirectedGraph<TKey, TNodeValue, TEdgeValue>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic directed graph data structure.
public class DirectedGraph<TKey, TNodeValue, TEdgeValue> : IDirectedGraph<TKey, TNodeValue, TEdgeValue>, IReadOnlyDirectedGraph<TKey, TNodeValue, TEdgeValue> where TKey : notnull
Type Parameters
TKeyKey type.
TNodeValueNode's value type.
TEdgeValueNode's edge type.
- Inheritance
-
DirectedGraph<TKey, TNodeValue, TEdgeValue>
- Implements
-
IDirectedGraph<TKey, TNodeValue, TEdgeValue>IReadOnlyDirectedGraph<TKey, TNodeValue, TEdgeValue>
- Inherited Members
Constructors
DirectedGraph()
Creates a new empty DirectedGraph<TKey, TNodeValue, TEdgeValue> instance with Default key comparer.
public DirectedGraph()
DirectedGraph(IEqualityComparer<TKey>)
Creates a new empty DirectedGraph<TKey, TNodeValue, TEdgeValue> instance.
public DirectedGraph(IEqualityComparer<TKey> keyComparer)
Parameters
keyComparerIEqualityComparer<TKey>Key equality comparer.
Properties
Edges
Specifies the current collection of IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instances that belong to this graph.
public IEnumerable<DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>> Edges { get; }
Property Value
- IEnumerable<DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>>
KeyComparer
Key equality comparer.
public IEqualityComparer<TKey> KeyComparer { get; }
Property Value
- IEqualityComparer<TKey>
Nodes
Specifies the current collection of IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instances that belong to this graph.
public IReadOnlyCollection<DirectedGraphNode<TKey, TNodeValue, TEdgeValue>> Nodes { get; }
Property Value
- IReadOnlyCollection<DirectedGraphNode<TKey, TNodeValue, TEdgeValue>>
Methods
AddEdge(TKey, TKey, TEdgeValue, GraphDirection)
Adds a new edge to this graph.
public DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> AddEdge(TKey firstKey, TKey secondKey, TEdgeValue value, GraphDirection direction = GraphDirection.Out)
Parameters
firstKeyTKeySource node's key.
secondKeyTKeyTarget node's key.
valueTEdgeValueEdge's value.
directionGraphDirectionEdge's direction. Equal to Out by default.
Returns
- DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>
Created IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance.
Remarks
When source and target nodes are the same, then the direction will be equal to Both.
Exceptions
- KeyNotFoundException
When source or target node's key does not exist in this graph.
- ArgumentException
When
directionis equal to None or when the edge already exists.
AddNode(TKey, TNodeValue)
Adds a new node to this graph.
public DirectedGraphNode<TKey, TNodeValue, TEdgeValue> AddNode(TKey key, TNodeValue value)
Parameters
keyTKeyNode's key.
valueTNodeValueNode's value.
Returns
- DirectedGraphNode<TKey, TNodeValue, TEdgeValue>
Created IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance.
Exceptions
- ArgumentException
When the provided
keyalready exists in this graph.
Clear()
Removes all nodes and edges from this graph.
public void Clear()
Contains(DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Checks whether or not the provided edge exists in this graph.
[Pure]
public bool Contains(DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> edge)
Parameters
edgeDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>Edge to check.
Returns
- bool
true when edge exists, otherwise false.
Contains(DirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Checks whether or not the provided node exists in this graph.
[Pure]
public bool Contains(DirectedGraphNode<TKey, TNodeValue, TEdgeValue> node)
Parameters
nodeDirectedGraphNode<TKey, TNodeValue, TEdgeValue>Node to check.
Returns
- bool
true when node exists, otherwise false.
Contains(IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Checks whether or not the provided edge exists in this graph.
[Pure]
public bool Contains(IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> edge)
Parameters
edgeIDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>Edge to check.
Returns
- bool
true when edge exists, otherwise false.
Contains(IDirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Checks whether or not the provided node exists in this graph.
[Pure]
public bool Contains(IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> node)
Parameters
nodeIDirectedGraphNode<TKey, TNodeValue, TEdgeValue>Node to check.
Returns
- bool
true when node exists, otherwise false.
ContainsEdge(TKey, TKey)
Checks whether or not an edge that connects nodes associated with the firstKey and secondKey
exists in this graph.
[Pure]
public bool ContainsEdge(TKey firstKey, TKey secondKey)
Parameters
firstKeyTKeyKey of the first node to check.
secondKeyTKeyKey of the second node to check.
Returns
- bool
true when edge exists, otherwise false.
ContainsNode(TKey)
Checks whether or not a node with the specified key exists in this graph.
[Pure]
public bool ContainsNode(TKey key)
Parameters
keyTKeyKey to check.
Returns
- bool
true when node exists, otherwise false.
GetEdge(TKey, TKey)
Returns the edge that connects nodes associated with the specified firstKey and secondKey.
[Pure]
public DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> GetEdge(TKey firstKey, TKey secondKey)
Parameters
firstKeyTKeyFirst node's key.
secondKeyTKeySecond node's key.
Returns
- DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>
IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance that connects nodes associated with
firstKeyandsecondKey.
Exceptions
- KeyNotFoundException
When
firstKeydoes not exist in this graph or when an edge from the first node to the node associated with the specifiedsecondKeydoes not exist.
GetNode(TKey)
Returns the node associated with the specified key.
[Pure]
public DirectedGraphNode<TKey, TNodeValue, TEdgeValue> GetNode(TKey key)
Parameters
keyTKeyNode's key.
Returns
- DirectedGraphNode<TKey, TNodeValue, TEdgeValue>
IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance associated with the provided
key.
Exceptions
- KeyNotFoundException
When key does not exist in this graph.
GetOrAddNode(TKey, TNodeValue)
Gets an existing node or adds a new node to this graph if it does not exist.
public DirectedGraphNode<TKey, TNodeValue, TEdgeValue> GetOrAddNode(TKey key, TNodeValue value)
Parameters
keyTKeyNode's key.
valueTNodeValueNode's value.
Returns
- DirectedGraphNode<TKey, TNodeValue, TEdgeValue>
An existing IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> with unchanged Value or a created IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance if it did not exist.
Remove(DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Attempts to remove the provided edge.
public bool Remove(DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> edge)
Parameters
edgeDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>Edge to remove.
Returns
- bool
true when edge was removed, otherwise false.
Remove(DirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Attempts to remove the provided node.
public bool Remove(DirectedGraphNode<TKey, TNodeValue, TEdgeValue> node)
Parameters
nodeDirectedGraphNode<TKey, TNodeValue, TEdgeValue>Node to remove.
Returns
- bool
true when node was removed, otherwise false.
Remarks
Removes all connected edges as well.
Remove(IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Attempts to remove the provided edge.
public bool Remove(IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> edge)
Parameters
edgeIDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>Edge to remove.
Returns
- bool
true when edge was removed, otherwise false.
Remove(IDirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Attempts to remove the provided node.
public bool Remove(IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> node)
Parameters
nodeIDirectedGraphNode<TKey, TNodeValue, TEdgeValue>Node to remove.
Returns
- bool
true when node was removed, otherwise false.
Remarks
Removes all connected edges as well.
RemoveEdge(TKey, TKey)
Attempts to remove an edge that connects nodes associated
with the specified firstKey and secondKey.
public bool RemoveEdge(TKey firstKey, TKey secondKey)
Parameters
firstKeyTKeyKey of the first node.
secondKeyTKeyKey of the second node.
Returns
- bool
true when edge was removed, otherwise false.
RemoveEdge(TKey, TKey, out TEdgeValue)
Attempts to remove an edge that connects nodes associated
with the specified firstKey and secondKey.
public bool RemoveEdge(TKey firstKey, TKey secondKey, out TEdgeValue removed)
Parameters
firstKeyTKeyKey of the first node.
secondKeyTKeyKey of the second node.
removedTEdgeValueout parameter that returns the removed edge's value.
Returns
- bool
true when edge was removed, otherwise false.
RemoveNode(TKey)
Attempts to remove a node associated with the specified key.
public bool RemoveNode(TKey key)
Parameters
keyTKeyKey to remove.
Returns
- bool
true when node was removed, otherwise false.
Remarks
Removes all connected edges as well.
RemoveNode(TKey, out TNodeValue)
Attempts to remove a node associated with the specified key.
public bool RemoveNode(TKey key, out TNodeValue removed)
Parameters
keyTKeyKey to remove.
removedTNodeValueout parameter that returns the removed node's value.
Returns
- bool
true when node was removed, otherwise false.
Remarks
Removes all connected edges as well.
TryAddEdge(TKey, TKey, TEdgeValue, GraphDirection, out DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Attempts to add a new edge to this graph.
public bool TryAddEdge(TKey firstKey, TKey secondKey, TEdgeValue value, GraphDirection direction, out DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> added)
Parameters
firstKeyTKeySource node's key.
secondKeyTKeyTarget node's key.
valueTEdgeValueEdge's value.
directionGraphDirectionEdge's direction.
addedDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>out parameter that returns the created IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance.
Returns
- bool
true when a new edge was added, otherwise false.
Remarks
When source and target nodes are the same, then the direction will be equal to Both.
Exceptions
- ArgumentException
When
directionis equal to None.
TryAddNode(TKey, TNodeValue, out DirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Attempts to add a new node to this graph.
public bool TryAddNode(TKey key, TNodeValue value, out DirectedGraphNode<TKey, TNodeValue, TEdgeValue> added)
Parameters
keyTKeyNode's key.
valueTNodeValueNode's value.
addedDirectedGraphNode<TKey, TNodeValue, TEdgeValue>out parameter that returns the created IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance.
Returns
- bool
true when a new node was added, otherwise false.
TryGetEdge(TKey, TKey, out DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>)
Returns the edge that connects nodes associated with the specified firstKey and secondKey.
public bool TryGetEdge(TKey firstKey, TKey secondKey, out DirectedGraphEdge<TKey, TNodeValue, TEdgeValue> result)
Parameters
firstKeyTKeyFirst node's key.
secondKeyTKeySecond node's key.
resultDirectedGraphEdge<TKey, TNodeValue, TEdgeValue>out parameter that returns an IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance that connects nodes associated with
firstKeyandsecondKey.
Returns
- bool
true when the edge exists, otherwise false.
TryGetNode(TKey, out DirectedGraphNode<TKey, TNodeValue, TEdgeValue>)
Attempts to return the node associated with the specified key.
public bool TryGetNode(TKey key, out DirectedGraphNode<TKey, TNodeValue, TEdgeValue> result)
Parameters
keyTKeyNode's key.
resultDirectedGraphNode<TKey, TNodeValue, TEdgeValue>out parameter that returns an IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance associated with the provided
key.
Returns
- bool
true when the node exists, otherwise false.