图---邻接表的深度搜索与广度搜索测试一

来源:互联网 发布:vb绘图控件 编辑:程序博客网 时间:2024/06/07 23:44

 

import java.util.LinkedList;import java.util.Queue;//线链class Line {int index; // 顶点下标Line nextLine;}// 节点class Point {String name;boolean isVisit;Line firstLine;public Point(String name) {this.name = name;}}public class GraphLink {static final int LENGTH = 10;Point[] points;int curLength;public GraphLink() {points = new Point[LENGTH];}void addPoint(Point point) {points[curLength++] = point;}void addLine(int x, int y) {Line line = new Line();line.index = y;line.nextLine = points[x].firstLine;points[x].firstLine = line;Line line2 = new Line();line2.index = x;line2.nextLine = points[y].firstLine;points[y].firstLine = line2;}    private void initPoints() {        for (int i = 0; i < curLength; i++) {            points[i].isVisit = false;        }    }    /**     * 深度搜索     * A节点开始,循环A的线链,如果下一个节点F,未被访问,就递归到节点F的线链,     * 如果下一个节点已经被访问过,就把A的线链指向再下一个节点C,直到没后继节点为止     */public void dfs() {for(int i = 0; i < curLength; i++){Point point = points[i];showPoint(point);}        initPoints();}private void showPoint(Point point) {if(!point.isVisit){showName(point.name);            showNbsp();point.isVisit = true;}Line line = point.firstLine;while(line != null){Point p = points[line.index];if(!p.isVisit){showPoint(p);}line  = line.nextLine;}}    /**     * 广度搜索     * 循环各个节点的线链     */    private void bfs() {        Queue<Integer> queue = new LinkedList<Integer>();        for (int i = 0; i < curLength; i++) {            Point point  = points[i];            if(!point.isVisit){                queue.add(i);                showName(point.name);                showNbsp();                while (!queue.isEmpty()){                    Point _point = points[queue.peek()];                    _point.isVisit = true;                    Line line = _point.firstLine;                    while (line != null){                        Point p = points[line.index];                        if(!p.isVisit){                            showName(p.name);                            showNbsp();                            p.isVisit = true;                            queue.add(line.index);                        }                        line = line.nextLine;                    }                    queue.poll();                }            }        }        initPoints();    }    /**     * 最小生成树     * 区别是打印当前节点     */    public void mfs() {        for (int i = 0; i < curLength; i++) {            Point point = points[i];            showPointMfs(point);        }        initPoints();    }    private void showPointMfs(Point point) {        if (!point.isVisit) {            point.isVisit = true;        }        Line line = point.firstLine;        while (line != null) {            Point p = points[line.index];            if (!p.isVisit) {                showName(point.name);                showName(p.name);                showNbsp();                showPointMfs(p);            }            line = line.nextLine;        }    }    public void showNbsp() {        System.out.print(" ");    }    private void showName(String name) {System.out.print(name);}public static void printLn(String name) {System.out.println("");System.out.println(name);}public static void main(String[] args) {GraphLink g = new GraphLink();for (int i = 65; i <= 70; i++) {char c = (char) i;Point point = new Point(c + "");g.addPoint(point);}g.addLine(0, 1); // abg.addLine(0, 2); // acg.addLine(0, 5); // afg.addLine(1, 3); // bdg.addLine(2, 3); // cdg.addLine(2, 4); // ceg.addLine(3, 4); // deprintLn("深度搜索:");g.dfs();        printLn("广度搜索:");        g.bfs();        printLn("最小生成树:");        g.mfs();}}


 

 

0 0
原创粉丝点击