##【0】README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 无权最短路径 的思想并用源代码加以实现;
##【1】无权最短路径相关概念(边的权值赋值为1)
**1.1)概述:**下图就是表示一个无权图G。使用某个顶点s作为输入参数, 我们想要计算 从s到所有其他顶点的最短路径;
**1.2)0路径:**若我们选择s 为 v3,此时立刻可以说出从s 到v3的最短路径是0路径;
1.3)算法描述:
- **step1)**寻找所有与s 距离为 1 的顶点, 此时我们看到 v1和v6 与s只有一边之遥;
- **step2)**寻找所有与s 距离为 2 的顶点, 即找出所有邻接到 v1 和 v6的顶点(与v1和v6 距离为1的顶点), 得到v2和v4, 所以v1到v2或者v4的距离为2; ......
- step3)循环这个过程,直到所有的顶点都被遍历到;
Conclusion)以上搜索过程称为广度优先搜索 广度优先搜索)该方法按层处理顶点: 距开始点最近的 那些顶点首先被赋值, 而最远的那些顶点最后被赋值, 这很像对树的层次遍历;
##【2】如何实现上述算法? 2.1)对于每个顶点, 我们将跟踪三个信息(info):
- **I1)dv栏(distance):**我们把从s开始到顶点的距离放到 dv栏中,开始的时候,除开s所有的 顶点都是不可达到的,而s的路径长为0;
- I2)pv栏(path): pv栏中的项为薄记变量, 它将使我们能够显示出实际的路径;
- I3)known栏: Known中的项点被处理后置为1;
**2.2)**开始,所有的顶点都不是 Known,包括开始顶点。当一个顶点被标记为已知是, 我们就确信不会再找到更便宜的路径,因此对该顶点的处理实质上已经完成了;
##【3】我们使用类似于 拓扑排序的做法来提供寻找无权最短路径算法的性能 **3.1)算法描述:**在任一时刻, 只存在两种类型的未知顶点,他们的dv≠∞, 一些顶点的dv=CurrDist, 而其余的则有 dv=CurrDist+1; 3.2)使用队列把这种想法进一步精化: 在迭代开始的时候, 队列只含有距离为CurrDist的那些顶点。当我们添加距离为 CurrDist+1的那些邻接顶点时, 由于它们自队尾入队, 因此这就保证了它们直到所有距离为 CurrDist 的顶点都被处理后才被处理。在距离为 CurrDist处的最后一个顶点出队并被处理之后, 队列只含有距离为 CurrDist+1的顶点, 因此该过程将不断进行下去。 我们只需要把开始的节点放入队列中以启动这个过程即可; 3.3)时间复杂度: 只要使用邻接表, 则运行时间就是 O(|E| + |V|); 3.4)无权最短路径算法实例图如下: (显然, 某顶点出队后, 更新其相应的known为1;再查找出队顶点vertex的邻接顶点,如果有的话,将他们依次入队,依次更新distance自增1且path路径更新为出队顶点vertex的顶点编号)
##【4】source code + printing results 4.1)download source code: https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter9/p224_unweighted_shortest_path 4.2)source code at a glance (for full code, please download source code following the given link above):
#include "unweightedTable.h"
// allocate the memory for every element in unweighted table
UnweightedTable makeEmptyUnweightedTable()
{
UnweightedTable element;
element = (UnweightedTable)malloc(sizeof(struct UnweightedTable));
if(!element)
{
Error("out of space ,from func makeEmptyUnweightedTable");
return NULL;
}
element->known = 0; // 1 refers to accessed , also 0 refers to not accessed
element->distance = MaxInt;
element->path = -1; // index starts from 0
return element;
}
//allocate the memory for initializing unweighted table
UnweightedTable *initUnweightedTable(int size)
{
UnweightedTable* table;
int i;
table = (UnweightedTable*)malloc(sizeof(UnweightedTable) * size);
if(!table)
{
Error("out of space ,from func initUnweightedTable");
return NULL;
}
for(i = 0; i < size; i++)
{
table[i] = makeEmptyUnweightedTable();
if(!table[i])
return NULL;
}
return table;
}
//computing the unweighted shortest path between the vertex under initIndex and other vertexs
void unweighted_shortest_path(AdjTable* adj, int size, int startVertex, Queue queue)
{
int adjVertex;
UnweightedTable* table;
ElementType vertex;
AdjTable temp;
table = initUnweightedTable(size);
enQueue(queue, startVertex-1); //if let start vertex equals to v3, then initIndex=3 and let index=2 enter the queue
table[startVertex-1]->distance = 0;// update the distance
table[startVertex-1]->path = 0;// update the path of starting vertex
while(!isEmpty(queue))
{
vertex = deQueue(queue); // if the queue is not empty, conducting departing queue
table[vertex]->known = 1; // update the vertex as accessed, also responding known 1
temp = adj[vertex]->next;
while(temp)
{
adjVertex = temp->index; // let each adjVertex adjacent to vertex enter the queue
if(table[adjVertex]->path == -1) // key that judge whether corresponding element's path equals to -1 ,-1 means the element has never entered the queue
{
enQueue(queue, adjVertex);
table[adjVertex]->distance = table[vertex]->distance + 1;// update the distance
table[adjVertex]->path = vertex; //update the path of adjVertex, also responding path evaluated as vertex
}
temp = temp->next;
}
}
disposeQueue(queue);
//print unweighted table
printUnweightedtable(table, size, startVertex);
printf("
");
}
//print unweighted table
void printUnweightedtable(UnweightedTable* table, int size, int startVertex)
{
int i;
int path;
char *str[4] =
{
"vertex",
"known",
"distance",
"path"
};
printf("
unweighted shortest path table are as follows:
");
printf("
%6s%6s%9s%5s", str[0], str[1], str[2], str[3]);
for(i=0; i<size; i++)
{
if(i != startVertex-1)
printf("
%-3d %3d %5d v%-3d ", i+1, table[i]->known, table[i]->distance, table[i]->path+1);
else
printf("
*%-3d %3d %5d %-3d ", i+1, table[i]->known, table[i]->distance, 0);
}
}
int main()
{
AdjTable* adj;
Queue queue;
int size = 7;
int i;
int j;
int column = 4;
int startVertex;
int adjTable[7][4] =
{
{2, 4, 0, 0},
{4, 5, 0, 0},
{1, 6, 0, 0},
{3, 5, 6, 7},
{7, 0, 0, 0},
{0, 0, 0, 0},
{6, 0, 0, 0}
};
printf("
====== test for topological sorting with adjoining table ======
");
adj = initAdjTable(size);
queue = initQueue(size);
printf("
====== the initial adjoining table is as follows:======
");
for(i = 0; i < size; i++)
for(j = 0; j < column; j++)
if(adjTable[i][j])
insertAdj(adj, adjTable[i][j]-1, i); // insertAdj the adjoining table over
printAdjTable(adj, size);
// finding the unweighted shortest path starts
// void unweighted_shortest_path(AdjTable* adj, int size, int initIndex, Queue queue)
startVertex = 3; // we set the vertex3 as the start vertex (but you know, whose index in array is 2)
unweighted_shortest_path(adj, size, startVertex, queue);
return 0;
}
4.3)printing results:
设计模式学习之简单工厂(Simple Factory,创建型模式)(1)
JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
JS图片延迟加载分析及简单的demo
SVN服务器搭建和使用(三)(转载)
SVN服务器搭建和使用(二)(转载)
SVN服务器搭建和使用(一)(转载)
JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
Entity FrameWork 中使用Expression<Func<T,true>>访问数据库性能优化
JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)
- 最新文章
-
css:多个div在同一行显示
java线程状态及转换
<input type="date">设置默认当前日期
js:Date格式化
java内存模型:Happens-Before
java内存模型:简单理解
java并发:CAS算法和ABA问题
java并发:AtomicInteger 以及CAS无锁算法【转载】
大型网站应用之海量数据和高并发解决方案总结一二 【转载】
活动 Activity 四种加载模式
- 热门文章
-
Android ADB 端口占用问题解决方案
emulatorarm.exe已停止工作
textview点击后selector的pressed无效果
在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!
Android运行时异常“Binary XML file line # : Error inflating class”
在唯一密钥属性“value”设置为“Default.aspx”时,无法添加类型为“add”的重复集合项
HTTP 错误 404.3
ADT eclipse打开时出现Error: Error parsing C:Usersadmin*.androiddevices.xml
Error:No marked region found along edge.
设计模式学习之抽象工厂(Abstract Factory,创建型模式)(3)