Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi, i have a code that works for dfs. but i ran it through a graph that has a cycle and it still bave me

hi, i have a code that works for dfs. but i ran it through a graph that has a cycle and it still bave me an answer. i need a loop that makes it not do the dfs when the vertex has been visited(like when it ha a cycle in it) i attached a picture of a graph that has a cycle in it. thanks!
image text in transcribed
that is the code i have :
public class DFS {
private Stack stack;
public DFS(){
this.stack = new Stack();
}
public void dfs(List vertexList){
while(!stack.contains(vertexList)){
for(Vertex v : vertexList){
if(!v.isVisited()){
v.setVisited(true);
dfsWithStack(v);
}
}
}
}
private void dfsWithStack(Vertex rootVertex) {
this.stack.add(rootVertex);
rootVertex.setVisited(true);
while (!stack.isEmpty()) {
Vertex actualVertex = this.stack.pop();
System.out.print(actualVertex + " ");
for (Vertex v : actualVertex.getAdjencyList()) {
if (!v.isVisited()) {
v.setVisited(true);
this.stack.push(v);
}
}
}
}
import java.util.ArrayList;
import java.util.List;
/**
*
* @author CHIDERA
*/
public class Vertex {
private String name;
private boolean visited;
private List adjencyList;
public Vertex(String name){
this.name = name;
this.adjencyList = new ArrayList();
}
public void addVerticesNeighbors(Vertex vertex){
this.adjencyList.add(vertex);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public boolean isVisited(){
return visited;
}
public void setVisited(boolean visited){
this.visited = visited;
}
public List getAdjencyList(){
return adjencyList;
}
public void setAdjencyList(List adjencyList){
this.adjencyList = adjencyList;
}
@Override
public String toString(){
return this.name;
}
}
package data;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author CHIDERA
*/
public class Data {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Vertex v1 = new Vertex("a");
Vertex v2 = new Vertex("b");
Vertex v3 = new Vertex("c");
Vertex v4 = new Vertex("d");
Vertex v5 = new Vertex("e");
Vertex v6 = new Vertex("f");
Vertex v7 = new Vertex("g");
List list = new ArrayList();
v1.addVerticesNeighbors(v2);
v1.addVerticesNeighbors(v3);
v2.addVerticesNeighbors(v7);
v2.addVerticesNeighbors(v5);
v3.addVerticesNeighbors(v6);
v4.addVerticesNeighbors(v1);
v4.addVerticesNeighbors(v2);
v4.addVerticesNeighbors(v3);
v4.addVerticesNeighbors(v6);
v4.addVerticesNeighbors(v7);
v7.addVerticesNeighbors(v6);
v7.addVerticesNeighbors(v5);
list.add(v1);
list.add(v2);
list.add(v3);
list.add(v4);
list.add(v5);
list.add(v6);
list.add(v7);
System.out.println("Graph 1");
DFS dfs = new DFS();
dfs.dfs(list);
System.out.println();
System.out.println("Graph 2");
Vertex v8 = new Vertex("a");
Vertex v9 = new Vertex("b");
Vertex v10 = new Vertex("c");
Vertex v11 = new Vertex("d");
Vertex v12 = new Vertex("e");
Vertex v13 = new Vertex("f");
Vertex v14 = new Vertex("g");
List list2 = new ArrayList();
v8.addVerticesNeighbors(v9);
v9.addVerticesNeighbors(v10);
v10.addVerticesNeighbors(v11);
v11.addVerticesNeighbors(v14);
v14.addVerticesNeighbors(v12);
v12.addVerticesNeighbors(v8);
v13.addVerticesNeighbors(v12);
v13.addVerticesNeighbors(v9);
v13.addVerticesNeighbors(v10);
v13.addVerticesNeighbors(v14);
list2.add(v8);
list2.add(v9);
list2.add(v10);
list2.add(v11);
list2.add(v12);
list2.add(v13);
list2.add(v14);
DFS dfs2 = new DFS();
dfs2.dfs(list2);
}
}
(6)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Database And Knowledge Base Systems

Authors: S Krishna

1st Edition

9810206208, 978-9810206208

More Books

Students also viewed these Databases questions

Question

12.6 Prediction

Answered: 1 week ago

Question

a neglect of quality in relationship to international competitors;

Answered: 1 week ago