Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Spring, Year: 2021), B.Sc. in CSE (Day)

LAB REPORT NO # 01
Course Title: Artificial Intelligence Lab
Course Code: CSE-404 Section: 193D3

Student Details

Name ID
Md Shoeb Sikder Pappu 193002041

Submission Date : 08 – 11 – 2022


Course Teacher’s Name : Amena Zahan

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status


Marks: ………………………………… Signature: …..................
Comments: …........................................... Date: …...........................
Name of Experiment
• Implement IDDFS algorithm using Java.

Implementation

• Consider making a breadth-first search into an iterative deepening


Search.

• We can do this by having aside a DFS which will search up to a


limit. It first does a search to a pre-defined limit depth to depth and
then generates a route length1.

• This is done by creating routes of length 1 in the DFS way. Next, it


makes way for routes of depth limit 2, 3, and onwards.

• It even can delete all the preceding calculation all-time at the


beginning of the loop and iterate. Hence at some depth eventually
the solution will be found if there is any in the tree because the
enumeration takes place in order.

Graph
Code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

class Edge
{
int source, dest;

public Edge(int source, int dest)


{
this.source = source;
this.dest = dest;
}
}

class Graph
{

List<List<Integer>> adjList = null;

Graph(List<Edge> edges, int n)


{
adjList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjList.add(new ArrayList<>());
}

for (Edge edge: edges)


{
int src = edge.source;
int dest = edge.dest;

adjList.get(src).add(dest);
adjList.get(dest).add(src);
}
}
}

class Main
{

public static void iterativeDFS(Graph graph, int v, boolean[] discovered)


{

Stack<Integer> stack = new Stack<>();

stack.push(v);

while (!stack.empty())
{

v = stack.pop();

if (discovered[v]) {
continue;
}

discovered[v] = true;
System.out.print(v + " ");

List<Integer> adjList = graph.adjList.get(v);


for (int i = adjList.size() - 1; i >= 0; i--)
{
int u = adjList.get(i);
if (!discovered[u]) {
stack.push(u);
}
}
}
}

public static void main(String[] args)


{

List<Edge> edges = Arrays.asList(


new Edge(1, 2), new Edge(1, 3), new Edge(1, 4), new Edge(2, 5),
new Edge(2, 6), new Edge(5, 7), new Edge(5, 8), new Edge(4, 9),
new Edge(4, 10), new Edge(9, 11), new Edge(9, 12)

);

int n = 13;

Graph graph = new Graph(edges, n);

boolean[] discovered = new boolean[n];

for (int i = 0; i < n; i++)


{
if (!discovered[i]) {
iterativeDFS(graph, i, discovered);
}
}
}
}

Output:
Discussion:
Iterative deepening depth-first search is a hybrid algorithm emerging out of
BFS and DFS. IDDFS might not be used directly in many applications of
Computer Science, yet the strategy is used in searching data of infinite
space by incrementing the depth limit by progressing iteratively. This is
quite useful and has applications in AI and the emerging data sciences
industry.

You might also like