open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  • Graph
  • Graph Search Concept
  • Graph Representation (data structures)
  • Adjacency Lists
  • Graph Search In Details
  • Breadth First Search (BFS)
  • Depth First Search(DFS)
  1. MIT - 6.006

Graph & Search

PreviousStructure Of ComputationNextTree & Search

Last updated 6 years ago

Graph

Graph 由节点(vertex)和边(edge)构成,通常用 G=(V,E)G = (V, E)G=(V,E) 来表示:

  • V 表示节点的集合

  • E 表示边的集合,通常每条边由一对节点来表示,如 (v,w)(v, w)(v,w) ,具体可分为

    • 有序对:有向(directed)边

    • 无序对:无向(undirected) 边

Graph Search Concept

Graph 搜索,就是在图上探索的,从当前节点走到下一个节点,直到达到目的,如:

  • 找到从节点 A 到节点 B 的一条路径

  • 遍历图上的所有节点

  • 找到从节点 A 出发能到达的所有节点

  • ...

Applications

Graph Search 算法的应用非常广泛,举例如下:

  • web crawling

  • social networking

  • network broadcast routing

  • garbage collection

  • model checking (finite state machine)

  • checking mathematical conjectures

  • solving puzzles & games

如解魔方(Rubik's cube),我们可以把解魔方的过程抽象成一个图:

  • 图上的节点表示魔方可能所处的状态

  • 图上的边表示每个最小的移动,每次移动把魔方从一个状态转移到下一个状态;同时,这里的边是无向的,因此每次移动也都是可逆的

如图 1 所示,左边第一层的节点是魔方已经解决(solved)的状态,左边第二层节点是从解决状态移动一次所能达到的节点,依次类推到最右边的节点。此时,一种暴力解就是从 solved 节点出发,在图上搜索到魔方的起始状态。

Graph Representation (data structures)

Adjacency Lists

  • 如果是无向图:同一条边会出现在两个节点的 adjacency list 中

  • 如果是有向图:adjacency list 只保存以该节点为起点可达的邻居

Adjacency Lists 的优势:

  • 可以在同一组点上保存多个图

Graph Search In Details

Breadth First Search (BFS)

逐层地以起点为中心向外扩张地搜索,如图 2 所示:

BFS 在 Adjacency Lists 图表示上的伪代码示意如下:

def BFS(s, Adj):
    level = {s: 0}
    parent = {s: None}
    i = 1
    frontier = [s]
    while frontier:
        next = []
        for u in frontier:
            for v in Adj[u]:
                # 检查是否曾经访问过该点,在树结构中不需要担心这个问题
                if v not in level:
                    level[v] = i
                    parent[v] = u
                    next.append(v)
        frontier = next
        i += 1

复杂度分析:

Depth First Search(DFS)

DFS 就像走迷宫,每当遇到分叉口的时候,可以做一个标记(breadcrumb),然后选择其中一个岔口继续探索,当遇到死胡同时,返回上一个标记处,选择另外一个岔口继续寻找,直到到达出口或者遍历完所有可能的路径。

DFS 在 Adjacency Lists 图表示上的伪代码示意如下:

# explore the entire graph
def dfs(V, Adj):
    parent={}
    for s in V:
        if s not in parent:
            parent[s] = None
            dfs_visit(V, Adj, s, parent)
# explore all vertexes reachable from s
def dfs_visit(V, Adj, s, parent):
    if not parent:
        parent = {}
    for v in Adj[s]:
        if v not in parent:
            parent[v] = s
            dfs_visit(V, Adj, v, parent)

每个图 G=(V,E)G = (V, E)G=(V,E) 由 ∣V∣|V|∣V∣ 个链表构成,每个链表保存着该节点的所有邻居,成为它的 adjacency list:

时间复杂度:每个节点 v 只进入 next 一次,因此 Adj[v] 也只被遍历一次,时间复杂度为 ∑∣Adj[v]∣\sum |Adj[v]|∑∣Adj[v]∣ ,对于有向图来说是 ∣E∣|E|∣E∣ ,对于无向图来说是 2∣E∣2|E|2∣E∣ ,因此时间复杂度分析为 O(E)O(E)O(E) 。

图 1 - 解魔方示意图
图 2 - BFS 示意