import [Link].
Scanner;
class DFSGraph { static final int MAX = 10; // Maximum number of vertices
static int[][] adjMatrix = new int[MAX][MAX];
static int[] visited = new int[MAX];
static int vertices; // Function to perform Depth First Search
static void DFS(int vertex)
{ [Link](vertex + " ");
visited[vertex] = 1;
for (int i = 0; i < vertices; i++) {
if (adjMatrix[vertex][i] == 1 && visited[i] == 0) {
DFS(i);
} } }
// Function to initialize graph
static void initializeGraph() {
for (int i = 0; i < MAX; i++) {
visited[i] = 0;
for (int j = 0; j < MAX; j++) {
adjMatrix[i][j] = 0;
}
}
}
// Function to add an edge (Undirected graph)
static void addEdge(int src, int dest) {
adjMatrix[src][dest] = 1;
adjMatrix[dest][src] = 1;
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of vertices: ");
vertices = [Link]();
[Link]("Enter number of edges: ");
int edges = [Link]();
initializeGraph();
[Link]("Enter edges (source destination):");
for (int i = 0; i < edges; i++) {
int src = [Link]();
int dest = [Link]();
addEdge(src, dest);
}
[Link]("Depth First Search (starting from vertex 0):");
DFS(0);
[Link]();
}}