waysun一路阳光

不轻易服输,不轻言放弃.--心是梦的舞台,心有多大,舞台有多大。踏踏实实做事,认认真真做人。

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  167 随笔 :: 1 文章 :: 64 评论 :: 0 Trackbacks
来源:http://blog.chinaunix.net/u1/50399/showart_410126.html

public class DFSTest
{ 
   public static void main(String[] args)
   {
       int[][] graph={
        {0,1,1,0,0,0,0,0},
        {1,0,0,1,1,0,0,0},
        {1,0,0,0,0,1,1,0},
        {0,1,0,0,0,0,0,1},
        {0,1,0,0,0,0,0,1},
        {0,0,1,0,0,0,1,0},
        {0,0,1,0,0,1,0,0},
        {0,0,0,1,1,0,0,0},
       };
       int[] list;
       DFS dfs=new DFS();
       dfs.input(graph, 0);
       list=dfs.getList();
       for(int i=0; i<graph.length; i++){
           System.out.print(list[i]+" ");
       }
   }
}

class DFS
{
    int[][] graph;
    int[] list;
    
    int[] visited;
    int j;
    
    void input(int[][] graph, int v)
    {
        this.graph=graph;
        visited=new int[graph.length];
        list=new int[graph.length];
        for(int i: visited) i=0;
        j=0;
        calculate(v);
    }
    
    void calculate(int v)
    {
        visited[v]=1;
        list[j++]=v;
        for(int k=0; k<graph.length; k++){
            if(graph[v][k]==&& visited[k]==0){
                calculate(k);
            }
        }
    }
    
    int[] getList()
    {
        return list;
    }
}


posted on 2009-04-15 22:24 weesun一米阳光 阅读(237) 评论(0)  编辑  收藏 所属分类: JAVA源码总结备用

只有注册用户登录后才能发表评论。


网站导航: