1. Introduction

图形由顶点/节点和连接这些顶点的边/线组成。
图可以是无向的(意味着在与每个双向边相关联的两个顶点之间没有区别)或者可以指向图(意味着其边缘从一个顶点指向另一个顶点但不一定在另一个方向上)。
可以对图形进行加权(通过向每个边缘分配权重,其表示与该连接相关联的数值)或者图形可以是未加权的(所有边缘具有单位权重1或者所有边缘具有相同的恒定权重)。

1-1. 简单图形

我们在VisuAlgo中讨论的大多数图形问题都涉及简单图

在一个简单图中,没有(自 - )环边(连接顶点与自身的边),没有多边/平行边(同一对顶点之间的边)。换句话说:在一对不同的顶点之间最多只能有一条边。

简单图中的边E的数量范围仅为0到O(V2)。

简单图上的图算法比非简单图上的算法更容易。

1-2. 术语,第1部分

An undirected edge e: (u, v) is said to be incident with its two end-point vertices: u and v. Two vertices are called adjacent (or neighbor) if they are incident with a common edge. For example, edge (0, 2) is incident to vertices 0+2 and vertices 0+2 are adjacent.


Two edges are called adjacent if they are incident with a common vertex. For example, edge (0, 2) and (2, 4) are adjacent.


The degree of a vertex v in an undirected graph is the number of edges incident with vertex v. A vertex of degree 0 is called an isolated vertex. For example, vertex 0/2/6 has degree 2/3/1, respectively.


A subgraph G' of a graph G is a (smaller) graph that contains subset of vertices and edges of G. For example, a triangle {0, 1, 2} is a subgraph of the currently displayed graph.

1-3. 术语,第2部分

(无向)图G中(长度为n)的路径是顶点序列{v0,v1,...,vn-1,vn},使得在vivi+1∀i∈[0..n-1]之间存在边缘。
如果路径上没有重复的顶点,我们称这样的路径为简单路径。
例如,{0,1,2,4,5}是当前显示的图形中的一个简单路径。

1-4. 术语,第3部分

An undirected graph G is called connected if there is a path between every pair of distinct vertices of G. For example, the currently displayed graph is not a connected graph.


An undirected graph C is called a connected component of the undirected graph G if:
1). C is a subgraph of G;
2). C is connected;
3). no connected subgraph of G has C as a subgraph and contains vertices or edges that are not in C (i.e., C is the maximal subgraph that satisfies the other two criteria).


For example, the currently displayed graph have {0, 1, 2, 3, 4} and {5, 6} as its two connected components.

1-5. 术语,第4部分

In a directed graph, some of the terminologies mentioned earlier have small adjustments.


If we have a directed edge e: (uv), we say that v is adjacent to u but not necessarily in the other direction. For example, 1 is adjacent to 0 but 0 is not adjacent to 1 in the currently displayed directed graph.


In a directed graph, we have to further differentiate the degree of a vertex v into in-degree and out-degree. The in-degree/out-degree is the number of edges coming-into/going-out-from v, respectively. For example, vertex 1 has in-degree/out-degree of 2/1, respectively.


In a directed graph, we extend the concept of Connected Component (CC) into Strongly Connected Component (SCC). In the currently displayed directed graph, we have {0}, {1, 2, 3}, and {4, 5, 6, 7} as its three SCCs.

1-6. 术语,第5部分

A cycle is a path that starts and ends with the same vertex.


An acyclic graph is a graph that contains no cycle.


In an undirected graph, each of its undirected edge causes a trivial cycle (of length 2) although we usually will not classify it as a cycle.


A directed graph that is also acyclic has a special name: Directed Acyclic Graph (DAG), as shown above.


There are interesting algorithms that we can perform on acyclic graphs that will be explored in this visualization page and in other graph visualization pages in VisuAlgo.

1-7. 特殊图形

具有涉及其顶点和/或边结构的特定属性的图可以使用其特定名称来调用,如树(如当前所示),完整图,二分图,有向无环图(DAG),以及使用频率较低的图:平面图,线图,星图,轮图等

在此可视化中,我们将在稍后突出显示前四个特殊图表。

2. 图是普遍的

图表在现实生活中经常以各种形式出现。因此,解决图形问题的最重要部分是图形建模部分,即将手中的问题简化为图形术语:顶点,边,权重等。

2-1. 示例 - 更容易看到,1

社交网络:顶点可以代表人,边缘代表人与人之间的联系(通常是无向和未加权)。
例如,请参阅当前显示的无向图。此图显示了它们之间的7个顶点(人)和8个边(连接/关系)。也许我们可以提出这样的问题:
  1. 谁是0号的朋友?
  2. 谁拥有最多的朋友?
  3. 有没有孤立的人(那些没有朋友的人)?
  4. 两个陌生人之间是否有共同的朋友:3号人和5号人?
  5. 等等...

2-2. 示例 - 更容易看到,2

运输网络:顶点可以表示站点,边缘表示站点之间的连接(通常是加权的)。

例如,请参阅当前显示的有向加权图。该图显示了5个顶点(站点/位置)和6个边缘(站点之间的连接/道路,具有正的权重行进时间,如图所示)。假设我们正在开车。我们或许可以问一下从0号站到4号站的路径是什么,以便我们用最少的时间到达4号站?

讨论:想想其他一些可以建模为图形的现实生活场景。

2-3. 示例 - 更难看到

[This is a hidden slide]

3. 模式

要在图形绘制模式之间切换,请选择相应的标题。 我们有:

  1. U/U = 无向/不加权,
  2. U/W = 无向/加权,
  3. D/U = 有向/不加权, and
  4. D/W = 有向/加权.

我们根据所选模式限制您可以绘制的图形类型。

4. 可视化

You can click any one of the example graphs and see its example graph drawing, which is a two-dimensional depiction of that graph. Note that the same graph can have (infinitely) many possible graph drawings.


You can further edit (add/delete/reposition the vertices or add/change the weight of/delete the edges) the currently displayed graph by clicking "Edit Graph" (read the associated Help message in that Edit Graph window).

4-1. 可视化约束

We limit the graphs discussed in VisuAlgo to be simple graphs. Refer to its discussion in this slide.


While now we do not really limit the number of vertices that you can draw on screen, we recommend that you draw not more than 10 vertices, ranging from vertex 0 to vertex 9 (as the Adjacency Matrix of this graph will already contain 10x10 = 100 cells). This, together with the simple graph constraint earlier, limit the number of undirected/directed edges to be 45/90, respectively.

5. 图形的示例

All example graphs can be found here. We provide seven "most relevant" example graphs per category (U/U, U/W, D/U, D/W).


Remember that after loading one of these example graphs, you can further edit the currently displayed graph to suit your needs.

6. 特殊图形

Tree, Complete, Bipartite, Directed Acyclic Graph (DAG) are properties of special graphs. As you edit the graph, these properties are checked and updated instantly.


There are other less frequently used special graphs: Planar Graph, Line Graph, Star Graph, Wheel Graph, etc, but they are not currently auto-detected in this visualization.

6-1. 特殊图形 - 树,第1部分

Tree is a connected graph with V vertices and E = V-1 edges, acyclic, and has one unique path between any pair of vertices. Usually a Tree is defined on undirected graph.


An undirected Tree (see above) actually contains trivial cycles (caused by its bidirectional edges) but it does not contain non-trivial cycle (of length 3 or larger). A directed Tree is clearly acyclic.


As a Tree only have V-1 edges, it is usually considered a sparse graph.


We currently show our U/U: Tree example. You can go to 'Exploration Mode' and edit/draw your own trees.

6-2. 特殊图形 - 树,第2部分

Not all Trees have the same graph drawing layout of having a special root vertex at the top and leaf vertices (vertices with degree 1) at the bottom. The (star) graph shown above is also a Tree as it satisfies the properties of a Tree.


Tree with one of its vertex designated as root vertex is called a rooted Tree.


We can always transform any Tree into a rooted Tree by designating a specific vertex (usually vertex 0) as the root, and run a DFS or BFS algorithm from the root. This process of "rooting the tree" (of a Tree that is not visually drawn as a tree yet) has a visual explanation. Imagine that each vertex is a small ball (with non-zero weight) and each edge is a rope of the same length connecting two adjacent balls. Now, if we pick the root ball/vertex and pull it up, then gravity will pull the rest of the balls downwards and that is the DFS/BFS spanning tree of the tree.

6-3. 特殊图形 - 树,第3部分

In a rooted tree, we have the concept of hierarchies (parent, children, ancestors, descendants), subtrees, levels, and height. We will illustrate these concepts via examples as their meanings are as with real-life counterparts:

  1. The parent of 0/1/7/9/4 are none/0/1/8/3, respectively,
  2. The children of 0/1/7 are {1,8}/{2,3,6,7}/none, respectively,
  3. The ancestors of 4/6/8 are {3,1,0}/{1,0}/{0}, respectively,
  4. The lowest common ancestor between 4 and 6 is 1.
  5. The descendants of 1/8 are {2,3,4,5,6,7}/{9}, respectively,
  6. The subtree rooted at 1 includes 1, its descendants, and all associated edges,
  7. Level 0/1/2/3 members are {0}/{1,8}/{2,3,6,7,9}/{4,5}, respectively,
  8. The height of this rooted tree is its maximum level = 3.

6-4. 特殊图形 - 树,第4部分

对于有根树,我们还可以定义其他属性:
二叉树是一个有根树,其中一个顶点最多有两个孩子,它们被恰当地命名为:left和right child(左子节点和右子节点)。在讨论二叉搜索树二叉堆时,我们经常会看到这种形式。
满二叉树是一个其中每个非叶(也称为内部)顶点恰好有两个子节点的二叉树。上面显示的二叉树符合此标准。
一个完全二叉树的每个级别都被完全填充,除了最后一级可能尽可能地填充。我们经常会在讨论二叉堆时看到这种形式。

6-5. 特殊图表 - 完整

Complete graph is a graph with V vertices and E = V*(V-1)/2 edges (or E = O(V2)), i.e., there is an edge between any pair of vertices. We denote a Complete graph with V vertices as KV.


Complete graph is the most dense simple graph.


We currently show our U/W: K5 (Complete) example. You can go to 'Exploration Mode' and edit/draw your own complete graphs (a bit tedious for larger V though).

6-6. 特殊图表 - 二分法

Bipartite graph is an undirected graph with V vertices that can be partitioned into two disjoint set of vertices of size m and n where V = m+n. There is no edge between members of the same set. Bipartite graph is also free from odd-length cycle.


We currently show our U/U: Bipartite example. You can go to 'Exploration Mode' and draw/edit your own bipartite graphs.


A Bipartite Graph can also be complete, i.e., all m vertices from one disjoint set are connected to all n vertices from the other disjoint set. When m = n = V/2, such Complete Bipartite Graphs also have E = O(V2).


A Tree is also a Bipartite Graph, i.e., all vertices on the even levels form one set, and all vertices on the odd levels form the other set.

6-7. 特殊图表 - DAG

Directed Acyclic Graph (DAG) is a directed graph that has no cycle, which is very relevant for Dynamic Programming (DP) techniques.


Each DAG has at least one Topological Sort/Order which can be found with a simple tweak to DFS/BFS Graph Traversal algorithm. DAG will be revisited again in DP technique for SSSP on DAG.


We currently show our D/W: Four 0→4 Paths example. You can go to 'Exploration Mode' and draw your own DAGs.

7. 三个图形数据结构

有许多方法可以将图形信息存储到图形数据结构中。在此可视化中,我们显示了三个图形数据结构:邻接矩阵,邻接列表和边缘列表 - 每个都有自己的优点和缺点。

7-1. 邻接矩阵(AM)

Adjacency Matrix (AM) is a square matrix where the entry AM[i][j] shows the edge's weight from vertex i to vertex j. For unweighted graphs, we can set a unit weight = 1 for all edge weights.


We usually set AM[i][j] = 0 to indicate that there is no edge (i, j). However, if the graph contains 0-weighted edge, we have to use another symbol to indicate "no edge" (e.g., -1, None, null, etc).


We simply use a C++/Python/Java native 2D array/list of size VxV to implement this data structure.

7-2. AM-继续

空间复杂度分析:不幸的是,AM需要O(V2)的大空间复杂度,即使图形实际上是稀疏的(边缘不多)。

讨论:了解AM的大空间复杂性,何时使用它是有益的?或者AM总是一个劣质的图形数据结构,永远不该使用?

7-3. 答案

[This is a hidden slide]

7-4. 邻接列表(AL)

邻接列表(AL)是有V个列表的数组,每个顶点一个(通常以递增的顶点数排序),其中对于每个顶点i,AL [i]存储i的邻居列表。对于加权图,我们可以存储(邻居顶点,此边的权重)对。
我们使用一个嵌套的Vector对(用于加权图)来实现此数据结构。在C++中:vector<vector<pair<int, int>>> AL; Python: AL = [[] for _ in range(N)] Java: Vector<Vector<IntegerPair>> AL; // Java 中的IntegerPair 类似于 C++中的pair<int, int>

7-5. Class IntegerPair (in Java)

class IntegerPair implements Comparable<IntegerPair> {
Integer _f, _s;
public IntegerPair(Integer f, Integer s) { _f = f; _s = s; }
public int compareTo(IntegerPair o) {
if (!this.first().equals(o.first())) // this.first() != o.first()
return this.first() - o.first(); // is wrong as we want to
else // compare their values,
return this.second() - o.second(); // not their references
}
Integer first() { return _f; }
Integer second() { return _s; }
}
// IntegerTriple is similar to IntegerPair, just that it has 3 fields

7-6. 为什么用Vector对的Vector?

We use pairs as we need to store pairs of information for each edge: (neighbor vertex number, edge weight) where the weight field can be set to 1, 0, unused, or simply dropped for unweighted graph.


We use Vector of Pairs due to Vector's auto-resize feature. If we have k neighbors of a vertex, we just add k times to an initially empty Vector of Pairs of this vertex (this Vector can be replaced with Linked List).


We use Vector of Vectors of Pairs for Vector's indexing feature, i.e., if we want to enumerate neighbors of vertex u, we use AL[u] (C++/Python) or AL.get(u) (Java) to access the correct Vector of Pairs.

7-7. AL-继续

空间复杂度分析:AL具有O(V + E)的空间复杂度,比AM效率高得多,并且通常是大多数图形算法中的默认图形数据结构。
讨论:AL是最常用的图形数据结构,但讨论AL哪种情况实际上不是最佳的图形数据结构?

7-8. 答案

[This is a hidden slide]

7-9. 边缘列表(EL)

边缘列表(EL)是具有连接顶点及其权重的边的集合。通常,这些边是按权重增加来排序的,例如, Kruskal's algorithm的一部分用于最小生成树(MST)的问题。但是,在此可视化中,我们通过增加第一个顶点数来对边进行排序,如果是连接,则通过增加第二个顶点数来对边进行排序请注意,无向/有向图中的双向边分别列出一次/两次。
我们使用三元组Vector来实现这种数据结构。C++:vector<tuple<int, int, int>> EL; Python: EL = [] Java: Vector<IntegerTriple> EL; // Java中的IntegerTriple类似于C++中的tuple<int, int, int>

7-10. EL-继续

空间复杂度分析:EL具有O(E)的空间复杂度,其比AM更有效并且与AL一样有效。

讨论:除了Kruskal的最小生成树(MST)算法之外,详细说明EL的潜在用法!

7-11. 答案

[This is a hidden slide]

8. 简单应用

将图形信息存储到图形数据结构后,我们可以回答几个简单的问题。

  1. 计数 V,
  2. 计数 E,
  3. 枚举顶点u的邻居,
  4. 检查边缘(u,v)的存在等。

8-1. 将V计数

在AM和AL中,V只是数据结构的行数,可以在O(V中或甚至在O(1中获得 - 取决于实际实现。
讨论:如果图存储在EL中,如何计算V
PS:有时这个数字是存储/维护在一个单独的变量中,这样我们就不必每次都重新计算它 - 特别是如果图形在创建之后永远/很少改变,因此我们有O(1)性能,例如:对于上面显示的示例图,我们可以存储(在我们的AM / AL / EL数据结构中)有7个顶点。

8-2. 答案

[This is a hidden slide]

8-3. 将 E 计数

在边表(EL)中,E只是行数,可以用O(E)计数。请注意,根据需要,我们可以在EL中存储一次双向边,但在其他情况下,我们将两个有向边存储在EL内。
在邻接表(AL)中,可以通过将所有V列表的长度相加来找到 E,并将最终答案除以2(对于无向图)。这需要O(V+E计算时间,因为每个顶点和每个边缘仅被处理一次。
讨论:如果图表存储在邻接矩阵(AM)中,如何计算E
PS:为了提高效率,有时将这个数字存储/维护在一个单独的变量中,例如:对于上面显示的示例图,我们可以记住(在我们的AM / AL / EL数据结构中)有8个无向边。

8-4. 答案

[This is a hidden slide]

8-5. 枚举顶点的邻居u

在AM中,如果AM [u],我们需要遍历AM [u] [j]∀j∈[0..V-1]的所有列并报告(j,AM [u] [j])是否[j]是零。这是 O(V) - 慢。
在AL中,我们只需要遍历AL [u]。如果顶点u只有k个邻居,那么我们只需要O(k来枚举它们 - 这被称为输出敏感时间复杂度,已经是最好的了。
我们通常按顶点数量递增的方式列出邻居。例如,上面的示例图中的顶点1的邻居是{0,2,3},按增加的顶点数顺序。
讨论:如果图形存储在EL中,如何执行此操作?

8-6. 答案

[This is a hidden slide]

8-7. 检查边缘(u,v)的存在

在AM中,我们可以简单地检查AM [u] [v]是否为非零。这是O(1) - 最快的。
在AL中,我们必须检查AL [u]是否包含顶点v。这是O(k) - 更慢。
例如,上面的示例图中存在边(2,4),但边不存在边(2,6)。
请注意,如果我们找到了edge(u,v),我们也可以访问和/或更新其权重。
讨论:如果图形存储在EL中,如何执行此操作?

8-8. 答案

[This is a hidden slide]

8-9. 讨论

Quiz: So, what is the best graph data structure?

Adjacency List
Adjacency Matrix
It Depends
Edge List

讨论: 为什么?

8-10. 答案

[This is a hidden slide]

9. 附加功能

You have reached the end of the basic stuffs of this relatively simple Graph Data Structures and we encourage you to explore further in the Exploration Mode by editing the currently drawn graph, by drawing your own custom graphs, or by inputting Edge List/Adjacency Matrix/Adjacency List input and ask VisuAlgo to propose a "good enough" graph drawing of that input graph.


However, we still have a few more interesting Graph Data Structures challenges for you that are outlined in this section.


Note that graph data structures are usually just the necessary but not sufficient part to solve the harder graph problems like MST, SSSP, MF, Matching, MVC, ST, or TSP.

9-1. 在线测验

有关此数据结构的一些有趣问题,请练习Graph Data Structures培训模块(无需登录)。

9-2. 实现细节

请查看 C++/Python/Java/OCaml 对于此课中讲到的这三种图结构的实现:邻接矩阵(Adjacency Matrix),邻接表(Adjacency List),边表(Edge List):graph_ds.cpp | py | java | ml.

9-3. 在线测评练习

尝试解决两个基本的编程问题,这些问题需要使用图形数据结构,而不需要任何花哨的图形算法:

  1. UVa 10895 - Matrix Transpose and,
  2. Kattis - flyingsafely.

9-4. 讨论

[This is a hidden slide]