-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathripplepathalgo
More file actions
112 lines (94 loc) · 3.95 KB
/
ripplepathalgo
File metadata and controls
112 lines (94 loc) · 3.95 KB
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
import java.util.*;
class RipplePathAlgorithm {
// Graph representation using adjacency list and edge weights
static class Graph {
Map<String, List<Edge>> adjList = new HashMap<>();
void addEdge(String src, String dest, int weight) {
adjList.putIfAbsent(src, new ArrayList<>());
adjList.putIfAbsent(dest, new ArrayList<>());
adjList.get(src).add(new Edge(dest, weight));
adjList.get(dest).add(new Edge(src, weight)); // For undirected graph
}
static class Edge {
String destination;
int weight;
Edge(String destination, int weight) {
this.destination = destination;
this.weight = weight;
}
}
}
// Function to find the shortest path using Ripple Path Algorithm
public static List<String> findShortestPath(Graph graph, String start, String goal) {
// Create a map to track the distance from start node
Map<String, Integer> distances = new HashMap<>();
// Create a map to track the previous node in the path
Map<String, String> previousNodes = new HashMap<>();
// Initialize all distances to infinity
for (String node : graph.adjList.keySet()) {
distances.put(node, Integer.MAX_VALUE);
}
distances.put(start, 0);
// Relaxation: Propagate outward like a wave (Ripple)
Queue<String> queue = new LinkedList<>();
queue.offer(start);
Set<String> visited = new HashSet<>();
while (!queue.isEmpty()) {
String current = queue.poll();
visited.add(current);
for (Graph.Edge edge : graph.adjList.get(current)) {
String neighbor = edge.destination;
int newDist = distances.get(current) + edge.weight;
// If a shorter path to the neighbor is found
if (newDist < distances.get(neighbor)) {
distances.put(neighbor, newDist);
previousNodes.put(neighbor, current);
if (!visited.contains(neighbor)) {
queue.offer(neighbor);
}
}
}
}
// Detect negative weight cycles by checking for further relaxation
for (String node : graph.adjList.keySet()) {
for (Graph.Edge edge : graph.adjList.get(node)) {
String neighbor = edge.destination;
if (distances.get(node) + edge.weight < distances.get(neighbor)) {
// Negative weight cycle detected
return Collections.singletonList("Negative weight cycle detected.");
}
}
}
// Reconstruct the shortest path
List<String> path = new ArrayList<>();
String currentNode = goal;
while (currentNode != null) {
path.add(currentNode);
currentNode = previousNodes.get(currentNode);
}
// If no path found, return empty list
if (path.size() == 1 && !path.get(0).equals(goal)) {
return Collections.singletonList("No valid path found.");
}
Collections.reverse(path);
return path;
}
public static void main(String[] args) {
Graph graph = new Graph();
// Create graph with nodes and edges (with weights, including negative weights)
graph.addEdge("A", "B", 4);
graph.addEdge("A", "C", 2);
graph.addEdge("B", "C", 5);
graph.addEdge("B", "D", 10);
graph.addEdge("C", "D", 4); // Negative edge
graph.addEdge("D", "E", 3);
graph.addEdge("C", "E", 7);
// Define start and goal
String start = "A";
String goal = "E";
// Find the shortest path
List<String> result = findShortestPath(graph, start, goal);
// Output the result
System.out.println("Shortest path found: " + result);
}
}