提出这个问题最阴险的方式是什么?

发布于 2024-07-15 10:40:19 字数 1062 浏览 10 评论 0原文

迄今为止我最好的镜头:

送货车辆需要进行一系列送货(d1、d2、...dn),并且可以以任意顺序执行此操作 - 换句话说,集合 D = {d1,d2,...dn< 的所有可能排列/sub>} 是有效的解决方案 - 但需要在离开路线一端的基站之前确定特定的解决方案(例如,想象一下包裹需要按后进先出的方式装载到车辆中)。

此外,各种排列的成本也不相同。 它可以计算为 di -1 和 di 之间行驶距离的平方和,其中 d0 被视为基站,但需要注意的是,任何涉及方向改变的路段成本都是原来的 3 倍(想象一下这是在铁路或气动管道上发生的,备份会扰乱其他交通)。

给定一组交付 D 表示为距基站的距离(因此 abs(di-d< /code>j) 是两次交付之间的距离) 和一个迭代器 permutations(D) ,它将连续产生每个排列,找到一个成本小于或等于任何其他排列的排列。

现在,根据此描述直接实现可能会生成如下代码:

function Cost(D) ...

function Best_order(D)
    for D1 in permutations(D)
        Found = true
        for D2 in permutations(D)
            Found = false if cost(D2) > cost(D1)
        return D1 if Found

这是 O(n*n!^2),例如非常糟糕 - 特别是与 O(n log(n)) 相比,有洞察力的人会发现, 我的问题是

:你能想出一个看似合理的问题描述吗?它自然会导致粗心的人陷入更糟糕(或者更糟糕的)排序算法实现?

My best shot so far:

A delivery vehicle needs to make a series of deliveries (d1,d2,...dn), and can do so in any order--in other words, all the possible permutations of the set D = {d1,d2,...dn} are valid solutions--but the particular solution needs to be determined before it leaves the base station at one end of the route (imagine that the packages need to be loaded in the vehicle LIFO, for example).

Further, the cost of the various permutations is not the same. It can be computed as the sum of the squares of distance traveled between di -1 and di, where d0 is taken to be the base station, with the caveat that any segment that involves a change of direction costs 3 times as much (imagine this is going on on a railroad or a pneumatic tube, and backing up disrupts other traffic).

Given the set of deliveries D represented as their distance from the base station (so abs(di-dj) is the distance between two deliveries) and an iterator permutations(D) which will produce each permutation in succession, find a permutation which has a cost less than or equal to that of any other permutation.

Now, a direct implementation from this description might lead to code like this:

function Cost(D) ...

function Best_order(D)
    for D1 in permutations(D)
        Found = true
        for D2 in permutations(D)
            Found = false if cost(D2) > cost(D1)
        return D1 if Found

Which is O(n*n!^2), e.g. pretty awful--especially compared to the O(n log(n)) someone with insight would find, by simply sorting D.

My question: can you come up with a plausible problem description which would naturally lead the unwary into a worse (or differently awful) implementation of a sorting algorithm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幻梦 2024-07-22 10:40:19

我假设您在面试中使用这个问题是为了看看申请人是否可以在看似复杂的问题中注意到一个简单的解决方案。

[这个假设是不正确的 - MarkusQ]

您提供了太多信息。

解决这个问题的关键是认识到这些点是一维的,并且只需要排序即可。 为了让这个问题变得更加困难,尽可能隐藏这个事实。

最大的线索是距离公式。 它引入了改变方向的惩罚。 我首先想到的是尽量减少这种惩罚。 为了消除惩罚,我必须将它们按某个方向排序,这种排序是自然排序顺序。

我会取消改变方向的惩罚,这太过分了。

另一个主要线索是算法的输入值:整数列表。 给他们一个排列列表,甚至所有排列。 这让他们认为 O(n!) 算法实际上是可以预期的。

我将其表述为:

