BFS DFS 判断DAG(有向无环图)


title: BFS DFS 判断DAG(有向无环图)
date: 2020-03-26 18:56:47
tags: Algorithm


BFS DFS 判断DAG(有向无环图)

前几天美团笔试 ,笔试里有一个单源最短路问题(直接弃了,完全没想到会考图论的问题,Dijkstra算法也完全想不起来),最近看了下leetcode上一道图论的问题,AOV的拓扑排序问题。

一、DAG 和 Topological sorting

以下基本都是一些本科数据结构的知识,不过因为本科听课不太认真,几乎都是现查现学的。

DAG

DAG(Directed acyclic graph)即有向无环图,维基上的介绍: Directed acyclic graph

简单的介绍DAG,是一个图且是一个有向图,而且整个图没有回路,不会构成环。据说目前比较热门的区块链技术好像也有应用这种数据结构,文章: 解释有向无环图(达格),真正的区块链3.0

DAG 具有 拓扑顺序(Topological ordering)关于拓扑顺序,引用维基上的一段话:

Every directed acyclic graph has a topological ordering, an ordering of the vertices such that the starting endpoint of every edge occurs earlier in the ordering than the ending endpoint of the edge. The existence of such an ordering can be used to characterize DAGs: a directed graph is a DAG if and on ly if it has a topological ordering. In general, this ordering is not unique; a DAG has a unique topological ordering if and on ly if it has a directed path c on taining all the vertices, in which case the ordering is the same as the order in which the vertices appear in the path.[[9]

The family of topological orderings of a DAG is the same as the family of linear extensi on s of the reachability relati on for the DAG,[10] so any two graphs representing the same partial order have the same set of topological orders.

大概意思就是 Graph中顶点之间有顺序上的约束关系,这种约束关系用边表示。即:如果图中从V到W有一条有向路径,则V一定排在W之前。满足此条件的顶点序列称为一个拓扑序。(维基里涉及到族的概念,由于不是科班的,也没有学过拓扑学,搞不懂)

Topological sorting

维基的解释:

Topological sorting is the algorithmic problem of finding a topological ordering of a given DAG. It can be solved in linear time.[16] Kahn’s algorithm for topological sorting builds the vertex ordering directly. It maintains a list of vertices that have no incoming edges from other vertices that have not already been included in the partially c on structed topological ordering; initially this list c on sists of the vertices with no incoming edges at all. Then, it repeatedly adds on e vertex from this list to the end of the partially c on struct

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值