图结构 java实现

http://blog.csdn.net/love_ubuntu/article/details/6689898

package com.datestrut.graph;

public class Graph
{

    private int length;     //默认长度,节点长度
    private GraphList[] list;   
    private int[][]  weight;     //权值

    public Graph(int length) {
        this.length = length;
        list = new  GraphList[length];
        weight = new int[length][length];
    }


    public void   dfs(int v) {
        int[] ajd = new int[length];
        int ajdlength = list[v].getAjd(ajd);
        list[v].visitable = true;
        System.out.print(v + " ");
        for (int i = 0; i < ajdlength; i++) {
            int w = ajd[i];
            if (!list[w].visitable) {
                dfs(w);
            }
        }
    }



    // 深度优先遍历
    public void dfsTravel() {
        for (int i = 0; i < length; i++) {
            list[i].visitable = false;
        }
        for (int i = 0; i < length; i++) {
            if (!list[i].visitable) {
                dfs(i);
            }
        }
    }

    // 广度优先遍历
    public void bfsTravel() {
        for (int i = 0; i < length; i++) {
            list[i].visitable = false;
        }
        bfs();
    }

private void bfs() {
        Queue   queue =  new Queue();
        for (int index = 0; index < length; index++) {
            if (!list[index].visitable) {
                queue.addQueue(index);
                list[index].visitable = true;
                System.out.print(index + " ");
                while (!queue.isEmpty()) {
                    int temp = queue.front();
                    queue.deleteQueue();
                    int[] ajd = new int[length];
                    int ajdlength = list[temp].getAjd(ajd);
                    for (int i = 0; i < ajdlength; i++) {
                        int w = ajd[i];
                        if (!list[w].visitable) {
                            System.out.print(w + " ");
                            queue.addQueue(w);
                            list[w].visitable = true;
                        }
                    }
                }
            }

        }
    }

    // 最短路径
    private int[] shortPath(int v) {
        int[] shortPath = new int[length];
        boolean[]   weightFound = new boolean[length];
        for (int i = 0; i < length; i++) {
                                                // 趋近无穷
            shortPath[i] =   9999;
            weightFound[i] = false;
        }

        shortPath[v] = 0;
        weightFound[v] = true;
        Queue queue = new Queue();
        queue.addQueue(v);

        while (!queue.isEmpty()) {
            int temp = queue.front();
            queue.deleteQueue();
            int[]  ajd = new int[length];
            int ajdlength =   list[temp].getAjd(ajd);


            for (int i = 0; i < ajdlength; i++) {
                int w = ajd[i];
                if (!weightFound[w]) {
                    if (shortPath[w] > shortPath[temp] + weight[temp][w]){
                        shortPath[w] = shortPath[temp] + weight[temp][w];
                    }
                }
            }

            int minWeightNode = 0;  
            for (int i = 0; i < length; i++) {
                if (!weightFound[i]) {
                    minWeightNode = i;
                    for (int j = 0; j < length; j++) {
                        if (!weightFound[j]) {
                            if (shortPath[j] < shortPath[minWeightNode]) {
                                minWeightNode = j;
                            }
                        }
                    }
                    break;
                }
            }

            if (!weightFound[minWeightNode]) {
                weightFound[minWeightNode] = true;
                queue.addQueue(minWeightNode);
            }
        }
                  return shortPath;
    }

    // 长度
    public int  length() {
        System.out.println("length="+length);
        return length;
    }


    public boolean isEmpty() {
        return  length == 0;
    }


    // 添加节点
    public void addGraph(int info) {
        for (int i = 0; i < length; i++) {
            if (list[i] == null) {
                GraphList g = new GraphList();
                g.addNode(info);
                list[i] = g;
                break;
            }
        }
    }


    // 添加边
    public void addEdge(int vfrom, int vto, int value) {    //起点,终点,权值
        list[vfrom].addNode(vto);
        weight[vfrom][vto] = value;
    }

    // 打印图
    public void print() {
        for (int i = 0; i < length; i++) {
            GraphNode current =  list[i].first;
            while (current != null) {
                System.out.print(current.info + " ");
                current = current.link;
            }
               System.out.println("");
        }
    }

    // 打印图
    public void print2() {
        for (int i = 0; i < length; i++) {
            GraphNode current =  list[i].first;
            int length = -1;
            while (current != null) {
//                System.out.print(current.info + " ");
                length++;
                current = current.link;
            }
               System.out.print(length+" ");
        }
        System.out.println();
    }


public static void main(String[] args) {
        Graph graph = new Graph(5);  
        System.out.println("create graph start");
        for (int i = 0; i < 5; i++) {
              graph.addGraph(i);
        }
        //节点
        graph.print();

        //构建链接矩阵
        graph.addEdge(0, 1, 16);
        graph.addEdge(0, 3, 2);
        graph.addEdge(0, 4, 3);
        graph.addEdge(1, 2, 5);
        graph.addEdge(2, 1, 3);
        graph.addEdge(3, 4, 7);
        graph.addEdge(3, 1, 12);
        graph.addEdge(4, 1, 10);
        graph.addEdge(4, 3, 5);
        graph.addEdge(4, 2, 4);
        System.out.println("图结点的边表是:");
        graph.print();
        System.out.println("create graph end"); 
        System.out.println("各个节点的边表长度:");
        graph.print2();
        System.out.println("图中节点数:"+graph.length());

        System.out.println("深度优先遍历:");
        graph.dfsTravel();
        System.out.println();

        System.out.println("广度优先遍历: ");
        graph.bfsTravel();
        System.out.println();


        int[] shortPath =  graph.shortPath(0);
        for (int i = 0; i < shortPath.length; i++) {
             System.out.print(shortPath[i] + " ");
        }
    }

}

//建立图的顶点
 class GraphNode {      //链式存储
    public GraphNode  link;    //指针
    public int   info;  //顶点
}

 //建立图的前驱和后继节点
class GraphList {
     public GraphNode first;
     public GraphNode last;
     public boolean   visitable;

     //存储边表的长度
     public int getAjd(int[] ajd) {
         GraphNode current = first;
         int length = 0;
         while(current != null) {
             ajd[length++] = current.info;
             current = current.link;
         }
         return length;
     }


     //添加节点
     public void addNode(int v) {
         GraphNode node = new GraphNode();
         node.info = v;
         if(first == null) {
             first = node;
             last = node;
         } else {
             last.link = node;
             last = node;
         }
     }
 }


class Queue {
    public GraphNode first;
    public GraphNode last;
    public int count;
    public void addQueue(int info) {
        GraphNode node = new GraphNode();
        node.info = info;
        if(first == null) {
            first = node;
            last = node;
        } else {
            last.link = node;
            last = last.link;
        }
        count++;
    } 

    public void deleteQueue() {
        if(first == null) {
            System.out.println("null queue");
        } else {
            first = first.link;
            count--;
        }
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public int front() {
        if(first == null) {
            return -1;
        }
        return first.info;
    }

    public int back() {
        if(last == null) {
            return -1;
        }
        return last.info;
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值