Write in Javascript //Perform depth-limited search from initial state, using def
ID: 3746797 • Letter: W
Question
Write in Javascript
//Perform depth-limited search from initial state, using defined "is_goal_state"
//and "find_successors" functions
//Will not examine paths longer than "depth_limit" (i.e. paths that have "depth_limit" states in them, or "depth_limit-1" actions in them)
//Returns: null if no goal state found
//Returns: object with two members, "actions" and "states", where:
// actions: Sequence(Array) of action ids required to reach the goal state from the initial state
// states: Sequence(Array) of states that are moved through, ending with the reached goal state (and EXCLUDING the initial state)
// The actions and states arrays should both have the same length.
function depth_limited_search(initial_state,depth_limit) {
/***Your code for depth-limited search here!***/
/***DO NOT do repeated state or loop checking!***/
/*
Hint: You may implement DLS either iteratively (with open set) or recursively.
In the iterative case, you will need to do similar to breadth-first search and augment
the state. In addition to predecessor and action, you will also need to store depth.
(You should be able to re-use your BFS code and only make a small amount of changes to
accomplish this. Be sure to remove repeat checking!)
In the recursive case, you don't need the above. Building the solution path is a little
trickier, but I suggest you look into the Array.unshift() function.
*/
}
Explanation / Answer
HI, please find below a Java code for your reference. you can use its functions in your javascript code to get the required solution.
import java.util.*;
public class GFG
{
// This class represents a directed graph using adjacency list representation
static class Graph
{
int V; //Number of Vertices
LinkedList<Integer>[] adj; // adjacency lists
//Constructor
Graph(int V)
{
this.V = V;
adj = new LinkedList[V];
for (int i = 0; i < adj.length; i++)
adj[i] = new LinkedList<Integer>();
}
void addEdge(int v, int w)
{
adj[v].add(w);
}
void DFS(int s)
{
Vector<Boolean> visited = new Vector<Boolean>(V);
for (int i = 0; i < V; i++)
visited.add(false);
Stack<Integer> stack = new Stack<>();
stack.push(s);
while(stack.empty() == false)
{
s = stack.peek();
stack.pop();
// Stack may contain same vertex twice. So we need to print the popped item only if it is not visited.
if(visited.get(s) == false)
{
System.out.print(s + " ");
visited.set(s, true);
}
// Get all adjacent vertices of the popped vertex and if the adjacent has not been visited, then push it to the stack.
Iterator<Integer> itr = adj[s].iterator();
while (itr.hasNext())
{
int v = itr.next();
if(!visited.get(v))
stack.push(v);
}
}
}
}
// Driver program to test methods of graph class
public static void main(String[] args)
{
Graph g = new Graph(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(1, 4);
System.out.println("Following is the Depth First Traversal");
g.DFS(0);
}
}
HI, please find below a Java code for your reference. you can use its functions in your javascript code to get the required solution.
import java.util.*;
public class GFG
{
// This class represents a directed graph using adjacency list representation
static class Graph
{
int V; //Number of Vertices
LinkedList<Integer>[] adj; // adjacency lists
//Constructor
Graph(int V)
{
this.V = V;
adj = new LinkedList[V];
for (int i = 0; i < adj.length; i++)
adj[i] = new LinkedList<Integer>();
}
void addEdge(int v, int w)
{
adj[v].add(w);
}
void DFS(int s)
{
Vector<Boolean> visited = new Vector<Boolean>(V);
for (int i = 0; i < V; i++)
visited.add(false);
Stack<Integer> stack = new Stack<>();
stack.push(s);
while(stack.empty() == false)
{
s = stack.peek();
stack.pop();
// Stack may contain same vertex twice. So we need to print the popped item only if it is not visited.
if(visited.get(s) == false)
{
System.out.print(s + " ");
visited.set(s, true);
}
// Get all adjacent vertices of the popped vertex and if the adjacent has not been visited, then push it to the stack.
Iterator<Integer> itr = adj[s].iterator();
while (itr.hasNext())
{
int v = itr.next();
if(!visited.get(v))
stack.push(v);
}
}
}
}
// Driver program to test methods of graph class
public static void main(String[] args)
{
Graph g = new Graph(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(1, 4);
System.out.println("Following is the Depth First Traversal");
g.DFS(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.