Graph Class |
The Graph class represents an undirected graph of vertices named 0 through V - 1. It supports the following two primary operations: add an edge to the graph, iterate over all of the vertices adjacent to a vertex. It also provides methods for returning the number of vertices V and the number of edges E. Parallel edges and self-loops are permitted.
This implementation uses an adjacency-lists representation, which is a vertex-indexed array of objects. All operations take constant time (in the worst case) except iterating over the vertices adjacent to a given vertex, which takes time proportional to the number of such vertices.
Namespace: Algs4Net
public class Graph
The Graph type exposes the following members.
Name | Description | |
---|---|---|
![]() | Graph(Int32) |
Initializes an empty graph with V vertices and 0 edges.
param V the number of vertices |
![]() | Graph(Graph) |
Initializes a new graph that is a deep copy of G. |
![]() | Graph(TextInput) | Initializes a graph from an initialized TextInput stream
The format is the number of vertices V,
followed by the number of edges E,
followed by E pairs of vertices, with each entry separated by whitespace. |
Name | Description | |
---|---|---|
![]() | E |
Returns the number of edges in this graph. |
![]() | V |
Returns the number of vertices in this graph. |
Name | Description | |
---|---|---|
![]() | AddEdge(Edge) |
Adds the undirected edge to this graph. |
![]() | AddEdge(Int32, Int32) |
Adds the undirected edge v-w to this graph. |
![]() | Adj |
Returns the vertices adjacent to vertex v. |
![]() | Degree |
Returns the degree of vertex v. |
![]() ![]() | MainTest |
Demo test the Graph data type. |
![]() | ToString |
Returns a string representation of this graph. (Overrides ObjectToString.) |
For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
This class is a C# port from the original Java class Graph implementation by the respective authors.