回溯算法的一些碎碎念

回溯法,一般可以解决如下几种问题:

组合问题:N个数里面按一定规则找出k个数的集合
切割问题:一个字符串按一定规则有几种切割方式
子集问题:一个N个数的集合里有多少符合条件的子集
排列问题:N个数按一定规则全排列,有几种排列方式
棋盘问题:N皇后,解数独等等

回溯法模板

树中节点孩子的数量 等于 集合的大小构成了树的宽度
递归的深度构成树的深度

void backtracking(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
        处理节点;
        backtracking(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FP-growth算法的C语言实现可以参考以下代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义FP树的节点结构 typedef struct FPNode { char item; // 节点代表的项 int count; // 项出现的次数 struct FPNode *parent; // 父节点指针 struct FPNode *child; // 子节点指针 struct FPNode *sibling; // 兄弟节点指针 struct FPNode *next; // 下一个相同项的节点指针 } FPNode; // 定义项头表项结构 typedef struct HeaderItem { char item; // 项 int count; // 项出现的次数 struct FPNode *nodeLink; // 指向项的第一个节点指针 } HeaderItem; // 构建项头表 void buildHeaderTable(char *transactions[], int numTransactions, HeaderItem *headerTable, int minSupport) { int i, j; for (i = 0; i < numTransactions; i++) { char *transaction = transactions[i]; for (j = 0; j < strlen(transaction); j++) { char item = transaction[j]; int k; for (k = 0; k < minSupport; k++) { if (headerTable[k].item == item) { headerTable[k].count++; break; } } } } } // 构建FP树 void buildFPTree(char *transactions[], int numTransactions, HeaderItem *headerTable, int minSupport, FPNode **root) { int i, j; for (i = 0; i < numTransactions; i++) { char *transaction = transactions[i]; FPNode *currentNode = *root; FPNode *parentNode = NULL; for (j = 0; j < strlen(transaction); j++) { char item = transaction[j]; FPNode *childNode = NULL; while (1) { if (currentNode->item == item) { currentNode->count++; break; } if (currentNode->sibling != NULL) { currentNode = currentNode->sibling; } else { childNode = (FPNode*)malloc(sizeof(FPNode)); childNode->item = item; childNode->count = 1; childNode->parent = currentNode; childNode->child = NULL; childNode->sibling = NULL; childNode->next = headerTable[item].nodeLink; headerTable[item].nodeLink = childNode; if (currentNode->child != NULL) { currentNode = currentNode->child; while (currentNode->sibling != NULL) { currentNode = currentNode->sibling; } currentNode->sibling = childNode; } else { currentNode->child = childNode; } break; } } currentNode = childNode; } } } // 打印FP树 void printFPTree(FPNode *node, int depth) { int i; for (i = 0; i < depth; i++) { printf("\t"); } printf("%c:%d\n", node->item, node->count); FPNode *childNode = node->child; while (childNode != NULL) { printFPTree(childNode, depth + 1); childNode = childNode->sibling; } } // 主函数 int main() { char *transactions[] = {"ABCE", "ACD", "ABDE", "ACDE", "ABCDE"}; int numTransactions = sizeof(transactions) / sizeof(transactions[0]); int minSupport = 2; // 构建项头表 HeaderItem headerTable[26] = {0}; buildHeaderTable(transactions, numTransactions, headerTable, minSupport); // 构建FP树 FPNode *root = (FPNode*)malloc(sizeof(FPNode)); root->item = '\0'; root->count = 0; root->parent = NULL; root->child = NULL; root->sibling = NULL; root->next = NULL; buildFPTree(transactions, numTransactions, headerTable, minSupport, &root); // 打印FP树 printFPTree(root, 0); return 0; } ``` 以上是一个简单的FP-growth算法的C语言实现。在主函数中,我们定义了一些样本事务,设置了最小支持度为2。然后通过调用`buildHeaderTable`和`buildFPTree`函数构建了项头表和FP树。最后,通过调用`printFPTree`函数打印了构建好的FP树。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值