-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathIDS.java
More file actions
77 lines (62 loc) · 2.07 KB
/
IDS.java
File metadata and controls
77 lines (62 loc) · 2.07 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
// IDS algorithm in Java
package Programs;
import java.util.LinkedList;
class IDS {
private final int v; // Total number of vertices
private final LinkedList<Integer>[] adj; // Adjacency list
private final boolean[] visited; // To keep track of visited nodes
IDS(int v) {
this.v = v;
this.adj = new LinkedList[v];
this.visited = new boolean[v];
// Initialize adjacency list and visited array
for (int i = 0; i < this.v; i++) {
adj[i] = new LinkedList<>();
this.visited[i] = false;
}
}
// Add edge from u -> v
public void addEdge(int u, int v) {
this.adj[u].add(v);
}
// Helper function to perform Depth Limited Search (for IDS)
private void DFS(int source, int depthLimit) {
if (depthLimit == 0) {
return; // Reached maximum depth, stop further exploration
}
// If the node is already visited, backtrack
if (this.visited[source]) {
return;
}
// Mark the node as visited
System.out.print(source + " ");
this.visited[source] = true;
// Recur for all the neighbours with the reduced depth limit
LinkedList<Integer> neighbours = this.adj[source];
for (int next : neighbours) {
DFS(next, depthLimit - 1);
}
}
// Iterative Deepening Search (IDS) method
public void ids(int source) {
for (int depth = 0; depth < v; depth++) {
System.out.println("\nDepth limit " + depth + ":");
// Reset the visited array for each depth iteration
for (int i = 0; i < v; i++) {
visited[i] = false;
}
DFS(source, depth); // Perform DFS for the current depth limit
}
}
public static void main(String[] args) {
IDS g = new IDS(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
// Start Iterative Deepening Search from node 2
g.ids(2);
}
}