电路板问题

There are n terminals on each side of a circuit board indexed with 1, 2, …, n from left to right, as shown in the following figure. We need to connect terminal i (1 ≤ in) on the top side with a unique terminal π(i) on the bottom side, and vice versa. In other words, we need n lines (i, π(i)) (1 ≤ in) to connect n pairs of terminals,.

Two lines (i, π(i)) and (j, π(j)) ( i ¹ j) will cross each other if i < j but π(i) > π(j), or vice versa. Two lines are called compatible if they do not cross each other. We wish to find the largest set of mutually compatible lines so that they can be layout on the same layer of circuit without crossing each other.

 

Please model this problem as a graph problem first and then design an O(n2) algorithm to solve it.

 

Solution:

 

We construct a directed graph G(V, E) as follows.

(1)   For each line (i, π(i)) (1 ≤ in), we construct a vertex vi.

(2)   If lines (i, π(i)) and (j, π(j)) ( i < j) are compatible, then draw an edge from vi to vj.

(3)   Add one vertex s to V, so V = {s} È { v1, v2, v3, ...., vn }.

(4)   Draw an edge from s to each vi (1 ≤ in).

 

It is easy to see that lines with indices i1 < i2 < i3 < .... < vk are mutually compatible if and only if there is a path <s, v1, v2, v3, ...., vk>. Therefore, the longest path in G corresponds to the largest compatible set. We can use the algorithm in problem 3 to find the longest path in G.

 

The time complexity is O(n2) because we may check every pair of lines when we construct the graph G. Finding the longest path needs O(n+m) = O(n2) time in the worst case.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值