Table of Contents

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

TKey

Key type.

TNodeValue

Node's value type.

TEdgeValue

Node'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

keyComparer IEqualityComparer<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

firstKey TKey

Source node's key.

secondKey TKey

Target node's key.

value TEdgeValue

Edge's value.

direction GraphDirection

Edge'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 direction is 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

key TKey

Node's key.

value TNodeValue

Node's value.

Returns

DirectedGraphNode<TKey, TNodeValue, TEdgeValue>

Created IDirectedGraphNode<TKey, TNodeValue, TEdgeValue> instance.

Exceptions

ArgumentException

When the provided key already 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

edge DirectedGraphEdge<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

node DirectedGraphNode<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

edge IDirectedGraphEdge<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

node IDirectedGraphNode<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

firstKey TKey

Key of the first node to check.

secondKey TKey

Key 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

key TKey

Key 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

firstKey TKey

First node's key.

secondKey TKey

Second node's key.

Returns

DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>

IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance that connects nodes associated with firstKey and secondKey.

Exceptions

KeyNotFoundException

When firstKey does not exist in this graph or when an edge from the first node to the node associated with the specified secondKey does not exist.

GetNode(TKey)

Returns the node associated with the specified key.

[Pure]
public DirectedGraphNode<TKey, TNodeValue, TEdgeValue> GetNode(TKey key)

Parameters

key TKey

Node'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

key TKey

Node's key.

value TNodeValue

Node'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

edge DirectedGraphEdge<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

node DirectedGraphNode<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

edge IDirectedGraphEdge<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

node IDirectedGraphNode<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

firstKey TKey

Key of the first node.

secondKey TKey

Key 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

firstKey TKey

Key of the first node.

secondKey TKey

Key of the second node.

removed TEdgeValue

out 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

key TKey

Key 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

key TKey

Key to remove.

removed TNodeValue

out 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

firstKey TKey

Source node's key.

secondKey TKey

Target node's key.

value TEdgeValue

Edge's value.

direction GraphDirection

Edge's direction.

added DirectedGraphEdge<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 direction is 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

key TKey

Node's key.

value TNodeValue

Node's value.

added DirectedGraphNode<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

firstKey TKey

First node's key.

secondKey TKey

Second node's key.

result DirectedGraphEdge<TKey, TNodeValue, TEdgeValue>

out parameter that returns an IDirectedGraphEdge<TKey, TNodeValue, TEdgeValue> instance that connects nodes associated with firstKey and secondKey.

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

key TKey

Node's key.

result DirectedGraphNode<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.