Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Graph Code #392

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ public class AStar_GridHeuristic {

// An edge class to represent a directed edge
// between two nodes with a certain non-negative cost.
static class Edge {
double cost;
int from, to;

public Edge(int from, int to, double cost) {
if (cost < 0) throw new IllegalArgumentException("No negative edge weights");
this.from = from;
this.to = to;
this.cost = cost;
}
static class NonNegativeCostEdge extends WeightedEdge<Double> {

@Override
public String toString() {
return from + ":" + to;
public NonNegativeCostEdge(int from, int to, double cost) {
super(from, to, cost, c -> c >= 0d);
}
}

Expand Down Expand Up @@ -55,7 +45,12 @@ public int compareTo(Node other) {
// starting node and the destination node the returned value is set to be
// Double.POSITIVE_INFINITY.
public static double astar(
double[] X, double[] Y, Map<Integer, List<Edge>> graph, int start, int end, int n) {
double[] X,
double[] Y,
Map<Integer, List<NonNegativeCostEdge>> graph,
int start,
int end,
int n) {

// In the event that you wish to rebuild the shortest path
// you can do so using the prev array and starting at some node 'end'
Expand Down Expand Up @@ -87,24 +82,24 @@ public static double astar(

if (node.id == end) return G[end];

List<Edge> edges = graph.get(node.id);
List<NonNegativeCostEdge> edges = graph.get(node.id);
if (edges != null) {
for (int i = 0; i < edges.size(); i++) {

Edge edge = edges.get(i);
if (closedSet.contains(edge.to)) continue;
NonNegativeCostEdge edge = edges.get(i);
if (closedSet.contains(edge.getTo())) continue;

double g = node.g + edge.cost;
double h = heuristic(X, Y, edge.to, end);
double g = node.g + edge.getCost();
double h = heuristic(X, Y, edge.getTo(), end);

if (g < G[edge.to] || !openSet.contains(edge.to)) {
if (g < G[edge.getTo()] || !openSet.contains(edge.getTo())) {

G[edge.to] = g;
G[edge.getTo()] = g;
// prev[edge.to] = edge.from;

if (!openSet.contains(edge.to)) {
pq.offer(new Node(edge.to, g, h));
openSet.add(edge.to);
if (!openSet.contains(edge.getTo())) {
pq.offer(new Node(edge.getTo(), g, h));
openSet.add(edge.getTo());
}
}
}
Expand All @@ -130,7 +125,7 @@ public static void main(String[] args) {
Random RANDOM = new Random();

int n = 20 * 20;
Map<Integer, List<Edge>> graph = new HashMap<>();
Map<Integer, List<NonNegativeCostEdge>> graph = new HashMap<>();
for (int i = 0; i < n; i++) graph.put(i, new ArrayList<>());

double[] X = new double[n];
Expand Down Expand Up @@ -185,10 +180,10 @@ public static void main(String[] args) {
}

static void addEdge(
Map<Integer, List<Edge>> graph, int f, int t, int fx, int fy, int tx, int ty) {
Map<Integer, List<NonNegativeCostEdge>> graph, int f, int t, int fx, int fy, int tx, int ty) {
double dx = Math.abs(fx - tx);
double dy = Math.abs(fy - ty);
graph.get(f).add(new Edge(f, t, dx + dy));
graph.get(f).add(new NonNegativeCostEdge(f, t, dx + dy));
}

// Node class to track the nodes to visit while running Dijkstra's
Expand All @@ -213,7 +208,8 @@ public int compareTo(DNode other) {
// from a starting node to an ending node. If there is no path between the
// starting node and the destination node the returned value is set to be
// Double.POSITIVE_INFINITY.
public static double dijkstra(Map<Integer, List<Edge>> graph, int start, int end, int n) {
public static double dijkstra(
Map<Integer, List<NonNegativeCostEdge>> graph, int start, int end, int n) {

// Maintain an array of the minimum distance to each node
double[] dists = new double[n];
Expand Down Expand Up @@ -243,21 +239,21 @@ public static double dijkstra(Map<Integer, List<Edge>> graph, int start, int end
// processing this node so we can ignore it.
if (node.value > dists[node.id]) continue;

List<Edge> edges = graph.get(node.id);
List<NonNegativeCostEdge> edges = graph.get(node.id);
if (edges != null) {
for (int i = 0; i < edges.size(); i++) {
Edge edge = edges.get(i);
NonNegativeCostEdge edge = edges.get(i);

// You cannot get a shorter path by revisiting
// a node you have already visited before
if (visited[edge.to]) continue;
if (visited[edge.getTo()]) continue;

// Update minimum cost if applicable
double newDist = dists[edge.from] + edge.cost;
if (newDist < dists[edge.to]) {
// prev[edge.to] = edge.from;
dists[edge.to] = newDist;
pq.offer(new DNode(edge.to, dists[edge.to]));
double newDist = dists[edge.getFrom()] + edge.getCost();
if (newDist < dists[edge.getTo()]) {
// prev[edge.getTo()] = edge.getFrom();
dists[edge.getTo()] = newDist;
pq.offer(new DNode(edge.getTo(), dists[edge.getTo()]));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@

public class BellmanFordEdgeList {

// A directed edge
public static class Edge {
double cost;
int from, to;

public Edge(int from, int to, double cost) {
this.to = to;
this.from = from;
this.cost = cost;
}
}

/**
* An implementation of the Bellman-Ford algorithm. The algorithm finds the shortest path between
* a starting node and all other nodes in the graph. The algorithm also detects negative cycles.
Expand All @@ -30,7 +18,7 @@ public Edge(int from, int to, double cost) {
* @param V - The number of vertices in the graph.
* @param start - The id of the starting node
*/
public static double[] bellmanFord(Edge[] edges, int V, int start) {
public static double[] bellmanFord(WeightedEdge<Double>[] edges, int V, int start) {

double[] dist = new double[V];
java.util.Arrays.fill(dist, Double.POSITIVE_INFINITY);
Expand All @@ -44,9 +32,9 @@ public static double[] bellmanFord(Edge[] edges, int V, int start) {
// For each vertex, apply relaxation for all the edges
for (int v = 0; v < V - 1 && relaxedAnEdge; v++) {
relaxedAnEdge = false;
for (Edge edge : edges) {
if (dist[edge.from] + edge.cost < dist[edge.to]) {
dist[edge.to] = dist[edge.from] + edge.cost;
for (WeightedEdge<Double> edge : edges) {
if (dist[edge.getFrom()] + edge.getCost() < dist[edge.getTo()]) {
dist[edge.getTo()] = dist[edge.getFrom()] + edge.getCost();
relaxedAnEdge = true;
}
}
Expand All @@ -58,9 +46,9 @@ public static double[] bellmanFord(Edge[] edges, int V, int start) {
relaxedAnEdge = true;
for (int v = 0; v < V - 1 && relaxedAnEdge; v++) {
relaxedAnEdge = false;
for (Edge edge : edges) {
if (dist[edge.from] + edge.cost < dist[edge.to]) {
dist[edge.to] = Double.NEGATIVE_INFINITY;
for (WeightedEdge<Double> edge : edges) {
if (dist[edge.getFrom()] + edge.getCost() < dist[edge.getTo()]) {
dist[edge.getTo()] = Double.NEGATIVE_INFINITY;
relaxedAnEdge = true;
}
}
Expand All @@ -70,20 +58,21 @@ public static double[] bellmanFord(Edge[] edges, int V, int start) {
return dist;
}

@SuppressWarnings("unchecked")
public static void main(String[] args) {

int E = 10, V = 9, start = 0;
Edge[] edges = new Edge[E];
edges[0] = new Edge(0, 1, 1);
edges[1] = new Edge(1, 2, 1);
edges[2] = new Edge(2, 4, 1);
edges[3] = new Edge(4, 3, -3);
edges[4] = new Edge(3, 2, 1);
edges[5] = new Edge(1, 5, 4);
edges[6] = new Edge(1, 6, 4);
edges[7] = new Edge(5, 6, 5);
edges[8] = new Edge(6, 7, 4);
edges[9] = new Edge(5, 7, 3);
WeightedEdge<Double>[] edges = new WeightedEdge[E];
edges[0] = new WeightedEdge<>(0, 1, 1d);
edges[1] = new WeightedEdge<>(1, 2, 1d);
edges[2] = new WeightedEdge<>(2, 4, 1d);
edges[3] = new WeightedEdge<>(4, 3, -3d);
edges[4] = new WeightedEdge<>(3, 2, 1d);
edges[5] = new WeightedEdge<>(1, 5, 4d);
edges[6] = new WeightedEdge<>(1, 6, 4d);
edges[7] = new WeightedEdge<>(5, 6, 5d);
edges[8] = new WeightedEdge<>(6, 7, 4d);
edges[9] = new WeightedEdge<>(5, 7, 3d);

double[] d = bellmanFord(edges, V, start);

Expand Down
Loading