《Introduction to Algorithm》22.4-2

Description for 22.4-2

在这里插入图片描述
在这里插入图片描述

Solutions for 22.4-2

  1. 第22章第4节讲述了拓扑排序的应用,最直观的做法是先利用拓扑排序对图22.8进行DFS从而获得一个拓扑序列T,之后从t开始依次向前扫描处理直到s (利用了有向无环图中无环即无反向边的性质)

    下图为其中一个拓扑排序:
    在这里插入图片描述

算法一 (求p到v的简单路径数):

	1)将所有节点赋值num=0,v赋值num=1
	2)从v开始向前依次遍历,对于当前节点y:
		3)若y之前存在节点x,并且在此拓扑序列中存在边(x,y)
		4)x.num += y.num
		5)如果v==p,输出p.num

2.下面的例子方便理解,算法正确性参考DFS以及拓扑排序(有向无环图)的相关性质。考虑到时间复杂度,我们可以直接修改DFS,在拓扑序列生成前,完成对s到t的简单路径数的计算:

算法二 (DFS修改): 调用DFS(G,s)

DFS(G,s,t)
	1) for each vertex u ∈ G.V
	2) 		u.color = WHITE
	3)		u.sum = 0
	3) num = DFS-VISIT(G,s,t)
	4) return num
DFS-VISIT(G,u,t)
	1)u.color = Gray
	2) if u == t
	3) 		return 1
	4for each v ∈ G:adj[u]
	5if u.color == WHITE
	6) 			sum += DFS(G,v,t)
	7) u.Color == BLACK
	8return u.sum
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
May 2001 Part I: Foundations Chapter List Chapter 1: The Role of Algorithms in Computing Chapter 2: Getting Started Chapter 3: Growth of Functions Chapter 4: Recurrences Chapter 5: Probabilistic Analysis and Randomized Algorithms Introduction This part will get you started in thinking about designing and analyzing algorithms. It is intended to be a gentle introduction to how we specify algorithms, some of the design strategies we will use throughout this book, and many of the fundamental ideas used in algorithm analysis. Later parts of this book will build upon this base. Chapter 1 is an overview of algorithms and their place in modern computing systems. This chapter defines what an algorithm is and lists some examples. It also makes a case that algorithms are a technology, just as are fast hardware, graphical user interfaces, objectoriented systems, and networks. In Chapter 2, we see our first algorithms, which solve the problem of sorting a sequence of n numbers. They are written in a pseudocode which, although not directly translatable to any conventional programming language, conveys the structure of the algorithm clearly enough that a competent programmer can implement it in the language of his choice. The sorting algorithms we examine are insertion sort, which uses an incremental approach, and merge sort, which uses a recursive technique known as "divide and conquer." Although the time each requires increases with the value of n, the rate of increase differs between the two algorithms. We determine these running times in Chapter 2, and we develop a useful notation to express them. Chapter 3 precisely defines this notation, which we call asymptotic notation. It starts by defining several asymptotic notations, which we use for bounding algorithm running times from above and/or below. The rest of Chapter 3 is primarily a presentation of mathematical notation. Its purpose is more to ensure that your use of notation matches that in this book than to teach you new mathematical concepts. Chapter 4 delves further into the divide-and-conquer method introduced in Chapter 2. In particular, Chapter 4 contains methods for solving recurrences, which are useful for describing the running times of recursive algorithms. One powerful technique is the "master method," which can be used to solve recurrences that arise from divide-and-conquer algorithms. Much of Chapter 4 is devoted to proving the correctness of the master method, though this proof may be skipped without harm.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值