Class UGraph<A>
- Type Parameters:
A- the type of vertices in the simple undirected graph
- All Implemented Interfaces:
Graph<A>
vertices and a set edges such that:
- The set
edgesis a subset of the setverticeschoose 2, which is the set of all subsets ofverticeswith size 2. - Each edge in
edgesis undirected so that it can be represented as a mathematical set, or an unordered pair, and the edge {u, v} = {v, u} for any u, v invertices. - There are no self-loops, so no vertex has an edge to itself. Hence for all edges
{u, v} where u, v in
vertices, we have that u ≠v.
This class models the mathematical simple undirected graph in the following way:
verticescontains elements all of typeAedgescontainsUEdgeobjects which are unordered pairs. Moreover, the condition of no self-loops is enforced in the construction ofUEdge.
- Since:
- 2025
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a newUGraphwith the specifiededgeadded.static <A> UGraph<A>Constructs the complete undirected graph on the given set of vertices.static <A> UGraph<A>Constructs the cycle graph from a listvertices[v1, v2,..., vn] such that there is an edge between each two consecutive vertices in the list and an edge, {vn, v1}, between the last vertex and the first vertex in the list.longReturns the degree of vertexv.Returns the non-increasing degree sequence of the graph as a list.edges()Returns the set of edges in the simple undirected graph.intReturns the number of edges in the graph.booleanstatic <A> UGraph<A>Constructs a new simple undirected from a set of undirected edges.booleanReturnstrueif the graph contains an edge between vertexvand vertexu.inthashCode()intReturns the number of incidences in the graph.neighbours(A v) Returns the set of vertices that are adjacent to vertexv.static <A> UGraph<A>Constructs the path graph from a listvertices[v1, v2,..., vn] such that there is an edge between each two consecutive vertices in the list.removeEdge(UEdge<A> edge) Returns a newUGraphwithout the specifiededge.removeVertex(A v) Returns a newUGraphwith the specified vertexvremoved.static <A> UGraph<A>Constructs the star graph with one central vertex adjacent to all other vertices in a given set of vertices and a centre vertexsubdivideEdge(A w, UEdge<A> edge) Returns a newUGraphwith the specified vertexwadded in such a way that it subdivides the specifiededgeinto two new edges by placing the specified vertexwin between the end vertices of the specifiededge.toString()vertices()Returns the set of vertices in the simple undirected graph.intReturns the number of vertices in the graph.
-
Constructor Details
-
UGraph
Constructs a new simple undirected graph from a set of vertices and undirected edges.- Parameters:
vertices- the set of vertices in the undirected graphedges- the set of undirected edges,UEdgeobjects, in the graph
-
-
Method Details
-
fromEdges
Constructs a new simple undirected from a set of undirected edges. The set of vertices is inferred from the set of edges, so that the resulting graph is connected.Constructs a graph with the set of edges
edgesand sets the set of verticesverticesso that the graph is connected. In other words, it is the set of all endpoints ofedges.- Parameters:
edges- the set of all edges in the graph- Returns:
- the connected graph with the edge set
edges - Throws:
NullPointerException- ifedgesis null
-
complete
Constructs the complete undirected graph on the given set of vertices.For a set of vertices
Vof sizen, this returns the graph containing alln(n-1)/2possible unordered edges between distinct vertices so that every pair of vertices is adjacent- Type Parameters:
A- the vertex type- Parameters:
vertices- the set of vertices V- Returns:
- the complete graph on
vertices - Throws:
NullPointerException- if setverticesis nullIllegalArgumentException- if setverticescontains less than 2 elements
-
star
Constructs the star graph with one central vertex adjacent to all other vertices in a given set of vertices and a centre vertexFor a set of vertices
V, this returns the graph where the only edges are the edges connecting the centre vertex to each of the other vertices- Type Parameters:
A- the vertex type- Parameters:
vertices- the set of vertices Vcentre- the centre vertex in V- Returns:
- the star graph on
verticeswithcentreas the central vertex - Throws:
NullPointerException- if provided set of vertices or centre vertex is nullIllegalArgumentException- if the provided centre vertex is not contained in the set of vertices
-
path
Constructs the path graph from a listvertices[v1, v2,..., vn] such that there is an edge between each two consecutive vertices in the list.From a list of vertices [v1, v2,..., vn], this returns a graph where the set of vertices is the set of the elements of the list {v1, v2,..., vn} and the set of edges is {{v(i), v(i + 1)}} for i = 0, 1,..., n - 1.
- Type Parameters:
A- the vertex type- Parameters:
vertices- the list of vertices- Returns:
- the path graph from the list
vertices - Throws:
NullPointerException- if listverticesis nullIllegalArgumentException- if the listverticeshas less than 2 elements or if the list contains duplicate elements
-
cycle
Constructs the cycle graph from a listvertices[v1, v2,..., vn] such that there is an edge between each two consecutive vertices in the list and an edge, {vn, v1}, between the last vertex and the first vertex in the list.From a list of vertices [v1, v2,..., vn], this returns a graph where the set of vertices is the set of the elements of the list {v1, v2,..., vn} and the set of edges is {{v(i), v(i + 1)}} ∪ {vn, v1} for i = 0, 1,..., n - 1.
- Type Parameters:
A- the vertex type- Parameters:
vertices- the list of vertices- Returns:
- the cycle graph from the list
vertices - Throws:
NullPointerException- if listverticesis nullIllegalArgumentException- if the listverticeshas less than 3 elements or if the list contains duplicate elements
-
hasEdge
Returnstrueif the graph contains an edge between vertexvand vertexu. In other words, returnstrueif vertexvis adjacent (a neighbour of) vertexu.Returns true if and only if the set of edges
edgescontains theUEdgewith componentsvandu.- Parameters:
v-u-- Returns:
trueif the vertexvis adjacent to vertexuin the graph
-
degree
Returns the degree of vertexv.Counts the number of edges that the vertex
vis a part of and returns the result.vis a part of an edge if theUEdgehas one of its components equal tov- Parameters:
v- the vertex- Returns:
- the number of edges the vertex
vis a part of - Throws:
IllegalArgumentException- ifvis not contained in the set of vertices
-
neighbours
Returns the set of vertices that are adjacent to vertexv. In other words, returns the set of neighbours, or the neighbourhood ofv.Returns a subset of the set of all
verticesin the graph such that each vertex,w, in the subset is adjacent tov, hence the set of edges contains theUEdgewith componentsvandw.- Parameters:
v- the vertex- Returns:
- a subset of
verticessuch that all vertices in the subset are adjacent tov - Throws:
IllegalArgumentException- ifvis not contained in the set of all vertices
-
incidencesCount
public int incidencesCount()Returns the number of incidences in the graph.Returns the number of all possible ordered pairs (v, e) in the graph where v is a vertex in
verticesand e is an edge inedges. This number corresponds to twice the size of the setedges, or the sum of the degrees of all vertices.- Returns:
- the number of incidences in the graph
- See Also:
-
edgesCount
public int edgesCount()Returns the number of edges in the graph.Returns the size of the set
edges.- Returns:
- the number of edges in the graph
-
verticesCount
public int verticesCount()Returns the number of vertices in the graph.Returns the size of the set
vertices.- Returns:
- the number of vertices in the graph
-
degreeSequence
Returns the non-increasing degree sequence of the graph as a list.For each vertex in
vertices, this finds the degree of the vertex and adds the result to a list that is sorted in reverse order so that the result is a non-increasing sequence of degrees of all vertices in the graph.- Returns:
- the non-increasing degree sequence of the graph as a list
- See Also:
-
removeEdge
Returns a newUGraphwithout the specifiededge.Returns a new
UGraphwith the same setverticesand the set of edges asedges - edge, thus all edges inedgesexcept for the specifiededgeto remove.- Parameters:
edge- the edge to remove in the new graph- Returns:
- the new
UGraphwithout the specifiededge - Throws:
IllegalArgumentException- ifedgeis not contained in the setedges
-
addEdge
Returns a newUGraphwith the specifiededgeadded.Returns a new
UGraphwith the same setverticesand the set of edges asedges ∪ edge, thus the set containing all edges inedgesand the specifiededgeto add.- Parameters:
edge- the edge to add to the new graph- Returns:
- the new
UGraphwith the specifiededgeadded. - Throws:
IllegalArgumentException- if the specifiededgeis between vertices that are not contained in the setvertices
-
removeVertex
Returns a newUGraphwith the specified vertexvremoved.Returns a new
UGraphwith the set of vertices asvertices - v, thus all vertices inverticesexcept for the specified vertexvto remove and the set of edges as all the edges inedgesexcept for those that have the vertexvas one of its ends.- Parameters:
v- the vertex to remove in the new graph- Returns:
- the new
UGraphwith the specified vertexvremoved - Throws:
IllegalArgumentException- if the specified vertexvis not contained in the setvertices
-
subdivideEdge
Returns a newUGraphwith the specified vertexwadded in such a way that it subdivides the specifiededgeinto two new edges by placing the specified vertexwin between the end vertices of the specifiededge.Specifically, if the specified
edgehas the end verticesuandv, the new vertexwis placed so that the specifiededgeis replaced with two new edges,{u, w}and{w, v}. Then, returns a newUGraphwith the set of vertices asvertices ∪ w, thus the set containing all vertices inverticesand the specified vertexwto add, and the set of edges as(edges - edge) ∪ {{u, w}, {w, v}}, hence the setedgeswithedgereplaced by the edges{u, w}and{w, v}.- Parameters:
w- the vertex to add to the new graphedge- the edge to subdivide in the new graph- Returns:
- the new
UGraphwith theedgesubdivided by the vertexw
-
vertices
Returns the set of vertices in the simple undirected graph.- Returns:
- the set of vertices
-
edges
Returns the set of edges in the simple undirected graph.- Returns:
- the set of edges
-
equals
-
hashCode
public int hashCode() -
toString
-