- class Graph1 {
- protected int n;
- protected int mat[][];
- protected int visited[];
- public Graph1(int m1[][]){
- n = m1.length;
- mat = new int [n][n];
- System.arraycopy(m1, 0, mat,
0, n);
- visited= new int [n];
- }
- public Graph1(){
- }
- public void depthFirstSearch(){
- System.out.println("深度优先遍历Depth first search:");
- for(int k=1;k<=n;k++){
- depthfs(k);
- System.out.println();
- unvisited();
- }
- }
- private void unvisited() {
- int i;
- for(i = 0;i<visited.length;i++){
- visited[i]=0;
- }
- }
- private void depthfs(int k) {
- int i,j=0;
- System.out.print(" v"+k+"->");
- i = k - 1;
- visited[i]=1;
- while(j<n){
- if(mat[i][j]==1 && visited[j]==0){
- depthfs(j+1);
- }else {
- j++;
- }
- }
- }
- }
- public class testGraph1{
- public static
void main(String ag[]){
- int mat[][]= {{0,1,0,0},{0,0,1,1},{0,0,0,0},{1,0,1,0}};
- Graph1 g1 = new Graph1(mat);
- g1.depthFirstSearch();
- }
- }
posted on
2013-02-20 20:25
蜜雪薇琪
阅读(
182)
评论()
编辑
收藏
举报