Im making a program that takes vertices from a text file and creates a graph. The code takes in a text file with edges, constructs a graph using adjacency list data structure, then finally uses Depth first search (DFS) for searching the graph.
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Graph {
private int V;
private ArrayList<Integer>[] adjList;
public Graph(int V) {
this.V = V;
adjList = new ArrayList[V];
for (int i = 0; i < V; i++) {
adjList[i] = new ArrayList<Integer>();
}
}
public void addEdge(int u, int v) {
adjList[u].add(v);
}
public void DFS(int v, boolean[] visited) {
visited[v] = true;
System.out.print(v + " ");
for (int i = 0; i < adjList[v].size(); i++) {
int neighbor = adjList[v].get(i);
if (!visited[neighbor]) {
DFS(neighbor, visited);
}
}
}
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("input.txt"));
int V = scanner.nextInt();
Graph graph = new Graph(V);
while (scanner.hasNextInt()) {
int u = scanner.nextInt();
int v = scanner.nextInt();
graph.addEdge(u, v);
}
scanner.close();
for (int i = 0; i < V; i++) {
System.out.print(i + ": ");
for (int j = 0; j < graph.adjList[i].size(); j++) {
System.out.print(graph.adjList[i].get(j) + " ");
}
System.out.println();
}
Scanner userScanner = new Scanner(System.in);
System.out.print("Enter starting vertex: ");
int start = userScanner.nextInt();
userScanner.close();
boolean[] visited = new boolean[V];
System.out.println("DFS traversal from vertex " + start + ":");
graph.DFS(start, visited);
Scanner searchScanner = new Scanner(System.in);
System.out.print("\nEnter value to search for: ");
int value = searchScanner.nextInt();
searchScanner.close();
boolean[] searchVisited = new boolean[V];
System.out.println("Searching for value " + value + " starting from vertex " + start + ":");
boolean found = search(graph, start, value, searchVisited);
if (found) {
System.out.println("Value " + value + " found in the graph!");
} else {
System.out.println("Value " + value + " not found in the graph.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static boolean search(Graph graph, int v, int value, boolean[] visited) {
visited[v] = true;
if (v == value) {
return true;
}
for (int i = 0;graph.adjList[v].size(); i++) {
int neighbor = graph.adjList[v].get(i);
if (!visited[neighbor]) {
boolean found = search(graph, neighbor, value, visited);
if (found) {
return true;
}
}
}
return false;
}
}
The error is specifically the for loop in public static boolean search. I get an error saying "Type mismatch: cannot convert from int to boolean" where graph.adjList[v].size() is at.
>Solution :
the problem is in the for loop. the signature of a for is like this
for (statement 1; statement 2; statement 3) {
// logic
}
where
statement 1is executed oncestatement 2is the condition that decides if the loop must be executed one more timestatement 3will be executed after the logic inside the for block is executed
in your case you have
for (int i = 0;graph.adjList[v].size(); i++)
where the statement 2 which is graph.adjList[v].size() is an int. You have to change that and put a condition. Something like
i < graph.adjList[v].size()
or whatever you need