圖 G 由兩個集合 V 和 E 組成,記爲:
G=(V, E) 其中: V 是頂點的有窮非空集合,
E 是 V 中頂點偶對(稱爲邊)的有窮集。html
鄰接矩陣儲存法:採用2個數組來表示圖;一個是存儲全部頂點信息的一維數組(vetices[])、一個是存儲圖中頂點之間關聯關係的二維數組(adjMatrix[][]),這個二維數組稱爲鄰接矩陣。java
問題1:書中有關integer的代碼什麼意思?node
問題1解決方案:如圖:git
由於隊列中儲存的數據是Integer
類型的,而numVertices
是int
類型的,所以應該轉化爲Integer型,對應的方法爲:
那麼新的問題又來了:爲何必定要Integer,直接用int很差嗎?
答案:Integer是其包裝類,注意是一個類。而int 是基本數據類型.咱們使用integer是爲了在各類類型間轉化,經過各類方法的調用。算法
1. 初始化隊列:visited[n] = 0 2. 訪問頂點:visited[v] = 1 3. 頂點v加入隊列 4. 循環: while(隊列是否爲空) v = 隊列頭元素 w = v的第一個鄰接點 while(w存在) if(若是w未訪問) visited[w] = 1; 頂點w加入隊列 w = 頂點v的下一個鄰接點
1. 訪問數組初始化:visited[n] = 0 2. 訪問頂點:visited[v] = 1 3. 取v的第一個鄰接點w; 4. 循環遞歸: while(w存在) if(w未被訪問過) 從頂點w出發遞歸執行; w = v的下一個鄰接點;
public boolean isConnected() { boolean result = true; for(int i=0;i<numVertices;i++){ int temp=0; temp=getSizeOfIterator(this.iteratorBFS(vertices[i])); if(temp!=numVertices) { result = false; break; } } return result; }
問題2解決方案:首先 確定是 輸出方法出現了問題,解決後發現應該這樣:
數組
而後出現的問題是:
經分析:確定是哪句代碼錯了:致使所有連在一塊兒了!
值得注意的是 在無向圖中添加邊,應該兩邊的聯繫都要考慮:數據結構
public void addEdge(int index10 , int index20){ int index1 =index10-1; int index2 =index20-1; if (indexIsValid(index1)&&indexIsValid(index2)){ GraphNode temp = vertices[index1];//獲得首結點 while (temp.getNext()!=null){ if (temp.getNext().getElement() == vertices[index2].getElement()) { System.out.println("已存在該邊!"); break; } temp = temp.next; } temp.next = new GraphNode(vertices[index2].getElement());//有index10--》index20 GraphNode temp2 = vertices[index2]; while (temp2.getNext()!=null){ if (temp2.getNext().getElement() == vertices[index1].getElement()) { System.out.println("已存在該邊!"); break; } temp2 = temp2.next; } temp2.next = new GraphNode(vertices[index1].getElement());//有index20--》index10 } modCount++; }
Since a heap is a binary search tree, there is only one correct location for the insertion of a new node, and that is either the next open position from the left at level h or the first position on the left at level h+1 if level h is full. A .True B .Flase錯誤緣由:堆是一棵徹底二叉樹、不是一顆二叉搜索樹。本身瞎了眼!!!!測試
- 侯澤洋的博客一直寫的很細、很認真、完成的很是早。其中那個最短路程的那個算法寫的很是詳細,相關資料也給了出來。 - 仇夏和周亞傑的博客也都認真的寫了,總以爲本身寫的少了點什麼東西。 - 向他們學習,相信本身的博客會愈來愈好。
這一章內容比較簡單,或者能夠說書上的內容比較簡單,可是實現起來很難。也多是書上的例子比較少的緣由。還有就是發現裏面的幾個算法是很是有意思的,好比Dijkstra
算法。它能經過實際的幾個數組就能找到最短路徑!!!很驚訝!!!this
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 260/0 | 1/1 | 05/05 | |
第二週 | 300/560 | 1/2 | 13/18 | |
第三週 | 212/772 | 1/4 | 21/39 | |
第四周 | 330/1112 | 2/7 | 21/60 | |
第五週 | 1321/2433 | 1/8 | 30/90 | |
第六週 | 1024/3457 | 1/9 | 20/110 | |
第七週 | 1024/3457 | 1/9 | 20/130 | |
第八週 | 643/4100 | 2/11 | 30/170 | |
第八週 | 2800/6900 | 1 /12 | 30/200 |