Class DAG
java.lang.Object
com.thomas.graphs.DAG.DAG
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates a directional edge fromfromNametotoName.
Both bertices are create automatically if they do not existAdds a new vertex to the graph if it is not in the graphbooleanUses a depth-first search to check for cyclesGets a vertex with a given nameGets all the vertices in the graphbooleanhasCycle()Checks for cycles in the graph
Uses a depth-first searchbooleanChecks iffromNamehas an edge totoNamevoidPrints the graph to the standard outputbooleanremoveEdge(String fromName, String toName) Removes the directional edges fromfromNametotoNamebooleanremoveVertex(String name) Removes the vertex with a given name from the graph, including all of it's edgesUses Kahn's alogorithm to sort the graphtoString()
-
Field Details
-
vertices
-
-
Constructor Details
-
DAG
public DAG()Creates a new DAG
-
-
Method Details
-
addVertex
-
removeVertex
Removes the vertex with a given name from the graph, including all of it's edges- Parameters:
name- The name of the vertex- Returns:
trueif the vertex was removed
-
getVertex
-
getVertices
Gets all the vertices in the graph- Returns:
- An unmodifiable collection of the vertices
-
addEdge
Creates a directional edge fromfromNametotoName.
Both bertices are create automatically if they do not exist- Parameters:
fromName- The name of the source vertextoName- the name of the destination vertex- Throws:
IllegalArgumentException- if there is a self-loopIllegalArgumentException- if a cycle is created
-
removeEdge
-
hasEdge
-
hasCycle
public boolean hasCycle()Checks for cycles in the graph
Uses a depth-first search- Returns:
trueif there is a cycle in the graph
-
dfsHasCycle
-
topologicalSort
Uses Kahn's alogorithm to sort the graph- Returns:
- A sorted List of the Graph
- Throws:
IllegalStateException- If a cycle is detected
-
printGraph
public void printGraph()Prints the graph to the standard output -
toString
-