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
Nested ClassesModifier and TypeClassDescriptionprivate classGraph.Vertex<E extends T>This inner class implements vertices of this graph. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddNewVertex(int index) This method adds a new vertex to this graph.booleanaddNewVertex(int index, T data) This method adds a new vertex with given data to store in it to this graph.booleanaddNewVertices(@NotNull Collection<Integer> verticesToAdd) This method adds new vertices to this graph.booleanareVerticesOfGraph(@NotNull Collection<Integer> subset) This method checks whether givenCollectionis a subset of vertices of this graph.private intbreadthFirstSearch(@NotNull Collection<Graph<T>.Vertex<T>> subset) This method is a modified implementation of a known traverse algorithm in graphs - breadth-first search.private booleanThis 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.booleanconnectVertices(int indexV, int indexU) This method connects given two vertices by their indexes.private intdepthFirstSearch(@NotNull Collection<Graph<T>.Vertex<T>> subset) This method is a modified implementation of a known traverse algorithm in graphs - depth-first search.booleandisconnectVertices(int indexV, int indexU) This method disconnects given two vertices.booleandoInduceBipartiteSubGraph(@NotNull Collection<Integer> subset) This method checks whether givenCollectioninduces bipartite subgraph of this graph or does not.booleandoInduceConnectedSubGraph(Collection<Integer> subset) This method checks whether givenCollectioninduces 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 givenintindex.final @UnmodifiableView TgetVertexData(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 byintindex.This method returns an unmodifiable sorted set of vertices of this graph.booleanThis method checks whether this graph is bipartite or is not.private booleanisBipartiteSubGraph(@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.booleanisCDS(Collection<Integer> subset) This method checks whether givenCollectionis a connected dominating set of this graph or is not.booleanThis method checks whether this graph is complete or is not.booleanThis method checks whether this graph is connected or disconnected.private booleanisConnectedSubGraph(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.booleanisIndependentSet(Collection<Integer> subset) This method checks whether givenCollectionis an independent set of vertices of this graph or is not.private booleanisInSubsetNotVisited(@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.booleanisVertexOfGraph(int index) This method checks whether this graph contains a vertex with givenintindex.private booleanisVertexOfSubGraph(@NotNull Collection<Graph<T>.Vertex<T>> vertices, int index) This method checks whether given subset contains a vertex with givenintindex.private booleanisVertexOfSubGraph(@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) voidThis method connects required vertices in order to make a graph complete.mapVerticesToIndexes(@NotNull Collection<Graph<T>.Vertex<T>> vertices) booleanremoveVertex(int index) This method removes a vertex from this graph.booleanremoveVertices(@NotNull Collection<Integer> verticesToRemove) This method removes vertices from this graph.voidsetVertexData(int index, T data) This method set the data stored in a vertex by given index.toString()This method returns user-friendly representation ofGraphas 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 aStringparameter. 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 firstintparameter.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 firstintparameter.Default value for vertex indexing starts with
0if 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:
trueif this graph can be mapped to complete,falseotherwise.- Since:
- 1.1
-
getVertex
@NotNull private Graph<T>.@NotNull Vertex<T> getVertex(int index) throws NegativeVertexIndexException, NoSuchVertexIndexException This method returns vertex with givenintindex.- 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 givenintindex.- Since:
- 1.0-beta
- See Also:
-
getVertices
This method returns an unmodifiable sorted set of vertices of this graph. ReturnedSortedSetofIntegercorresponds 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 givenintindex.- 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 givenintindex.- 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 byintindex.- Parameters:
index- numerical index of a vertex.- Returns:
- unmodifiable sorted set of neighbours of a vertex given by
intindex. - Throws:
NegativeVertexIndexException- if parameter typeint < 0.NoSuchVertexIndexException- if this graph does not contain vertex with givenintindex.- Since:
- 1.0
-
isVertexOfGraph
This method checks whether this graph contains a vertex with givenintindex.- Parameters:
index- numerical index of vertex.- Returns:
trueif this graph contains vertex with given index,falseotherwise.- 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 givenintindex.- Parameters:
vertices- vertices subset of graph.index- numerical index of vertex.- Returns:
trueif given subset contains vertex with given index,falseotherwise.- 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:
trueif given subset contains given vertex,falseotherwise.- Since:
- 1.1
-
areVerticesOfGraph
public boolean areVerticesOfGraph(@NotNull @NotNull Collection<Integer> subset) throws NegativeVertexIndexException This method checks whether givenCollectionis a subset of vertices of this graph.- Parameters:
subset-Collectioncontaining indexes of vertices to check if they are in this graph.- Returns:
- true if given
Collectionis a subset of vertices of this graph, false otherwise. - Throws:
NegativeVertexIndexException- if givenCollectioncontains 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:
trueif vertex with givenintindex was added,falseotherwise.- 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:
trueif vertex with givenintindex was added,falseotherwise.- 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-Collectioncontaining indexes of vertices to add.- Returns:
trueif each vertex fromCollectionhas been added to this graph,falseotherwise.- Throws:
NegativeVertexIndexException- if givenCollectioncontains 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:
trueif vertices with given indexes have been connected,falseotherwise.- 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:
trueif vertices with given indexes have been disconnected,falseotherwise.- 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:
trueif vertex with given index has been removed.- Throws:
NegativeVertexIndexException- if parameter typeint < 0.NoSuchVertexIndexException- if this graph does not contain vertex with givenintindex.- 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-Collectioncontaining indexes of vertices to remove.- Returns:
trueif each vertex fromCollectionhas been removed.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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 givenCollectioncan 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:
trueif vertex is in the subset and was not yet visited,falseotherwise.- Since:
- 1.1
-
depthFirstSearch
This method is a modified implementation of a known traverse algorithm in graphs - depth-first search. Only vertices from givenCollectioncan 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:
trueif this graph is connected,falseotherwise.- Since:
- 1.0-beta
-
doInduceConnectedSubGraph
public boolean doInduceConnectedSubGraph(Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollectioninduces connected subgraph of this graph, or does not.- Parameters:
subset-Collectioncontaining indexes of vertices to check if they induce connected subgraph of this graph.- Returns:
trueif givenCollectioninduces connected subgraph of this graph,falseotherwise.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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:
trueif givenCollectioninduces connected subgraph of this graph,falseotherwise.- Since:
- 1.0-beta
- See Also:
-
isComplete
public boolean isComplete()This method checks whether this graph is complete or is not.- Returns:
trueif graph is complete,falseotherwise.- Since:
- 1.0
-
isBipartite
public boolean isBipartite()This method checks whether this graph is bipartite or is not.- Returns:
trueif this graph is bipartite,falseotherwise.- Since:
- 1.0
-
doInduceBipartiteSubGraph
public boolean doInduceBipartiteSubGraph(@NotNull @NotNull Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollectioninduces bipartite subgraph of this graph or does not.- Parameters:
subset-Collectioncontaining indexes of vertices to check if they induce bipartite subgraph of this graph.- Returns:
trueif givenCollectionis a subset of vertices of this graph that induces bipartite subgraph,falseotherwise.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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:
trueif givenCollectioninduces bipartite subgraph of this graph,falseotherwise.- Since:
- 1.0
-
isCDS
public boolean isCDS(Collection<Integer> subset) throws NegativeVertexIndexException, NoSuchVertexIndexException This method checks whether givenCollectionis a connected dominating set of this graph or is not.- Parameters:
subset-Collectioncontaining indexes of vertices to check if they induce a connected dominating set of this graph.- Returns:
trueif givenCollectionis a connected dominating set in this graph,falseotherwise.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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 givenCollectionis an independent set of vertices of this graph or is not.- Parameters:
subset-Collectioncontaining indexes of vertices to check if they induce an independent set in this graph.- Returns:
trueif givenCollectionis an independent set of this graph, false otherwise.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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
CollectionofGraph.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
CollectionofGraph.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
CollectionofGraph.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:
CollectionofGraph.Vertex.- Throws:
NegativeVertexIndexException- if givenCollectioncontains negative number(s).NoSuchVertexIndexException- if givenCollectioncontains 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-CollectionofGraph.Vertexto map from.- Returns:
- unmodifiable
SortedSetof vertices indexes mapped from a givenCollectionofGraph.Vertex. - Since:
- 1.0
- See Also:
-
toString
This method returns user-friendly representation ofGraphas 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
-