简体   繁体   English

找到有向图的中心

[英]Find Center of Directed Graph

I'm attempting to build a directed graph using JavaScript where there are N + 1 vertices (points) (numbered from 0 to N) and N directed edges between them.我正在尝试使用 JavaScript 构建有向图,其中有 N + 1 个顶点(点)(编号从 0 到 N)和它们之间的 N 个有向边。 The graph network is connected;图网络是连通的; that is, ignoring the directions of edges, there is a link between each pair of vertices.也就是说,忽略边的方向,每对顶点之间都有一个链接。

I need to find the center vertice that can be reached from all vertices on the graph, or decide that it is not there.我需要找到可以从图形上的所有顶点到达的中心顶点,或者确定它不存在。 The vertices are described by two arrays A and B of N integers each.顶点由 N 个整数的两个 arrays A 和 B 描述。 For each integer K (0 <= K < N), there exists an edge from vertice A[K] to vertice B[K].对于每个 integer K (0 <= K < N),从顶点 A[K] 到顶点 B[K] 存在一条边。

The function will be in this format: function 将采用以下格式:

function solution(A, B);

that, given two arrays A and B, returns the number of the vertice which is the center (the vertice that can be reached from all other vertices).也就是说,给定两个 arrays A 和 B,返回作为中心的顶点的编号(可以从所有其他顶点到达的顶点)。 If no such vertice exists, the function should return -1.如果不存在这样的顶点,则 function 应返回 -1。

Given A = [1,2,3] and B = [0,0,0], the function should return 0. The center has the number 0 on the graph.给定 A = [1,2,3] 和 B = [0,0,0],function 应返回 0。图中中心的数字为 0。

在此处输入图像描述

Given A = [0,1,2,4,5] and B = [2,3,3,3,2], the function should return 3. The center has the number 3 on the graph.给定 A = [0,1,2,4,5] 和 B = [2,3,3,3,2],function 应该返回 3。图中的中心是数字 3。 From points 1,2 and 4, there is a direct link to point 3. From points 0 and 5, the edges to point 3 go through point 2.从点 1,2 和 4 直接连接到点 3。从点 0 和 5,边缘通过点 2 到点 3 go。

在此处输入图像描述

Given A = [2,3,3,4] and B = [1,1,0,0], the function should return -1.给定 A = [2,3,3,4] 和 B = [1,1,0,0],function 应该返回 -1。 There is no center on the graph.图上没有中心。 在此处输入图像描述

The center vertice must have out-degree 0 .中心顶点的出度必须为 0 I feel like this sentence needs some mathematical proof, so here it goes:我觉得这句话需要一些数学证明,所以在这里:

Suppose the vertice c is the center.假设顶点c是中心。 Suppose there is an edge from c to another vertice d .假设从c到另一个顶点d有一条边。 Given the definition of the center, there must be a path from d to c , hence there must be a cycle, and a connected graph with N + 1 vertices and N edges is the textbook definition of a tree, which contains no cycles, so such edge with origin in c cannot exist.给定中心的定义,必须有一条从dc的路径,因此必须有一个环,并且具有N + 1个顶点和N条边的连通图是一棵树的教科书定义,它不包含环,所以这种以c为原点的边不能存在。

Obviously, all the other vertices must have out-degree bigger than 0 (since they must get to c somehow).显然,所有其他顶点的出度必须大于 0(因为它们必须以某种方式到达c )。

Knowing that, you can solve the problem in O(n) .知道了这一点,您可以解决O(n)中的问题。

Just search for a single vertice with out-degree 0. If it doesn't exist or you find more than one, there is no answer, otherwise the answer is the only value you find.只需搜索出度为 0 的单个顶点。如果它不存在或者您找到多个,则没有答案,否则答案是您找到的唯一值。

A possible solution is:一个可能的解决方案是:

function solution(A, B){
    var set = new Set();
    for(var i = 0; i <= N; i++) set.add(i);
    for(var i = 0; i < A.length; i++) set.delete(A[i]);
    if(set.size != 1) console.log("There is no answer");
    else console.log("The answer is " + set.values().next().value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM