Abridgelab

实验背景

本实验主要研究了割边(桥)的寻找算法(Tarjan)以及A*启发式算法

实验细节

1 Bridges

假设有连通图G,e是其中一条边,如果G-e是不连通的,则边e是图G的一条割割边,即桥。此情形下,G-e必包含两个连通分支。

1.1 makeGraph

makeGraph : edge seq -> ugraph

type vertex = int
 type edge = vertex * vertex
 type edges = edge seq

type ugraph = (vertex seq) seq

函数功能:

将所给的边集(Seq形式存储)转换成无向图(ugraph)

函数思路:

首先通过map将所给边集里的所有边映成无向边,然后通过collect整理即可

函数代码:

  fun cmp (x,y) =if x>y then GREATERelse if x<y then LESSelse EQUALfun makeGraph (E : edge seq) : ugraph = if length E = 0 then empty()else let val firSeq = append (E,(map (fn (x,y) => (y,x)) E))val secSeq = collect cmp firSeqval res = map (fn (x,y) => y) secSeqin resend

渐进复杂度分析:

firSeq: W=W(append)+W(map)=O(|E|) S=O(1)

secSeq: W=W(collect)=O(|E|log|E|) S=O(log^2|E|)

res: W=O(|V|) S=O(1)

而|E|<=|V|^2

故W(makeGraph)=O(|E|log|V|) S(makeGraph)=O(log^2|V|)

1.2 findBridges

findBridges : ugraph -> edge seq

函数功能:

求出所给无向图中的桥,即割边

函数思路:

对无向图运用Tarjan算法。即:

声明一个长度为全部点的个数的STSeq,其中每个元素均为三元组(isVisited,dfn,low)

分别表示:

isVisted:该点是否被访问过

dfn:在深度优先搜索中该点被访问的次序

low:深度优先搜索中该点可以往回追溯到的最先被访问的点

例如:

1->2->3->4

那么1,2,3,4的dfn分别为0,1,2,3,low则均为0

而本身点由0~|V|-1的整数表示,所以可以一一对应每个点的信息

初始化STSeq之后从给定点开始深度优先搜索,根据搜索到的点的访问与否可分为如下两种情况(假设某次搜索的边为(u,v)):

(1)v未访问

首先v.dfn以及v.low均为v被访问的次序

其次v.low修改为min(v.low,w.low),w为边(v,w)的终点

(2)v已访问

u.low修改为min(u.low,v.dfn)

注意:由于用(u,v),(v,u)来表示一条无向边,所以需要防止访问过v之后接着访问回u

深搜并修改完每个点的信息之后,对于边(u,v),如果u.dfn<v.low,那么这条边就是一条割边

