Class Graph<T>
- Type Parameters:
T
- the bounding type stored in this graph in its vertices.
Minimal size: 0 (no vertices)
Theoretical maximal size: Integer.MAX_VALUE
- Since:
- 1.0-beta
- Version:
- JDK 1.7
- Author:
- Ćukasz Malara
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate class
Graph.Vertex<E extends T>
This inner class implements vertices of this graph. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
addNewVertex
(int index) This method adds a new vertex to this graph.boolean
addNewVertex
(int index, T data) This method adds a new vertex with given data to store in it to this graph.boolean
addNewVertices
(@NotNull Collection<Integer> verticesToAdd) This method adds new vertices to this graph.boolean
areVerticesOfGraph
(@NotNull Collection<Integer> subset) This method checks whether givenCollection
is a subset of vertices of this graph.private int
breadthFirstSearch
(@NotNull Collection<Graph<T>.Vertex<T>> subset) This method is a modified implementation of a known traverse algorithm in graphs - breadth-first search.private boolean
This method checks whether this graph can be mapped to complete.complete
(int startIndex, int size) This method creates a complete graph sized of a number given as a second parameterint
.private @NotNull Collection<Graph<T>.Vertex<T>>
This method computes a minimal connected dominating set in this graph.private @NotNull Collection<Graph<T>.Vertex<T>>
This method computes a minimal dominating set in this graph.private @NotNull Collection<Graph<T>.Vertex<T>>
This method computes a maximal independent set in this graph.boolean
connectVertices
(int indexV, int indexU) This method connects given two vertices by their indexes.private int
depthFirstSearch
(@NotNull Collection<Graph<T>.Vertex<T>> subset) This method is a modified implementation of a known traverse algorithm in graphs - depth-first search.boolean
disconnectVertices
(int indexV, int indexU) This method disconnects given two vertices.boolean
doInduceBipartiteSubGraph
(@NotNull Collection<Integer> subset) This method checks whether givenCollection
induces bipartite subgraph of this graph or does not.boolean
doInduceConnectedSubGraph
(Collection<Integer> subset) This method checks whether givenCollection
induces connected subgraph of this graph, or does not.findMCDS()
This method finds a minimal connected dominating set in this graph.findMDS()
This method finds a minimal dominating set in this graph.findMIS()
This method finds a maximal independent set of this graph.generateCompleteIfEmpty
(int startIndex, int size) This method should be used only on empty graphs.getVertex
(int index) This method returns vertex with givenint
index.final @UnmodifiableView T
getVertexData
(int index) This method returns unmodifiable data stored in a vertex by given index.getVertexNeighbourhood
(int index) This method returns an unmodifiable sorted set of neighbours of vertex given byint
index.This method returns an unmodifiable sorted set of vertices of this graph.boolean
This method checks whether this graph is bipartite or is not.private boolean
isBipartiteSubGraph
(@NotNull Collection<Graph<T>.Vertex<T>> subset) This method checks whether the all given vertices are in a bipartite subgraph of this graph or are not.boolean
isCDS
(Collection<Integer> subset) This method checks whether givenCollection
is a connected dominating set of this graph or is not.boolean
This method checks whether this graph is complete or is not.boolean
This method checks whether this graph is connected or disconnected.private boolean
isConnectedSubGraph
(Collection<Graph<T>.Vertex<T>> subset) This method checks whether the all given vertices are in a connected subgraph of this graph or are not.boolean
isIndependentSet
(Collection<Integer> subset) This method checks whether givenCollection
is an independent set of vertices of this graph or is not.private boolean
isInSubsetNotVisited
(@NotNull Collection<Graph<T>.Vertex<T>> subset, Graph<T>.Vertex<T> vertex, HashSet<Graph<T>.Vertex<T>> visited) This method checks if given vertex was not already visited and is in given subset.boolean
isVertexOfGraph
(int index) This method checks whether this graph contains a vertex with givenint
index.private boolean
isVertexOfSubGraph
(@NotNull Collection<Graph<T>.Vertex<T>> vertices, int index) This method checks whether given subset contains a vertex with givenint
index.private boolean
isVertexOfSubGraph
(@NotNull Collection<Graph<T>.Vertex<T>> vertices, Graph<T>.Vertex<T> vertex) This method checks whether a given subset contains a given vertex.private @NotNull Collection<Graph<T>.Vertex<T>>
mapIndexesToVertices
(@NotNull Collection<Integer> indexes) void
This method connects required vertices in order to make a graph complete.mapVerticesToIndexes
(@NotNull Collection<Graph<T>.Vertex<T>> vertices) boolean
removeVertex
(int index) This method removes a vertex from this graph.boolean
removeVertices
(@NotNull Collection<Integer> verticesToRemove) This method removes vertices from this graph.void
setVertexData
(int index, T data) This method set the data stored in a vertex by given index.toString()
This method returns user-friendly representation ofGraph
as each vertex index with its neighbourhood list.
-
Field Details
-
itsVertices
This field represents all vertices of this graph.Minimal size: 0 (empty) Theoretical maximal size:
Integer.MAX_VALUE
- Since:
- 1.0-beta
-
-
Constructor Details
-
Graph
This constructor creates a graph based on a strictly defined pattern provided in a text file. Path to file is given as aString
parameter. The graph is created only if the algorithm ends successfully.- Parameters:
fileSource
- absolute or relative path to a file required to create a graph.- Throws:
NegativeVertexIndexException
- if negative number was provided in a file.- Since:
- 1.0-beta
-
-
Method Details
-
complete
This method creates a complete graph sized of a number given as a second parameterint
. Vertex indexing begins from a value of firstint
parameter.Default value for vertex indexing starts with
0
- if any index will exceedInteger.MAX_VALUE
.- Parameters:
startIndex
- numerical index of first vertex of graph.size
- number of vertices to create in a graph.- Returns:
- complete graph.
- Throws:
NegativeVertexIndexException
- if first parameter typeint < 0
.- Since:
- 1.0
- See Also:
-
generateCompleteIfEmpty
private Graph<T> generateCompleteIfEmpty(int startIndex, int size) throws NegativeVertexIndexException This method should be used only on empty graphs.
It generates a complete graph sized of a number given as a second parameterint
. Vertex indexing begins from value of firstint
parameter.Default value for vertex indexing starts with
0
if any index would exceedInteger.MAX_VALUE
.- Parameters:
startIndex
- numerical index of first vertex of graphsize
- number of vertices to generate for a graph.- Returns:
- complete graph if graph was empty, the same graph otherwise.
- Throws:
NegativeVertexIndexException
- if first parameter typeint < 0
.- Since:
- 1.0
- See Also:
-
mapToComplete
public void mapToComplete()This method connects required vertices in order to make a graph complete.- Since:
- 1.0
-
canBeMappedToComplete
private boolean canBeMappedToComplete()This method checks whether this graph can be mapped to complete.- Returns:
true
if this graph can be mapped to complete,false
otherwise.- Since:
- 1.1
-
getVertex
@NotNull private Graph<T>.@NotNull Vertex<T> getVertex(int index) throws NegativeVertexIndexException, NoSuchVertexIndexException This method returns vertex with givenint
index.- Parameters:
index
- numerical index of vertex.- Returns:
- vertex by given index.
- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with givenint
index.- Since:
- 1.0-beta
- See Also:
-
getVertices
This method returns an unmodifiable sorted set of vertices of this graph. ReturnedSortedSet
ofInteger
corresponds to user-friendly representation of vertices ofGraph
.- Returns:
- unmodifiable sorted set of vertices of this graph.
- Since:
- 1.0-beta
- See Also:
-
getVertexData
public final @UnmodifiableView T getVertexData(int index) throws NegativeVertexIndexException, NoSuchVertexIndexException This method returns unmodifiable data stored in a vertex by given index.- Parameters:
index
- numerical index of vertex.- Returns:
- data stored in vertex by given index.
- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with givenint
index.- Since:
- 2.0
-
setVertexData
public void setVertexData(int index, T data) throws NegativeVertexIndexException, NoSuchVertexIndexException This method set the data stored in a vertex by given index.- Parameters:
index
- numerical index of vertexdata
- data to store in a vertex by given index.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with givenint
index.- Since:
- 2.0
-
getVertexNeighbourhood
@NotNull public final @NotNull @Unmodifiable Set<Integer> getVertexNeighbourhood(int index) throws NegativeVertexIndexException, NoSuchVertexIndexException This method returns an unmodifiable sorted set of neighbours of vertex given byint
index.- Parameters:
index
- numerical index of a vertex.- Returns:
- unmodifiable sorted set of neighbours of a vertex given by
int
index. - Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with givenint
index.- Since:
- 1.0
-
isVertexOfGraph
This method checks whether this graph contains a vertex with givenint
index.- Parameters:
index
- numerical index of vertex.- Returns:
true
if this graph contains vertex with given index,false
otherwise.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.- Since:
- 1.0-beta
- See Also:
-
isVertexOfSubGraph
private boolean isVertexOfSubGraph(@NotNull @NotNull Collection<Graph<T>.Vertex<T>> vertices, int index) throws NegativeVertexIndexException This method checks whether given subset contains a vertex with givenint
index.- Parameters:
vertices
- vertices subset of graph.index
- numerical index of vertex.- Returns:
true
if given subset contains vertex with given index,false
otherwise.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.- Since:
- 1.1
-
isVertexOfSubGraph
@Contract(pure=true) private boolean isVertexOfSubGraph(@NotNull @NotNull Collection<Graph<T>.Vertex<T>> vertices, Graph<T>.Vertex<T> vertex) This method checks whether a given subset contains a given vertex.- Parameters:
vertices
- vertices subset of graph.vertex
- a vertex to check if it is in the subset.- Returns:
true
if given subset contains given vertex,false
otherwise.- Since:
- 1.1
-
areVerticesOfGraph
public boolean areVerticesOfGraph(@NotNull @NotNull Collection<Integer> subset) throws NegativeVertexIndexException This method checks whether givenCollection
is a subset of vertices of this graph.- Parameters:
subset
-Collection
containing indexes of vertices to check if they are in this graph.- Returns:
- true if given
Collection
is a subset of vertices of this graph, false otherwise. - Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).- Since:
- 1.0
-
addNewVertex
This method adds a new vertex to this graph. The vertex is added only if the graph does not contain it already.- Parameters:
index
- numerical index of vertex.- Returns:
true
if vertex with givenint
index was added,false
otherwise.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.- Since:
- 1.0-beta
- See Also:
-
addNewVertex
This method adds a new vertex with given data to store in it to this graph. The vertex is added only if the graph does not contain it already.- Parameters:
index
- numerical index of vertex.data
- data to store in added vertex.- Returns:
true
if vertex with givenint
index was added,false
otherwise.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.- Since:
- 2.0
-
addNewVertices
public boolean addNewVertices(@NotNull @NotNull Collection<Integer> verticesToAdd) throws NegativeVertexIndexException This method adds new vertices to this graph. A vertex is added only if the graph does not already contain it.Adding a vertex is stopped once there is an occurrence of negative number in given
Collection
.- Parameters:
verticesToAdd
-Collection
containing indexes of vertices to add.- Returns:
true
if each vertex fromCollection
has been added to this graph,false
otherwise.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).- Since:
- 1.0
-
connectVertices
public boolean connectVertices(int indexV, int indexU) throws NegativeVertexIndexException, NoSuchVertexIndexException This method connects given two vertices by their indexes.- Parameters:
indexV
- numerical index of the first vertex.indexU
- numerical index of another vertex.- Returns:
true
if vertices with given indexes have been connected,false
otherwise.- Throws:
NegativeVertexIndexException
- if anyint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with given either index.- Since:
- 1.0-beta
-
disconnectVertices
public boolean disconnectVertices(int indexV, int indexU) throws NegativeVertexIndexException, NoSuchVertexIndexException This method disconnects given two vertices.- Parameters:
indexV
- numerical index of the first vertex.indexU
- numerical index of another vertex.- Returns:
true
if vertices with given indexes have been disconnected,false
otherwise.- Throws:
NegativeVertexIndexException
- if anyint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with given either index.- Since:
- 1.0
-
removeVertex
public boolean removeVertex(int index) throws NegativeVertexIndexException, NoSuchVertexIndexException This method removes a vertex from this graph. The Vertex is removed only if the graph contains it.- Parameters:
index
- numerical index of a vertex.- Returns:
true
if vertex with given index has been removed.- Throws:
NegativeVertexIndexException
- if parameter typeint < 0
.NoSuchVertexIndexException
- if this graph does not contain vertex with givenint
index.- Since:
- 1.0
- See Also:
-
removeVertices
public boolean removeVertices(@NotNull @NotNull Collection<Integer> verticesToRemove) throws NegativeVertexIndexException, NoSuchVertexIndexException This method removes vertices from this graph.Removing a vertex is stopped once there is an occurrence of either:
negative number in given
Collection
, number that could not be identified with any vertex index.- Parameters:
verticesToRemove
-Collection
containing indexes of vertices to remove.- Returns:
true
if each vertex fromCollection
has been removed.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0
-
breadthFirstSearch
This method is a modified implementation of a known traverse algorithm in graphs - breadth-first search. Only vertices from givenCollection
can be visited. It returns the number of visited vertices.- Parameters:
subset
- subset of vertices of this graph.- Returns:
- numbers of visited vertices.
- Since:
- 1.0
- See Also:
-
isInSubsetNotVisited
private boolean isInSubsetNotVisited(@NotNull @NotNull Collection<Graph<T>.Vertex<T>> subset, Graph<T>.Vertex<T> vertex, HashSet<Graph<T>.Vertex<T>> visited) This method checks if given vertex was not already visited and is in given subset.- Parameters:
subset
- subset of verticesvertex
- a vertex to check if it is given subset, and it was not yet visited.visited
- set of visited vertices.- Returns:
true
if vertex is in the subset and was not yet visited,false
otherwise.- Since:
- 1.1
-
depthFirstSearch
This method is a modified implementation of a known traverse algorithm in graphs - depth-first search. Only vertices from givenCollection
can be visited. It returns the number of visited vertices.- Parameters:
subset
- subset of vertices of this graph.- Returns:
- numbers of visited vertices.
- Since:
- 1.0-beta
- See Also:
-
isConnected
public boolean isConnected()This method checks whether this graph is connected or disconnected.- Returns:
true
if this graph is connected,false
otherwise.- Since:
- 1.0-beta
-
doInduceConnectedSubGraph
public boolean doInduceConnectedSubGraph(Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollection
induces connected subgraph of this graph, or does not.- Parameters:
subset
-Collection
containing indexes of vertices to check if they induce connected subgraph of this graph.- Returns:
true
if givenCollection
induces connected subgraph of this graph,false
otherwise.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0-beta
-
isConnectedSubGraph
This method checks whether the all given vertices are in a connected subgraph of this graph or are not.- Parameters:
subset
- subset of vertices of this graph.- Returns:
true
if givenCollection
induces connected subgraph of this graph,false
otherwise.- Since:
- 1.0-beta
- See Also:
-
isComplete
public boolean isComplete()This method checks whether this graph is complete or is not.- Returns:
true
if graph is complete,false
otherwise.- Since:
- 1.0
-
isBipartite
public boolean isBipartite()This method checks whether this graph is bipartite or is not.- Returns:
true
if this graph is bipartite,false
otherwise.- Since:
- 1.0
-
doInduceBipartiteSubGraph
public boolean doInduceBipartiteSubGraph(@NotNull @NotNull Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollection
induces bipartite subgraph of this graph or does not.- Parameters:
subset
-Collection
containing indexes of vertices to check if they induce bipartite subgraph of this graph.- Returns:
true
if givenCollection
is a subset of vertices of this graph that induces bipartite subgraph,false
otherwise.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0
- See Also:
-
isBipartiteSubGraph
This method checks whether the all given vertices are in a bipartite subgraph of this graph or are not.- Parameters:
subset
- subset of vertices of this graph.- Returns:
true
if givenCollection
induces bipartite subgraph of this graph,false
otherwise.- Since:
- 1.0
-
isCDS
public boolean isCDS(Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollection
is a connected dominating set of this graph or is not.- Parameters:
subset
-Collection
containing indexes of vertices to check if they induce a connected dominating set of this graph.- Returns:
true
if givenCollection
is a connected dominating set in this graph,false
otherwise.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0-beta
-
isIndependentSet
public boolean isIndependentSet(Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollection
is an independent set of vertices of this graph or is not.- Parameters:
subset
-Collection
containing indexes of vertices to check if they induce an independent set in this graph.- Returns:
true
if givenCollection
is an independent set of this graph, false otherwise.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0
-
findMCDS
This method finds a minimal connected dominating set in this graph. The result is an unmodifiable-sorted subset of vertices of this graph.To learn more details, read here
computeMCDS()
.- Returns:
- minimal connected dominating set of this graph.
- Since:
- 1.0-beta
-
computeMCDS
This method computes a minimal connected dominating set in this graph.It is an implementation of an approximation algorithm for finding minimum connected dominating set in
Graph
. Since domination problem is a NP-C problem, this method can find not necessary an optimal solution. Hence, we will say it computes minimal connected dominating set, which is indeed always a true.- Returns:
- minimal connected dominating set in this graph as a
Collection
ofGraph.Vertex
. - Since:
- 1.0-beta
- See Also:
-
findMDS
This method finds a minimal dominating set in this graph. The result is an unmodifiable-sorted subset of vertices of this graph.To learn more details, read here:
computeMDS()
.- Returns:
- minimal dominating set of this graph.
- Since:
- 1.0
-
computeMDS
This method computes a minimal dominating set in this graph.It is an implementation of an approximation algorithm for finding minimum dominating set in
Graph
. Since domination problem is a NP-C problem, this method can find not necessary an optimal solution. Hence, we will say it computes minimal dominating set, which is indeed always a true.- Returns:
- minimal dominating set in this graph as a
Collection
ofGraph.Vertex
. - Since:
- 1.0
- See Also:
-
findMIS
This method finds a maximal independent set of this graph. The result is an unmodifiable-sorted subset of vertices of this graph.To learn more details, read here:
computeMIS()
- Returns:
- maximal independent set of this graph.
- Since:
- 1.0
-
computeMIS
This method computes a maximal independent set in this graph.It is an implementation of an approximation algorithm for finding maximum independent set in
Graph
. Since maximum independent set problem is a NP-hard problem that is hard to approximate, this method can find not necessary an optimal solution. Hence, we will say it computes maximal independent set, which is indeed always a true.- Returns:
- maximal independent set in this graph as a
Collection
ofGraph.Vertex
. - Since:
- 1.0
- See Also:
-
mapIndexesToVertices
@NotNull private @NotNull Collection<Graph<T>.Vertex<T>> mapIndexesToVertices(@NotNull @NotNull Collection<Integer> indexes) throws NegativeVertexIndexException, NoSuchVertexIndexException - Parameters:
indexes
- subset of vertices indexes of this graph.- Returns:
Collection
ofGraph.Vertex
.- Throws:
NegativeVertexIndexException
- if givenCollection
contains negative number(s).NoSuchVertexIndexException
- if givenCollection
contains number that could not be identified with any vertex index.- Since:
- 1.0-beta
-
mapVerticesToIndexes
@NotNull private @NotNull @UnmodifiableView Set<Integer> mapVerticesToIndexes(@NotNull @NotNull Collection<Graph<T>.Vertex<T>> vertices) - Parameters:
vertices
-Collection
ofGraph.Vertex
to map from.- Returns:
- unmodifiable
SortedSet
of vertices indexes mapped from a givenCollection
ofGraph.Vertex
. - Since:
- 1.0
- See Also:
-
toString
This method returns user-friendly representation ofGraph
as each vertex index with its neighbourhood list.It displays in following pattern:
<optional>int -> [<optional>int, ..., <optional>int], content: T
. . .<optional>int -> [<optional>int, ..., <optional>int], content: T
-