|
boolean checkNeighbor(Node first, Node second){ |
|
if(first.neighbors.contains(second)){ |
|
return false; |
|
}else if((second.neighbors.contains(first))){ |
|
return false; |
|
}else{ |
|
return true; |
|
} |
|
} |
this is a clever way to check if a node is a neighbor of another, but it would be better to combine your if and else if into one check. only return false if first is a neighbor of second and second is a neighbor of first. if someone were to hardcode one as a neighbor but not vice versa, this could cause an issue
|
static Graph createRandomUnweightedGraphIter(int n){ //creates n random nodes with randomly assigned unweighted, bidirectional edges |
|
Graph graph = new Graph(); |
|
|
|
for(int i = 0; i<n; i++){ |
|
graph.addNode(i); |
|
} |
for this random graph, you don't add n random nodes to the graph, you always add specific nodes 0 through n-1
|
int randomNode1 = rand.nextInt(n); |
|
int randomNode2 = rand.nextInt(n); |
|
graph.addUndirectedEdge(graph.listOfNodes.get(randomNode1), graph.listOfNodes.get(randomNode2)); |
this code could potentially generate the same 2 random numbers, so you would add an edge from a node to itself
|
return nullArray; //returns empty array if path doesn't exist |
you are covering the case of not returning the path if the destination node is not in the path, but the problem said to return null and not an empty array
|
if(!queue.contains(first) & !flag){ //add first node |
for this if statement the and symbol should be &&, doing one & will throw an error
|
ArrayList<Node> BFTRec(final Graph graph){ //which recursively returns an ArrayList of the Nodes in the Graphi n a valid Breadth-First Traversal order. |
|
ArrayList<Node> vistedNodes = new ArrayList<>(); |
|
Queue<Node> queue = new LinkedList<>(); |
|
boolean flag = false; |
|
return BFTRecHelper(vistedNodes, queue, flag, graph); |
|
} |
|
|
|
ArrayList<Node> BFTRecHelper(ArrayList<Node> visitedNodes, Queue<Node> queue, boolean flag, Graph graph){ //which recursively returns an ArrayList of the Nodes in the Graphi n a valid Breadth-First Traversal order. |
|
Node first = graph.listOfNodes.get(0); |
|
if(!queue.contains(first) & !flag){ //add first node |
|
System.out.print(first.value + "->"); |
|
queue.add(first); |
|
} |
|
Node current = queue.poll(); |
|
current.visted = true; |
|
visitedNodes.add(current); |
|
for(Node neighbor: current.neighbors){ |
|
if(!neighbor.visted){ |
|
neighbor.visted = true; |
|
System.out.print(neighbor.value + "->"); |
|
queue.add(neighbor); |
|
} |
|
} |
|
if(queue.isEmpty()){ //base case |
|
return visitedNodes; |
|
} |
|
return BFTRecHelper(visitedNodes, queue, true, graph); |
|
} |
your BFT looks good, but your code won't cover the case of if the graph contains any disconnected nodes
|
static DirectedGraph createRandomDAGIter(final int n){ |
|
DirectedGraph graph = new DirectedGraph(); |
|
|
|
for(int i = 0; i<n; i++){ |
|
graph.addNode(i); |
|
} |
|
Random rand = new Random(); |
|
int counter = rand.nextInt(n); |
|
|
|
while(counter > 0){ |
|
int randomNode1 = rand.nextInt(n); |
|
int randomNode2 = rand.nextInt(n); |
|
graph.addDirectedEdge(graph.listOfNodes.get(randomNode1), graph.listOfNodes.get(randomNode2)); |
|
counter--; |
|
} |
|
|
|
return graph; |
|
} |
for your random DAG, when you generate 2 random nodes to add an edge to you don't check to make sure the edge is not going backwards. this would create a cycle in the graph and a DAG is acyclic
|
for(int i = 0; i<n; i++){ |
|
graph.addNode(i); |
|
} |
for this random DAG, you don't add n random nodes to the graph, you always add specific nodes 0 through n-1
|
int randomNode1 = rand.nextInt(n); |
|
int randomNode2 = rand.nextInt(n); |
|
graph.addDirectedEdge(graph.listOfNodes.get(randomNode1), graph.listOfNodes.get(randomNode2)); |
this could potentially generate the same random number, and so you would add an edge from a node to itself
|
Stack<Node> stack = new Stack<Node>(); |
|
Stack<Node> output= new Stack<Node>(); |
|
for(Node node: graph.listOfNodes){ |
|
if(!node.visited){ |
|
output = DFSHelper(node, stack); |
|
} |
|
} |
you could save memory space by just using one stack and making your helper method a void method