<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    E81086713E446D36F62B2AA2A3502B5EB155

    Java雜家

    雜七雜八。。。一家之言

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      40 Posts :: 1 Stories :: 174 Comments :: 0 Trackbacks
    很早就想總結一下了,一直沒有時間,OK,進入正題。

    一 圖的基本概念及存儲結構
    圖G是由頂點的有窮集合,以及頂點之間的關系組成,頂點的集合記為V,頂點之間的關系構成邊的集合E
    G=(V,E).
    說一條邊從v1,連接到v2,那么有v1Ev2(E是V上的一個關系)《=》<v1,v2>∈E.
    圖有有向圖,無向圖之分,無向圖的一條邊相當于有向圖的中兩條邊,即如果無向圖的頂點v1和v2之間有一條邊
    ,那么既有從v1連接到v2的邊,也有從v2連接到v1的邊,<v1,v2>∈E 并且<v2,v1>∈E,而有向圖是嚴格區分方向的。

    一般圖的存儲有兩種方式
    1)相鄰矩陣,用一個矩陣來保持邊的情況,<v1,v2>∈E則Matrix[v1][v2]=Weight.
    2)鄰接表,需要保存一個順序存儲的頂點表和每個頂點上的邊的鏈接表。
    這里的實現采用第二種方案,另外圖又復雜圖,簡單圖之分,復雜圖可能在兩點之間同一個方向有多條邊,我們考慮的都是無環簡單圖,無環簡單圖是指頂點沒有自己指向自己的邊的簡單圖,即任取vi屬于V => <vi,vi>不屬于E并且沒有重復邊。

    我們先給出圖的ADT:
    package algorithms.graph;

    /**
     * The Graph ADT
     * 
    @author yovn
     *
     
    */
    public interface Graph {
        
        
    //mark
        public static interface Edge{
            
    public int getWeight();
        }
        
        
    int vertexesNum();
        
        
    int edgeNum();
        
    boolean isEdge(Edge edge);
        
    void setEdge(int from,int to, int weight);
        Edge firstEdge(
    int vertex);
        Edge nextEdge(Edge edge);
        
    int toVertex(Edge edge);
        
    int fromVertex(Edge edge);
        String getVertexLabel(
    int vertex);
        
    void assignLabels(String[] labels);
        
    void deepFirstTravel(GraphVisitor visitor);
        
    void breathFirstTravel(GraphVisitor visitor);
        
        

    }
    其中的方法大多數比較一目了然,
    其中
    1)Edge firstEdge(int vertex)返回指定節點的邊的鏈表里存的第一條邊
    2)
    Edge nextEdge(Edge edge),順著邊鏈表返回下一條邊
    3)fromVertex,toVertex很簡單返回該邊的起始頂點和終結頂點
    4)
    getVertexLabel返回該定點對應的標號,assignLabels給所有頂點標上號

    GraphVisitor是一個很簡單的接口:
    package algorithms.graph;

    /**
     * 
    @author yovn
     *
     
    */
    public interface GraphVisitor {
        
        
    void visit(Graph g,int vertex);

    }
    OK,下面是該部分實現:
    package algorithms.graph;

    import java.util.Arrays;


    /**
     * 
    @author yovn
     *
     
    */
    public class DefaultGraph implements Graph {
        

        
    private static class _Edge implements Edge{
            
            
            
    private static final _Edge NullEdge=new _Edge();
            
            
    int from;
            
    int to;
            
    int weight;
            
            _Edge nextEdge;
            
            
    private _Edge()
            {
                weight
    =Integer.MAX_VALUE;
            }
            
            _Edge(
    int from, int to, int weight)
            {
                
                
    this.from=from;
                
    this.to=to;
                
    this.weight=weight;
            }
            
    public int getWeight()
            {
                
    return weight;
            }
            
            
        }
        
        
    private static class _EdgeStaticQueue
        {
            _Edge first;
            _Edge last;
        }
        
        
    private int numVertexes;
        
    private String[] labels;
        
    private int numEdges;
        
        
        
    private _EdgeStaticQueue[] edgeQueues;
        
        
    //tag the specified vertex be visited or not
        private boolean[] visitTags;

        
    /**
         * 
         
    */
        
    public DefaultGraph(int numVertexes) {
            
    if(numVertexes<1)
            {
                
    throw new IllegalArgumentException();
            }
            
    this.numVertexes=numVertexes;
            
    this.visitTags=new boolean[numVertexes];
            
    this.labels=new String[numVertexes];
            
    for(int i=0;i<numVertexes;i++)
            {
                labels[i]
    =i+"";
                
                
            }
            
    this.edgeQueues=new _EdgeStaticQueue[numVertexes];
            
    for(int i=0;i<numVertexes;i++)
            {
                edgeQueues[i]
    =new _EdgeStaticQueue();
                edgeQueues[i].first
    =edgeQueues[i].last=_Edge.NullEdge;
                
            }
            
    this.numEdges=0;
        }

        

        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#edgeNum()
         
    */
        @Override
        
    public int edgeNum() {
            
            
    return numEdges;
        }

        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#firstEdge(int)
         
    */
        @Override
        
    public Edge firstEdge(int vertex) {
            
    if(vertex>=numVertexes)    throw new IllegalArgumentException();
            
    return edgeQueues[vertex].first;
            
        }

        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#isEdge(algorithms.graph.Graph.Edge)
         
    */
        @Override
        
    public boolean isEdge(Edge edge) {
            
            
    return (edge!=_Edge.NullEdge);
        }

        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#nextEdge(algorithms.graph.Graph.Edge)
         
    */
        @Override
        
    public Edge nextEdge(Edge edge) {
            
            
    return ((_Edge)edge).nextEdge;
        }

        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#vertexesNum()
         
    */
        @Override
        
    public int vertexesNum() {
            
            
    return numVertexes;
        }


        @Override
        
    public int fromVertex(Edge edge) {
            
            
    return ((_Edge)edge).from;
        }

        @Override
        
    public void setEdge(int from, int to, int weight) {
            
    //we don't allow ring exist 
            if(from<0||from>=numVertexes||to<0||to>=numVertexes||weight<0||from==to)throw new IllegalArgumentException();
            _Edge edge
    =new _Edge(from,to,weight);
            edge.nextEdge
    =_Edge.NullEdge;
            
    if(edgeQueues[from].first==_Edge.NullEdge)
                edgeQueues[from].first
    =edge;
            
    else 
                edgeQueues[from].last.nextEdge
    =edge;
            edgeQueues[from].last
    =edge;
            
        }

        @Override
        
    public int toVertex(Edge edge) {

            
    return ((_Edge)edge).to;
        }

        @Override
        
    public String getVertexLabel(int vertex) {
            
    return labels[vertex];
        }

        @Override
        
    public void assignLabels(String[] labels) {
            System.arraycopy(labels, 
    0this.labels, 0, labels.length);
            
        }

           
    //to be continue

    }

    二 深度優先周游
    即從從某一點開始能繼續往前就往前不能則回退到某一個還有邊沒訪問的頂點,沿這條邊看該邊指向的點是否已訪問,如果沒有訪問,那么從該指向的點繼續操作。

    那么什么時候結束呢,這里我們在圖的ADT實現里加上一個標志數組。該數組記錄某一頂點是否已訪問,如果找不到不到能繼續往前訪問的未訪問點,則結束。

    你可能會問,如果指定圖不是連通圖(既有2個以上的連通分量)呢?
    OK,解決這個問題,我們可以讓每一個頂點都有機會從它開始周游。
    下面看deepFirstTravel的實現:

     /* (non-Javadoc)
         * @see algorithms.graph.Graph#deepFirstTravel(algorithms.graph.GraphVisitor)
         
    */
        @Override
        
    public void deepFirstTravel(GraphVisitor visitor) {
            Arrays.fill(visitTags, 
    false);//reset all visit tags
            for(int i=0;i<numVertexes;i++)
            {
                
    if(!visitTags[i])do_DFS(i,visitor);
            }

        }
        

        
    private final void do_DFS(int v, GraphVisitor visitor) {
            
    //first visit this vertex
            visitor.visit(this, v);
            visitTags[v]
    =true;
            
            
    //for each edge from this vertex, we do one time
            
    //and this for loop is very classical in all graph algorithms
            for(Edge e=firstEdge(v);isEdge(e);e=nextEdge(e))
            {
                
    if(!visitTags[toVertex(e)])
                {
                    do_DFS(toVertex(e),visitor);
                }
            }
            
            
        }

    三 廣度優先周游
    廣度優先周游從每個指定頂點開始,自頂向下一層一層的訪問。上一層所有頂點訪問完了才繼續下一層的訪問。可以把二叉樹的廣度優先周游看成圖的廣度優先周游的特例。
    (二叉樹是連通的無回路的有向圖,也是一棵根樹)
    同樣,廣度優先也要借助與一個隊列來存儲待訪問頂點

    下面是breathFirstTravel的實現,為了減小Java庫的影響,我自己實現一個很簡單的隊列。(你也可以使用
    ArrayList,但是記住隊列的定義,只能在頭刪除,在尾插入):
    private static class _IntQueue
        {
            
    private static class _IntQueueNode
            {
                _IntQueueNode next;
                
    int value;
            }
            _IntQueueNode first;
            _IntQueueNode last;
            
            
    //queue can only insert to the tail
            void add(int i)
            {
                _IntQueueNode node
    =new _IntQueueNode();
                node.value
    =i;
                node.next
    =null;
                
    if(first==null)first=node;
                
    else last.next=node;
                last
    =node;
            }
            
            
            
    boolean isEmpty()
            {
                
    return first==null;
            }
            
    //queue can only remove from the head
            int remove()
            {
                
    int val=first.value;
                
    if(first==last)
                    first
    =last=null;
                
    else
                    first
    =first.next;
                
    return val;
            }
            
        }
        
    /* (non-Javadoc)
         * @see algorithms.graph.Graph#breathFirstTravel(algorithms.graph.GraphVisitor)
         
    */
        @Override
        
    public void breathFirstTravel(GraphVisitor visitor) {
            Arrays.fill(visitTags, 
    false);//reset all visit tags
            
            
            
    for(int i=0;i<numVertexes;i++)
            {
                
    if(!visitTags[i])
                {
                        
                    do_BFS(i,visitor);
                }
            }

        }

        
    private void do_BFS(int v, GraphVisitor visitor) {
            
    //and BFS will use an queue to keep the unvisited vertexes
            
    // we  can also just use java.util.ArrayList
            _IntQueue queue=new _IntQueue();
            queue.add(v);
            
    while(!queue.isEmpty())
            {
                
    int fromV=queue.remove();
                visitor.visit(
    this, fromV);
                visitTags[fromV]
    =true;
                
    for(Edge e=firstEdge(fromV);isEdge(e);e=nextEdge(e))
                {
                    
    if(!visitTags[toVertex(e)])
                    {
                        queue.add(toVertex(e));
                    }
                }
            }
        }

    OK,最后我們測試一下:
    /**
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            
    /**
             * For example, we have a graph:
             * 0→1→2
             * ↓ ↑↓
             * 3  4 5
             * ↓ ↑↓
             * 6→7←8
             * 
             *  here ,all weight is 0, its a DAG(Directed Acyclic Graph) 
             
    */

            DefaultGraph g
    =new DefaultGraph(9);
            g.setEdge(
    010);
            g.setEdge(
    030);
            g.setEdge(
    120);
            g.setEdge(
    410);
            g.setEdge(
    250);
            
            g.setEdge(
    360);
            g.setEdge(
    740);
            g.setEdge(
    580);
            g.setEdge(
    670);
            g.setEdge(
    870);
            
            
    //now we visit it
            GraphVisitor visitor=new GraphVisitor()
            {
                @Override
                
    public void visit(Graph g, int vertex) {
                    System.out.print(g.getVertexLabel(vertex)
    +" ");
                    
                }
                
            };
            System.out.println(
    "DFS==============:");
            g.deepFirstTravel(visitor);
            System.out.println();
            System.out.println(
    "BFS==============:");
            g.breathFirstTravel(visitor);
            System.out.println();
        }

    posted on 2007-12-14 14:46 DoubleH 閱讀(4663) 評論(1)  編輯  收藏 所屬分類: Memorandum

    Feedback

    # re: 圖及其算法復習(Java實現) 一:存儲結構,深度優先周游,廣度優先周游[未登錄] 2013-05-26 21:38 123
    <script>alert(1);<script>  回復  更多評論
      

    主站蜘蛛池模板: 99精品视频在线视频免费观看| 国产成人亚洲综合在线| 免费人成在线观看网站| 久久精品国产亚洲精品| 免费人成在线观看播放a| 国产男女猛烈无遮档免费视频网站| 亚洲综合欧美色五月俺也去| 又黄又爽又成人免费视频| 亚洲a视频在线观看| 日韩免费一级毛片| 亚洲人成无码网站在线观看| 女性自慰aⅴ片高清免费| 亚洲国产午夜精品理论片在线播放 | 18禁免费无码无遮挡不卡网站 | 偷自拍亚洲视频在线观看99| 国产免费怕怕免费视频观看| 免费的黄色网页在线免费观看| 亚洲午夜日韩高清一区| 天黑黑影院在线观看视频高清免费 | 国产精品亚洲一区二区三区久久| 永久中文字幕免费视频网站| 黄页网站在线免费观看| 亚洲精品一级无码鲁丝片| 女同免费毛片在线播放| 久久精品国产亚洲av日韩| 在线观看av永久免费| 羞羞网站免费观看| 亚洲精品无码久久久久| 91免费国产在线观看| 亚洲人成电影网站免费| 亚洲性日韩精品一区二区三区| 男女午夜24式免费视频| 亚洲影院天堂中文av色| 亚洲人成无码网WWW| 亚洲精品免费在线观看| 亚洲最大av资源站无码av网址| 久久精品国产亚洲精品| 男女免费观看在线爽爽爽视频 | 亚洲第一成年免费网站| 一级成人a做片免费| 亚洲宅男天堂a在线|