Table of Contents

Class UndirectedMultigraph<TVertex, TEdge>

Namespace
Graph1x
Assembly
Graph1x.dll

An undirected multigraph: parallel edges between the same endpoints and self-loops are all allowed, and endpoint order never matters. Backed by per-vertex incidence lists.

public class UndirectedMultigraph<TVertex, TEdge> : IMutableGraph<TVertex, TEdge>, IReadOnlyGraph<TVertex, TEdge> where TVertex : notnull where TEdge : IEdge<TVertex>

Type Parameters

TVertex

The vertex type.

TEdge

The edge type.

Inheritance
UndirectedMultigraph<TVertex, TEdge>
Implements
IMutableGraph<TVertex, TEdge>
IReadOnlyGraph<TVertex, TEdge>
Inherited Members
Extension Methods

Constructors

UndirectedMultigraph()

Initializes an empty multigraph using the default vertex comparer.

public UndirectedMultigraph()

UndirectedMultigraph(IEqualityComparer<TVertex>)

Initializes an empty multigraph using vertexComparer to identify vertices.

public UndirectedMultigraph(IEqualityComparer<TVertex> vertexComparer)

Parameters

vertexComparer IEqualityComparer<TVertex>

The comparer used to identify vertices.

Exceptions

ArgumentNullException

vertexComparer is null.

Properties

AllowsParallelEdges

Gets a value indicating whether the graph admits parallel edges between the same endpoints.

public bool AllowsParallelEdges { get; }

Property Value

bool

EdgeCount

Gets the number of edges in the graph.

public int EdgeCount { get; }

Property Value

int

Edges

Gets the edges of the graph.

public IEnumerable<TEdge> Edges { get; }

Property Value

IEnumerable<TEdge>

IsDirected

Gets a value indicating whether edges are directed.

public bool IsDirected { get; }

Property Value

bool

VertexComparer

Gets the comparer used to identify vertices.

public IEqualityComparer<TVertex> VertexComparer { get; }

Property Value

IEqualityComparer<TVertex>

VertexCount

Gets the number of vertices in the graph.

public int VertexCount { get; }

Property Value

int

Vertices

Gets the vertices of the graph.

public IEnumerable<TVertex> Vertices { get; }

Property Value

IEnumerable<TVertex>

Methods

AddEdge(TEdge)

Adds edge to the graph, adding missing endpoint vertices automatically.

public bool AddEdge(TEdge edge)

Parameters

edge TEdge

The edge to add.

Returns

bool

true if added; false if the graph forbids the edge (e.g. a parallel edge on a simple graph).

AddVertex(TVertex)

Adds vertex to the graph.

public bool AddVertex(TVertex vertex)

Parameters

vertex TVertex

The vertex to add.

Returns

bool

true if added; false if it was already present.

AdjacentEdges(TVertex)

Gets the edges incident to vertex. On directed graphs this includes both incoming and outgoing edges.

public IEnumerable<TEdge> AdjacentEdges(TVertex vertex)

Parameters

vertex TVertex

The vertex whose incident edges to enumerate.

Returns

IEnumerable<TEdge>

The incident edges.

Exceptions

ArgumentException

The vertex is not in the graph.

Clear()

Removes every vertex and edge from the graph.

public void Clear()

ContainsEdge(TVertex, TVertex)

Determines whether the graph contains an edge between source and target. On undirected graphs the endpoint order is irrelevant.

public bool ContainsEdge(TVertex source, TVertex target)

Parameters

source TVertex

The first endpoint.

target TVertex

The second endpoint.

Returns

bool

true if such an edge is present.

ContainsVertex(TVertex)

Determines whether the graph contains vertex.

public bool ContainsVertex(TVertex vertex)

Parameters

vertex TVertex

The vertex to look up.

Returns

bool

true if the vertex is present.

Degree(TVertex)

Gets the degree of vertex. Self-loops count twice on undirected graphs; on directed graphs the degree is in-degree plus out-degree.

public int Degree(TVertex vertex)

Parameters

vertex TVertex

The vertex to measure.

Returns

int

The number of edge endpoints incident to the vertex.

Exceptions

ArgumentException

The vertex is not in the graph.

GetEdges(TVertex, TVertex)

Gets every parallel edge between source and target, in either orientation.

public IEnumerable<TEdge> GetEdges(TVertex source, TVertex target)

Parameters

source TVertex

One endpoint.

target TVertex

The other endpoint.

Returns

IEnumerable<TEdge>

The matching edges, possibly empty.

Exceptions

ArgumentException

source is not in the graph.

RemoveEdge(TVertex, TVertex)

Removes one edge between source and target, whatever its payload or orientation.

public bool RemoveEdge(TVertex source, TVertex target)

Parameters

source TVertex

One endpoint.

target TVertex

The other endpoint.

Returns

bool

true if an edge was removed.

RemoveEdge(TEdge)

Removes edge from the graph. Endpoint vertices stay.

public bool RemoveEdge(TEdge edge)

Parameters

edge TEdge

The edge to remove.

Returns

bool

true if removed; false if it was not present.

Remarks

The stored edge must equal edge under the edge type's default equality — VertexComparer locates the endpoints but never compares payloads. To remove an edge by its endpoints alone, use the endpoint-based overloads on the concrete graph types.

RemoveVertex(TVertex)

Removes vertex and every edge incident to it.

public bool RemoveVertex(TVertex vertex)

Parameters

vertex TVertex

The vertex to remove.

Returns

bool

true if removed; false if it was not present.