Search code examples
c#algorithmmatrixgraphadjacency-matrix

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). 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. 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? 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);
            }
        }

    }

Solution

  • 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)

    enter image description here

    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.

    enter image description here

    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...)

    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. So, you find the one node connected with sting and you are done. That is the tail.

    The node that is connected with tail(apart from sting) is the body.

    The body would be with degree >= 2. So if there is a vertex with that much degree, then that's the body for sure. And the nodes connected with it are the feets.

    Now you may say, the feets are with degree 2 so why are not tail? Because they are not connected to sting.(which you have computed earlier)

    You may also say, the feets are with degree 1 so why not sting? because its connected to some node that has degree > 2, which cannot be(as the tail has degree of 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.

    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. And the connections are bidirectional. You can see that from adjacency matrix.