Question

Implement the depth-first search (DFS) algorithm. To make your algorithm complete, write the graph search version...

Implement the depth-first search (DFS) algorithm. To make your algorithm complete, write the graph search version of DFS, which avoids expanding any already visited states. Your code should quickly find a solution for tinyMaze.txt (below)

%%%%%%%
% S%
% %%% %
% % %
%% %%
%F %%%%
%%%%%%%

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assuming that we can move in all 8 directions:

The algorithm goes as follows:-

  • From a cell u, run dfs(v) for all v belongs to neighbour of u if they are not visited. Also visit them before calling dfs. If solution is found, return the solution else set visited of v to be false and continue. If no solution is found, then return false.
  • Note that if value of node is %, then return false.
  • Note that if value of node is ' ' then only continue.
  • Note that if the value is 'F' then return true.

Pseudo code:

dfs(i, j):

if val[i][j] == 'D' then return true

if val[i][j] == '%' then return false

for k,l in neighbours(i, j):

if visited[k][l] is false:

visited[k][l] = true

if dfs(k, l) is true then return true

visited[k][l] = false

return false

call dfs(x, y) where val[x][y] is 'S'

neighbours(i, j):

return list((i +- 0,1, j +- 0,1))

In case of any doubts, please ask in comments

Add a comment
Know the answer?
Add Answer to:
Implement the depth-first search (DFS) algorithm. To make your algorithm complete, write the graph search version...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph in Java....

    Implement Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph in Java.(Can be any graph, just an example of DFS and BFS is sufficient) If it cannot be done for a graph, then just an example of DFS and BFS are enough.

  • Write the pseudocode of the Depth First Search Algorithm DFS(G) using adjacencymatrix representation of the graph...

    Write the pseudocode of the Depth First Search Algorithm DFS(G) using adjacencymatrix representation of the graph G. What is the running time of your pseudocode? Specification: start from the psedocode discussed in class and do only the modifications needed for adjacency-matrix graph representation. Below is the pseudocode discussed in class:

  • LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents th...

    JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...

  • Name J#: Q7:20 pts) Conduct a Depth First Search (DFS) on the graph assigned to you....

    Name J#: Q7:20 pts) Conduct a Depth First Search (DFS) on the graph assigned to you. Clearly indicate the Tree edges and Back edges. Identify the articulation points. Show all the steps (incl. the order the vertices are visited. Start your DES from ationpoints. visited). Start your DES from Vertex 1

  • Q6: 20 pts) For the directed graph assigned to you, run the Depth First Search algorithm....

    Q6: 20 pts) For the directed graph assigned to you, run the Depth First Search algorithm. (a) Clearly show the order in which the vertices are pushed and popped. (b) Clearly write the list of edges and their classification into one of the four categories as determined using DFS. (c) Determine whether the directed graph assigned to you is a DAG or not? If it is a DAG. write the topological sort of the vertices.

  • in Java Write a program DFSTrace that contains a version of a depth first search that...

    in Java Write a program DFSTrace that contains a version of a depth first search that prints a trace of its traversal. Write a public static method: public static void dfsPrintTrace(Graph g) { // *** Declare and initialize the marked array. dfsPrintTrace(g, 0, marked, 0); } This method calls a private method: private static void dfsPrintTrace(Graph g, int start, boolean[] marked, int indent) All references to a method below refer to this second method. Every message printed should be preceded...

  • Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

    Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...

  • Figure 1: Graph for Problem 1 Problem 1 Consider a depth-first search on the graph shown in Figure 1, starting with nod...

    Figure 1: Graph for Problem 1 Problem 1 Consider a depth-first search on the graph shown in Figure 1, starting with node c. Consider a node to be "visited" whenever there is a call to dfs with the node as the second argument a) Which nodes are visited, and in what order? Use the convention that graph.neighbors ) produces successors in ascending order of label b) Suppose you call dfs_times (graph, 'c') on the graph above. This function returns dictionaries...

  • Implement a depth-first search of a graph using a recursive function. The result should print the...

    Implement a depth-first search of a graph using a recursive function. The result should print the nodes along the path. Use the following node class. /* * Each node permits iteration to the adjoining nodes. */ class Node implements Iterable { public String getName(); ... }; /* * Return whether a path can be found to the goal. * Return null if no path can be found. */ List findPath(Node from, Node to, Set visited, List sofar) { ... }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT