# 图

## 1. Introduction

### 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]之间存在边缘。

### 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.

## 2. 图是普遍的

### 2-1. 示例 - 更容易看到，1

1. 谁是0号的朋友？
2. 谁拥有最多的朋友？
3. 有没有孤立的人（那些没有朋友的人）？
4. 两个陌生人之间是否有共同的朋友：3号人和5号人？
5. 等等...

### 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-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-3. 答案

[This is a hidden slide]

### 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-8. 答案

[This is a hidden slide]

### 7-11. 答案

[This is a hidden slide]

## 8. 简单应用

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

### 8-1. 将V计数

PS：有时这个数字是存储/维护在一个单独的变量中，这样我们就不必每次都重新计算它 - 特别是如果图形在创建之后永远/很少改变，因此我们有O(1)性能，例如：对于上面显示的示例图，我们可以存储（在我们的AM / AL / EL数据结构中）有7个顶点。

### 8-2. 答案

[This is a hidden slide]

### 8-3. 将 E 计数

PS：为了提高效率，有时将这个数字存储/维护在一个单独的变量中，例如：对于上面显示的示例图，我们可以记住（在我们的AM / AL / EL数据结构中）有8个无向边。

### 8-4. 答案

[This is a hidden slide]

### 8-6. 答案

[This is a hidden slide]

### 8-8. 答案

[This is a hidden slide]

### 8-9. 讨论

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

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-4. 讨论

[This is a hidden slide]