Skip to Content
图数据库,第2版
book

图数据库,第2版

by Ian Robinson, Jim Webber, Emil Eifrem
May 2025
Beginner to intermediate
238 pages
3h 8m
Chinese
O'Reilly Media, Inc.
Content preview from 图数据库,第2版

第 7 章 用图形理论进行预测分析 利用图论进行预测分析

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在本章中,我们将研究一些处理图数据的分析技术和算法。图理论和图算法都是计算科学中成熟且广为人知的领域,我们将演示如何利用这两种方法从图数据库中挖掘复杂的信息。尽管具有计算科学背景的读者无疑会认识这些算法和技术,但本章的讨论并不涉及数学,以鼓励好奇的非专业人士深入了解。

深度优先和广度优先搜索

在了解高阶分析技术之前,我们需要重新熟悉一下基本的广度优先搜索算法,它是迭代整个图的基础。我们在本书中看到的大多数查询都是深度优先查询,而不是广度优先查询。也就是说,它们先从一个起点节点向外遍历到某个终点节点,然后再从同一个起点节点沿着不同的路径重复类似的搜索。当我们试图沿着一条路径发现离散的信息时,深度优先是一种很好的策略。

虽然我们使用深度优先搜索作为一般图遍历的基础策略,但许多有趣的算法都是以广度优先的方式遍历整个图的。也就是说,它们每次探索一层图,首先访问从起始节点开始的深度为 1 的每个节点,然后访问深度为 2 的每个节点,最后访问深度为 3 的节点,依此类推,直到访问完整个图为止。如图 7-1 所示,从标有O (表示原点)的节点开始,每次向外扩散一层,可以很容易地将这一过程可视化。

grdb 0701
图 7-1. 广度优先搜索的过程

搜索的终止取决于正在执行的算法--大多数有用的算法都不是纯粹的广度优先搜索,而是在一定程度上进行了知情搜索。广度优先搜索通常用于路径搜索算法,或需要系统搜索整个图时(如我们在第 3 章中讨论的图全局算法)。

使用 Dijkstra 算法寻路

广度优先搜索是包括 Dijkstra 算法在内的众多经典图算法的基础。Dijkstra 算法(通常简称为 Dijkstra)用于寻找图中两个节点之间的最短路径。Dijkstra 算法非常成熟,1956 年就已发表,此后被计算机科学家广泛研究和优化。它的行为如下

  1. 选取起点和终点节点,并将起点节点添加到已解节点集合(即已知从起点节点出发路径最短的节点集合)中,其值为 0(根据定义,起点节点与自身的路径长度为 0)。

  2. 从起始节点开始,以广度为先遍历最近的邻居节点,并记录每个邻居节点的路径长度。

  3. 取其中一个邻居的最短路径(如果出现并列,可任意选择),并将该节点标记为已解决,因为我们现在知道了从起始节点到该邻居的最短路径。

  4. 从已求解的节点集合中,访问最近的邻居(注意先呼吸后前进),并记录从起始节点到这些新邻居的路径长度。不要访问任何已经解决的邻近节点,因为我们已经知道了到这些节点的最短路径。

  5. 重复步骤 3 和 4,直到目的地节点被标记为已解决。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

人工智能驱动的商业智能

人工智能驱动的商业智能

Tobias Zwingmann
Neo4j:权威指南

Neo4j:权威指南

Luanne Misquitta, Christophe Willemsen
游戏化头脑风暴

游戏化头脑风暴

Dave Gray, Sunni Brown, James Macanufo

Publisher Resources

ISBN: 9798341658516