C 合并节点向量以获得公共节点向量
假设我有以下节点向量C 合并节点向量以获得公共节点向量,c,algorithm,merge,C,Algorithm,Merge,假设我有以下节点向量U1,U2,U3 U1 = {0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 0.8}; U2 = {0.21, 0.25, 0.3, 0.6, 0.7, 0.8}; U3 = {0.25, 0.3, 0.7, 0.8}; 现在我想计算它们的公共结向量,即包含U1、U2、U3所有元素且长度最小的结向量。例如,包含U1的所有元素意味着包含 {0.25,0.25,0.5,0.5,0.75,0.75,0.8},而不是{0.25,0.5,0.75,0.8}。 对
U1,U2,U3
U1 = {0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 0.8};
U2 = {0.21, 0.25, 0.3, 0.6, 0.7, 0.8};
U3 = {0.25, 0.3, 0.7, 0.8};
现在我想计算它们的公共结向量,即包含U1、U2、U3
所有元素且长度最小的结向量。例如,包含U1
的所有元素意味着包含
{0.25,0.25,0.5,0.5,0.75,0.75,0.8}
,而不是{0.25,0.5,0.75,0.8}
。
对于这种情况,U1、U2、U3的公共结向量为:
commonKnotsVector = {0.21, 0.25, 0.25, 0.3, 0.5, 0.5, 0.6, 0.7, 0.75, 0.75, 0.8}
对于公共结向量,我的Mathematica算法如下所示:
commonKnotsVector[knots_] :=
Module[{step1, step2, step3, step4},
step1 = SortBy[Join @@ Tally /@ knots, First];
step2 = GatherBy[step1, First];
step3 = Last /@ (SortBy[#, Last] & /@ step2);
step4 = step3 /. {x_, n_} :> ConstantArray[x, n];
Flatten[step4]
]
commonKnotsVector[{U1, U2, U3}]
(*{0.21, 0.25, 0.25, 0.3, 0.5, 0.5, 0.6, 0.7, 0.75, 0.75, 0.8}*)
问题:
- 现在我想知道如何在C中处理这个问题?我对C算法一无所知
您提到的合并
标记-这是正确的方法,使用基于三向合并
步骤的算法
有三个排序向量。伪代码:
While vectors are not empty:
Find the smallest element among the heads of all vectors
Extract it into `current` element
Add `current` to output vector
Until next smallest element is equal to `current`:
Just remove it from vector head.
您可以使用基于二进制堆的最简单优先级队列-这里堆将只包含三个元素。您提到的merge
tag-这是正确的方法,使用基于三向merge
步骤的算法
有三个排序向量。伪代码:
While vectors are not empty:
Find the smallest element among the heads of all vectors
Extract it into `current` element
Add `current` to output vector
Until next smallest element is equal to `current`:
Just remove it from vector head.
您可以使用基于二进制堆的最简单优先级队列-这里堆将只包含三个元素。使用以下算法
i j k min_val
1 1 1 0.21 j++
1 2 1 0.25 i++ j++ k++
2 3 2 0.25 i++
3 3 2 0.3 j++ k++
3 4 3 0.5 i++
4 4 3 0.5 i++
5 4 3 0.6 j++
5 5 3 0.7 j++ k++
5 6 4 0.75 i++
6 6 4 0.75 i++
7 6 4 0.8 i++ j++ k++
//===>common vector
U = {0.21, 0.25, 0.25, 0.3, 0.5, 0.5, 0.6, 0.7, 0.75, 0.75, 0.8}
两个向量的C函数
定义最小二(a,b)(a)>(b)?(b) :(a)
//count表示U1、U2的公共向量的长度
int two_vec_common(双*U1,双*U2,int len1,int len2,双*temp){
int i,j;
双最小值;
整数计数=0;
i=j=0;
而(i
三向量的C函数
double min_三(双x,双y,双z){
如果(x使用以下算法
i j k min_val
1 1 1 0.21 j++
1 2 1 0.25 i++ j++ k++
2 3 2 0.25 i++
3 3 2 0.3 j++ k++
3 4 3 0.5 i++
4 4 3 0.5 i++
5 4 3 0.6 j++
5 5 3 0.7 j++ k++
5 6 4 0.75 i++
6 6 4 0.75 i++
7 6 4 0.8 i++ j++ k++
//===>common vector
U = {0.21, 0.25, 0.25, 0.3, 0.5, 0.5, 0.6, 0.7, 0.75, 0.75, 0.8}
两个向量的C函数
定义最小二(a,b)(a)>(b)?(b):(a)
//count表示U1、U2的公共向量的长度
int two_vec_common(双*U1,双*U2,int len1,int len2,双*temp){
int i,j;
双最小值;
整数计数=0;
i=j=0;
而(i
三向量的C函数
double min_三(双x,双y,双z){
if(x)