图遍历算法:从入门到AI实战应用,一篇搞定面试与业务场景 - }#mermaid-svg-DNrcXQal2af1p7tK
图遍历算法:从入门到AI实战应用,一篇搞定面试与业务场景
你是否遇到过这些问题?
面试时被问到BFS和DFS的区别,只能支支吾吾?知道图遍历很重要,但不知道在AI项目中怎么用?看了很多算法文章,还是云里雾里?
别担心!这篇文章用最通俗的语言、最直观的图示,带你彻底搞懂图遍历算法,并深入其在AI领域的实战应用!
目录
一、什么是图?为什么需要遍历?二、图遍历的两种核心方式三、图遍历在AI中的核心应用四、实战案例:AI教育平台知识图谱五、面试高频问题全解析六、总结与进阶路线
一、什么是图?为什么需要遍历?
1.1 图的直观理解
你和你的朋友是节点(Vertex)你们之间的好友关系是边(Edge)整个社交网络就是一个图(Graph)
#mermaid-svg-SWrD9xP3aXAm5wWs{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-SWrD9xP3aXAm5wWs .error-icon{fill:#552222;}#mermaid-svg-SWrD9xP3aXAm5wWs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SWrD9xP3aXAm5wWs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SWrD9xP3aXAm5wWs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SWrD9xP3aXAm5wWs .marker.cross{stroke:#333333;}#mermaid-svg-SWrD9xP3aXAm5wWs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SWrD9xP3aXAm5wWs p{margin:0;}#mermaid-svg-SWrD9xP3aXAm5wWs .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster-label text{fill:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster-label span{color:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster-label span p{background-color:transparent;}#mermaid-svg-SWrD9xP3aXAm5wWs .label text,#mermaid-svg-SWrD9xP3aXAm5wWs span{fill:#333;color:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs .node rect,#mermaid-svg-SWrD9xP3aXAm5wWs .node circle,#mermaid-svg-SWrD9xP3aXAm5wWs .node ellipse,#mermaid-svg-SWrD9xP3aXAm5wWs .node polygon,#mermaid-svg-SWrD9xP3aXAm5wWs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-SWrD9xP3aXAm5wWs .rough-node .label text,#mermaid-svg-SWrD9xP3aXAm5wWs .node .label text,#mermaid-svg-SWrD9xP3aXAm5wWs .image-shape .label,#mermaid-svg-SWrD9xP3aXAm5wWs .icon-shape .label{text-anchor:middle;}#mermaid-svg-SWrD9xP3aXAm5wWs .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-SWrD9xP3aXAm5wWs .rough-node .label,#mermaid-svg-SWrD9xP3aXAm5wWs .node .label,#mermaid-svg-SWrD9xP3aXAm5wWs .image-shape .label,#mermaid-svg-SWrD9xP3aXAm5wWs .icon-shape .label{text-align:center;}#mermaid-svg-SWrD9xP3aXAm5wWs .node.clickable{cursor:pointer;}#mermaid-svg-SWrD9xP3aXAm5wWs .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-SWrD9xP3aXAm5wWs .arrowheadPath{fill:#333333;}#mermaid-svg-SWrD9xP3aXAm5wWs .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-SWrD9xP3aXAm5wWs .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-SWrD9xP3aXAm5wWs .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-SWrD9xP3aXAm5wWs .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-SWrD9xP3aXAm5wWs .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-SWrD9xP3aXAm5wWs .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster text{fill:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs .cluster span{color:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-SWrD9xP3aXAm5wWs .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-SWrD9xP3aXAm5wWs rect.text{fill:none;stroke-width:0;}#mermaid-svg-SWrD9xP3aXAm5wWs .icon-shape,#mermaid-svg-SWrD9xP3aXAm5wWs .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-SWrD9xP3aXAm5wWs .icon-shape p,#mermaid-svg-SWrD9xP3aXAm5wWs .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-SWrD9xP3aXAm5wWs .icon-shape rect,#mermaid-svg-SWrD9xP3aXAm5wWs .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-SWrD9xP3aXAm5wWs .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-SWrD9xP3aXAm5wWs .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-SWrD9xP3aXAm5wWs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
好友
好友
好友
好友
好友
你
小明
小红
小刚
小美
1.2 图的基本概念
概念说明生活案例节点(Vertex)图中的数据元素微信用户、城市、网页边(Edge)节点之间的关系好友关系、道路、超链接有向图边有方向关注关系(A关注B,B不一定关注A)无向图边无方向好友关系(双向的)权重边的代价或价值道路距离、亲密度
1.3 为什么需要遍历?
遍历 = 系统地访问图中的每个节点
实际场景:
搜索引擎:爬取网页时需要遍历整个互联网图吝 导航软件:找最短路径需要遍历城市图 游戏AI:寻找敌人或资源需要遍历地图烙 AI推荐:分析社交关系需要遍历用户图
二、图遍历的两种核心方式
2.1 深度优先搜索(DFS)
核心思想:一条路走到黑,撞墙再回头
#mermaid-svg-MqMY2iYZowTTLZen{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MqMY2iYZowTTLZen .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MqMY2iYZowTTLZen .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MqMY2iYZowTTLZen .error-icon{fill:#552222;}#mermaid-svg-MqMY2iYZowTTLZen .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MqMY2iYZowTTLZen .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MqMY2iYZowTTLZen .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MqMY2iYZowTTLZen .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MqMY2iYZowTTLZen .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MqMY2iYZowTTLZen .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MqMY2iYZowTTLZen .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MqMY2iYZowTTLZen .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MqMY2iYZowTTLZen .marker.cross{stroke:#333333;}#mermaid-svg-MqMY2iYZowTTLZen svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MqMY2iYZowTTLZen p{margin:0;}#mermaid-svg-MqMY2iYZowTTLZen .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MqMY2iYZowTTLZen .cluster-label text{fill:#333;}#mermaid-svg-MqMY2iYZowTTLZen .cluster-label span{color:#333;}#mermaid-svg-MqMY2iYZowTTLZen .cluster-label span p{background-color:transparent;}#mermaid-svg-MqMY2iYZowTTLZen .label text,#mermaid-svg-MqMY2iYZowTTLZen span{fill:#333;color:#333;}#mermaid-svg-MqMY2iYZowTTLZen .node rect,#mermaid-svg-MqMY2iYZowTTLZen .node circle,#mermaid-svg-MqMY2iYZowTTLZen .node ellipse,#mermaid-svg-MqMY2iYZowTTLZen .node polygon,#mermaid-svg-MqMY2iYZowTTLZen .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MqMY2iYZowTTLZen .rough-node .label text,#mermaid-svg-MqMY2iYZowTTLZen .node .label text,#mermaid-svg-MqMY2iYZowTTLZen .image-shape .label,#mermaid-svg-MqMY2iYZowTTLZen .icon-shape .label{text-anchor:middle;}#mermaid-svg-MqMY2iYZowTTLZen .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MqMY2iYZowTTLZen .rough-node .label,#mermaid-svg-MqMY2iYZowTTLZen .node .label,#mermaid-svg-MqMY2iYZowTTLZen .image-shape .label,#mermaid-svg-MqMY2iYZowTTLZen .icon-shape .label{text-align:center;}#mermaid-svg-MqMY2iYZowTTLZen .node.clickable{cursor:pointer;}#mermaid-svg-MqMY2iYZowTTLZen .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MqMY2iYZowTTLZen .arrowheadPath{fill:#333333;}#mermaid-svg-MqMY2iYZowTTLZen .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MqMY2iYZowTTLZen .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MqMY2iYZowTTLZen .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MqMY2iYZowTTLZen .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MqMY2iYZowTTLZen .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MqMY2iYZowTTLZen .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MqMY2iYZowTTLZen .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MqMY2iYZowTTLZen .cluster text{fill:#333;}#mermaid-svg-MqMY2iYZowTTLZen .cluster span{color:#333;}#mermaid-svg-MqMY2iYZowTTLZen div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MqMY2iYZowTTLZen .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MqMY2iYZowTTLZen rect.text{fill:none;stroke-width:0;}#mermaid-svg-MqMY2iYZowTTLZen .icon-shape,#mermaid-svg-MqMY2iYZowTTLZen .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MqMY2iYZowTTLZen .icon-shape p,#mermaid-svg-MqMY2iYZowTTLZen .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MqMY2iYZowTTLZen .icon-shape rect,#mermaid-svg-MqMY2iYZowTTLZen .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MqMY2iYZowTTLZen .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MqMY2iYZowTTLZen .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MqMY2iYZowTTLZen :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
A
B
C
D
E
F
G
DFS遍历顺序:A → B → D → E → C → F → G
def dfs(graph, start, visited=None):
"""
深度优先搜索
:param graph: 图的邻接表表示
:param start: 起始节点
:param visited: 已访问节点集合
"""
if visited is None:
visited = set()
visited.add(start)
print(f"访问节点: {start}")
for neighbor in graph[start]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
return visited
# 示例
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F', 'G'],
'D': [],
'E': [],
'F': [],
'G': []
}
dfs(graph, 'A')
执行过程可视化:
调用栈
DFS函数
主程序
调用栈
DFS函数
主程序
#mermaid-svg-BmRm6bXFGKrE468u{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BmRm6bXFGKrE468u .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BmRm6bXFGKrE468u .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BmRm6bXFGKrE468u .error-icon{fill:#552222;}#mermaid-svg-BmRm6bXFGKrE468u .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BmRm6bXFGKrE468u .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BmRm6bXFGKrE468u .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BmRm6bXFGKrE468u .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BmRm6bXFGKrE468u .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BmRm6bXFGKrE468u .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BmRm6bXFGKrE468u .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BmRm6bXFGKrE468u .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BmRm6bXFGKrE468u .marker.cross{stroke:#333333;}#mermaid-svg-BmRm6bXFGKrE468u svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BmRm6bXFGKrE468u p{margin:0;}#mermaid-svg-BmRm6bXFGKrE468u .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BmRm6bXFGKrE468u text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-BmRm6bXFGKrE468u .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BmRm6bXFGKrE468u .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-BmRm6bXFGKrE468u .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-BmRm6bXFGKrE468u .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-BmRm6bXFGKrE468u #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-BmRm6bXFGKrE468u .sequenceNumber{fill:white;}#mermaid-svg-BmRm6bXFGKrE468u #sequencenumber{fill:#333;}#mermaid-svg-BmRm6bXFGKrE468u #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-BmRm6bXFGKrE468u .messageText{fill:#333;stroke:none;}#mermaid-svg-BmRm6bXFGKrE468u .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BmRm6bXFGKrE468u .labelText,#mermaid-svg-BmRm6bXFGKrE468u .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-BmRm6bXFGKrE468u .loopText,#mermaid-svg-BmRm6bXFGKrE468u .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-BmRm6bXFGKrE468u .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BmRm6bXFGKrE468u .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-BmRm6bXFGKrE468u .noteText,#mermaid-svg-BmRm6bXFGKrE468u .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-BmRm6bXFGKrE468u .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BmRm6bXFGKrE468u .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BmRm6bXFGKrE468u .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BmRm6bXFGKrE468u .actorPopupMenu{position:absolute;}#mermaid-svg-BmRm6bXFGKrE468u .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-BmRm6bXFGKrE468u .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BmRm6bXFGKrE468u .actor-man circle,#mermaid-svg-BmRm6bXFGKrE468u line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-BmRm6bXFGKrE468u :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
访问 A
访问 B
访问 D
访问 E
dfs(A)
压入 A
dfs(B) [A的邻居]
压入 B
dfs(D) [B的邻居]
压入 D
弹出 D [无未访问邻居]
弹出 B [无其他未访问邻居]
dfs(E) [B的另一邻居]
2.2 广度优先搜索(BFS)
核心思想:层层推进,先近后远
#mermaid-svg-n7us7NLr1cnRmtGB{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-n7us7NLr1cnRmtGB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-n7us7NLr1cnRmtGB .error-icon{fill:#552222;}#mermaid-svg-n7us7NLr1cnRmtGB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-n7us7NLr1cnRmtGB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-n7us7NLr1cnRmtGB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-n7us7NLr1cnRmtGB .marker.cross{stroke:#333333;}#mermaid-svg-n7us7NLr1cnRmtGB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-n7us7NLr1cnRmtGB p{margin:0;}#mermaid-svg-n7us7NLr1cnRmtGB .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-n7us7NLr1cnRmtGB .cluster-label text{fill:#333;}#mermaid-svg-n7us7NLr1cnRmtGB .cluster-label span{color:#333;}#mermaid-svg-n7us7NLr1cnRmtGB .cluster-label span p{background-color:transparent;}#mermaid-svg-n7us7NLr1cnRmtGB .label text,#mermaid-svg-n7us7NLr1cnRmtGB span{fill:#333;color:#333;}#mermaid-svg-n7us7NLr1cnRmtGB .node rect,#mermaid-svg-n7us7NLr1cnRmtGB .node circle,#mermaid-svg-n7us7NLr1cnRmtGB .node ellipse,#mermaid-svg-n7us7NLr1cnRmtGB .node polygon,#mermaid-svg-n7us7NLr1cnRmtGB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-n7us7NLr1cnRmtGB .rough-node .label text,#mermaid-svg-n7us7NLr1cnRmtGB .node .label text,#mermaid-svg-n7us7NLr1cnRmtGB .image-shape .label,#mermaid-svg-n7us7NLr1cnRmtGB .icon-shape .label{text-anchor:middle;}#mermaid-svg-n7us7NLr1cnRmtGB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-n7us7NLr1cnRmtGB .rough-node .label,#mermaid-svg-n7us7NLr1cnRmtGB .node .label,#mermaid-svg-n7us7NLr1cnRmtGB .image-shape .label,#mermaid-svg-n7us7NLr1cnRmtGB .icon-shape .label{text-align:center;}#mermaid-svg-n7us7NLr1cnRmtGB .node.clickable{cursor:pointer;}#mermaid-svg-n7us7NLr1cnRmtGB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-n7us7NLr1cnRmtGB .arrowheadPath{fill:#333333;}#mermaid-svg-n7us7NLr1cnRmtGB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-n7us7NLr1cnRmtGB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-n7us7NLr1cnRmtGB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-n7us7NLr1cnRmtGB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-n7us7NLr1cnRmtGB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-n7us7NLr1cnRmtGB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-n7us7NLr1cnRmtGB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-n7us7NLr1cnRmtGB .cluster text{fill:#333;}#mermaid-svg-n7us7NLr1cnRmtGB .cluster span{color:#333;}#mermaid-svg-n7us7NLr1cnRmtGB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-n7us7NLr1cnRmtGB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-n7us7NLr1cnRmtGB rect.text{fill:none;stroke-width:0;}#mermaid-svg-n7us7NLr1cnRmtGB .icon-shape,#mermaid-svg-n7us7NLr1cnRmtGB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-n7us7NLr1cnRmtGB .icon-shape p,#mermaid-svg-n7us7NLr1cnRmtGB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-n7us7NLr1cnRmtGB .icon-shape rect,#mermaid-svg-n7us7NLr1cnRmtGB .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-n7us7NLr1cnRmtGB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-n7us7NLr1cnRmtGB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-n7us7NLr1cnRmtGB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
A
B
C
D
E
F
G
BFS遍历顺序:A → B → C → D → E → F → G
from collections import deque
def bfs(graph, start):
"""
广度优先搜索
:param graph: 图的邻接表表示
:param start: 起始节点
"""
visited = set([start])
queue = deque([start])
while queue:
node = queue.popleft()
print(f"访问节点: {node}")
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return visited
bfs(graph, 'A')
执行过程可视化:
队列
BFS函数
主程序
队列
BFS函数
主程序
#mermaid-svg-0j04oNpQaJW6dT89{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-0j04oNpQaJW6dT89 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0j04oNpQaJW6dT89 .error-icon{fill:#552222;}#mermaid-svg-0j04oNpQaJW6dT89 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0j04oNpQaJW6dT89 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0j04oNpQaJW6dT89 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0j04oNpQaJW6dT89 .marker.cross{stroke:#333333;}#mermaid-svg-0j04oNpQaJW6dT89 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0j04oNpQaJW6dT89 p{margin:0;}#mermaid-svg-0j04oNpQaJW6dT89 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0j04oNpQaJW6dT89 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-0j04oNpQaJW6dT89 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-0j04oNpQaJW6dT89 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-0j04oNpQaJW6dT89 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-0j04oNpQaJW6dT89 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-0j04oNpQaJW6dT89 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-0j04oNpQaJW6dT89 .sequenceNumber{fill:white;}#mermaid-svg-0j04oNpQaJW6dT89 #sequencenumber{fill:#333;}#mermaid-svg-0j04oNpQaJW6dT89 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-0j04oNpQaJW6dT89 .messageText{fill:#333;stroke:none;}#mermaid-svg-0j04oNpQaJW6dT89 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0j04oNpQaJW6dT89 .labelText,#mermaid-svg-0j04oNpQaJW6dT89 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-0j04oNpQaJW6dT89 .loopText,#mermaid-svg-0j04oNpQaJW6dT89 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-0j04oNpQaJW6dT89 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-0j04oNpQaJW6dT89 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-0j04oNpQaJW6dT89 .noteText,#mermaid-svg-0j04oNpQaJW6dT89 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-0j04oNpQaJW6dT89 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0j04oNpQaJW6dT89 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0j04oNpQaJW6dT89 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0j04oNpQaJW6dT89 .actorPopupMenu{position:absolute;}#mermaid-svg-0j04oNpQaJW6dT89 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-0j04oNpQaJW6dT89 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0j04oNpQaJW6dT89 .actor-man circle,#mermaid-svg-0j04oNpQaJW6dT89 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-0j04oNpQaJW6dT89 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
访问 A
访问 B, C
访问 D, E
访问 F, G
bfs(A)
入队 [A]
出队 A
入队 [B, C]
出队 B
入队 [D, E]
出队 C
入队 [F, G]
2.3 DFS vs BFS 对比
维度DFS(深度优先)BFS(广度优先)数据结构栈(递归调用栈)队列空间复杂度O(V) 最坏情况O(V) 最坏情况时间复杂度O(V + E)O(V + E)适用场景路径查找、拓扑排序最短路径、层级遍历实现方式递归或显式栈队列搜索策略一条路走到底逐层扩展
#mermaid-svg-ivwI8F4CjDT3XnWL{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ivwI8F4CjDT3XnWL .error-icon{fill:#552222;}#mermaid-svg-ivwI8F4CjDT3XnWL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ivwI8F4CjDT3XnWL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ivwI8F4CjDT3XnWL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ivwI8F4CjDT3XnWL .marker.cross{stroke:#333333;}#mermaid-svg-ivwI8F4CjDT3XnWL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ivwI8F4CjDT3XnWL p{margin:0;}#mermaid-svg-ivwI8F4CjDT3XnWL .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster-label text{fill:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster-label span{color:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster-label span p{background-color:transparent;}#mermaid-svg-ivwI8F4CjDT3XnWL .label text,#mermaid-svg-ivwI8F4CjDT3XnWL span{fill:#333;color:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL .node rect,#mermaid-svg-ivwI8F4CjDT3XnWL .node circle,#mermaid-svg-ivwI8F4CjDT3XnWL .node ellipse,#mermaid-svg-ivwI8F4CjDT3XnWL .node polygon,#mermaid-svg-ivwI8F4CjDT3XnWL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ivwI8F4CjDT3XnWL .rough-node .label text,#mermaid-svg-ivwI8F4CjDT3XnWL .node .label text,#mermaid-svg-ivwI8F4CjDT3XnWL .image-shape .label,#mermaid-svg-ivwI8F4CjDT3XnWL .icon-shape .label{text-anchor:middle;}#mermaid-svg-ivwI8F4CjDT3XnWL .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ivwI8F4CjDT3XnWL .rough-node .label,#mermaid-svg-ivwI8F4CjDT3XnWL .node .label,#mermaid-svg-ivwI8F4CjDT3XnWL .image-shape .label,#mermaid-svg-ivwI8F4CjDT3XnWL .icon-shape .label{text-align:center;}#mermaid-svg-ivwI8F4CjDT3XnWL .node.clickable{cursor:pointer;}#mermaid-svg-ivwI8F4CjDT3XnWL .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ivwI8F4CjDT3XnWL .arrowheadPath{fill:#333333;}#mermaid-svg-ivwI8F4CjDT3XnWL .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ivwI8F4CjDT3XnWL .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ivwI8F4CjDT3XnWL .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ivwI8F4CjDT3XnWL .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ivwI8F4CjDT3XnWL .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ivwI8F4CjDT3XnWL .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster text{fill:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL .cluster span{color:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ivwI8F4CjDT3XnWL .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ivwI8F4CjDT3XnWL rect.text{fill:none;stroke-width:0;}#mermaid-svg-ivwI8F4CjDT3XnWL .icon-shape,#mermaid-svg-ivwI8F4CjDT3XnWL .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ivwI8F4CjDT3XnWL .icon-shape p,#mermaid-svg-ivwI8F4CjDT3XnWL .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ivwI8F4CjDT3XnWL .icon-shape rect,#mermaid-svg-ivwI8F4CjDT3XnWL .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ivwI8F4CjDT3XnWL .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ivwI8F4CjDT3XnWL .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ivwI8F4CjDT3XnWL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
对比
BFS特点
队列实现
层层推进
适合找最优解
DFS特点
栈实现
深入探索
适合找所有解
三、图遍历在AI中的核心应用
3.1 知识图谱推理
场景:智能教育平台的个性化学习路径推荐
#mermaid-svg-s9SASpNcT7X4fQJe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-s9SASpNcT7X4fQJe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-s9SASpNcT7X4fQJe .error-icon{fill:#552222;}#mermaid-svg-s9SASpNcT7X4fQJe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-s9SASpNcT7X4fQJe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-s9SASpNcT7X4fQJe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-s9SASpNcT7X4fQJe .marker.cross{stroke:#333333;}#mermaid-svg-s9SASpNcT7X4fQJe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-s9SASpNcT7X4fQJe p{margin:0;}#mermaid-svg-s9SASpNcT7X4fQJe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-s9SASpNcT7X4fQJe .cluster-label text{fill:#333;}#mermaid-svg-s9SASpNcT7X4fQJe .cluster-label span{color:#333;}#mermaid-svg-s9SASpNcT7X4fQJe .cluster-label span p{background-color:transparent;}#mermaid-svg-s9SASpNcT7X4fQJe .label text,#mermaid-svg-s9SASpNcT7X4fQJe span{fill:#333;color:#333;}#mermaid-svg-s9SASpNcT7X4fQJe .node rect,#mermaid-svg-s9SASpNcT7X4fQJe .node circle,#mermaid-svg-s9SASpNcT7X4fQJe .node ellipse,#mermaid-svg-s9SASpNcT7X4fQJe .node polygon,#mermaid-svg-s9SASpNcT7X4fQJe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-s9SASpNcT7X4fQJe .rough-node .label text,#mermaid-svg-s9SASpNcT7X4fQJe .node .label text,#mermaid-svg-s9SASpNcT7X4fQJe .image-shape .label,#mermaid-svg-s9SASpNcT7X4fQJe .icon-shape .label{text-anchor:middle;}#mermaid-svg-s9SASpNcT7X4fQJe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-s9SASpNcT7X4fQJe .rough-node .label,#mermaid-svg-s9SASpNcT7X4fQJe .node .label,#mermaid-svg-s9SASpNcT7X4fQJe .image-shape .label,#mermaid-svg-s9SASpNcT7X4fQJe .icon-shape .label{text-align:center;}#mermaid-svg-s9SASpNcT7X4fQJe .node.clickable{cursor:pointer;}#mermaid-svg-s9SASpNcT7X4fQJe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-s9SASpNcT7X4fQJe .arrowheadPath{fill:#333333;}#mermaid-svg-s9SASpNcT7X4fQJe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-s9SASpNcT7X4fQJe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-s9SASpNcT7X4fQJe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-s9SASpNcT7X4fQJe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-s9SASpNcT7X4fQJe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-s9SASpNcT7X4fQJe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-s9SASpNcT7X4fQJe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-s9SASpNcT7X4fQJe .cluster text{fill:#333;}#mermaid-svg-s9SASpNcT7X4fQJe .cluster span{color:#333;}#mermaid-svg-s9SASpNcT7X4fQJe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-s9SASpNcT7X4fQJe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-s9SASpNcT7X4fQJe rect.text{fill:none;stroke-width:0;}#mermaid-svg-s9SASpNcT7X4fQJe .icon-shape,#mermaid-svg-s9SASpNcT7X4fQJe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-s9SASpNcT7X4fQJe .icon-shape p,#mermaid-svg-s9SASpNcT7X4fQJe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-s9SASpNcT7X4fQJe .icon-shape rect,#mermaid-svg-s9SASpNcT7X4fQJe .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-s9SASpNcT7X4fQJe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-s9SASpNcT7X4fQJe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-s9SASpNcT7X4fQJe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
Python基础
变量与数据类型
控制流
列表操作
字典操作
列表推导式
字典推导式
装饰器
深浅拷贝
问题:学生做错了"装饰器"的题目,如何找到根本原因?
DFS归因诊断算法:
class KnowledgeGraphDFS:
"""基于DFS的知识图谱归因诊断"""
def __init__(self, graph):
self.graph = graph
self.visited = set()
self.mastery_levels = {} # 用户对各知识点的掌握程度
def find_root_cause(self, wrong_knowledge_point, user_id):
"""
找到错误的根本原因
使用DFS遍历前置依赖知识点
"""
root_causes = []
self.visited = set()
self._dfs_find_weak_prerequisites(
wrong_knowledge_point,
user_id,
root_causes,
depth=0
)
return root_causes
def _dfs_find_weak_prerequisites(self, kp, user_id, root_causes, depth):
"""DFS递归查找薄弱的前置知识点"""
if kp in self.visited:
return
self.visited.add(kp)
# 获取该知识点的前置依赖
prerequisites = self.graph.get_prerequisites(kp)
if not prerequisites:
# 如果没有前置依赖,说明是基础知识点
mastery = self.mastery_levels.get(kp, 0)
if mastery < 0.6: # 掌握度低于60%
root_causes.append({
'knowledge_point': kp,
'mastery': mastery,
'depth': depth,
'reason': '基础知识点掌握不足'
})
return
# 递归检查所有前置依赖
for prereq in prerequisites:
mastery = self.mastery_levels.get(prereq, 0)
if mastery < 0.6:
# 前置知识点掌握不足,继续深入查找
self._dfs_find_weak_prerequisites(
prereq, user_id, root_causes, depth + 1
)
else:
# 前置知识点掌握良好,标记为潜在问题
if depth == 0:
root_causes.append({
'knowledge_point': prereq,
'mastery': mastery,
'depth': depth + 1,
'reason': '需要巩固前置知识'
})
# 使用示例
knowledge_graph = {
'装饰器': ['闭包', '高阶函数'],
'闭包': ['函数对象', '作用域'],
'高阶函数': ['函数对象'],
'函数对象': ['变量与数据类型'],
'作用域': ['变量与数据类型'],
'变量与数据类型': []
}
dfs_analyzer = KnowledgeGraphDFS(knowledge_graph)
dfs_analyzer.mastery_levels = {
'装饰器': 0.3,
'闭包': 0.4,
'高阶函数': 0.8,
'函数对象': 0.9,
'作用域': 0.3,
'变量与数据类型': 0.9
}
root_causes = dfs_analyzer.find_root_cause('装饰器', user_id=1)
print("根本原因分析:", root_causes)
# 输出:根本原因分析:[{'knowledge_point': '作用域', 'mastery': 0.3, 'depth': 2, 'reason': '基础知识点掌握不足'}]
执行过程:
#mermaid-svg-Dr33rJdavDh3LOC5{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Dr33rJdavDh3LOC5 .error-icon{fill:#552222;}#mermaid-svg-Dr33rJdavDh3LOC5 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Dr33rJdavDh3LOC5 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Dr33rJdavDh3LOC5 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Dr33rJdavDh3LOC5 .marker.cross{stroke:#333333;}#mermaid-svg-Dr33rJdavDh3LOC5 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Dr33rJdavDh3LOC5 p{margin:0;}#mermaid-svg-Dr33rJdavDh3LOC5 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster-label text{fill:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster-label span{color:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster-label span p{background-color:transparent;}#mermaid-svg-Dr33rJdavDh3LOC5 .label text,#mermaid-svg-Dr33rJdavDh3LOC5 span{fill:#333;color:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 .node rect,#mermaid-svg-Dr33rJdavDh3LOC5 .node circle,#mermaid-svg-Dr33rJdavDh3LOC5 .node ellipse,#mermaid-svg-Dr33rJdavDh3LOC5 .node polygon,#mermaid-svg-Dr33rJdavDh3LOC5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Dr33rJdavDh3LOC5 .rough-node .label text,#mermaid-svg-Dr33rJdavDh3LOC5 .node .label text,#mermaid-svg-Dr33rJdavDh3LOC5 .image-shape .label,#mermaid-svg-Dr33rJdavDh3LOC5 .icon-shape .label{text-anchor:middle;}#mermaid-svg-Dr33rJdavDh3LOC5 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Dr33rJdavDh3LOC5 .rough-node .label,#mermaid-svg-Dr33rJdavDh3LOC5 .node .label,#mermaid-svg-Dr33rJdavDh3LOC5 .image-shape .label,#mermaid-svg-Dr33rJdavDh3LOC5 .icon-shape .label{text-align:center;}#mermaid-svg-Dr33rJdavDh3LOC5 .node.clickable{cursor:pointer;}#mermaid-svg-Dr33rJdavDh3LOC5 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Dr33rJdavDh3LOC5 .arrowheadPath{fill:#333333;}#mermaid-svg-Dr33rJdavDh3LOC5 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Dr33rJdavDh3LOC5 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Dr33rJdavDh3LOC5 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Dr33rJdavDh3LOC5 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Dr33rJdavDh3LOC5 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Dr33rJdavDh3LOC5 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster text{fill:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 .cluster span{color:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Dr33rJdavDh3LOC5 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Dr33rJdavDh3LOC5 rect.text{fill:none;stroke-width:0;}#mermaid-svg-Dr33rJdavDh3LOC5 .icon-shape,#mermaid-svg-Dr33rJdavDh3LOC5 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Dr33rJdavDh3LOC5 .icon-shape p,#mermaid-svg-Dr33rJdavDh3LOC5 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Dr33rJdavDh3LOC5 .icon-shape rect,#mermaid-svg-Dr33rJdavDh3LOC5 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Dr33rJdavDh3LOC5 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Dr33rJdavDh3LOC5 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Dr33rJdavDh3LOC5 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
装饰器 错误
检查前置依赖
闭包 掌握度0.4
高阶函数 掌握度0.8
继续深入
函数对象 0.9 ✓
作用域 0.3 ✗
变量与数据类型 0.9 ✓
3.2 社交网络分析
场景:AI推荐系统中的好友推荐
#mermaid-svg-J6RcvFWlYAugi5pc{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-J6RcvFWlYAugi5pc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-J6RcvFWlYAugi5pc .error-icon{fill:#552222;}#mermaid-svg-J6RcvFWlYAugi5pc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-J6RcvFWlYAugi5pc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-J6RcvFWlYAugi5pc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-J6RcvFWlYAugi5pc .marker.cross{stroke:#333333;}#mermaid-svg-J6RcvFWlYAugi5pc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-J6RcvFWlYAugi5pc p{margin:0;}#mermaid-svg-J6RcvFWlYAugi5pc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-J6RcvFWlYAugi5pc .cluster-label text{fill:#333;}#mermaid-svg-J6RcvFWlYAugi5pc .cluster-label span{color:#333;}#mermaid-svg-J6RcvFWlYAugi5pc .cluster-label span p{background-color:transparent;}#mermaid-svg-J6RcvFWlYAugi5pc .label text,#mermaid-svg-J6RcvFWlYAugi5pc span{fill:#333;color:#333;}#mermaid-svg-J6RcvFWlYAugi5pc .node rect,#mermaid-svg-J6RcvFWlYAugi5pc .node circle,#mermaid-svg-J6RcvFWlYAugi5pc .node ellipse,#mermaid-svg-J6RcvFWlYAugi5pc .node polygon,#mermaid-svg-J6RcvFWlYAugi5pc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-J6RcvFWlYAugi5pc .rough-node .label text,#mermaid-svg-J6RcvFWlYAugi5pc .node .label text,#mermaid-svg-J6RcvFWlYAugi5pc .image-shape .label,#mermaid-svg-J6RcvFWlYAugi5pc .icon-shape .label{text-anchor:middle;}#mermaid-svg-J6RcvFWlYAugi5pc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-J6RcvFWlYAugi5pc .rough-node .label,#mermaid-svg-J6RcvFWlYAugi5pc .node .label,#mermaid-svg-J6RcvFWlYAugi5pc .image-shape .label,#mermaid-svg-J6RcvFWlYAugi5pc .icon-shape .label{text-align:center;}#mermaid-svg-J6RcvFWlYAugi5pc .node.clickable{cursor:pointer;}#mermaid-svg-J6RcvFWlYAugi5pc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-J6RcvFWlYAugi5pc .arrowheadPath{fill:#333333;}#mermaid-svg-J6RcvFWlYAugi5pc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-J6RcvFWlYAugi5pc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-J6RcvFWlYAugi5pc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-J6RcvFWlYAugi5pc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-J6RcvFWlYAugi5pc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-J6RcvFWlYAugi5pc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-J6RcvFWlYAugi5pc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-J6RcvFWlYAugi5pc .cluster text{fill:#333;}#mermaid-svg-J6RcvFWlYAugi5pc .cluster span{color:#333;}#mermaid-svg-J6RcvFWlYAugi5pc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-J6RcvFWlYAugi5pc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-J6RcvFWlYAugi5pc rect.text{fill:none;stroke-width:0;}#mermaid-svg-J6RcvFWlYAugi5pc .icon-shape,#mermaid-svg-J6RcvFWlYAugi5pc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-J6RcvFWlYAugi5pc .icon-shape p,#mermaid-svg-J6RcvFWlYAugi5pc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-J6RcvFWlYAugi5pc .icon-shape rect,#mermaid-svg-J6RcvFWlYAugi5pc .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-J6RcvFWlYAugi5pc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-J6RcvFWlYAugi5pc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-J6RcvFWlYAugi5pc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
好友
好友
好友
好友
好友
好友
用户A
用户B
用户C
用户D
用户E
用户F
BFS二度好友推荐算法:
class SocialNetworkBFS:
"""基于BFS的社交网络分析"""
def __init__(self, social_graph):
self.graph = social_graph
def recommend_friends(self, user_id, max_degree=2):
"""
推荐好友
使用BFS找到二度好友(朋友的朋友)
"""
visited = {user_id}
queue = deque([(user_id, 0)]) # (用户ID, 关系层级)
recommendations = {}
while queue:
current_user, degree = queue.popleft()
if degree >= max_degree:
continue
for friend in self.graph.get(current_user, []):
if friend not in visited:
visited.add(friend)
queue.append((friend, degree + 1))
if degree + 1 == 2: # 二度好友
# 计算共同好友数量
mutual_friends = self._count_mutual_friends(
user_id, friend
)
recommendations[friend] = {
'degree': degree + 1,
'mutual_friends': mutual_friends
}
# 按共同好友数量排序
sorted_recommendations = sorted(
recommendations.items(),
key=lambda x: x[1]['mutual_friends'],
reverse=True
)
return sorted_recommendations
def _count_mutual_friends(self, user1, user2):
"""计算共同好友数量"""
friends1 = set(self.graph.get(user1, []))
friends2 = set(self.graph.get(user2, []))
return len(friends1 & friends2)
# 使用示例
social_graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'E'],
'D': ['B', 'F'],
'E': ['B', 'C'],
'F': ['D']
}
bfs_analyzer = SocialNetworkBFS(social_graph)
recommendations = bfs_analyzer.recommend_friends('A', max_degree=2)
print("好友推荐:", recommendations)
# 输出:好友推荐:[('E', {'degree': 2, 'mutual_friends': 2}), ('D', {'degree': 2, 'mutual_friends': 1})]
3.3 AI游戏寻路
场景:游戏AI中的路径规划
#mermaid-svg-3FMBwZ2yZRdTHgla{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3FMBwZ2yZRdTHgla .error-icon{fill:#552222;}#mermaid-svg-3FMBwZ2yZRdTHgla .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3FMBwZ2yZRdTHgla .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3FMBwZ2yZRdTHgla .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3FMBwZ2yZRdTHgla .marker.cross{stroke:#333333;}#mermaid-svg-3FMBwZ2yZRdTHgla svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3FMBwZ2yZRdTHgla p{margin:0;}#mermaid-svg-3FMBwZ2yZRdTHgla .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster-label text{fill:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster-label span{color:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster-label span p{background-color:transparent;}#mermaid-svg-3FMBwZ2yZRdTHgla .label text,#mermaid-svg-3FMBwZ2yZRdTHgla span{fill:#333;color:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla .node rect,#mermaid-svg-3FMBwZ2yZRdTHgla .node circle,#mermaid-svg-3FMBwZ2yZRdTHgla .node ellipse,#mermaid-svg-3FMBwZ2yZRdTHgla .node polygon,#mermaid-svg-3FMBwZ2yZRdTHgla .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3FMBwZ2yZRdTHgla .rough-node .label text,#mermaid-svg-3FMBwZ2yZRdTHgla .node .label text,#mermaid-svg-3FMBwZ2yZRdTHgla .image-shape .label,#mermaid-svg-3FMBwZ2yZRdTHgla .icon-shape .label{text-anchor:middle;}#mermaid-svg-3FMBwZ2yZRdTHgla .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-3FMBwZ2yZRdTHgla .rough-node .label,#mermaid-svg-3FMBwZ2yZRdTHgla .node .label,#mermaid-svg-3FMBwZ2yZRdTHgla .image-shape .label,#mermaid-svg-3FMBwZ2yZRdTHgla .icon-shape .label{text-align:center;}#mermaid-svg-3FMBwZ2yZRdTHgla .node.clickable{cursor:pointer;}#mermaid-svg-3FMBwZ2yZRdTHgla .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-3FMBwZ2yZRdTHgla .arrowheadPath{fill:#333333;}#mermaid-svg-3FMBwZ2yZRdTHgla .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-3FMBwZ2yZRdTHgla .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-3FMBwZ2yZRdTHgla .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3FMBwZ2yZRdTHgla .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-3FMBwZ2yZRdTHgla .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3FMBwZ2yZRdTHgla .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster text{fill:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla .cluster span{color:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-3FMBwZ2yZRdTHgla .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-3FMBwZ2yZRdTHgla rect.text{fill:none;stroke-width:0;}#mermaid-svg-3FMBwZ2yZRdTHgla .icon-shape,#mermaid-svg-3FMBwZ2yZRdTHgla .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3FMBwZ2yZRdTHgla .icon-shape p,#mermaid-svg-3FMBwZ2yZRdTHgla .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-3FMBwZ2yZRdTHgla .icon-shape rect,#mermaid-svg-3FMBwZ2yZRdTHgla .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3FMBwZ2yZRdTHgla .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-3FMBwZ2yZRdTHgla .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-3FMBwZ2yZRdTHgla :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
游戏地图
起点S
A
B
C
D
E
终点G
F
BFS最短路径算法:
class GamePathfindingBFS:
"""基于BFS的游戏寻路"""
def __init__(self, game_map):
self.map = game_map
def find_shortest_path(self, start, end):
"""
找到最短路径
BFS保证找到的第一条路径是最短的
"""
if start == end:
return [start]
visited = {start}
queue = deque([(start, [start])]) # (当前位置, 路径)
while queue:
current, path = queue.popleft()
for neighbor in self.map.get(current, []):
if neighbor == end:
return path + [neighbor]
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, path + [neighbor]))
return None # 没有找到路径
# 使用示例
game_map = {
'S': ['A', 'B'],
'A': ['C', 'D'],
'B': ['D', 'E'],
'C': ['G'],
'D': ['G'],
'E': ['F'],
'F': ['G'],
'G': []
}
pathfinder = GamePathfindingBFS(game_map)
path = pathfinder.find_shortest_path('S', 'G')
print("最短路径:", path)
# 输出:最短路径:['S', 'A', 'C', 'G'] 或 ['S', 'A', 'D', 'G']
3.4 AI对话系统中的意图识别
场景:对话系统中基于图的意图转移分析
#mermaid-svg-1bxzozyo5MwNPqfg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1bxzozyo5MwNPqfg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1bxzozyo5MwNPqfg .error-icon{fill:#552222;}#mermaid-svg-1bxzozyo5MwNPqfg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1bxzozyo5MwNPqfg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1bxzozyo5MwNPqfg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg .marker.cross{stroke:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1bxzozyo5MwNPqfg p{margin:0;}#mermaid-svg-1bxzozyo5MwNPqfg defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-1bxzozyo5MwNPqfg g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-1bxzozyo5MwNPqfg g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-1bxzozyo5MwNPqfg g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-1bxzozyo5MwNPqfg g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-1bxzozyo5MwNPqfg .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-1bxzozyo5MwNPqfg .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-1bxzozyo5MwNPqfg .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-1bxzozyo5MwNPqfg .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-1bxzozyo5MwNPqfg .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-1bxzozyo5MwNPqfg .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-1bxzozyo5MwNPqfg .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-1bxzozyo5MwNPqfg .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1bxzozyo5MwNPqfg .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-1bxzozyo5MwNPqfg .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1bxzozyo5MwNPqfg .edgeLabel .label text{fill:#333;}#mermaid-svg-1bxzozyo5MwNPqfg .label div .edgeLabel{color:#333;}#mermaid-svg-1bxzozyo5MwNPqfg .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-1bxzozyo5MwNPqfg .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-1bxzozyo5MwNPqfg .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-1bxzozyo5MwNPqfg .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1bxzozyo5MwNPqfg .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1bxzozyo5MwNPqfg #statediagram-barbEnd{fill:#333333;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1bxzozyo5MwNPqfg .cluster-label,#mermaid-svg-1bxzozyo5MwNPqfg .nodeLabel{color:#131300;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-1bxzozyo5MwNPqfg .note-edge{stroke-dasharray:5;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-note text{fill:black;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram-note .nodeLabel{color:black;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagram .edgeLabel{color:red;}#mermaid-svg-1bxzozyo5MwNPqfg #dependencyStart,#mermaid-svg-1bxzozyo5MwNPqfg #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-1bxzozyo5MwNPqfg .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-1bxzozyo5MwNPqfg :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
用户想做题
用户问问题
用户倾诉
用户提交答案
继续做题
答错需讲解
继续练习
情绪低落
情绪好转
结束对话
欢迎
出题
知识问答
心理咨询
判题
基于BFS的对话状态转移:
class DialogStateManager:
"""对话状态管理器"""
def __init__(self):
self.state_graph = {
'欢迎': ['出题', '知识问答', '心理咨询'],
'出题': ['判题'],
'判题': ['出题', '知识问答'],
'知识问答': ['出题', '心理咨询'],
'心理咨询': ['知识问答', '结束对话']
}
def find_dialog_path(self, from_state, to_state):
"""
找到从一个状态到另一个状态的最短转移路径
使用BFS确保找到最短路径
"""
if from_state == to_state:
return [from_state]
visited = {from_state}
queue = deque([(from_state, [from_state])])
while queue:
current, path = queue.popleft()
for next_state in self.state_graph.get(current, []):
if next_state == to_state:
return path + [next_state]
if next_state not in visited:
visited.add(next_state)
queue.append((next_state, path + [next_state]))
return None
# 使用示例
dialog_manager = DialogStateManager()
path = dialog_manager.find_dialog_path('欢迎', '知识问答')
print("对话转移路径:", path)
# 输出:对话转移路径:['欢迎', '知识问答']
四、实战案例:AI教育平台知识图谱
4.1 业务背景
在E教千问智能教育平台中,我们使用图遍历算法实现了以下核心功能:
归因诊断:学生做错题目后,DFS找到根本原因学习路径推荐:BFS生成最优学习顺序知识点完整性检查:确保试卷覆盖所有前置知识
4.2 完整实现
from typing import Dict, List, Set, Tuple
from collections import deque
import asyncio
class KnowledgeGraphEngine:
"""
知识图谱引擎
结合DFS和BFS实现智能教育功能
"""
def __init__(self, neo4j_driver):
self.driver = neo4j_driver
async def diagnose_learning_gap(
self,
user_id: int,
wrong_knowledge_point: str
) ->List[Dict]:
"""
学习差距诊断(DFS实现)
找到学生知识体系中的薄弱环节
"""
async with self.driver.session() as session:
# Cypher查询:DFS遍历前置依赖
query = """
MATCH path = (wrong:KnowledgePoint {name: $kp_name})
<-[:PREREQUISITE*]-(prereq:KnowledgePoint)
WHERE NOT (prereq)<-[:PREREQUISITE]-()
OPTIONAL MATCH (ur:UserRecord {user_id: $user_id})
-[:MASTERS]->(prereq)
WITH prereq, ur.mastery_level as mastery
WHERE mastery < 0.6 OR mastery IS NULL
RETURN prereq.name as name,
coalesce(mastery, 0) as mastery,
size((prereq)-[:PREREQUISITE]->()) as dependency_count
ORDER BY mastery ASC, dependency_count DESC
LIMIT 5
"""
result = await session.run(
query,
{'kp_name': wrong_knowledge_point, 'user_id': user_id}
)
return [record.data() async for record in result]
async def generate_learning_path(
self,
user_id: int,
target_knowledge_point: str
) ->List[Dict]:
"""
学习路径生成(BFS实现)
找到从当前知识水平到目标的最优路径
"""
async with self.driver.session() as session:
# Cypher查询:BFS找到最短学习路径
query = """
MATCH path = (start:KnowledgePoint)
-[:PREREQUISITE*]->(target:KnowledgePoint
{name: $target_name})
WHERE NOT (start)<-[:PREREQUISITE]-()
WITH nodes(path) as kps
UNWIND kps as kp
WITH distinct kp
OPTIONAL MATCH (ur:UserRecord {user_id: $user_id})
-[:MASTERS]->(kp)
RETURN kp.name as name,
kp.difficulty as difficulty,
coalesce(ur.mastery_level, 0) as mastery
ORDER BY mastery ASC, kp.difficulty ASC
"""
result = await session.run(
query,
{'target_name': target_knowledge_point, 'user_id': user_id}
)
return [record.data() async for record in result]
async def check_paper_completeness(
self,
question_ids: List[int]
) ->Dict:
"""
试卷知识点完整性检查(BFS实现)
确保试卷覆盖所有必要的前置知识
"""
async with self.driver.session() as session:
query = """
MATCH (q:Question)-[:TESTS]->(kp:KnowledgePoint)
WHERE q.id IN $question_ids
WITH collect(DISTINCT kp) as tested_kps
MATCH (kp:KnowledgePoint)-[:PREREQUISITE]->(required:KnowledgePoint)
WHERE kp IN tested_kps AND NOT required IN tested_kps
RETURN required.name as missing_name,
count(kp) as impact_count,
collect(DISTINCT kp.name) as affected_topics
ORDER BY impact_count DESC
"""
result = await session.run(
query,
{'question_ids': question_ids}
)
missing = [record.data() async for record in result]
return {
'is_complete': len(missing) == 0,
'missing_prerequisites': missing,
'recommendation': self._generate_recommendation(missing)
}
def _generate_recommendation(self, missing: List[Dict]) ->str:
"""生成改进建议"""
if not missing:
return "试卷知识点覆盖完整,无需调整。"
recommendations = []
for item in missing[:3]: # 最多显示3个建议
recommendations.append(
f"建议增加【{item['missing_name']}】相关题目,"
f"影响{item['impact_count']}个知识点"
)
return "\n".join(recommendations)
class AdaptiveLearningSystem:
"""
自适应学习系统
综合运用图遍历算法
"""
def __init__(self, kg_engine: KnowledgeGraphEngine):
self.kg_engine = kg_engine
async def analyze_student_performance(
self,
user_id: int,
wrong_questions: List[Dict]
) ->Dict:
"""
分析学生表现并生成个性化学习方案
"""
# 1. 收集错误的知识点
wrong_kps = set()
for q in wrong_questions:
wrong_kps.update(q.get('knowledge_points', []))
# 2. DFS诊断每个错误知识点的根本原因
diagnosis_results = []
for kp in wrong_kps:
root_causes = await self.kg_engine.diagnose_learning_gap(
user_id, kp
)
diagnosis_results.append({
'wrong_knowledge_point': kp,
'root_causes': root_causes
})
# 3. BFS生成学习路径
learning_paths = []
for result in diagnosis_results:
for root_cause in result['root_causes']:
path = await self.kg_engine.generate_learning_path(
user_id,
result['wrong_knowledge_point']
)
learning_paths.append({
'target': result['wrong_knowledge_point'],
'root_cause': root_cause['name'],
'learning_path': path
})
# 4. 生成个性化推荐
return {
'diagnosis': diagnosis_results,
'learning_paths': learning_paths,
'priority_topics': self._prioritize_topics(diagnosis_results),
'estimated_study_time': self._estimate_study_time(learning_paths)
}
def _prioritize_topics(self, diagnosis_results: List[Dict]) ->List[str]:
"""确定学习优先级"""
topic_scores = {}
for result in diagnosis_results:
for cause in result['root_causes']:
topic = cause['name']
mastery = cause['mastery']
# 掌握度越低,优先级越高
if topic not in topic_scores:
topic_scores[topic] = 0
topic_scores[topic] += (1 - mastery)
return sorted(topic_scores.keys(),
key=lambda x: topic_scores[x],
reverse=True)
def _estimate_study_time(self, learning_paths: List[Dict]) ->int:
"""估算学习时间(分钟)"""
total_time = 0
for path in learning_paths:
# 每个知识点平均学习时间 = 难度 * 30分钟
for kp in path['learning_path']:
difficulty = kp.get('difficulty', 0.5)
mastery = kp.get('mastery', 0)
# 掌握度越低,需要时间越长
study_time = difficulty * 30 * (1 - mastery)
total_time += study_time
return int(total_time)
4.3 实际效果
# 使用示例
import asyncio
async def main():
# 模拟Neo4j驱动
from neo4j import AsyncGraphDatabase
driver = AsyncGraphDatabase.driver(
"bolt://localhost:7687",
auth=("neo4j", "password")
)
kg_engine = KnowledgeGraphEngine(driver)
learning_system = AdaptiveLearningSystem(kg_engine)
# 分析学生表现
wrong_questions = [
{
'id': 1,
'knowledge_points': ['装饰器', '闭包']
},
{
'id': 2,
'knowledge_points': ['深浅拷贝']
}
]
analysis = await learning_system.analyze_student_performance(
user_id=1,
wrong_questions=wrong_questions
)
print("=== 学习诊断报告 ===")
print(f"根本原因分析: {analysis['diagnosis']}")
print(f"学习路径: {analysis['learning_paths']}")
print(f"优先学习主题: {analysis['priority_topics']}")
asyncio.run(main())
输出示例:
=== 学习诊断报告 ===
根本原因分析: [
{
'wrong_knowledge_point': '装饰器',
'root_causes': [
{'name': '作用域', 'mastery': 0.3, 'dependency_count': 2}
]
}
]
学习路径: [
{
'target': '装饰器',
'root_cause': '作用域',
'learning_path': [
{'name': '作用域', 'difficulty': 0.4, 'mastery': 0.3},
{'name': '闭包', 'difficulty': 0.6, 'mastery': 0.5},
{'name': '装饰器', 'difficulty': 0.8, 'mastery': 0.2}
]
}
] 拼多多帮助力入口
优先学习主题: ['作用域', '变量与数据类型']
五、面试高频问题全解析
5.1 基础概念题
Q1: DFS和BFS的区别是什么?各有什么应用场景?
答案:
维度DFSBFS数据结构栈(递归或显式栈)队列搜索策略深入探索,一条路走到底层层推进,先近后远空间复杂度O(V) - 递归深度O(V) - 队列大小时间复杂度O(V + E)O(V + E)最短路径不保证保证(无权图)应用场景拓扑排序、连通性检测、路径查找最短路径、层级遍历、社交网络分析
代码对比:
# DFS - 递归实现
def dfs_recursive(graph, node, visited):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs_recursive(graph, neighbor, visited)
# DFS - 栈实现
def dfs_iterative(graph, start):
visited = set()
stack = [start]
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
stack.extend(neighbor for neighbor in graph[node]
if neighbor not in visited)
# BFS - 队列实现
def bfs(graph, start):
from collections import deque
visited = set([start])
queue = deque([start])
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
Q2: 为什么BFS能找到最短路径,而DFS不能?
答案:
BFS按层级遍历,先访问距离起点近的节点,后访问远的节点。因此,第一次到达目标节点时,必然是最短路径。
DFS深入探索,可能走很远的路才到达目标,不保证最短。
图示说明:
#mermaid-svg-H4EOGBhY3d5XbytN{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-H4EOGBhY3d5XbytN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-H4EOGBhY3d5XbytN .error-icon{fill:#552222;}#mermaid-svg-H4EOGBhY3d5XbytN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-H4EOGBhY3d5XbytN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-H4EOGBhY3d5XbytN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-H4EOGBhY3d5XbytN .marker.cross{stroke:#333333;}#mermaid-svg-H4EOGBhY3d5XbytN svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-H4EOGBhY3d5XbytN p{margin:0;}#mermaid-svg-H4EOGBhY3d5XbytN .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-H4EOGBhY3d5XbytN .cluster-label text{fill:#333;}#mermaid-svg-H4EOGBhY3d5XbytN .cluster-label span{color:#333;}#mermaid-svg-H4EOGBhY3d5XbytN .cluster-label span p{background-color:transparent;}#mermaid-svg-H4EOGBhY3d5XbytN .label text,#mermaid-svg-H4EOGBhY3d5XbytN span{fill:#333;color:#333;}#mermaid-svg-H4EOGBhY3d5XbytN .node rect,#mermaid-svg-H4EOGBhY3d5XbytN .node circle,#mermaid-svg-H4EOGBhY3d5XbytN .node ellipse,#mermaid-svg-H4EOGBhY3d5XbytN .node polygon,#mermaid-svg-H4EOGBhY3d5XbytN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-H4EOGBhY3d5XbytN .rough-node .label text,#mermaid-svg-H4EOGBhY3d5XbytN .node .label text,#mermaid-svg-H4EOGBhY3d5XbytN .image-shape .label,#mermaid-svg-H4EOGBhY3d5XbytN .icon-shape .label{text-anchor:middle;}#mermaid-svg-H4EOGBhY3d5XbytN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-H4EOGBhY3d5XbytN .rough-node .label,#mermaid-svg-H4EOGBhY3d5XbytN .node .label,#mermaid-svg-H4EOGBhY3d5XbytN .image-shape .label,#mermaid-svg-H4EOGBhY3d5XbytN .icon-shape .label{text-align:center;}#mermaid-svg-H4EOGBhY3d5XbytN .node.clickable{cursor:pointer;}#mermaid-svg-H4EOGBhY3d5XbytN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-H4EOGBhY3d5XbytN .arrowheadPath{fill:#333333;}#mermaid-svg-H4EOGBhY3d5XbytN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-H4EOGBhY3d5XbytN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-H4EOGBhY3d5XbytN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H4EOGBhY3d5XbytN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-H4EOGBhY3d5XbytN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H4EOGBhY3d5XbytN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-H4EOGBhY3d5XbytN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-H4EOGBhY3d5XbytN .cluster text{fill:#333;}#mermaid-svg-H4EOGBhY3d5XbytN .cluster span{color:#333;}#mermaid-svg-H4EOGBhY3d5XbytN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-H4EOGBhY3d5XbytN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-H4EOGBhY3d5XbytN rect.text{fill:none;stroke-width:0;}#mermaid-svg-H4EOGBhY3d5XbytN .icon-shape,#mermaid-svg-H4EOGBhY3d5XbytN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H4EOGBhY3d5XbytN .icon-shape p,#mermaid-svg-H4EOGBhY3d5XbytN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-H4EOGBhY3d5XbytN .icon-shape rect,#mermaid-svg-H4EOGBhY3d5XbytN .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H4EOGBhY3d5XbytN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-H4EOGBhY3d5XbytN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-H4EOGBhY3d5XbytN :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
S
A
B
C
T
BFS路径:S → A → C → T(或 S → B → C → T),长度为3DFS可能路径:S → A → C → T,但也可能先走 S → B → C → T,取决于遍历顺序
关键:BFS保证第一次到达目标时就是最短的。
Q3: 如何检测图中的环?
答案:
方法1:DFS + 颜色标记
def has_cycle_dfs(graph):
"""
使用DFS检测有向图中的环
颜色标记:WHITE(未访问), GRAY(访问中), BLACK(已访问)
"""
WHITE, GRAY, BLACK = 0, 1, 2
color = {node: WHITE for node in graph}
def dfs(node):
color[node] = GRAY
for neighbor in graph[node]:
if color[neighbor] == GRAY:
# 遇到正在访问的节点,说明有环
return True
if color[neighbor] == WHITE:
if dfs(neighbor):
return True
color[node] = BLACK
return False
for node in graph:
if color[node] == WHITE:
if dfs(node):
return True
return False
方法2:拓扑排序
def has_cycle_topological(graph):
"""
使用拓扑排序检测环
如果无法完成拓扑排序,说明有环
"""
from collections import deque
# 计算入度
in_degree = {node: 0 for node in graph}
for node in graph:
for neighbor in graph[node]:
in_degree[neighbor] = in_degree.get(neighbor, 0) + 1
# 入度为0的节点入队
queue = deque([node for node in in_degree if in_degree[node] == 0])
count = 0
while queue:
node = queue.popleft()
count += 1
for neighbor in graph[node]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
# 如果处理的节点数不等于总节点数,说明有环
return count != len(graph)
5.2 进阶应用题
Q4: 如何在AI推荐系统中应用图遍历算法?
答案:
场景1:社交好友推荐(BFS)
def recommend_friends_bfs(social_graph, user_id, max_degree=2):
"""
基于BFS的好友推荐
找到二度好友(朋友的朋友)
"""
from collections import deque
visited = {user_id}
queue = deque([(user_id, 0)])
recommendations = {}
while queue:
current, degree = queue.popleft()
if degree >= max_degree:
continue
for friend in social_graph.get(current, []):
if friend not in visited:
visited.add(friend)
queue.append((friend, degree + 1))
if degree + 1 == 2:
# 计算推荐分数
mutual_friends = len(
set(social_graph[user_id]) &
set(social_graph[friend])
)
recommendations[friend] = mutual_friends
return sorted(recommendations.items(),
key=lambda x: x[1],
reverse=True)
场景2:知识图谱推理(DFS)
def knowledge_reasoning_dfs(kg, start_entity, target_relation, max_depth=3):
"""
基于DFS的知识图谱推理
找到实体之间的关系路径
"""
paths = []
def dfs(entity, path, depth):
if depth > max_depth:
return
for relation, next_entity in kg.get(entity, []):
new_path = path + [(entity, relation, next_entity)]
if relation == target_relation:
paths.append(new_path)
dfs(next_entity, new_path, depth + 1)
dfs(start_entity, [], 0)
return paths
Q5: 如何优化大规模图的遍历性能?
答案:
优化策略:
双向BFS:从起点和终点同时搜索
def bidirectional_bfs(graph, start, end):
"""
双向BFS
时间复杂度从O(b^d)降到O(b^(d/2))
"""
if start == end:
return [start]
# 前向搜索
front_visited = {start}
front_queue = deque([(start, [start])])
# 后向搜索
back_visited = {end}
back_queue = deque([(end, [end])])
while front_queue and back_queue:
# 前向扩展一层
for _ in range(len(front_queue)):
node, path = front_queue.popleft()
for neighbor in graph.get(node, []):
if neighbor in back_visited:
# 找到交汇点
back_path = next(
p for n, p in back_queue if n == neighbor
)
return path + back_path[::-1][1:]
if neighbor not in front_visited:
front_visited.add(neighbor)
front_queue.append((neighbor, path + [neighbor]))
# 后向扩展一层
for _ in range(len(back_queue)):
node, path = back_queue.popleft()
for neighbor in graph.get(node, []):
if neighbor in front_visited:
front_path = next(
p for n, p in front_queue if n == neighbor
)
return front_path + path[::-1][1:]
if neighbor not in back_visited:
back_visited.add(neighbor)
back_queue.append((neighbor, path + [neighbor]))
return None
A*算法:启发式搜索
import heapq
def a_star_search(graph, start, end, heuristic):
"""
A*算法
f(n) = g(n) + h(n)
g(n): 从起点到n的实际代价
h(n): 从n到终点的估计代价
"""
open_set = [(0, start)]
came_from = {}
g_score = {start: 0}
while open_set:
_, current = heapq.heappop(open_set)
if current == end:
# 重建路径
path = [current]
while current in came_from:
current = came_from[current]
path.append(current)
return path[::-1]
for neighbor, cost in graph.get(current, []):
tentative_g = g_score[current] + cost
if neighbor not in g_score or tentative_g < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = tentative_g
f_score = tentative_g + heuristic(neighbor, end)
heapq.heappush(open_set, (f_score, neighbor))
return None
并行化遍历:多线程/多进程
from concurrent.futures import ThreadPoolExecutor
import threading
class ParallelBFS:
"""并行BFS"""
def __init__(self, graph):
self.graph = graph
self.lock = threading.Lock()
def parallel_bfs(self, start_nodes, num_threads=4):
"""
并行BFS
从多个起点同时开始遍历
"""
visited = set()
results = {}
def bfs_worker(start):
local_visited = set()
queue = deque([start])
while queue:
node = queue.popleft()
if node in local_visited:
continue
local_visited.add(node)
for neighbor in self.graph.get(node, []):
if neighbor not in local_visited:
queue.append(neighbor)
with self.lock:
for node in local_visited:
if node not in visited:
visited.add(node)
results[node] = start
with ThreadPoolExecutor(max_workers=num_threads) as executor:
executor.map(bfs_worker, start_nodes)
return results
5.3 系统设计题
Q6: 设计一个基于知识图谱的智能学习系统
答案:
系统架构:
#mermaid-svg-DNrcXQal2af1p7tK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-DNrcXQal2af1p7tK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-DNrcXQal2af1p7tK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-DNrcXQal2af1p7tK .error-icon{fill:#552222;}#mermaid-svg-DNrcXQal2af1p7tK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-DNrcXQal2af1p7tK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-DNrcXQal2af1p7tK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-DNrcXQal2af1p7tK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-DNrcXQal2af1p7tK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-DNrcXQal2af1p7tK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-DNrcXQal2af1p7tK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-DNrcXQal2af1p7tK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-DNrcXQal2af1p7tK .marker.cross{stroke:#333333;}#mermaid-svg-DNrcXQal2af1p7tK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-DNrcXQal2af1p7tK p{margin:0;}#mermaid-svg-DNrcXQal2af1p7tK .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-DNrcXQal2af1p7tK .cluster-label text{fill:#333;}#mermaid-svg-DNrcXQal2af1p7tK .cluster-label span{color:#333;}#mermaid-svg-DNrcXQal2af1p7tK .cluster-label span p{background-color:transparent;}#mermaid-svg-DNrcXQal2af1p7tK .label text,#mermaid-svg-DNrcXQal2af1p7tK span{fill:#333;color:#333;}#mermaid-svg-DNrcXQal2af1p7tK .node rect,#mermaid-svg-DNrcXQal2af1p7tK .node circle,#mermaid-svg-DNrcXQal2af1p7tK .node ellipse,#mermaid-svg-DNrcXQal2af1p7tK .node polygon,#mermaid-svg-DNrcXQal2af1p7tK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DNrcXQal2af1p7tK .rough-node .label text,#mermaid-svg-DNrcXQal2af1p7tK .node .label text,#mermaid-svg-DNrcXQal2af1p7tK .image-shape .label,#mermaid-svg-DNrcXQal2af1p7tK .icon-shape .label{text-anchor:middle;}#mermaid-svg-DNrcXQal2af1p7tK .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-DNrcXQal2af1p7tK .rough-node .label,#mermaid-svg-DNrcXQal2af1p7tK .node .label,#mermaid-svg-DNrcXQal2af1p7tK .image-shape .label,#mermaid-svg-DNrcXQal2af1p7tK .icon-shape .label{text-align:center;}#mermaid-svg-DNrcXQal2af1p7tK .node.clickable{cursor:pointer;}#mermaid-svg-DNrcXQal2af1p7tK .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-DNrcXQal2af1p7tK .arrowheadPath{fill:#333333;}#mermaid-svg-DNrcXQal2af1p7tK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-DNrcXQal2af1p7tK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-DNrcXQal2af1p7tK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DNrcXQal2af1p7tK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-DNrcXQal2af1p7tK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DNrcXQal2af1p7tK .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-DNrcXQal2af1p7tK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-DNrcXQal2af1p7tK .cluster text{fill:#333;}#mermaid-svg-DNrcXQal2af1p7tK .cluster span{color:#333;}#mermaid-svg-DNrcXQal2af1p7tK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-DNrcXQal2af1p7tK .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-DNrcXQal2af1p7tK rect.text{fill:none;stroke-width:0;}#mermaid-svg-DNrcXQal2af1p7tK .icon-shape,#mermaid-svg-DNrcXQal2af1p7tK .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DNrcXQal2af1p7tK .icon-shape p,#mermaid-svg-DNrcXQal2af1p7tK .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-DNrcXQal2af1p7tK .icon-shape rect,#mermaid-svg-DNrcXQal2af1p7tK .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DNrcXQal2af1p7tK .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-DNrcXQal2af1p7tK .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-DNrcXQal2af1p7tK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
数据层
算法层
应用层
用户层
学生端
学习建议
教师端
教学报告
归因诊断引擎
路径推荐引擎
质量评估引擎
DFS归因算法
BFS路径算法
图遍历优化
Neo4j知识图谱
MySQL业务数据
Redis缓存
核心组件设计:
class IntelligentLearningSystem:
"""
智能学习系统
集成图遍历算法实现个性化学习
"""
def __init__(self, config):
self.neo4j_driver = self._init_neo4j(config)
self.mysql_engine = self._init_mysql(config)
self.redis_client = self._init_redis(config)
self.diagnosis_engine = DiagnosisEngine(self.neo4j_driver)
self.recommendation_engine = RecommendationEngine(self.neo4j_driver)
self.quality_engine = QualityEngine(self.neo4j_driver)
async def process_student_answer(
self,
user_id: int,
question_id: int,
answer: str
) ->Dict:
"""
处理学生答题
1. 判断对错
2. 错误则诊断原因
3. 生成学习建议
"""
# 1. 获取题目信息
question = await self._get_question(question_id)
# 2. 判断答案
is_correct = self._check_answer(question, answer)
# 3. 记录答题
await self._record_answer(user_id, question_id, answer, is_correct)
if is_correct:
return {
'status': 'correct',
'feedback': '回答正确!继续保持!'
}
# 4. 错误诊断(DFS)
diagnosis = await self.diagnosis_engine.diagnose(
user_id,
question['knowledge_points']
)
# 5. 生成学习路径(BFS)
learning_path = await self.recommendation_engine.generate_path(
user_id,
diagnosis['root_causes']
)
# 6. 缓存结果
await self._cache_result(user_id, diagnosis, learning_path)
return {
'status': 'incorrect',
'diagnosis': diagnosis,
'learning_path': learning_path,
'recommendation': self._generate_recommendation(diagnosis)
}
class DiagnosisEngine:
"""归因诊断引擎(DFS实现)"""
def __init__(self, neo4j_driver):
self.driver = neo4j_driver
async def diagnose(self, user_id: int, knowledge_points: List[str]) ->Dict:
"""
诊断学习问题
使用DFS遍历知识图谱找到根本原因
"""
root_causes = []
for kp in knowledge_points:
causes = await self._dfs_find_root_cause(user_id, kp)
root_causes.extend(causes)
# 去重并排序
unique_causes = self._deduplicate_causes(root_causes)
return {
'wrong_knowledge_points': knowledge_points,
'root_causes': unique_causes,
'severity': self._calculate_severity(unique_causes)
}
async def _dfs_find_root_cause(self, user_id: int, kp: str) ->List[Dict]:
"""DFS递归查找根本原因"""
async with self.driver.session() as session:
query = """
MATCH path = (wrong:KnowledgePoint {name: $kp})
<-[:PREREQUISITE*]-(prereq:KnowledgePoint)
WHERE NOT (prereq)<-[:PREREQUISITE]-()
OPTIONAL MATCH (ur:UserRecord {user_id: $user_id})
-[:MASTERS]->(prereq)
WITH prereq, ur.mastery_level as mastery
WHERE mastery < 0.6 OR mastery IS NULL
RETURN prereq.name as name,
coalesce(mastery, 0) as mastery
ORDER BY mastery ASC
LIMIT 3
"""
result = await session.run(query, {
'kp': kp,
'user_id': user_id
})
return [record.data() async for record in result]
class RecommendationEngine:
"""路径推荐引擎(BFS实现)"""
def __init__(self, neo4j_driver):
self.driver = neo4j_driver
async def generate_path(
self,
user_id: int,
root_causes: List[Dict]
) ->List[Dict]:
"""
生成学习路径
使用BFS找到最短学习路径
"""
all_paths = []
for cause in root_causes:
path = await self._bfs_learning_path(user_id, cause['name'])
all_paths.append({
'root_cause': cause['name'],
'path': path,
'estimated_time': self._estimate_time(path)
})
# 合并并优化路径
optimized_path = self._optimize_paths(all_paths)
return optimized_path
async def _bfs_learning_path(
self,
user_id: int,
root_cause: str
) ->List[Dict]:
"""BFS生成学习路径"""
async with self.driver.session() as session:
query = """
MATCH (start:KnowledgePoint {name: $root_cause})
CALL apoc.path.subgraphAll(start, {
relationshipFilter: 'PREREQUISITE>',
maxLevel: 5
})
YIELD nodes
UNWIND nodes as kp
OPTIONAL MATCH (ur:UserRecord {user_id: $user_id})
-[:MASTERS]->(kp)
RETURN kp.name as name,
kp.difficulty as difficulty,
coalesce(ur.mastery_level, 0) as mastery
ORDER BY mastery ASC, difficulty ASC
"""
result = await session.run(query, {
'root_cause': root_cause,
'user_id': user_id
})
return [record.data() async for record in result]
六、总结与进阶路线
6.1 核心要点回顾
#mermaid-svg-SBLDlbvSI3ivcC0T{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-SBLDlbvSI3ivcC0T .error-icon{fill:#552222;}#mermaid-svg-SBLDlbvSI3ivcC0T .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-SBLDlbvSI3ivcC0T .marker{fill:#333333;stroke:#333333;}#mermaid-svg-SBLDlbvSI3ivcC0T .marker.cross{stroke:#333333;}#mermaid-svg-SBLDlbvSI3ivcC0T svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-SBLDlbvSI3ivcC0T p{margin:0;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge{stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 text{fill:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth--1{stroke-width:17;}#mermaid-svg-SBLDlbvSI3ivcC0T .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-0{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-0{stroke-width:14;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-1{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-1{stroke-width:11;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 text{fill:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-2{stroke-width:8;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-3{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-3{stroke-width:5;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-4{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-4{stroke-width:2;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-5{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-5{stroke-width:-1;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-6{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-6{stroke-width:-4;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-7{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-7{stroke-width:-7;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-8{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-8{stroke-width:-10;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-9{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-9{stroke-width:-13;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 polygon,#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 text{fill:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .node-icon-10{font-size:40px;color:black;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .edge-depth-10{stroke-width:-16;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled circle,#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:lightgray;}#mermaid-svg-SBLDlbvSI3ivcC0T .disabled text{fill:#efefef;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-root rect,#mermaid-svg-SBLDlbvSI3ivcC0T .section-root path,#mermaid-svg-SBLDlbvSI3ivcC0T .section-root circle,#mermaid-svg-SBLDlbvSI3ivcC0T .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-SBLDlbvSI3ivcC0T .section-root text{fill:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-root span{color:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .section-2 span{color:#ffffff;}#mermaid-svg-SBLDlbvSI3ivcC0T .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-SBLDlbvSI3ivcC0T .edge{fill:none;}#mermaid-svg-SBLDlbvSI3ivcC0T .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-SBLDlbvSI3ivcC0T :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
图遍历算法
基础概念
节点与边
有向图与无向图
权重与路径
核心算法
DFS深度优先
栈实现
递归实现
应用场景
BFS广度优先
队列实现
最短路径
层级遍历
AI应用
知识图谱推理
社交网络分析
游戏寻路
对话系统
优化策略
双向BFS
A*算法
并行化
6.2 学习路线图
#mermaid-svg-yewqHKrJSZ7HhXyN{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yewqHKrJSZ7HhXyN .error-icon{fill:#552222;}#mermaid-svg-yewqHKrJSZ7HhXyN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yewqHKrJSZ7HhXyN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yewqHKrJSZ7HhXyN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yewqHKrJSZ7HhXyN .marker.cross{stroke:#333333;}#mermaid-svg-yewqHKrJSZ7HhXyN svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yewqHKrJSZ7HhXyN p{margin:0;}#mermaid-svg-yewqHKrJSZ7HhXyN .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster-label text{fill:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster-label span{color:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster-label span p{background-color:transparent;}#mermaid-svg-yewqHKrJSZ7HhXyN .label text,#mermaid-svg-yewqHKrJSZ7HhXyN span{fill:#333;color:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN .node rect,#mermaid-svg-yewqHKrJSZ7HhXyN .node circle,#mermaid-svg-yewqHKrJSZ7HhXyN .node ellipse,#mermaid-svg-yewqHKrJSZ7HhXyN .node polygon,#mermaid-svg-yewqHKrJSZ7HhXyN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yewqHKrJSZ7HhXyN .rough-node .label text,#mermaid-svg-yewqHKrJSZ7HhXyN .node .label text,#mermaid-svg-yewqHKrJSZ7HhXyN .image-shape .label,#mermaid-svg-yewqHKrJSZ7HhXyN .icon-shape .label{text-anchor:middle;}#mermaid-svg-yewqHKrJSZ7HhXyN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yewqHKrJSZ7HhXyN .rough-node .label,#mermaid-svg-yewqHKrJSZ7HhXyN .node .label,#mermaid-svg-yewqHKrJSZ7HhXyN .image-shape .label,#mermaid-svg-yewqHKrJSZ7HhXyN .icon-shape .label{text-align:center;}#mermaid-svg-yewqHKrJSZ7HhXyN .node.clickable{cursor:pointer;}#mermaid-svg-yewqHKrJSZ7HhXyN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yewqHKrJSZ7HhXyN .arrowheadPath{fill:#333333;}#mermaid-svg-yewqHKrJSZ7HhXyN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yewqHKrJSZ7HhXyN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yewqHKrJSZ7HhXyN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yewqHKrJSZ7HhXyN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yewqHKrJSZ7HhXyN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yewqHKrJSZ7HhXyN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster text{fill:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN .cluster span{color:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yewqHKrJSZ7HhXyN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yewqHKrJSZ7HhXyN rect.text{fill:none;stroke-width:0;}#mermaid-svg-yewqHKrJSZ7HhXyN .icon-shape,#mermaid-svg-yewqHKrJSZ7HhXyN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yewqHKrJSZ7HhXyN .icon-shape p,#mermaid-svg-yewqHKrJSZ7HhXyN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yewqHKrJSZ7HhXyN .icon-shape rect,#mermaid-svg-yewqHKrJSZ7HhXyN .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yewqHKrJSZ7HhXyN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yewqHKrJSZ7HhXyN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yewqHKrJSZ7HhXyN :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
基础阶段
进阶阶段
实战阶段
专家阶段
理解图的基本概念
掌握DFS和BFS实现
刷LeetCode基础题
学习图的高级算法
理解算法复杂度
掌握优化技巧
实现知识图谱系统
构建推荐系统
优化大规模图
分布式图计算
图神经网络
前沿论文研究
6.3 推荐资源
书籍:
《算法导论》- 图算法章节《图算法》- O’Reilly《知识图谱:方法、实践与应用》
在线课程:
实践项目:
LeetCode图专题Neo4j官方教程NetworkX文档
6.4 面试准备清单
能手写DFS和BFS代码 理解两者的时间空间复杂度 掌握至少3种应用场景 能解释为什么BFS能找最短路径 了解图的存储方式(邻接矩阵、邻接表) 掌握拓扑排序算法 了解A*算法原理 能设计基于图的系统
最后的话
图遍历算法是计算机科学的基础,也是AI应用的核心技术之一。从社交网络到知识图谱,从游戏AI到智能推荐,图遍历无处不在。
记住这个公式:
DFS = 栈 = 深入探索 = 找所有解 BFS = 队列 = 层层推进 = 找最优解
希望这篇文章能帮助你彻底搞懂图遍历算法,并在面试和实际项目中游刃有余!
互动话题:
你在实际项目中用过图遍历算法吗?遇到了哪些挑战?欢迎在评论区分享你的经验!
关注我,获取更多AI技术与算法实战文章!