mission-peace/interview · error · IllegalArgumentException
Cloning non directed graph
Error message
Cloning non directed graph
What it means
clone() only supports cloning directed graphs; it uses graph.isDirected as a precondition check. If a graph whose isDirected flag is false is passed, it throws IllegalArgumentException("Cloning non directed graph") because the clone routine builds a Graph<>(true) and assumes directed edge semantics.
Solutions
- Construct the graph as directed (new Graph<>(true)) before cloning
- If undirected cloning is needed, implement or use a separate clone routine for undirected graphs
- Check graph.isDirected before calling and branch to the appropriate cloner
Example fix
// before
Graph<T> copy = cloner.clone(undirectedGraph);
// after
if (undirectedGraph.isDirected) {
Graph<T> copy = cloner.clone(undirectedGraph);
} else {
throw new UnsupportedOperationException("use undirected clone");
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!graph.isDirected) {
throw new UnsupportedOperationException("clone requires a directed graph");
} Type guard
boolean isClonable(Graph<T> g) { return g != null && g.isDirected; } Try / catch
try {
Graph<T> copy = cloner.clone(graph);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("undirected graph passed to directed clone", e);
} Prevention
- Always construct graphs used with this cloner via new Graph<>(true)
- Centralize graph creation in one factory so the directed flag is consistent
- Add a unit test asserting clone throws for undirected input
When it happens
Trigger: Calling clone(graph) with a graph created via new Graph<>(false) or any undirected Graph instance.
Common situations: Passing a graph built by a different factory/constructor default, or reusing an undirected graph utility where a directed one was expected after an API change.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
AI-assisted analysis of mission-peace/interview@94be5deb0c (2026-09-08).
Data as JSON: /api/errors/2bdef693a5ad2a97.
Report an issue: GitHub.
Appendix: source
Thrown at src/com/interview/graph/CloneDirectedGraph.java:23
/**
* Given a directed graph clone it in O(n) time where n is total number of edges
* Test cases
* Graph is directed/non directed
* Graph has 0 edges
* Graph has cycle
* Graph is linear
* Graph is dense
* Graph is sparse
*/
public class CloneDirectedGraph<T> {
public Graph<T> clone(Graph<T> graph){
if(graph == null){
return null;
}
if(!graph.isDirected){
throw new IllegalArgumentException("Cloning non directed graph");
}
if(graph.getAllVertex().size() == 0){
throw new IllegalArgumentException("No vertex in the graph");
}
Map<Vertex<T>,Vertex<T>> cloneMap = new HashMap<Vertex<T>,Vertex<T>>();
for(Vertex<T> vertex : graph.getAllVertex()){
clone(vertex,cloneMap);
}
Graph<T> clonedGraph = new Graph<>(true);
for(Vertex<T> vertex : cloneMap.values()){
clonedGraph.addVertex(vertex);
}
return clonedGraph;
}
private void clone(Vertex<T> origVertex,Map<Vertex<T>,Vertex<T>> cloneMap){
Vertex<T> cloneVertex = null;
if(cloneMap.containsKey(origVertex)){View on GitHub (pinned to 94be5deb0c)