数据结构---图

图的两种表示方式,邻接表(点及点对应一级节点链表),邻接矩阵(多少个点即多少数组长度,二维)

图的广度搜索(层层递进,最近距离,利用队列收集每一层的节点,遍历下一层节点),

深度搜索(递归获取直属节点,终极节点为终结条件)

public class TableGraph{
        int pointCount;
        java.util.ArrayList<Integer> childrenList[];
        public TableGraph(int i){
            pointCount = i;
            this.childrenList=new java.util.ArrayList[pointCount];
            for(int j=0;j<i;j++){
                this.childrenList[j] = new java.util.ArrayList();
            }
        }
        
        public void bfs(int s, int e){
            if(s == e) return;
            boolean[] visited = new boolean[this.pointCount];
            visited[s] = true;
            java.util.Queue<Integer> queue = new java.util.LinkedList();
            queue.add(s);
            int prev[] = new int[this.pointCount];
            for(int i=0;i<this.pointCount;++i){
                prev[i] = -1;
            }
            while(queue.size()!=0){
                int w = queue.poll();
                for(int l=0;l<this.childrenList[w].size();l++){
                    int q = this.childrenList[w].get(l);
                    if(!visited[q]){
                        prev[q]=w;
                        if(q == e){
                            System.out.println("找到了");
                            return;
                        }
                        visited[q] = true;
                        queue.add(q);
                    }
                }
            }
        }
    }
    
    public class MatrixGraph{
        int graghArray[][];
        public MatrixGraph(int i){
            this.graghArray = new int[i][i];
            for(int j=0;j<i;j++){
                for(int k=0;k<i;k++){
                    this.graghArray[j][i] = 0;
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值