1 / 3

CS 220 Fall 2013 Lecture 56

CS 220 Fall 2013 Lecture 56. Recursive DFS. import java.util.Scanner; import java.io.*; public class dfs { int a[][]; boolean visited[]; public dfs(String in) { try { Scanner s = new Scanner(new File(in)); int i = s.nextInt(); int j; a = new int[i][i];

sofia
Télécharger la présentation

CS 220 Fall 2013 Lecture 56

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 220 Fall 2013 Lecture 56 Recursive DFS

  2. import java.util.Scanner; import java.io.*; public class dfs { int a[][]; boolean visited[]; public dfs(String in) { try { Scanner s = new Scanner(new File(in)); int i = s.nextInt(); int j; a = new int[i][i]; visited = new boolean[i]; while (s.hasNextInt()) { i = s.nextInt(); j = s.nextInt(); a[i][j] = 1; a[j][i] = 1; } } catch (Exception e) {System.out.println(e.getMessage());} for (int i = 0; i < a.length; i++) visited[i] = false; }

  3. public String dfsR() { visited[a.length-1] = true; return dfsR(a.length-1); } public String dfsR(int i) { String s = new String(""+i+" "); for (int j = 0; j < a[i].length; j++) { if (a[i][j] == 1 && !visited[j]) { visited[j] = true; s = s+dfsR(j); } } return s; }

More Related