1. Graph -- Set of vertices connected pairwise by edges.
2. Graph Applications :
3. Graph terminology :
-- Path: Sequence of vertices connected by edges.
-- Cycle: Path whose first and last vertices are the same.
-- Two vertices are connected if there is a path between them.
4. Graph API
public class Graph { Graph(int V) {...} //create an empty graph with V vertices Graph(In in) {...} //create a graph from input stream void addEdge(int v, int w) {...} //add an edge v-w Iterable<Integer> adj(int v) {...} //vertices adjacent to v int V() {...} //number of vertices int E() {...} //number of edges String toString() {...} //string representation }
5. Typical graph-processing code
//compute the degree of v public static int degree(Graph G, int v) { int degree = 0; for (int w : G.adj(v)) degree++; return degree; } //compute maximum degree public static int maxDegree(Graph G) { int max = 0; for (int v = 0; v < G.V(); v++) if (degree(G, v) > max) max = degree(G, v); return max; } //compute average degree public static double averageDegree(Graph G) { return 2.0 * G.E() / G.V(); } //count self-loops public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) if (v == w) count++; return count/2; // each edge counted twice }
6. Graph Representation
-- Set-of-edges : Maintain a list of the edges (linked list or array).
-- Adjacency-matrix : Maintain a two-dimensional V-by-V boolean array; for each edge v–w in graph: adj[v][w] = adj[w][v] = true.
-- Adjacency-list : Maintain vertex-indexed array of lists. Each list contains all the vertices adjacent to the vertex represented by that array entry.
-- In practice. Use adjacency-lists representation.
-- Algorithms based on iterating over vertices adjacent to v.
-- Real-world graphs tend to be sparse.
7. Adjacency-list graph representation Java implementation :
public class Graph { private final int V; private Bag<Integer>[] adj; public Graph(int V) { this.V = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Integer>(); } public void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v); } public Iterable<Integer> adj(int v) { return adj[v]; } } }
8. Design pattern for graph processing:
-- Decouple graph data type from graph processing.
-- Create a Graph object.
-- Pass the Graph to a graph-processing routine.
-- Query the graph-processing routine for information.
public class Paths { Paths(Graph G, int s) {...} //find paths in G from source s boolean hasPathTo(int v) {...} //is there a path from s to v? Iterable<Integer> pathTo(int v) {...} //path from s to v; null if no such path }
9. Depth First Search
-- Goal: Systematically search through a graph.
-- Typical applications:
-- Find all vertices connected to a given source vertex.
-- Find a path between two vertices.
-- To visit a vertex v :
-- Mark vertex v as visited.
-- Recursively visit all unmarked vertices adjacent to v.
-- Implementation :
public class DepthFirstPaths { private boolean[] marked; //edgeTo[v] = previous vertex on path from s to v private int[] edgeTo; private int s; public DepthFirstSearch(Graph G, int s) { int V = G.V(); this.marked = new boolean[V]; this.edgeTo = new int[V]; this.s = s; dfs(G, s); } private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) { dfs(G, w); edgeTo[w] = v; } } public boolean hasPathTo(int v) { return marked[v]; } public Iterable<Integer> pathTo(int v) { if (!hasPathTo(v)) return null; Stack<Integer> path = new Stack<Integer>(); for (int x = v; x != s; x = edgeTo[x]) path.push(x); path.push(s); return path; } }
10. DFS marks all vertices connected to s in time proportional to the sum of their degrees. After DFS, can find vertices connected to s in linear time and can find a path to s (if one exists) in time proportional to its length.
11. Breadth First Search
-- Intuition: examines vertices in increasing distance from s
-- Typical application : find path from s to t that uses fewest number of edges.
-- BFS from source vertex s :
-- Put s onto a FIFO queue, and mark s as visited.
-- Repeat until the queue is empty:
-- remove the least recently added vertex v
-- add each of v's unvisited neighbors to the queue, and mark them as visited.
-- Implementation :
public class BreadthFirstPaths { private boolean[] marked; private int[] edgeTo; private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; } } } } }
12. BFS computes shortest paths (fewest number of edges) from s to all other vertices in a graph in time proportional to E + V.
13. Connected Components:
-- Vertices v and w are connected if there is a path between them.
-- The relation "is connected to" is an equivalence relation
-- Reflexive: v is connected to v.
-- Symmetric: if v is connected to w, then w is connected to v.
-- Transitive: if v connected to w and w connected to x, then v connected to x.
-- A connected component is a maximal set of connected vertices.
-- Goal: preprocess graph to answer queries of whether v is connected to w in constant time.
-- Algorithm:
-- Initialize all vertices v as unmarked.
-- For each unmarked vertex v, run DFS to identify all vertices discovered as part of the same component.
-- Implementation:
public class CC { private boolean[] marked; private int[] id; //id[v] = id of component containing v private int count; //number of components public CC(Graph G) { marked = new boolean[G.V()]; id = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { dfs(G, v); count++; } } } public int count() { return count; } public int id(int v) { return id[v]; } private void dfs(Graph G, int v) { marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } }
14. Graph-processing Problem :
-- Is a graph bipartite (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.) We can use DFS/BFS to color vertices with two different color if they are directly connected. If we found some edge that's connects two already visited vertices that have been colored the same color , then it's not a bipartite.
-- Find a cycle : use DFS/BFS to visit the vertices , when we visit v , if an edge (v, w) with pathTo[v] != w, then there is a cycle.
-- Find a (general) cycle that uses every edge exactly once. It's Eulerian tour , yes iff connected and all vertices have even degree.
-- Find a cycle that visits every vertex exactly once. It's Hamiltonian cycle (NP-Complete problem)
-- Are two graphs identical except for vertex names? Graph isomorphism is longstanding open problem
-- Lay out a graph in the plane without crossing edges. Linear-time DFS-based planarity algorithm
discovered by Tarjan in 1970s (too complicated for most practitioners)
相关推荐
哈密顿图是图论中的一个基本概念,指的是在一个图中能够找到一个闭合的路径,使得每个顶点恰好被经过一次。确定一个图是否是哈密顿图是图论中一个著名的难题,它与许多理论和实际问题相关。文章作者徐万东提出了一些...
Trees of an Undirected Graph in ParallelChen-Hsing Peng, Member, IEEE, Biing-Feng Wang, Member, IEEE, and Jia-Shung WangAbstractÐLet G be an undirected graph and T be a spanning tree of G. In this ...
Kruskal algorithm implements a given undirected graph. If any two vertices are connected and a tree, then we call it a spanning tree. If it is a undirected graph with weights, then the spanning tree ...
### 统计力学在复杂网络中的应用 #### 引言 复杂网络的研究是现代网络科学的一个重要分支,它关注的是由大量相互连接的节点组成的系统的行为。这些系统可以包括社交网络、生物网络、互联网等。...
[groups,orphans] = graph_analysis(W); [groups,orphans] = graph_analysis(W,'field',value,...); [groups,~] = graph_analysis(W,'field',value,...); W 是对称图的 N x N 邻接矩阵。 因此 W 应该是一个对称...
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 ...
无向图排序是一种在图形理论中的重要问题,特别是在算法设计和数据处理中。在这个场景中,我们关注的是MATLAB环境中如何实现对无向图的排序。`SORTGRAPH`函数是解决这个问题的一个工具,它接受两个参数:`edgesPath`...
最后,为了验证算法的正确性,我们可以使用MATLAB的可视化工具,例如`graph`和`plot`函数,来绘制无向加权图并标记出每个节点间的最短距离。 ```matlab G = graph(G); % 将邻接矩阵转换为graph对象 plot(G, 'Node...
根据边的方向,图可以分为有向图(Directed Graph)和无向图(Undirected Graph)。在有向图中,边有一个明确的起点和终点,表示从一个节点到另一个节点的方向;而在无向图中,每条边没有明确的方向,任何两个节点间...
In computer science, Prim s algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected graph. This means it finds a subset of the edges that forms a tree ...
- 无向图(Undirected Graph):边没有方向,连接顶点对的两个顶点是等价的。 - 有向图(Directed Graph):边有方向,连接顶点对的两个顶点是有序的。 - 完全图(Complete Graph):图中任意两个顶点间都存在边...
Observations suggest that the node degrees of an unstructured P2P network are power law distributed thus we model it as a power law undirected graph. We study propagation process of proactive P2P ...
- **无向图 (Undirected Graph)**:图中的边没有方向性。 - **有向图 (Directed Graph)**:图中的边具有方向性,通常用箭头表示。 **1.2 图的分类** - **简单图 (Simple Graph)**:不包含自环且任意两个顶点之间...
- **无向图(Undirected Graph)**:图中的边没有方向性,表示两个顶点之间的双向连接。 - **简单图(Simple Graph)**:没有自环且任意两个顶点之间至多只有一条边的图。 - **完全图(Complete Graph)**:每个顶点都与...
图可以分为无向图(Undirected Graph),其中边没有方向,和有向图(Directed Graph),其中边具有方向性。此外,图还可以是有权图(Weighted Graph),其中每条边都有一个关联的数值,通常代表某种成本或距离。 图...
使用matlab实现最小生成树% Create an undirected graph with 6 nodes S=[1 1 2 2 3 4 4 5 5 6 6];%起始节点向量 E=[2 6 3 5 4 1 6 3 4 2 5]; %终止节点向量 W = [.41 .29 .51 .32 .50 .45 .38 .32 .36 .29 .21];%边...
按照边的方向性,图可以分为无向图(Undirected Graph)和有向图(Directed Graph)。在无向图中,边是没有方向的,顶点之间的关系是双向的;而在有向图中,每条边都有明确的方向,这通常用于表示流、顺序或者依赖...
- **无向图(Undirected Graph)**:如果图中的边没有方向,则该图被称为无向图。 - **有向图(Directed Graph)**:如果图中的每条边都有方向,则该图被称为有向图。 - **权重(Weight)**:可以为图中的边或顶点赋予权重...
用法给定一个通用的org.nnsoft.trudeau.api.UndirectedGraph<V> ,用户可以通过应用算法找到一组解决方案: import static org.nnsoft.trudeau.connectivity.ConnectivitySolver.findConnectedComponent;import java...
一个`graph`对象由顶点(vertices)和边(edges)组成,可以是无向图(undirected graph)或有向图(directed graph)。创建一个`graph`对象可以通过`g = graph(V,E)`,其中`V`是顶点向量,`E`是边对的集合。例如,...