Git Product home page Git Product logo

graphs's Introduction

Graphs

BFT, DFS, Mazes, DAG, Dijkstra, Greedy Path-finding Algos, A*, Kahns, mDFS

-TraverseThisTown: DFS and BFT both recursive and iterative
-ThankUVertext: mDFS, Kahns
-UnoDoTreCuatroINodeYouWantMe: Dijkstra's Algorithm
-WhenYouWishUponAStar: A* Algorithm

graphs's People

Contributors

ananyasingh7 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

graphs's Issues

Andrew Peters - Code Review

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

Code Review - Siddharth Patel

You're probably running into situations where the same node is being randomly generated for graphs that only have a few nodes. You could increase the bound.

while(counter > 0){
int randomNode1 = rand.nextInt(n);
int randomNode2 = rand.nextInt(n);
graph.addUndirectedEdge(graph.listOfNodes.get(randomNode1), graph.listOfNodes.get(randomNode2));
counter--;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.