1. Digraph: Set of vertices connected pairwise by directed edges.
2. Digraph applications :
3. Some digraph problems:
-- Path: Is there a directed path from s to t ?
-- Shortest path: What is the shortest directed path from s to t ?
-- Topological sort: Can you draw a digraph so that all edges point upwards?
-- Strong connectivity: Is there a directed path between all pairs of vertices?
-- Transitive closure: For which vertices v and w is there a path from v to w ?
-- PageRank: What is the importance of a web page?
4. Digraph API:
public class Digraph { Digraph(int V) {} //create an empty digraph with V vertices Digraph(In in) {} //create a digraph from input stream void addEdge(int v, int w) {} //add a directed edge v→w Iterable<Integer> adj(int v) {} //vertices pointing from v int V() {} //number of vertices int E() {} //number of edges Digraph reverse() {} //reverse of this digraph String toString() {} //string representation }
5. Adjacency-lists digraph representation
-- Maintain vertex-indexed array of lists
-- Java implementation
public class Digraph { private final int V; private final Bag<Integer>[] adj; public Digraph(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); } public Iterable<Integer> adj(int v) { return adj[v]; } }
-- Performance :
6. Depth-first search in digraphs : Same method as for undirected graphs.
-- Every undirected graph is a digraph (with edges in both directions).
-- DFS is a digraph algorithm.
-- Algorithm:
-- Java Implementation :
public class DirectedDFS { private boolean[] marked; public DirectedDFS(Digraph G, int s) { marked = new boolean[G.V()]; dfs(G, s); } private void dfs(Digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } public boolean visited(int v) { return marked[v]; } }
7. Reachability application:
-- program control-flow analysis:
1) Every program is a digraph.
-- Vertex = basic block of instructions (straight-line program).
-- Edge = jump.
2) Dead-code elimination.
-- Find (and remove) unreachable code.
3) Infinite-loop detection.
-- Determine whether exit is unreachable.
-- mark-sweep garbage collector:
1) Every data structure is a digraph.
-- Vertex = object.
-- Edge = reference.
2) Roots: Objects known to be directly accessible by program (e.g., stack).
3) Reachable objects: Objects indirectly accessible by program
4) Mark-sweep algorithm:
-- Mark: mark all reachable objects.
-- Sweep: if object is unmarked, it is garbage (so add to free list).
5) Memory cost: Uses 1 extra mark bit per object (plus DFS stack).
8. Breadth-first search in digraphs : Same method as for undirected graphs.
-- Every undirected graph is a digraph (with edges in both directions).
-- BFS is a digraph algorithm.
-- BFS algorithm :
-- BFS computes shortest paths (fewest number of edges) from s to all other vertices in a digraph in time proportional to E + V.
9. Multiple-source shortest paths: Given a digraph and a set of source vertices, find shortest path from any vertex in the set to each other vertex. Solution: use BFS, but initialize by enqueuing all source vertices.
10. Web crawler :
-- Goal: Crawl web, starting from some root web page.
-- Solution:
-- Choose root web page as source s.
-- Maintain a Queue of websites to explore.
-- Maintain a SET of discovered websites.
-- Dequeue the next website and enqueue websites to which it links (provided you haven't done so before).
-- Java Implementation:
Queue<String> queue = new Queue<String>(); SET<String> marked = new SET<String>(); String root = "http://www.princeton.edu"; queue.enqueue(root); marked.add(root); while (!queue.isEmpty()) { String v = queue.dequeue(); StdOut.println(v); In in = new In(v); String input = in.readAll(); String regexp = "http://(\\w+\\.)*(\\w+)"; Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(input); while (matcher.find()) { String w = matcher.group(); if (!marked.contains(w)) { marked.add(w); queue.enqueue(w); } } }
11. Precedence scheduling :
-- Goal. Given a set of tasks to be completed with precedence constraints, in which order should we schedule the tasks?
-- Digraph model. vertex = task; edge = precedence constraint.
-- Solution : Topological sort : Redraw DAG (Directed Acyclic Graph) so all edges point upwards.
12. Topological sort :
-- Algorithm:
1) Run depth-first search.
2) Return vertices in reverse postorder.
-- Java Implementation:
public class DepthFirstOrder { private boolean[] marked; private Stack<Integer> reversePost; public DepthFirstOrder(Digraph G) { reversePost = new Stack<Integer>(); marked = new boolean[G.V()]; for (int v = 0; v < G.V(); v++) if (!marked[v]) dfs(G, v); } private void dfs(Digraph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); reversePost.push(v); } public Iterable<Integer> reversePost() { return reversePost; } }
-- Proposition. Reverse DFS postorder of a DAG is a topological order.
Pf. Consider any edge v→w. When dfs(v) is called:
-- Case 1: dfs(w) has already been called and returned.
Thus, w was done before v.
-- Case 2: dfs(w) has not yet been called.
dfs(w) will get called directly or indirectly by dfs(v) and will finish before dfs(v).
Thus, w will be done before v.
-- Case 3: dfs(w) has already been called, but has not yet returned.
Can’t happen in a DAG: function call stack contains path from w to v, so v→w would complete a cycle.
13. Proposition. A digraph has a topological order iff no directed cycle.
-- If directed cycle, topological order impossible.
-- If no directed cycle, DFS-based algorithm finds a topological order.
14. Directed cycle detection application :
-- precedence scheduling, a directed cycle implies scheduling problem is infeasible.
-- cyclic inheritance. The Java compiler does cycle detection.
-- spreadsheet recalculation
15. Strongly Connected: Vertices v and w are strongly connected if there is both a directed path
from v to w and a directed path from w to v. Strong connectivity is an equivalence relation:
-- v is strongly connected to v.
-- If v is strongly connected to w, then w is strongly connected to v.
-- If v is strongly connected to w and w to x, then v is strongly connected to x.
A strong component is a maximal subset of strongly-connected vertices.
16. Strong component application :
-- ecological food webs
1) Food web graph. Vertex = species; Edge = from producer to consumer.
2) Strong component. Subset of species with common energy flow.
-- software modules
1) Software module dependency graph. Vertex = software module; Edge: from module to dependency.
2) Strong component. Subset of mutually interacting modules.
3) Usage :
-- Package strong components together.
-- Use to improve design.
16. Kosaraju-Sharir algorithm:
-- Reverse graph: Strong components in G are same as in G-reverse.
-- Kernel DAG: Contract each strong component into a single vertex.
-- Algorithm :
-- Phase 1: run DFS on G-reverse to compute reverse postorder.
-- Phase 2: run DFS on G, considering vertices in order given by first DFS.
-- Java Implementation :
public class KosarajuSharirSCC { private boolean marked[]; private int[] id; private int count; public KosarajuSharirSCC(Digraph G) { marked = new boolean[G.V()]; id = new int[G.V()]; DepthFirstOrder dfs = new DepthFirstOrder(G.reverse()); for (int v : dfs.reversePost()) { if (!marked[v]) { dfs(G, v); count++; } } } private void dfs(Digraph G, int v) { marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } public boolean stronglyConnected(int v, int w) { return id[v] == id[w]; } }
相关推荐
有向图(Directed Graph),作为图的一种,其特点是边具有方向性,即从一个顶点指向另一个顶点。在给定的文件中,我们看到一个关于有向图及其遍历算法的实现,具体涉及到了深度优先遍历(Depth First Traversal, DFT...
C#,图论与图算法,有向图(Directed Graph)的环(Cycle)的普通判断算法与源代码 给定一个有向图,检查该图是否包含循环。如果给定的图形至少包含一个循环,则函数应返回true,否则返回false。 方法:深度优先...
C#,图论与图算法,输出无向图(Un-directed Graph)全部环(cycle)的算法与源代码 1 无向图(Un-directed Graph)全部环 图算法中需要求解全部的环。 2 方法 使用图着色方法,用唯一的数字标记不同循环的所有...
Exp5-1_DirectedGraph_AdjList(1).cpp
`DirectedGraph`接口扩展了`Graph`,专用于有向图,增加了如获取边的目标和源顶点等方法。这些接口和类构成了JGrapht的前端API,为开发者提供了便利的编程接口,用于构建和操作复杂的图数据结构。 总的来说,...
在MATLAB中,有向图(Directed Graph)是一种数据结构,用于表示节点间具有方向性的连接。本项目“MatlabPlotgalleryDirectedGraphplot”专注于利用MATLAB进行有向图的绘制,这在图像处理和计算机视觉领域有着广泛的...
无向图上的聚类已经很成熟,这里是关于在有向图上聚类的几篇文章,体现了最近这些方向的发展
### Force-Directed Lombardi-Style Graph Drawing:关键技术与方法 #### 概述 "Force-Directed Lombardi-Style Graph Drawing" 这一研究探讨了一种新颖的图形布局算法,该算法受到美国艺术家马克·隆巴迪(Mark ...
在本案例中,我们探讨的是如何使用d3.js库创建一个基于canvas的力导向图(Force Directed Graph),并实现带有缩略图的关系图谱。d3.js是一个强大的数据驱动文档库,尤其适用于数据可视化,而力导向图是它的一个常见...
力导向图(Force-Directed Graph)是一种常用的可视化技术,用于展示网络数据,如社交网络、计算机网络或任何实体之间的关系图。在这个免费的代码训练营项目中,我们聚焦于使用JavaScript来实现力导向图,帮助学习者...
time streaming media communication, in this paper, we discuss a multicast routing algorithm based on searching a directed graph (MRASDH). During the process of the construction of the multicast tree, ...
Finding all the elementary circuits of a directed graph. D. B. Johnson, SIAM Journal on Computing 4, no. 1, 77-84, 1975. http://dx.doi.org/10.1137/0204007 用法 make echo "0 1\n0 2\n1 0\n1 3\n2 0\n3 0\...
根据给定文件的内容,以下是对标题《A Comprehensive Survey on Graph Neural Networks_V3_Thu, 8 Aug 2019 05:13:16.pdf》和描述中提到的知识点的详细说明: 首先,文档标题中的“Graph Neural Networks”(图神经...
图论算法是计算机科学中的一个核心领域,它研究如何通过节点(或顶点)和边来抽象表示连接性。在图论中,我们通常将节点标记为从1到n的整数,而m条边则连接着这些节点的某些对。边可以是一向的(有向边),也可以是...
C#,图论与图算法,任意一对节点之间最短距离的弗洛伊德·沃肖尔(Floyd Warshall)算法与源程序 Floyd-Warshall算法是图的最短路径算法。与Bellman-Ford算法或Dijkstra算法一样,它计算图中的最短路径。...
* Directed Graph:有向图中,每条边都是有向的,即(v,w)和(w,v)是不同的。 二、图的类型 * Complete Graph:完全图中,每对顶点之间都存在边。 * Undirected Complete Graph:无向完全图中,每对顶点之间都...
《MetaTrader 5脚本:Directed_Movement与双平滑RSI彩色云图解析》 在金融交易领域,MetaTrader 5(MT5)是一款广泛使用的交易平台,它为交易者提供了丰富的工具和技术分析手段。本篇文章将深入探讨一个基于MT5的...
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 ...
- **有向图 (Directed Graph)**:图中的边具有方向性,通常用箭头表示。 **1.2 图的分类** - **简单图 (Simple Graph)**:不包含自环且任意两个顶点之间最多只有一条边。 - **多重图 (Multigraph)**:允许存在多个...