Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package idddfs;
- import java.util.Scanner;
- /**
- *
- * @author Masud
- */
- public class Idddfs {
- /**
- * @param args the command line arguments
- */
- public static int vis[] = new int[20];
- public static int n, search, t, find = 0, depth = 0;
- public static int s = 0;
- public static int g[][] = new int[20][20];
- public static boolean breakk = false;
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner sn = new Scanner(System.in);
- System.out.println("Enter the Number of Node: ");
- n = sn.nextInt();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) { //Input
- g[i][j] = sn.nextInt();
- }
- }
- System.out.println("Enter the search Node: ");
- search = sn.nextInt();
- System.out.println("Output");
- int k = 0;
- while (breakk == false) {
- DFS(s, 0, k); //Level
- for (int j = 0; j < n; j++) {
- vis[j] = 0;
- }
- k++;
- System.out.println("");
- }
- System.out.println("Find at= " + find);
- }
- static void DFS(int s, int currentlevel, int maxlevel) {
- if (currentlevel > maxlevel) {
- return;
- }
- vis[s] = 1;
- System.out.print(s + " ");
- if (s == search) {
- breakk = true;
- find = currentlevel;
- }
- for (int v = 0; v < n; v++) {
- if (g[s][v] == 1 && vis[v] == 0) {
- DFS(v, currentlevel + 1, maxlevel);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement