Algorithm and Data Structure Review

本文深入探讨了常用的数据结构如数组、链表、堆、树等,并详细介绍了各种算法的基本思想、实现方式及应用场景,包括排序、搜索、图算法、字符串处理等。

常用总结。

1、常见数据结构

线性:
          数组:Merge Sorted Array
          链表:Merge k Sorted ListsPartition List
          队列,
          堆栈,
          块状数组(数组+链表)
          hash表,
          双端队列
        位图(bitmap)

树:
   二叉树: Minimum Depth of Binary Tree, Path Sum II, Inorder Travel
        堆(大顶堆、小顶堆)
          trie树(字母树or字典树)
          后缀树,
        后缀树组
          二叉排序/查找树,
          B+/B-,
        AVL树
        Treap
          红黑树
          splay树
        线段树
          树状数组

图:图

其它:并查集

2、常见算法

(1)       基本思想:枚举,
               递归: 
Flatten Binary Tree to Linked ListGenerate ParenthesesLetter Combinations of a Phone Number
               分治,
               模拟&贪心: Gray CodeInsert IntervalJump Game II, Multiply Strings, Next PermutationPalindrome NumberPascal’s Triangle II
贪心,动态规划,剪枝,回溯
               二分查找:Median of Two Sorted ArraysSearch in Rotated Sorted Array

(2)       图算法:深度优先遍历与广度优先遍历, 最短路径,最小生成树,拓扑排序

(3)       字符串算法:
                   字符串查找: Implement strStrLength Of Last WordLongest Common Prefix, 
                                     Longest Palindromic SubstringLongest Non-Repeating SubString
                   双指针: Minimum Window Substring
                   hash算法,KMP算法

(4)       排序算法:冒泡,插入,选择,快排,归并排序,堆排序,
               桶排序: First Missing Positive

(5)       动态规划:Distinct SubsequencesEdit DistanceInterleaving StringJump GameLargest Rectangle in Histogram
  背包问题,最长公共子序列,最优二分检索树

(6)       数论问题:素数问题,整数问题,进制转换,同余模运算,
                     进制转换:Integer To Roman
                     乘除法:Divide Two Integers 

(7)       排列组合:排列和组合算法

(8)       其它:LCA与RMQ问题
               
 (9) 水箱问题:Trapping Rain WaterContainer With Most Water

3. 常见设计题
(1)海量数据处理


UPDATE_GRAPH_PROMPT = """ You are an AI expert specializing in graph memory management and optimization. Your task is to analyze existing graph memories alongside new information, and update the relationships in the memory list to ensure the most accurate, current, and coherent representation of knowledge. Input: 1. Existing Graph Memories: A list of current graph memories, each containing source, target, and relationship information. 2. New Graph Memory: Fresh information to be integrated into the existing graph structure. Guidelines: 1. Identification: Use the source and target as primary identifiers when matching existing memories with new information. 2. Conflict Resolution: - If new information contradicts an existing memory: a) For matching source and target but differing content, update the relationship of the existing memory. b) If the new memory provides more recent or accurate information, update the existing memory accordingly. 3. Comprehensive Review: Thoroughly examine each existing graph memory against the new information, updating relationships as necessary. Multiple updates may be required. 4. Consistency: Maintain a uniform and clear style across all memories. Each entry should be concise yet comprehensive. 5. Semantic Coherence: Ensure that updates maintain or improve the overall semantic structure of the graph. 6. Temporal Awareness: If timestamps are available, consider the recency of information when making updates. 7. Relationship Refinement: Look for opportunities to refine relationship descriptions for greater precision or clarity. 8. Redundancy Elimination: Identify and merge any redundant or highly similar relationships that may result from the update. Memory Format: source -- RELATIONSHIP -- destination Task Details: ======= Existing Graph Memories:======= {existing_memories} ======= New Graph Memory:======= {new_memories} Output: Provide a list of update instructions, each specifying the source, target, and the new relationship to be set. Only include memories that require updates. """ EXTRACT_RELATIONS_PROMPT = """ You are an advanced algorithm designed to extract structured information from text to construct knowledge graphs. Your goal is to capture comprehensive and accurate information. Follow these key principles: 1. Extract only explicitly stated information from the text. 2. Establish relationships among the entities provided. 3. Use "USER_ID" as the source entity for any self-references (e.g., "I," "me," "my," etc.) in user messages. CUSTOM_PROMPT Relationships: - Use consistent, general, and timeless relationship types. - Example: Prefer "professor" over "became_professor." - Relationships should only be established among the entities explicitly mentioned in the user message. Entity Consistency: - Ensure that relationships are coherent and logically align with the context of the message. - Maintain consistent naming for entities across the extracted data. Strive to construct a coherent and easily understandable knowledge graph by eshtablishing all the relationships among the entities and adherence to the user’s context. Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction.""" DELETE_RELATIONS_SYSTEM_PROMPT = """ You are a graph memory manager specializing in identifying, managing, and optimizing relationships within graph-based memories. Your primary task is to analyze a list of existing relationships and determine which ones should be deleted based on the new information provided. Input: 1. Existing Graph Memories: A list of current graph memories, each containing source, relationship, and destination information. 2. New Text: The new information to be integrated into the existing graph structure. 3. Use "USER_ID" as node for any self-references (e.g., "I," "me," "my," etc.) in user messages. Guidelines: 1. Identification: Use the new information to evaluate existing relationships in the memory graph. 2. Deletion Criteria: Delete a relationship only if it meets at least one of these conditions: - Outdated or Inaccurate: The new information is more recent or accurate. - Contradictory: The new information conflicts with or negates the existing information. 3. DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes. 4. Comprehensive Analysis: - Thoroughly examine each existing relationship against the new information and delete as necessary. - Multiple deletions may be required based on the new information. 5. Semantic Integrity: - Ensure that deletions maintain or improve the overall semantic structure of the graph. - Avoid deleting relationships that are NOT contradictory/outdated to the new information. 6. Temporal Awareness: Prioritize recency when timestamps are available. 7. Necessity Principle: Only DELETE relationships that must be deleted and are contradictory/outdated to the new information to maintain an accurate and coherent memory graph. Note: DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes. For example: Existing Memory: alice -- loves_to_eat -- pizza New Information: Alice also loves to eat burger. Do not delete in the above example because there is a possibility that Alice loves to eat both pizza and burger. Memory Format: source -- relationship -- destination Provide a list of deletion instructions, each specifying the relationship to be deleted. """ def get_delete_messages(existing_memories_string, data, user_id): return DELETE_RELATIONS_SYSTEM_PROMPT.replace( "USER_ID", user_id ), f"Here are the existing memories: {existing_memories_string} \n\n New Information: {data}" 帮我按照上述思路分析一下
07-05
### 代码概述 这段代码定义了三个 Prompt 模板(提示词模板):`UPDATE_GRAPH_PROMPT`、`EXTRACT_RELATIONS_PROMPT` 和 `DELETE_RELATIONS_SYSTEM_PROMPT`,以及一个函数 `get_delete_messages`。这些 Prompt 被用于构建图记忆(graph memory)的更新、提取和删除逻辑,通常与知识图谱系统配合使用。 --- ### 代码解析 #### 1. `UPDATE_GRAPH_PROMPT` ```python UPDATE_GRAPH_PROMPT = """ You are an AI expert specializing in graph memory management and optimization... """ ``` - **功能**:该 Prompt 用于指导 AI 模型分析现有图记忆,并根据新的信息对关系进行更新。 - **输入**: - `existing_memories`: 已有的图记忆列表。 - `new_memories`: 新增的记忆内容。 - **输出**:需要执行的更新操作列表,每条包含源节点、目标节点和新关系。 - **核心原则**: - 更新只发生在源节点和目标节点一致的情况下; - 优先使用更准确或更近期的信息; - 提升语义一致性并避免冗余。 #### 2. `EXTRACT_RELATIONS_PROMPT` ```python EXTRACT_RELATIONS_PROMPT = """ You are an advanced algorithm designed to extract structured information from text to construct knowledge graphs... """ ``` - **功能**:用于从用户文本中提取结构化的关系信息,形成图记忆节点和边。 - **输入**:自然语言文本。 - **输出**:提取出的三元组形式(source -- relationship -- destination)的知识图谱数据。 - **关键点**: - 只提取明确提及的信息; - 对“我”、“你”等自指代词使用 `USER_ID` 表示; - 关系描述应简洁通用; - 实体名称需保持一致性。 #### 3. `DELETE_RELATIONS_SYSTEM_PROMPT` ```python DELETE_RELATIONS_SYSTEM_PROMPT = """ You are a graph memory manager specializing in identifying, managing, and optimizing relationships within graph-based memories... """ ``` - **功能**:用于判断是否应该删除图记忆中的某些关系。 - **输入**: - `existing_memories`: 现有图记忆; - `new_text`: 新输入的信息; - **输出**:要删除的关系列表。 - **删除依据**: - 关系过时或不准确; - 与新信息矛盾; - 不删除可能存在多种不同目标节点的相同类型关系。 #### 4. `get_delete_messages` 函数 ```python def get_delete_messages(existing_memories_string, data, user_id): return DELETE_RELATIONS_SYSTEM_PROMPT.replace( "USER_ID", user_id ), f"Here are the existing memories: {existing_memories_string} \n\n New Information: {data}" ``` - **作用**:封装生成删除任务所需的两个输入部分: - 替换 Prompt 中的 `USER_ID` 占位符为实际用户ID; - 构建包含现有记忆和新信息的输入字符串; - **返回值**: - 第一个元素是处理后的 Prompt; - 第二个元素是拼接好的上下文信息,供模型处理使用。 --- ### 知识点 1. **Prompt Engineering(提示工程)** - 使用标准化模板引导大模型完成特定任务,如提取、更新、删除图记忆; - 提高模型理解任务意图的一致性和准确性。 2. **知识图谱关系管理** - 包括新增、更新、删除图谱中的实体间关系; - 强调语义一致性、时间有效性及信息准确性。 3. **文本到图谱映射机制** - 将非结构化文本转化为结构化的三元组形式(源-关系-目标); - 支持基于图数据库的认知推理和记忆增强应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值