数据结构之图-有向图的拓扑排序(8)

Definition

Topological sorting is useful in situations in which items or events must be arranged in a specific order. For example, topological sorting can model job scheduling.Modeling job schedules with graphs is called critical path analysis(关键路线分析CPI).


The above figure adjacent matrix:

For a directed graph, the method that adds an edge thus needs only a single statement,

public void addEdge(int start, int end) // directed graph
{
adjMat[start][end] = 1;
}

The topological sorting algorithm

The idea behind the topological sorting algorithm is unusual but simple. Three steps are necessary:

1) Find a vertex that has no successors.

The successors to a vertex are those vertices that are directly “downstream” from it—that is, connected to it by an edge that points in their direction. If there is an edge pointing from A to B, then B is a successor to A.


2) Delete this vertex from the graph, and insert its label at the end of a list.

3) Step1 and step2 are repeated until all the vertices are gone. At this point, the list shows the vertices arranged in topological order.

Deleting is the heart of the algorithm. The algorithm works because if a vertex has no successors, it must be the last one in the topological ordering.

The topological sorting algorithm works on unconnected graphs(非连通图) as well as connected graphs(连通图). One kind of graph the topological-sort algorithm cannot handle is a graph with cycles.A topo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值