简体   繁体   English

C#顶点边

[英]C# Vertex Edges

I have a problem making this task: An n-vertex graph is a scorpion if it has a vertex 1(the sting) connected to a vertex two (the tail) connected a vertex 3 (the body) connected to the other vertexes (the feet). 我在执行此任务时遇到了一个问题:如果n顶点图的顶点1(to)连接到顶点,顶点2(尾部)连接顶点3(身体),而顶点3连接到其他顶点(即英尺)。 Some of the feet may be connected to other feet. 某些脚可能会连接到其他脚。 Design an algorithm that decides whether a given drawing represents a scorpion and tell in which line is sting,tail , body and feets. 设计一种算法,该算法确定给定的图形是否代表蝎子,并判断出哪条线是ing,尾巴,身体和脚。 This is my data file to read from: (+) is where is edge and (-) where are no edges 这是我要读取的数据文件: (+)是边,(-)是没有边

I'm trying to find the sting first but how basically i could search for connections with tail and body? 我想先找到the,但是基本上我怎么能找到与尾巴和身体的联系? also i have to use recursion EDIT: Ok now i habe found how much "+" there are in each line: 我也必须使用递归 编辑:好的,现在我发现每行有多少个“ +”:

int[] B = new int[100];
       for (int i = 0; i < n; i++)
       {
           for (int j = 0; j < n; j++)
           {
               int HowMuch = Regex.Matches(A[i,j], @"\+").Count;
               Saving[i] += HowMuch;
           }
           if(Saving[i]>=3)
           {
               Console.WriteLine("It's a scorpion!");
               Console.WriteLine("The body is in: " + i + " part");
           }
       }

And with recursion i'm trying to find path connections... How i should continue? 通过递归,我试图找到路径连接...我应该如何继续?

static void Find(string[,] A, int n, int j)
    {
        for (int i = 0; i < n; i++)
        {
            if(A[i,j]=="+")
            {
                j = i;
                Find(A, n, j);
            }
        }

    }

So, I'm giving you an idea on how to solve this. 所以,我给你一个解决方法的想法。 I took help from this . 我把帮助从这个 You should take a look there. 你应该去那里看看。 There is a hint on that site. 该网站上有一个提示。

My approach is slightly different from theirs. 我的方法与他们的方法略有不同。

From abstract point of view, you are asking, from an adjacency matrix, determine whether the given points are like this image(aka the scorpion). 从抽象的角度来看,您要从邻接矩阵中确定给定的点是否像此图像(又称蝎子)。 (taken from that site) (摘自该网站)

在此处输入图片说明

Now, how the adjacency matrix convert to scorpion? 现在,邻接矩阵如何转换为蝎子? Let's look at your example. 让我们看看您的示例。 I drawn the adjacency matrix and the graph by hand. 我用手绘制了邻接矩阵和图。 I hope its not too difficult to understand. 我希望它不太难理解。

在此处输入图片说明

Now how to solve it? 现在如何解决呢? You compute the degree for each node here. 您可以在此处计算每个节点的度数。 You can compute it from the adjacency matrix here. 您可以在此处从邻接矩阵中进行计算。 (The degree means the number of nodes one node is connected to, For example, for the graph i drawn there, the degree of 1 is 1, degree of 0 is 2 and so on...) (度数表示一个节点连接的节点数,例如,对于在此绘制的图i,度数1为1,度数0为2,依此类推...)

At first you find degree of all the nodes here(nodes means vertex and vice versa). 首先,您在这里找到所有节点的度数(节点表示顶点,反之亦然)。

So, the sting should be the one with degree one. 因此,刺痛应该是一级学位。 Now there is a problem with this, i'll get back to it. 现在有一个问题,我将重新解决。 But for now lets not consider it. 但是现在暂时不要考虑它。

The tail would be with degree 2. And it would be connected with the sting. 尾巴将具有2度。它将与st连接。 So, you find the one node connected with sting and you are done. 因此,您找到了一个与connected连接的节点,然后就完成了。 That is the tail. 那是尾巴。

The node that is connected with tail(apart from sting) is the body. 与拖尾连接的节点(除了ing)是身体。

The body would be with degree >= 2. So if there is a vertex with that much degree, then that's the body for sure. 物体的度数> =2。因此,如果某个顶点具有该度数,则可以肯定的是物体。 And the nodes connected with it are the feets. 与之相连的节点就是脚。

Now you may say, the feets are with degree 2 so why are not tail? 现在您可能会说,脚的高度为2,那么为什么不尾巴呢? Because they are not connected to sting.(which you have computed earlier) 因为它们没有连接到st。(您之前已经计算过)

You may also say, the feets are with degree 1 so why not sting? 您可能还会说,脚的高度为1,为什么不刺痛? because its connected to some node that has degree > 2, which cannot be(as the tail has degree of 2) 因为它连接到某个度数大于2的节点,而不能大于(因为尾部的度数为2)

Now thats all well and good, but consider a problem, If the graph is like this, 现在一切都很好,但是考虑一个问题,如果图表是这样的,

1-0-3-4

then what would be the sting and the what would be the feet? 那么刺痛和脚感会怎样呢? My answer is both. 我的回答都是。 Both 1 and 4 can be leg or sting. 1和4都可以是腿或st。

I hope you understand what i have said. 我希望你能理解我说的话。

Clarification on the image if needed: You said, where there is a + there is an edge. 必要时在图像上进行澄清:您说过,有+的地方是边缘。 Notice the + on 1 and 3 on row 0. So, 0 is connected to 1 and 4. I've connected them just like that. 注意第0行的1和3上的+。因此,0连接到1和4。我已经像这样将它们连接起来。 And the connections are bidirectional. 并且连接是双向的。 You can see that from adjacency matrix. 您可以从邻接矩阵中看到这一点。

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

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