函数代码:

  fun findBridges (G : ugraph) : edges = if length G = 0 then empty() elseletval preSTSeq = STSeq.fromSeq (tabulate (fn _ => (false,0,0)) (length G))fun DFS (fp,p) ((X,pre,label),v) = case (STSeq.nth X v) of (true,dfn,low) => if v=fp then (X,pre,label)else (STSeq.update (p,(true,#2 (STSeq.nth X p),min(#3 (STSeq.nth X p),dfn))) X ,pre,label) |(false,dfn,low) =>letval X' = STSeq.update (v,(true,label+1,label+1)) Xval neighbor = nth G vval (X'',pre',label') = iter (DFS (p,v)) (X',pre,label+1) neighborval X''' = STSeq.update (p,(true,#2 (STSeq.nth X'' p),min(#3 (STSeq.nth X'' p),#3 (STSeq.nth X'' v)))) X''val p' = STSeq.nth X''' p val v' = STSeq.nth X''' vval pre'' = if #2 p' < #3 v' then append (pre',singleton (p,v)) else pre' in(X''',pre'',label')endval res = #2 (DFS (0,0) ((preSTSeq,empty(),0),0))inresend

渐进复杂度分析:

由于采用STSeq存储各点信息,故有对其修改的操作均为常数时间

而割边相对于|E|、|V|很小,故每次发现割边的append操作可忽略

那么该算法时间复杂度即为基于STSeq的深度优先搜索的时间复杂度

即W=S= O(|V| + |E|)

2 Paths “R” Us

2.1 Using Dijkstra’s

2.1.1 Give an example of a graph on ≤ 4 vertices with negative edge weights where Dijkstra’s
algorithm fails to find the shortest paths. List the priority queue operations (i.e. insertions and updates
of shortest path lengths) up to the point of failure. Clearly point out where the algorithm has failed and
what it should have done.

(1)0入队,<(0,0)> | <>

(2)0出队,1,2入队,<(2,3),(1,5)> | <(0,0)>

(3)2出队,<(1,5)> | <(0,0),(2,3)>

(4)1出队,<> | <(0,0),(1,5),(2,3)>

(5)所得0->2路径长为3,显然最短路径应为1,故错误

2.1.2 Assuming there are no negative-weight cycles in G, how would you modify Dijkstra’s
to accomodate negative edge weights and return the correct solution?

使用Bellman-ford算法

2.2 The A ∗ Heuristic

2.2.1 Briefly argue why the Euclidean distance heuristic is both admissible and consistent for
edge weights that represent distances between vertices in Euclidean space.

admissible:由于欧式距离即两点之间的直线距离(即最短距离),故满足admissible

consistent:对于两点u,v,h(u)<h(v)+E(u,v)<=h(v)+w(u,v)  故满足consistent

2.2.2 Give a heuristic that causes A ∗ to perform exactly as Dijkstra’s algorithm would.

h为常数即可,例如h(v)=0

2.2.3 Give an example of a weighted graph on ≤ 4 vertices with a heuristic that is admissible
but inconsistent, where the Dijkstra-based A ∗ algorithm fails to find the shortest path from a single source
s to a single target t. Label each vertex with its heuristic value, and clearly mark the vertices s and t. In
2-3 clear sentences, explain why the shortest path is not found (e.g., when does the algorithm fail, what
exactly does it do wrong.)

如图,首先(0,0)入队出队后,(2,8+1),(1,5+5)入队,然后(2,8+1)优先出队,使得0->2的最短路径是(0,2),事

实上最短路径为0->1->2

2.2.4 Give an example of a weighted graph on ≤ 4 vertices with heuristic values that are inadmissible, where

the A ∗ algorithm fails to find the shortest path from a single source to a single target.
Again, clearly label your vertices with heuristic values and explain why the shortest path is not found.

如图,首先(0,0)入队出队后,(2,8+9),(1,5+13)入队,然后(2,8+9)优先出队,使得0->2最短路径为0->2,矛盾。

2.2.5 makeGraph

makeGraph : edge seq -> graph

type graph = (weight Table.table) Table.table

函数功能:

将所给边集转换成邻接表形式

函数思路:

将有向边映成无向边后collect整理即可

函数代码:

  fun makeGraph (E : edge Seq.seq) : graph = letval PreL = Seq.map (fn (u,v,w) => (u,Seq.singleton (v,w)) ) Eval PreR = Seq.map (fn (u,v,w) => (v,Seq.empty ()) ) Eval Res = Table.collect (Seq.append (PreL,PreR))inTable.map (fn x => (Table.fromSeq (Seq.flatten x))) Resend

渐进复杂度分析:

W= O(|E|log|V|) S=O(log 2 |V|)

2.2.6 findPath

findPath : heuristic -> graph -> (set * set)-> (vertex * real) option

函数功能:

返回所给图中A*启发式算法得出的最短路径

函数思路:

将每次入队的路径长度替换为d(v)+h(v),之后运用Dijkstra算法即可

函数代码:

  fun findPath h G (S, T) = letfun AStar (G, u) =letfun N(v) =case Table.find G vof NONE => Table.empty ()| SOME nbr => nbrfun dijkstra' D Q =case PQ.deleteMin Qof (NONE, _) => D| (SOME (d, v), Q') =>case Table.find D vof SOME _ => dijkstra' D Q'| NONE =>letval insert = Table.insert (fn _ => raise SegmentFault)val D' = insert (v, d) Dfun relax (q, (u, w)) = PQ.insert (d+w+h(u)-h(v), u) qval Q'' = Table.iter relax Q' (N v)in dijkstra' D' Q''endindijkstra' (Table.empty ()) (PQ.singleton (0.0+h(u), u))endval pre = Seq.flatten (Seq.map (fn x => Table.toSeq (AStar (G,x))) (Set.toSeq S))val res = Seq.filter (fn (x,y) => Set.find T x) preinif Seq.length res = 0 then NONEelseSOME (Seq.reduce (fn ((a,b),(c,d)) => if b<d then (a,b) else (c,d)) (#1 (Seq.nth res 0),100000.0) res)end

渐进复杂度分析:

上述修改仅为替换,相比较Dijkstra算法多了加减运算,故时间复杂度不变



SML-Abridgelab相关推荐

  1. educoder SML程序设计题线下编译环境搭建

    背景 最近<串并行数据结构与算法设计>的老师在educoder上布置了一些SML程序设计题,虽然网站上有在线编译功能,但还是在线下编译调试方便,特记录编译环境过程如下(我用的GVIM,但N ...

  2. sml完整形式_411的完整形式是什么?

    sml完整形式 411:信息 (411: Information) 411 is an abbreviation of "Information". 411是"信息&qu ...

  3. sml完整形式_教资会的完整形式是什么?

    sml完整形式 教资会:大学教育资助委员会 (UGC: University Grants Commission) UGC is an abbreviation of the University G ...

  4. 花书+吴恩达深度学习(二五)直面配分函数(CD, SML, SM, RM, NCE)

    文章目录 0. 前言 1. 对数似然梯度 1.1 朴素 MCMC 算法 1.2 对比散度算法 CD 1.3 随机最大似然 SML 2. 伪似然 3. 得分匹配 SM 4. 比率匹配 RM 5. 去噪得 ...

  5. cml sml区别_如何简单清晰地描述 CAPM 在投资学中的运用,以及 CAL、CML 和 SML 的关系和区别?...

    如何简单清晰地描述 CAPM 在投资学中的运用,以及 CAL.CML 和 SML 的关系和区别? 2018-09-21 [姚岑卓的回答(41票)]: 想深入了解CML和SML,我们首先得先知道它们是怎 ...

  6. 计算机l符号代表什么意思,衣服sml代表什么意思 分别是什么的标记

    衣服sml代表什么意思 分别是什么的标记 作者: 小丘 更新日期: 2020-03-28 22:16:54 人靠衣装,现在大家都非常关心自己穿的衣服是不是适合自己的风格的,是不是符合自己的身材的.尤其 ...

  7. 基于C语言实现的SML简单程序设计

    资源下载地址:https://download.csdn.net/download/sheziqiong/85896108 资源下载地址:https://download.csdn.net/downl ...

  8. cml sml区别_资本资产定价模型中cml与sml有什么区别?

    本期品职"学霸笔记"冠名的是投资英才招聘大会,最引人瞩目的两大公司是资本市场线CML与证券市场线SML,有很多应聘者带着自己的投资策划书慕名而来,却不知要投给哪一个.下面我们就详细 ...

  9. cml sml区别_cml和sml的区别

    ML与SML有何联系和区别? 资本市场线(Capital Market Line,CML) 什么是资本市场线 资本市场线是指表明有效组合的期望收益率和标准差之间的一种简单的线性关系的一条射线.它是沿着 ...

  10. sml基本语法(三)——函数

    写在前面 本博客更类似博主本人的学习笔记,请各位谨慎参考,以防被误导 由于写本篇时对函数式编程仍然理解不够深入,现在看来命令式思维很重,但是作为sml入门来说应该有些心得的确能帮助到大家 语法原型(数 ...

最新文章

  1. 自动化运维工具Saltstack(一)
  2. C++内存分配和管理
  3. C++ 原码、反码、补码理解笔记
  4. 【老孙随笔】属相影响你的职业前途吗?
  5. MADDPG中环境怎么配置,multiagent包解决
  6. JAVA awt eventqueue_线程“AWT-EventQueue-1”中的异常java.lang.NullPointerException
  7. 简单选择排序_Python3三种简单排序(冒泡、插入、选择)的比较
  8. Ⅴ0还有别的方法设置图案吗_水冷空调扇如何使用 水冷空调扇使用方法介绍【图文】...
  9. 修改服务器控件的ID和Name
  10. vue 前端使用 element-ui 实现省市级联动
  11. BinarySerializer
  12. 计算机声卡驱动程序,教大家如何安装声卡驱动【详细步骤】
  13. 知识星球<我们谈论数据科学>-python30天打卡训练营Day1
  14. 记模拟器出现横竖屏切换闪屏问题
  15. android 环信录音,环信音视频插件
  16. larval PHP artisan命令
  17. 【程序人生 | 价值扳机】你的寒假自律第一步(建议收藏)
  18. 从分歧走向融合:图神经网络历经了怎样的演化之路?
  19. leetcode 29.两数相除
  20. 机器学习不会解决自然语言理解(NLU)问题

热门文章

  1. 华为很快搭载鸿蒙系统,华为高管确认很快将会推出搭载鸿蒙操作系统的智能手表...
  2. 工作随记3:一次交换机环路故障
  3. 基于JAVA项目任务跟踪系统计算机毕业设计源码+数据库+lw文档+系统+部署
  4. 华为云云耀云服务器 中小企业的福音
  5. 其实英语实在太简单了
  6. Swift学习笔记 ——(一)
  7. 实时音视频数据传输协议介绍
  8. gensim训练wiki中文词向量
  9. 重磅!吴恩达深度学习又开新课啦!
  10. 【Devc++】双人跑酷小游戏1.3