Skip to content
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
77 changes: 77 additions & 0 deletions Programs/IDS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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);
}
}


8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ It is very easy to contribute, you may follow these steps -
95. [Strassen's Multiplication](https://github.com/PrajaktaSathe/Java/blob/main/Programs/StrassensMultiplication.java) - Program to multiply two square matrices using Strassen's Multiplication method.
96. [Xor Palindrome](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Xorpalindrome.java) - Program/Solution to Xor Palindrome
97. [Streams API](./Programs/JavaStreams.java) - Program to demo the Java Stream API with String and Integer Lists.
98.[Stack](https://github.com/PrajaktaSathe/Java/blob/main/Stack.java)-Program to demo the stack implementation
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
98. [Stack](https://github.com/PrajaktaSathe/Java/blob/main/Stack.java)-Program to demo the stack implementation
99. [RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
101. [Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [iterative deepening search](https://github.com/PrajaktaSathe/Java/blob/main/Programs/IDS.java) - Program demonstrates iterative deepening search algorithm.


# Contributors -
## A big thank you to all our contributors!!!
Expand Down