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.
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.
In a directed graph, some of the terminologies mentioned earlier have small adjustments.
If we have a directed edge e: (u → v), 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.
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.
要在图形绘制模式之间切换,请选择相应的标题。 我们有:
我们根据所选模式限制您可以绘制的图形类型。
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).
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.
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.
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.
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.
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.
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:
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).
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.
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.
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.
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
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.
边缘列表(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>
将图形信息存储到图形数据结构后,我们可以回答几个简单的问题。
Quiz: So, what is the best graph data structure?
讨论: 为什么?
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.
有关此数据结构的一些有趣问题,请练习Graph Data Structures培训模块(无需登录)。
尝试解决两个基本的编程问题,这些问题需要使用图形数据结构,而不需要任何花哨的图形算法: