A graph is a set of vertices and a collection of edges that each connect a pair of vertices. Vertex names are not important to the definition, but we need a way to refer to vertices. By convention, we use the names 0 through V-1 for the vertices in a V-vertex graph.
Glossary
A substantial amount of nomenclature is associated with graphs. Most of the terms have straightforward definitions, and, for reference, we consider them in one place: here.
When there is an edge connecting two vertices, we say that the vertices are adjacent to one another and that the edge is incident to both vertices. The degree of a vertex is the number of edges incident to it. A subgraph is a subset of a graph’s edges (and associated vertices) that constitutes a graph. Many computational tasks involve identifying subgraphs of various types. Of particular interest are edges that take us through a sequence of vertices in a graph.
A path in a graph is a sequence of vertices connected by edges. A simple path is one with no repeated vertices. A cycle is a path with at least one edge whose first and last vertices are the same. A simple cycle is a cycle with no repeated edges or vertices (except the requisite repetition of the first and last vertices). The length of a path or a cycle is its number of edges.
A graph is connected if there is a path from every vertex to everyother vertex in the graph. A graph that is not connected consists of a set of connected components, which are maximal connected subgraphs.
An acyclic graph is a graph with no cycles. Several of the algorithms that we consider are concerned with finding acyclic subgraphs of a given graph that satisfy certain properties. We need additional terminology to refer to these structures: A tree is an acyclic connected graph. A disjoint set of trees is called a forest. A spanning tree of a connected graph is a subgraph that contains all of that graph’s vertices and is a single tree. A spanning forest of a graph is the union of spanning trees of its connected components.
This definition of tree is quite general: with suitable refine- ments it embraces the trees that we typically use to model pro- gram behavior (function-call hierarchies) and data structures (BSTs, 2-3 trees, and so forth). A graph G with V vertices is a tree if and only if it satisfies any of the following five conditions:
■ G has V - 1 edges and no cycles.
■ G has V - 1 edges and is connected.
■ G is connected, but removing any edge disconnects it.
■ G is acyclic, but adding any edge creates a cycle.
■ Exactly one simple path connects each pair of vertices in G.
Several of the algorithms that we consider find spanning trees and forests, and these properties play an important role in their analysis and implementation.
The density of a graph is the proportion of possible pairs of vertices that are connected by edges. A sparse graph has relatively few of the possible edges present; a dense graph has relatively few of the possible edges missing. Generally, we think of a graph as being sparse if its number of different edges is within a small constant factor of V and as being dense otherwise.
A bipartite graph is a graph whose vertices we can divide into two sets such that all edges connect a vertex in one set with a vertex in the other set. The figure below gives an example of a bipartite graph, where one set of vertices is colored red and the other set of vertices is colored black. Bipartite graphs arise in a natural way in many situations.
Undirected graph data type
Representation alternatives.
■ An adjacency matrix, where we maintain a V-by-V boolean array, with the entry in row v and column w defined
to be true if there is an edge adjacent 0 to both vertex v and vertex w in the graph, and to be false otherwise. This representation fails on the first count -- graphs with millions of vertices are common and the space cost for the V 2 boolean values needed is prohibitive.
■ An array of edges, using an Edge class with two instance variables of type int. This direct representation is simple, but it fails on the second count -- implementing adj() would involve examining all the edges in the graph.
■ An array of adjacency lists, where we maintain a vertex-indexed array of lists of the vertices adjacent to each vertex. This data structure satisfies both requirements for typical applications and is the one that we will use throughout this chapter.
Beyond these performance objectives, a detailed examination reveals other considerations that can be important in some applications. For example, allowing parallel edges precludes the use of an adjacency matrix, since the adjacency matrix has no way to represent them.
public class Graph { private final int V; private int E; private Bag<Integer>[] adj; /** * Create an empty graph with V vertices. * * @throws java.lang.IllegalArgumentException if V < 0 */ public Graph(int V) { if (V < 0) throw new IllegalArgumentException("Number of vertices must be nonnegative"); this.V = V; this.E = 0; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) { adj[v] = new Bag<Integer>(); } } /** * Create a random graph with V vertices and E edges. * Expected running time is proportional to V + E. * * @throws java.lang.IllegalArgumentException if either V < 0 or E < 0 */ public Graph(int V, int E) { this(V); if (E < 0) throw new IllegalArgumentException("Number of edges must be nonnegative"); for (int i = 0; i < E; i++) { int v = (int) (Math.random() * V); int w = (int) (Math.random() * V); addEdge(v, w); } } /** * Create a digraph from input stream. */ public Graph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } } /** * Copy constructor. */ public Graph(Graph G) { this(G.V()); this.E = G.E(); for (int v = 0; v < G.V(); v++) { // reverse so that adjacency list is in same order as original Stack<Integer> reverse = new Stack<Integer>(); for (int w : G.adj[v]) { reverse.push(w); } for (int w : reverse) { adj[v].add(w); } } } /** * Return the number of vertices in the graph. */ public int V() { return V; } /** * Return the number of edges in the graph. */ public int E() { return E; } /** * Add the undirected edge v-w to graph. * * @throws java.lang.IndexOutOfBoundsException unless both 0 <= v < V and 0 <= w < V */ public void addEdge(int v, int w) { if (v < 0 || v >= V) throw new IndexOutOfBoundsException(); if (w < 0 || w >= V) throw new IndexOutOfBoundsException(); E++; adj[v].add(w); adj[w].add(v); } /** * Return the list of neighbors of vertex v as in Iterable. * * @throws java.lang.IndexOutOfBoundsException unless 0 <= v < V */ public Iterable<Integer> adj(int v) { if (v < 0 || v >= V) throw new IndexOutOfBoundsException(); return adj[v]; } /** * Return a string representation of the graph. */ public String toString() { StringBuilder s = new StringBuilder(); String NEWLINE = System.getProperty("line.separator"); s.append(V + " vertices, " + E + " edges " + NEWLINE); for (int v = 0; v < V; v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } }
Adjacency-lists data structure.
The standard graph representation for graphs that are not dense is called the adjacency-lists data structure, where we keep track of all the vertices adjacent to each vertex on a linked list that is associated with that vertex. This Graph implementation achieves the following performance characteristics:
■ Space usage proportional to V + E
■ Constant time to add an edge
■ Time proportional to the degree of v to iterate through vertices adjacent to v (constant time per adjacent vertex processed)
相关推荐
graph API depth-first search breadth-first search connected components
T. Kamada和S. Kawai在文[ 15]中提出的KK算 法改进了Eades的弹性模型
数据结构常用算法c++实现,程序目录如下: Array shuffle Prime test(trial division) Prime test(Miller-Rabin's method) 2D Array Arbitary Integer Linear congruential generator Maximum subarray problem Bit...
1.1 Adjacency Matrix: Undirected Graphs, Directed Graphs, Weighted Graphs 1 1.2 Incidence Matrix: Multi-Graphs, Hyper-Graphs, Multipartite Graphs . . . 22 Matrix Definition: Starting Vertices, Ending ...
algorithms Fibonacci 03 Big-O notation Exercises 1 Algorithms with ...32 Depth- rst search in undirected graphs 89 33 Depth- rst search in directed graphs 94 34 Strongly connected components 97 Exercises
directed_undirected_graphs:有向图和无向图的实现
【数据结构】数据结构 【数据结构】数据结构-图的基本概念 图的基本概念 图的简介 图(Graph)结构是⼀种⾮线性的数据结构,图在实际⽣活中有很多例⼦,⽐如交通运输⽹,地铁⽹络,社交⽹络,计算机中的状态执⾏ ...
4.1 Undirected Graphs 518 4.2 Directed Graphs 566 4.3 Minimum Spanning Trees 604 4.4 Shortest Paths 638 5 Strings . . . . . . . . . . . . . . . . . . . . . . . 695 5.1 String Sorts 702 5.2 Tries 730 ...
grapho 是图论数据结构和算法的 Go 实现。 完整的文档可以在找到 图表 有向图和无向图都是使用Graph结构创建的: graph := grapho.NewGraph(false) // true for directed Graphs, false for undirected Nodes ...
加权无向图和最小生成树这个项目的目的是适应将各种数据结构和算法结合在一大堆指向每一个方向的引用中的应用程序。 第一部分:实现加权无向图第二部分:最小生成树的 Kruskal 算法
JS-Adjacency-table-of-undirected-graph 数据结构实验-无向图邻接表
数据结构和 OJ 实践代码 数据结构: Basic data structure rewrite with C codes. Linear-Structure |-->Linklist |-->Tree |-->BinTree |-->RBTree |-->BSTree |-->AVLTree |-->SplayTree ...
ffl the Ford-Bellman Strategy for optimal paths in acyclic digraphs, ffl the Greedy Method for optimal forests and spanning trees in undirected graphs. In our general decision model, we define ...
类:UndirectedGraph.java 类实现向图添加顶点(addVertex)和边缘(addEdge)的方法。 通过以下两种算法实现了基于基类Graph.java的顶点搜索路径(getPath)的方法: DFS。 深入遍历图形(深度优先搜索)。 ...
该算法基于两篇论文: Chu-Liu-Edmonds 算法: YJ Chu 和 TH Liu,“关于有向图的最短树状结构”,Science Sinica,第 14 卷,1965 年,第 1396-1400 页。 J. Edmonds,“最佳支化”,J. 国家标准局研究,71B,1967...
org.jgrapht:The front-end API's interfaces and classes, including Graph, DirectedGraph and UndirectedGraph. org.jgrapht.alg:Algorithms provided with JGraphT. org.jgrapht.alg.util:Utilities used by ...
添加边和加权边 去除边缘 打印到标准输出 使用绘制图形 其他效用图函数 #Installation 在 linux 上运行命令 make #例子 Graph G1 = Graph::generateRandomGraph(10); Graph G4(Graph::undirected); Graph G2("a...
1) Given a set V of vertices and a set E of edges with weights in a multistage undirected graph G=(E,V) like below, please find the shortest path between any two vertices using dynamic programming ...
9.2.1 The weighted undirected vertex feedback set problem 9.2.2 The shortest superstring problem 9.2.3 How maximization versus minimization affects approximations 9.3 Better: approximation schemes ...
这个项目试图建立一个易于使用的图形数据结构库。 用法 创建图很简单: Graph<String> flightPaths = new GraphImpl<>(); // empty graph g.addEdge("LHR", 530, "EDI", false); // 2 vertices, one undirected ...