Below is my bfs algorithm, the algorithm works and finds the node given the start and target. But I want to save edges for the used path in a linkedList to draw the path.
My BFS:
public DGPath breadthFirstSearch(String startId, String targetId) {
V start = this.getVertexById(startId);
V target = this.getVertexById(targetId);
if (start == null || target == null) return null;
DGPath path = new DGPath();
path.start = start;
path.visited.add(start);
// easy target
if (start == target) return path;
// TODO calculate the path from start to target by breadth-first-search
// register all visited vertices while going, for statistical purposes
// if you hit the target: complete the path and bail out !!!
Queue<V> fifoQueue = new LinkedList<>();
Map<V,V> visitedFrom = new HashMap<>();
fifoQueue.offer(start);
visitedFrom.put(start, null);
while (!fifoQueue.isEmpty()) {
V current = fifoQueue.poll();
for (E e : current.getEdges()) {
V neighbour = e.getTo();
path.visited.add(neighbour);
if (neighbour == target) {
while (current != null) {
path.getEdges().addFirst(e);
current = visitedFrom.get(current);
}
return path;
} else if (!visitedFrom.containsKey(neighbour)) {
visitedFrom.put(neighbour,current);
fifoQueue.offer(neighbour);
}
}
}
// no path found, graph was not connected ???
return null;
}
The DGPath is the class that creates the path as shown below:
public class DGPath {
private V start = null;
private LinkedList<E> edges = new LinkedList<>();
private double totalWeight = 0.0;
private Set<V> visited = new HashSet<>();
/**
* representation invariants:
* 1. The edges are connected by vertices, i.e. FOR ALL i: 0 < i < edges.length: edges[i].from == edges[i-1].to
* 2. The path begins at vertex == start
* 3. if edges is empty, the path also ends at vertex == start
* otherwise edges[0].from == start and the path continues along edges[i].to for all 0 <= i < edges.length
**/
#Override
public String toString() {
StringBuilder sb = new StringBuilder(
String.format("Weight=%f Length=%d Visited=%d (",
this.totalWeight, 1 + this.edges.size(), this.visited.size()));
sb.append(start.getId());
for (E e : edges) {
sb.append(", " + e.getTo().getId());
}
sb.append(")");
return sb.toString();
}
public V getStart() {
return start;
}
public LinkedList<E> getEdges() {
return edges;
}
public double getTotalWeight() {
return totalWeight;
}
public Set<V> getVisited() {
return visited;
}
}
I want to save the right edges in de linkedlist edges from the BGPath class (called path in my BFS algo method). So I already saved the used vertices in a map to go back to the root. But when I add the edge to the path it just saves the last edge used multiple times.. The problem is the vertex can have multiple edges, so I need to add the edge from the previous that was pointing to the last "current" until I'm back to the root. But I cant wrap my head around the right way to do this.
The line where I now add the edge to the list of edges is: path.getEdges().add(e)
I think, your problem is the same line, where your are adding the edges, that line is adding the same edge in inner while loop again and again, so you are only traversing back but not adding those nodes to your edges list.
I think, it should be like this
while (!fifoQueue.isEmpty()) {
V current = fifoQueue.poll();
for (E e : current.getEdges()) {
V neighbour = e.getTo();
path.visited.add(neighbour);
if (neighbour == target) {
path.getEdges().addFirst(e):
while (current != null) {
path.getEdges().addFirst(current) ;
current = visitedFrom.get(current);
}
return path;
} else if (!visitedFrom.containsKey(neighbour)) {
visitedFrom.put(neighbour,current);
fifoQueue.offer(neighbour);
}
}
}
// no path found, graph was not connected ???
return null;
}
Related
I have 6 Red Black trees filled with different data, but the further down I go, the more my trees get mixed up. I've commented out all but two of them, here's my code for those two trees.
Scanner co2DateFile = new Scanner(new File("co2.csv"));
Scanner co2NumFile = new Scanner(new File("co2.csv"));
RedBlackBST co2DateKey = new RedBlackBST();
RedBlackBST co2NumKey = new RedBlackBST();
while (co2DateFile.hasNextLine()) {
String[] line3 = co2DateFile.nextLine().split(",");
if (line3[0].equals("World")) {
LocalDate date = parseDateString(line3[2]);
String stringDate = date.toString();
co2DateKey.root = co2DateKey.put(co2DateKey.root, stringDate, line3[3]);
}
}
while (co2NumFile.hasNextLine()) {
String[] line4 = co2NumFile.nextLine().split(",");
if (line4[0].equals("World")) {
co2NumKey.root = co2NumKey.put(co2NumKey.root, line4[3], line4[2]);
}
}
co2DateKey.inorder(co2DateKey.root);
I'm using the same file for both trees, but using different pieces of data for the keys and values. If I print co2DateKey inorder, then it prints the keys/values for co2DateKey and co2NumKey, but if I comment out the code for co2NumKey, then co2DateKey only prints its own keys/values. I'm not sure where they're getting crossed, so any help would be appreciated.
Here's my RedBlackBST class:
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
import java.io.File;
import java.io.FileNotFoundException;
class Node
{
Node left;
Node right; // initializing Nodes and variables
String key;
String value;
String type;
boolean color; // true means color is red, false is black
Node(String key, String value)
{
this.key = key;
this.value = value;
left = null;
right = null;
color = true; // new nodes are always red
}
}
public class RedBlackBST {
public static Node root = null; // root initialized
public Node rotateLeft(Node myNode)
{
Node child = myNode.right;
Node childLeft = child.left; // assigning variables
child.left = myNode;
myNode.right = childLeft; // rotating nodes to the left
return child;
}
public Node rotateRight(Node myNode)
{
Node child = myNode.left; // assigning variables
Node childRight = child.right;
child.right = myNode; // rotating nodes to the right
myNode.left = childRight;
return child;
}
public void flipColors(Node x, Node y) // flipping colors of two given nodes
{
boolean temp = x.color; // uses temp to store color of first node
x.color = y.color; // first node takes second node's color
y.color = temp; // second node takes first node's color
}
public String get(String key) {
return get(root, key); // this function is called from main, which calls recursive get
}
private String get(Node x, String key) {
while (x != null) {
int cmp = key.compareTo(x.key); // compares current key with the one we are searching for
if (cmp < 0)
x = x.left;
else if (cmp > 0)
x = x.right; // recursively searches through tree until key is found
else
return x.value; // returns value associated with said key
}
return null;
}
public boolean getColor(String key) {
return getColor(root, key); // this function is called from main, which calls recursive getColor
}
private boolean getColor(Node x, String key) {
while (x != null) {
int cmp = key.compareTo(x.key); // same idea as get
if (cmp < 0)
x = x.left;
else if (cmp > 0) // recursively searches through tree to find key
x = x.right;
else
return x.color; // returns color of node associated with said key
}
return false;
}
public boolean isRed(Node myNode)
{
if (myNode == null) // checks color of node passed into function, returns true if red
return false;
return (myNode.color == true);
}
// insertion into Left Leaning Red Black Tree.
public Node put(Node myNode, String key, String value)
{
// inserting node, checks for violations to left leaning red black tree come next
if (myNode == null)
return new Node(key, value);
if (key.compareTo(myNode.key) < 0) // compares keys, recursive calls to find proper spot
myNode.left = put(myNode.left, key, value);
else if (key.compareTo(myNode.key) > 0)
myNode.right = put(myNode.right, key, value);
else if (key.equals(myNode.key) == true) // if key is already in tree, numeric value is replaced
myNode.value = value;
else
return myNode;
// case 1.
// when right child is Red but left child is
// Black or doesn't exist.
if (isRed(myNode.right) && !isRed(myNode.left))
{
myNode = rotateLeft(myNode); // left rotate the node to make it into valid structure.
flipColors(myNode, myNode.left); // swap the colors as the child node should always be red
}
// case 2
// when left child as well as left grand child are Red
if (isRed(myNode.left) && isRed(myNode.left.left))
{
myNode = rotateRight(myNode); // right rotate the current node to make it into a valid structure, then flip colors
flipColors(myNode, myNode.right);
}
// case 3
// when both left and right child are Red in color.
if (isRed(myNode.left) && isRed(myNode.right))
{
myNode.color = !myNode.color; // invert the color of node as well it's left and right child
myNode.left.color = false; // change the color to black
myNode.right.color = false;
}
return myNode;
}
public Iterable<String> keys(Node node, Queue<String> queue) { // uses inorder traversal to put keys in right order
if (node != null)
{
keys(node.left, queue);
queue.add(node.key); // adds each key to queue in correct order
keys(node.right, queue);
}
return queue; // returns queue after all keys have been added
}
public String highest(Node node) {
Node current = node;
while (current.right != null) {
current = current.right;
}
return current.key;
}
void inorder(Node node)
{
if (node != null)
{
inorder(node.left);
System.out.println(node.key + " " + node.value);
inorder(node.right);
}
}
Problem is here:
public static Node root = null; // root initialized
The root is declared static, which means it is shared between all instances of RedBlackBST. They all have the same root. Sure they will be mixed up.
Remove the static keyword.
I have an assignment to create an A* algorithm through a road graph of the UK through a given template (all files for this assignment I have uploaded here
I think, however, that the problem is somewhere in my Planner.java file - the A* algorithm is not finding a node to expand, it just abruptly stops without expanding any nodes at all.
Here is the code for the Planner :
public class Planner implements PlannerInterface<Object> {
// declaration and instantination of our open and closed lists
private final OpenList openList = new OpenList();
private final ArrayList<SearchThroughNodes> closedList;
public boolean empty() {
int lengthOfItem = 0;
return (lengthOfItem == 0);
}
// constructor of closed list
public Planner() {
this.closedList = new ArrayList<>();
}
#Override
public List<GraphEdge> plan(RoadGraph graph, GraphNode origin, GraphNode destination) {
// Temporary costs and other data needed
SearchThroughNodes currentNode, temp1, temp2;
temp2 = new SearchThroughNodes(null, 0, 0, null);
GraphNode temp;
double temporaryCost, heuristics;
ArrayList<GraphEdge> finalResult;
finalResult = new ArrayList<>();
// check if origin and destination exist a.k.a are not set to null
boolean originExistence;
boolean destinationExistence;
destinationExistence = false;
originExistence = false;
if (origin != null && destination != null) {
originExistence = true;
destinationExistence = true;
}
// Pre-requisites for our A-Star to work
if (originExistence == true && !destination.equals(origin) && destinationExistence == true ) {
openList.add(new SearchThroughNodes(origin, 0, Utils.distanceInKM(origin, destination) / 120, null));
} else {
return null;
}
// A-star loop
while (!openList.empty()) {
// Get BEST node to expand
currentNode = (SearchThroughNodes) openList.get();
if (closedList.contains(currentNode)) {
continue;
}
// We reached the destination
// go back through nodes and read path
GraphNode checkCurrNode;
checkCurrNode = currentNode.getGraphNode();
if (destination.equals(checkCurrNode)) {
temp1 = currentNode;
temp2 = (SearchThroughNodes) currentNode.getPrecedingItem();
while (!temp2.getGraphNode().equals(origin)) {
finalResult.add(0, graph.getEdge(temp2.getGraphNode().getId(), temp1.getGraphNode().getId()) );
temp1 = temp2;
temp2 = (SearchThroughNodes) temp2.getPrecedingItem();
}
finalResult.add(0, graph.getEdge(temp2.getGraphNode().getId(), temp1.getGraphNode().getId()));
return finalResult;
}
closedList.add(currentNode);
long currentNodeId = currentNode.getGraphNode().getId();
List<GraphEdge> outEdges = graph.getNodeOutcomingEdges(currentNodeId);
//if expandable
if (outEdges != null) {
// traverse all nodes after currentNode
for (GraphEdge edge : graph.getNodeOutcomingEdges(currentNodeId)) {
long getFromEdge;
getFromEdge = edge.getToNodeId();
//Look at node at the end of the current edge
temp = graph.getNodeByNodeId(getFromEdge);
//store nodeID in tmp2 for traversal of openList
long tmp2GetID;
tmp2GetID = temp.getId();
temp2.setNodeID(tmp2GetID);
// set current edge length in kms,edge max allowed speed and current node cost to variables
// to later compute the temporary cost
double edgeLengthInKMs = edge.getLengthInMetres() / 1000;
double edgeMaxAllowSpeed = edge.getAllowedMaxSpeedInKmph();
double currCost = currentNode.getCost();
//new heuristics and cost
temporaryCost = currCost + edgeLengthInKMs / edgeMaxAllowSpeed;
heuristics = Utils.distanceInKM(temp, destination) / 120;
// Proceed here if node wasn't expanded
if (!closedList.contains(temp2)) {
// if node isn't contained currently in closedList
// if not,check and update accordingly
if (!openList.contains(temp2)) {
openList.add(new SearchThroughNodes(temp,
temporaryCost, heuristics, currentNode));
} else {
temp1 = openList.getNode(temp2);
double tempOneCost = temp1.getCost();
if (tempOneCost > temporaryCost) {
temp1.update(temporaryCost, currentNode);
}
openList.insert(temp1);
}
}
}
}
}
return null;
}
#Override
public AbstractOpenList<Object> getOpenList() {
return openList;
}
}
So I creating a minecraft plugin where I am in need of a graph to create a navigation system. I researched a bit and found out that I should be able to use Dijkstra, but I have a problem. When searching for the shortest path I am sometimes getting an infinite loop(not always, it usally works the first 2-3 runs but after that it goes into the loop).
When the player wants to get to a destination I search for the closest vertex and use computePaths with that vertex as parameter. When I then run the getShortestPathTo it sometimes gets stuck in an infinite loop and I run out of memory(which makes sence since im adding the same vertexes to the list). Can you see why it is getting stuck? As far as I knew Dijkstra should be able to handle going from A node to B node and from B node to A node right?
Below is my code:
public class Dijkstra {
public static void computePaths(Vertex source) {
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (Edge e : u.adjacencies) {
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
public static List<Vertex> getShortestPathTo(Vertex target) {
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous) {
path.add(vertex);
}
Collections.reverse(path);
return path;
}
}
and the vertex class:
public class Vertex implements Comparable<Vertex>
{
public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Location location;
public Vertex(String argName) { name = argName; }
public Vertex(String argName,Location l) { name = argName; location = l;}
public String toString() { return name; }
public int compareTo(Vertex other)
{
return Double.compare(minDistance, other.minDistance);
}
}
When first the plugin is enabled I load all the vertexes from a config file looking something like this(It is the test one i am using)
I am adding vertexes and edges here(not sure if relevent but thought it might be?):
public void loadAllVertex() {
ConfigurationSection section = nodeConfig.config.getConfigurationSection("nodes");
for (String key: section.getKeys(false)) {
String locationString = nodeConfig.getString("nodes." + key + ".location");
if (locationString == null)
return;
String[] locationSplit = locationString.split(",");
if (locationSplit.length <=1) {
log.log(Level.SEVERE, "Location is not specified correctly in nodes.yml");
return;
}
Location l = new Location(Bukkit.getWorlds().get(0),Integer.parseInt(locationSplit[0]),Integer.parseInt(locationSplit[1]),Integer.parseInt(locationSplit[2]));
Vertex tmpVertex = new Vertex(key, l);
allNodes.add(tmpVertex);
}
for (Vertex v : allNodes) {
String path = "nodes." + v.name + ".connectedTo";
List<String> connectedTo = nodeConfig.getStringList(path,true);
List<Edge> edges = new ArrayList<>();
for (String sideNodeName : connectedTo) {
Vertex vertexCon = GraphUtils.getVertexByName(allNodes, sideNodeName);
if (vertexCon == null) {
log.warning("The '" + sideNodeName + "' node is not defined");
return;
}
//A.adjacencies = new Edge[]{ new Edge(M, 8) };
edges.add(new Edge(vertexCon,vertexCon.location.distance(v.location)));
}
Edge[] arrayEdges = new Edge[edges.size()];
arrayEdges = edges.toArray(arrayEdges);
v.adjacencies = arrayEdges;
}
}
Think i found the error, so I never had any loops the first time I ran the compute path and findshortestpath so I finally figured out that I could not be resetting things correctly(should have been obvious I know) - I didn't update the vertexes afterwards. So I added a method to reset the mindistance and previous attributes and this seems to have done the trick.
I am fairly new to java and I have been struggling with this exercise for two weeks now(It's an homework exercise in my school). I need to create a topological sort and print out all of the possible connections. I have read a lot about topological sorting now, but we have this certain line of code that we have to work with. I'm pretty sure I could do the topological sorting when I have the list of vertices. My problem is, I don't know how to list all of the vertices from this given code. Could anyone give me some tips or leads or perhaps an example, I would really really appreciate it.
Here is the given code we need to work with:
import java.util.*;
public class Answer {
public static void main (String[] args) {
Answer a = new Answer();
a.run();
}
public void run() {
// TODO!!! YOUR TESTS HERE!
Graph g = new Graph ("G");
Vertex a = new Vertex ("A");
Vertex b = new Vertex ("B");
Vertex c = new Vertex ("C");
g.first = a;
a.next = b;
b.next = c;
Edge ab = new Edge ("AB");
Edge ac = new Edge ("AC");
Edge ba = new Edge ("BA");
Edge ca = new Edge ("CA");
a.first = ab;
b.first = ba;
c.first = ca;
ab.next = ac;
ab.target = b;
ac.target = c;
ba.target = a;
ca.target = a;
System.out.println (g);
}
class Vertex {
String id;
Vertex next;
Edge first;
Vertex (String s, Vertex v, Edge e) {
id = s;
next = v;
first = e;
}
Vertex (String s) {
this (s, null, null);
}
#Override
public String toString() {
return id;
}
// TODO!!! Your Vertex methods here!
} // Vertex
class Edge {
String id;
Vertex target;
Edge next;
Edge (String s, Vertex v, Edge e) {
id = s;
target = v;
next = e;
}
Edge (String s) {
this (s, null, null);
}
#Override
public String toString() {
return id;
}
// TODO!!! Your Edge methods here!
} // Edge
class Graph {
String id;
Vertex first;
Graph (String s, Vertex v) {
id = s;
first = v;
}
Graph (String s) {
this (s, null);
}
#Override
public String toString() {
String nl = System.getProperty ("line.separator");
StringBuffer sb = new StringBuffer (nl);
sb.append (id + nl);
Vertex v = first;
while (v != null) {
sb.append (v.toString() + " --> ");
Edge e = v.first;
while (e != null) {
sb.append (e.toString());
sb.append ("(" + v.toString() + "->"
+ e.target.toString() + ") ");
e = e.next;
}
sb.append (nl);
v = v.next;
}
return sb.toString();
}
// TODO!!! Your Graph methods here!
} // Graph
}
Apparently, the graph has a reference to the first vertex, and the vertices themselves are linked together into a singly linked list. This code should be all you need to collect the vertices into a Java list:
public List<Vertex> allVertices(Graph g) {
final List<Vertex> vertices = new ArrayList<>();
for (Vertex v = g.first; v != null; v = v.next)
vertices.add(v);
return vertices;
}
I would suggest that you add a "lastvisited" integer field to the edge which is set to zero, or use a boolean "visited"(true/false). Then start at one vertex. Assuming the graph is connected, you will reach all vertexes by going over the unvisited edges for one vertex, then following the edges to the vertex it leads to, marking the edge as followed, and calling your count function for this vertex recursively.
I.E.: count(node) = sum(my unvisited edges) mark_edge_as_visited(edge), count(edge.target)
Please note that you also have to consider that the graph appears to be a directed graph, so an edge leading from a to b and from b to a is counted as two edges.
Edit: I made a mistake, you also need to mark the vertex as visited, or it will be visited twice (I was thinking of an undirected graph).
Preface: I know that there are high quality graph APIs available. I'm interested in writing my own for self-improvement.
This is my function to add nodes:
public void addNode(Vertex v, Collection<Edge> neighbors) {
int originalSize = size();
if (head == null) {
head = v;
}
else {
Collection<Edge> inEdges = new ArrayList<Edge>();
inEdges.addAll(neighbors);
traverseGraphToAdd(head, inEdges, v);
}
assert originalSize + 1 == size() :
String.format("adding operation failed. original size: %d, current size: %d", originalSize, size());
}
private void traverseGraphToAdd(Vertex start, Collection<Edge> inEdges, Vertex toAdd) {
Iterator<Edge> iter = inEdges.iterator();
Edge e;
while (iter.hasNext()) {
e = iter.next();
if (e.getSource().equals(start)) {
start.addEdge(e);
iter.remove();
}
else if (! directionalEdges && e.getSink().equals(start)) {
start.addEdge(e);
iter.remove();
}
}
if (inEdges.size() > 0) { //otherwise there's no point in continuing to search
for (Edge arc : start.getOutEdges()) {
traverseGraphToAdd(arc.getSink(), inEdges, toAdd);
}
}
}
Size and its dependencies:
public int size() {
int count = 0;
if (head == null) {
return 0;
}
else {
count = countNodes(head);
}
clearVisited();
return count;
}
private int countNodes(Vertex start) {
int result = 1;
start.setVisited(true);
for (Edge e: start.getOutEdges()) {
if (! e.getSink().isVisited()) {
result += countNodes(e.getSink());
}
}
return result;
}
private void clearVisited() {
if (head != null) {
clearNode(head);
}
}
private void clearNode(Vertex start) {
start.setVisited(false);
for (Edge e: start.getOutEdges()) {
if (e.getSink().isVisited()) {
clearNode(e.getSink());
}
}
}
The Edge class:
public Edge(Vertex source, Vertex sink, int weight) {
this.source = source;
this.sink = sink;
this.weight = weight;
}
The following call works:
g.addNode(ftw, new HashSet<Edge>()); //first node - empty edges
g.addNode(odp, Arrays.asList(new Edge(ftw, odp, 3))); //link new node to one already in the graph
This does not:
g.addNode(tlt, Arrays.asList(new Edge(tlt, ftw, 2)));
In this one, the first argument of the Edge constructor is not the node already in the graph. I try to rectify this in addNode with the following (repeated from above):
if (e.getSource().equals(start)) { /*... */ }
else if (! directionalEdges && e.getSink().equals(start)) { /*... */ }
directionalEdges is a class field that determines whether or not this graph is directional or not.
However, this causes assertion errors:
Exception in thread "main" java.lang.AssertionError: adding operation failed. original size: 1, current size: 1
What is going on here?
The graph you're trying to create in your example looks like this:
tlt -> ftw -> odp
After creating ftw -> odp, you should (and do, I believe) have head == ftw. After adding tlt, you should have head == tlt if you want your traversal algorithm to work properly. But in the code you've shown us, there is only one place where head is assigned to, and that happens only in the condition when head == null, in the fifth line of addNode(). Therefore, head doesn't change when you add tlt, and so traverseGraphToAdd() therefore starts form ftw instead of tlt as you intend for it to.
You have a more general problem here, however, namely that your code isn't able to handle directed graphs which aren't rooted (that is, they have more than one source node.) Consider what would happen if you wanted a graph like this one:
a -> b <- c
I think you'd have a problem with this, since you no longer have a single head.