forked from Assasincoderzz/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
173 lines (140 loc) · 5.17 KB
/
Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Node {
char label;
double heuristic;
double cost;
boolean visited;
Node parent;
List<Edge> neighbors;
public Node(char label, double heuristic) {
this.label = label;
this.heuristic = heuristic;
this.cost = Double.MAX_VALUE;
this.visited = false;
this.parent = null;
this.neighbors = new ArrayList<>();
}
public void addNeighbor(Edge edge) {
this.neighbors.add(edge);
}
}
class Edge {
Node target;
double cost;
public Edge(Node target, double cost) {
this.target = target;
this.cost = cost;
}
}
public class Graph {
public static double aStar(Node start, Node goal) {
List<Node> openList = new ArrayList<Node>();
List<Node> closedList = new ArrayList<Node>();
start.cost = 0;
openList.add(start);
while (!openList.isEmpty()) {
Node current = findLowestCost(openList);
openList.remove(current);
closedList.add(current);
if (current == goal) {
return current.cost;
}
for (Edge neighborEdge : current.neighbors) {
Node neighbor = neighborEdge.target;
double tentativeCost = current.cost + neighborEdge.cost;
if (!closedList.contains(neighbor)) {
if (!openList.contains(neighbor) || tentativeCost < neighbor.cost) {
neighbor.cost = tentativeCost;
neighbor.parent = current; // Update the parent
if (!openList.contains(neighbor)) {
openList.add(neighbor);
}
}
}
}
}
return -1; // No path found
}
private static Node findLowestCost(List<Node> nodes) {
Node lowestCostNode = null;
double lowestCost = Double.MAX_VALUE;
for (Node node : nodes) {
if (node.cost + node.heuristic < lowestCost) {
lowestCost = node.cost + node.heuristic;
lowestCostNode = node;
}
}
return lowestCostNode;
}
private static List<Node> getPath(Node start, Node goal) {
List<Node> path = new ArrayList<>();
Node current = goal;
while (current != null) {
path.add(current);
current = current.parent;
}
Collections.reverse(path); // Reverse the path to get it from start to goal
return path;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of vertices: ");
int numVertices = input.nextInt();
Node[] nodes = new Node[numVertices];
for (int i = 0; i < numVertices; i++) {
System.out.print("Enter vertex details (label heuristic) for vertex " + (i + 1) + ": ");
char label = input.next().charAt(0);
double heuristic = input.nextDouble();
nodes[i] = new Node(label, heuristic);
}
System.out.print("Enter the number of edges: ");
int numEdges = input.nextInt();
for (int i = 0; i < numEdges; i++) {
System.out.print("Enter edge details (source destination cost) for edge " + (i + 1) + ": ");
char sourceLabel = input.next().charAt(0);
char targetLabel = input.next().charAt(0);
double cost = input.nextDouble();
Node source = getNodeByLabel(nodes, sourceLabel);
Node target = getNodeByLabel(nodes, targetLabel);
if (source != null && target != null) {
source.addNeighbor(new Edge(target, cost));
} else {
System.out.println("Invalid vertex label. Please try again.");
i--; // Decrement i to re-enter the current edge
}
}
System.out.print("Enter the start vertex label: ");
char startLabel = input.next().charAt(0);
System.out.print("Enter the goal vertex label: ");
char goalLabel = input.next().charAt(0);
input.close();
Node start = getNodeByLabel(nodes, startLabel);
Node goal = getNodeByLabel(nodes, goalLabel);
if (start != null && goal != null) {
double shortestPathCost = aStar(start, goal);
if (shortestPathCost != -1) {
System.out.println("Shortest path cost: " + shortestPathCost);
List<Node> path = getPath(start, goal);
System.out.print("Path: ");
for (Node node : path) {
System.out.print(node.label + " ");
}
} else {
System.out.println("No path found");
}
} else {
System.out.println("Invalid start or goal vertex label");
}
}
private static Node getNodeByLabel(Node[] nodes, char label) {
for (Node node : nodes) {
if (node.label == label) {
return node;
}
}
return null;
}
}