Let S be a set of 10 digit numbers. Given any two numbers v and w in S, I'd like to know if there is a sequence of numbers v=u_0, u_1, ... , u_k=w such that:
我们设是一组10位数的数字。给定任意两个S中的v和w,我想知道是否有一个v=u_0, u_1,…u_k = w这样:
- each u_i is in S
- 每个u_i都在S中
- for each i=1,..,k, the numbers u_{i-1} and u_i differ in exactly one position
- 对于每个i = 1,. .,k, u_{i-1}和u_i在一个位置上完全不同
As a plus, it would be even better to find an algorithm to find the shortest such sequence.
作为一个加号,最好能找到一个算法来找到最短的这样的序列。
Ideally, I would prefer a C (or pseudo-code) solution, but I really, really appreciate any and all suggestions on this one! Thanks!
理想的情况下,我更喜欢C(或伪代码)解决方案,但是我真的非常非常感谢关于这个方案的所有建议!谢谢!
2 个解决方案
#1
3
Form a graph from elements of S: u and v are adjacent iff they differ in exactly one coordinate.
从S: u和v的元素构成一个图,它们是相邻的iff它们在一个坐标上是不同的。
Now given u, do a breadth first search till you hit v.
给定u,先进行广度搜索,直到到达v。
#2
0
I would convert S
to a graph of node objects, where each node object contains links to the adjacent nodes. (In some programming languages, read 'links' as 'pointers'.) Adjacency is defined by your condition 2 on the sequence, so that any path through the resulting graph is a sequence matching the two conditions.
我将S转换为一个节点对象的图形,其中每个节点对象包含指向相邻节点的链接。(在一些编程语言中,将“链接”读为“指针”)。邻接由序列上的条件2定义,因此通过结果图的任何路径都是匹配两个条件的序列。
From there, it's a simple problem of checking for connectedness of two vertices in your graph. The simplest solution is a breadth-first search. (That particular algorithm also happens to find the shortest path(s).)
从这里开始,这是一个检查图中两个顶点连通性的简单问题。最简单的解决方案是广度优先搜索。(这个算法也恰好找到了最短路径。)
#1
3
Form a graph from elements of S: u and v are adjacent iff they differ in exactly one coordinate.
从S: u和v的元素构成一个图,它们是相邻的iff它们在一个坐标上是不同的。
Now given u, do a breadth first search till you hit v.
给定u,先进行广度搜索,直到到达v。
#2
0
I would convert S
to a graph of node objects, where each node object contains links to the adjacent nodes. (In some programming languages, read 'links' as 'pointers'.) Adjacency is defined by your condition 2 on the sequence, so that any path through the resulting graph is a sequence matching the two conditions.
我将S转换为一个节点对象的图形,其中每个节点对象包含指向相邻节点的链接。(在一些编程语言中,将“链接”读为“指针”)。邻接由序列上的条件2定义,因此通过结果图的任何路径都是匹配两个条件的序列。
From there, it's a simple problem of checking for connectedness of two vertices in your graph. The simplest solution is a breadth-first search. (That particular algorithm also happens to find the shortest path(s).)
从这里开始,这是一个检查图中两个顶点连通性的简单问题。最简单的解决方案是广度优先搜索。(这个算法也恰好找到了最短路径。)