给定所有可能的列表
n 个交付地点的排列,
其中每个交付排列
(d1, d2, ...,
dn) 的成本定义为:

返回排列 P,使得
P 的成本小于或等于任何
其他排列。

真正需要做的就是读取第一个排列并对其进行排序。

如果他们构建一个循环来比较成本,请询问他们算法的 big-o 运行时间是多少,其中 n 是送货地点的数量(另一个陷阱)。

I assume you're using this question for an interview to see if the applicant can notice a simple solution in a seemingly complex question.

[This assumption is incorrect -- MarkusQ]

You give too much information.

The key to solving this is realizing that the points are in one dimension and that a sort is all that is required. To make this question more difficult hide this fact as much as possible.

The biggest clue is the distance formula. It introduces a penalty for changing directions. The first thing an that comes to my mind is minimizing this penalty. To remove the penalty I have to order them in a certain direction, this ordering is the natural sort order.

I would remove the penalty for changing directions, it's too much of a give away.

Another major clue is the input values to the algorithm: a list of integers. Give them a list of permutations, or even all permutations. That sets them up to thinking that a O(n!) algorithm might actually be expected.

I would phrase it as:

Given a list of all possible
permutations of n delivery locations,
where each permutation of deliveries
(d1, d2, ...,
dn) has a cost defined by:

Return permutation P such that the
cost of P is less than or equal to any
other permutation.

All that really needs to be done is read in the first permutation and sort it.

If they construct a single loop to compare the costs ask them what the big-o runtime of their algorithm is where n is the number of delivery locations (Another trap).

多彩岁月 2024-07-22 10:40:19

这不是直接答案,但我认为需要更多澄清。

di 允许为负数吗? 如果是这样,据我所知,仅排序是不够的。

例如:

d0 = 0

deliveries = (-1,1,1,2)

在这种情况下,最佳路径似乎是<代码>1> 2> 1> -1。

编辑:这实际上可能不是最佳路径,但它说明了这一点。

This isn't a direct answer, but I think more clarification is needed.

Is di allowed to be negative? If so, sorting alone is not enough, as far as I can see.

For example:

d0 = 0

deliveries = (-1,1,1,2)

It seems the optimal path in this case would be 1 > 2 > 1 > -1.

Edit: This might not actually be the optimal path, but it illustrates the point.

旧竹 2024-07-22 10:40:19

首先找到最佳解决方案后,您可以将其重新表述为

“给我一个证明,证明以下说服对于以下规则集是最佳的,其中最佳意味着所有阶段成本之和产生的最小数字,考虑到考虑到所有阶段(A..Z)都需要出现一次且仅一次:

说服:

A->C->D->Y->P->...->N

阶段成本:

A->B = 5,
B->A = 3,
A->C = 2,
C->A = 4,
...
...
...
Y->Z = 7,
Z->Y = 24."

这应该让某人忙碌一段时间。

YOu could rephrase it, having first found the optimal solution, as

"Give me a proof that the following convination is the most optimal for the following set of rules, where optimal means the smallest number results from the sum of all stage costs, taking into account that all stages (A..Z) need to be present once and once only.

Convination:

A->C->D->Y->P->...->N

Stage costs:

A->B = 5,
B->A = 3,
A->C = 2,
C->A = 4,
...
...
...
Y->Z = 7,
Z->Y = 24."

That ought to keep someone busy for a while.

冷心人i 2024-07-22 10:40:19

这让我想起了背包问题,而不是旅行推销员。 但背包也是一个 NP 难问题,因此,如果人们将你的问题与背包相关联,你可能可以欺骗人们使用动态规划想出一个过于复杂的解决方案。 基本问题是:

能否达到至少 V 的值
不超过重量W?

现在的问题是,当 V 唯一时,您的距离可以找到一个相当好的解决方案,如下所示:

