图 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 |