每种类型的背包问题
项目 j 具有不同的值
重量单位 (vj = pj/wj) 是
被认为是最简单的之一
NP 完全问题。 确实是经验主义的
复杂度约为 O((log
n)2) 非常大的问题可以是
很快就解决了,例如2003年
求解所需的平均时间
n = 10,000 的实例低于 14
使用商品个人的毫秒
计算机1

因此,您可能想要声明多个站点/包裹可能共享相同的 vj,从而邀请人们思考真正困难的解决方案:

但是在
多个项目的退化情况
共享相同的值 vj 变成
极端情况下困难得多
vj = 常数的情况
具有复杂性的子集和问题
O(2N/2N)。

因此,如果您将每个值的权重替换为每个值的距离,并声明几个距离实际上可能共享相同的值,那么退化,有些人可能会落入这个陷阱。

This reminds me of the Knapsack problem, more than the Traveling Salesman. But the Knapsack is also an NP-Hard problem, so you might be able to fool people to think up an over complex solution using dynamic programming if they correlate your problem with the Knapsack. Where the basic problem is:

can a value of at least V be achieved
without exceeding the weight W?

Now the problem is a fairly good solution can be found when V is unique, your distances, as such:

The knapsack problem with each type of
item j having a distinct value per
unit of weight (vj = pj/wj) is
considered one of the easiest
NP-complete problems. Indeed empirical
complexity is of the order of O((log
n)2) and very large problems can be
solved very quickly, e.g. in 2003 the
average time required to solve
instances with n = 10,000 was below 14
milliseconds using commodity personal
computers1.

So you might want to state that several stops/packages might share the same vj, inviting people to think about the really hard solution to:

However in the
degenerate case of multiple items
sharing the same value vj it becomes
much more difficult with the extreme
case where vj = constant being the
subset sum problem with a complexity
of O(2N/2N).

So if you replace the weight per value to distance per value, and state that several distances might actually share the same values, degenerate, some folk might fall in this trap.

与他有关 2024-07-22 10:40:19

这不就是(NP-Hard)旅行推销员问题吗? 你似乎不会让事情变得更加困难。

也许对问题的表述使得实际的算法不清楚 - 例如,通过将路径描述为单轨铁路线,这样人们就必须从领域知识中推断出回溯的成本更高。

如果以一种让人忍不住进行递归比较的方式来描述问题——例如“你能通过使用你最好的(到目前为止)结果的最佳最大子集来加速算法吗”?

顺便说一句,这样做的目的是什么——听起来似乎是为了折磨受访者。

Isn't this just the (NP-Hard) Travelling Salesman Problem? It doesn't seem likely that you're going to make it much harder.

Maybe phrasing the problem so that the actual algorithm is unclear - e.g. by describing the paths as single-rail railway lines so the person would have to infer from domain knowledge that backtracking is more costly.

What about describing the question in such a way that someone is tempted to do recursive comparisions - e.g. "can you speed up the algorithm by using the optimum max subset of your best (so far) results"?

BTW, what's the purpose of this - it sounds like the intent is to torture interviewees.

骷髅 2024-07-22 10:40:19

您需要更清楚送货卡车是否必须返回基地(使其成为往返)。 如果卡车确实返回,那么简单的排序不会生成最短路线,因为从最远点到基地的返回平方成本非常高。 在“出去”的路上错过一些啤酒花并在回来的路上使用它们会更便宜。

如果你欺骗某人给出了错误的答案(例如,没有向他们提供所有信息),那么是他们的愚蠢还是你的欺骗造成了这种情况?

如果智者不留意自我的谎言,他们的智慧会有多大?

You need to be clearer on whether the delivery truck has to return to base (making it a round trip), or not. If the truck does return, then a simple sort does not produce the shortest route, because the square of the return from the furthest point to base costs so much. Missing some hops on the way 'out' and using them on the way back turns out to be cheaper.

If you trick someone into a bad answer (for example, by not giving them all the information) then is it their foolishness or your deception that has caused it?

How great is the wisdom of the wise, if they heed not their ego's lies?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文