Home c++ incidence matrix in the adjacency matrix of the edge graph

incidence matrix in the adjacency matrix of the edge graph

Author

Date

Category

It is necessary to remake from the incidence matrix in the adjacency matrix of the adjustment graph. For non-versatile algorithms in general, it is necessary: ​​it is necessary in the first matrix in each row to find all the elements with the value 1 and put 1 in the second matrix at the intersection of these elements. Posted by the program, but the aochy does not bring it quite true, please help

{1, 1, 0, 0}, {1,0,1,0}, {0.0 0.1}, {0,1,1,0}}
Redo from this matrix of incident in the arrangement matrix.
   for (int j = 1; j & lt; = v; j ++)
      {
        for (int i = 1; i & lt; = v; i ++)
        {
          IF (i [i] [j] == 1) {
            COUNT + = 1;
          }
        }
      }
      INT COUNT2 = COUNT / 2;
      COUT & LT; & LT; COUNT2;
      COUT & LT; & LT; "\ nmatritsa SMEJNOSTI \ N";
      INT SM [100] [100];
      INT P1, P2;
      For (int i = 1; i & lt; = count2; i ++) {
        For (int j = 1; j & lt; = count2; j ++) {
          SM [i] [j] = 0;
        }
      }
      COUT & LT; & LT; "\ NCALC \ N";
      For (int i = 1; i & lt; = count2; i ++) {
        For (int j = 1; j & lt; = count2; j ++) {
          IF (i [i] [j] == 1) {
            For (int k = j + 1; k & lt; = count2; k ++) {
              if (i [i] [k] == 1) {
                SM [k] [j] = 1;
            SM [j] [k] = 1;
              }
            }
          }
        }
      }
      // _________________________ Output
      COUT & LT; & LT; "\ NSM Matrix: \ N" & lt; & lt; Endl;
      For (int j = 1; j & lt; = count2; j ++)
      {
        for (int i = 1; i & lt; = count2; i ++)
        {
          COUT & LT; & LT; "" & lt; & lt; SM [i] [J];
        }
        COUT & LT; & LT; Endl;
      }
      COUT & LT; & LT; Endl;
      // End.
      _Getch ();
    }

Answer 1, Authority 100%

in size of the original matrix determine V and E – the number of vertices and edge (in the program for some reason implies their equality).

Next, go through all single pairs of rows (i.e., find a pair of ribs, incident to this top)

for (v = 1; v & lt; = v; v ++)
  For (E1 = 1; E1 & lt; = E-1; E1 ++)
    IF (I [V] [E1])
       For (E2 = E1 + 1; E2 & LT; = E; E2 ++)
         IF (I [V] [E2]) {
           Ar [E1] [E2] = 1;
           Ar [E2] [E1] = 1;
         }

Answer 2, AUTHORITY 60%

a little ahead of MBO 🙂 – While I painted all comments. Well, not to disappear code …

If you have C++, it is better to use vectors.

# include & lt; vector & gt;
#Include & lt; iostream & gt;
Using Namespace STD;
Vector & lt; vector & lt; int & gt; & gt; Rebuild (Const Vector & LT; vector & lt; int & gt; & gt; & amp; g)
{
  Size_t VXS = G [0] .size (); // Number of columns
  // here you need to make sure that
  // in each row the same number of columns
  Vector & lt; vector & lt; int & gt; & gt; R (VXS, vector & lt; int & gt; (VXS, 0)); // Result
  // Passage by lines
  For (Const Auto & amp; S: G)
  {
    Vector & lt; Size_t & gt; IX; // Collect nonzero values
    For (Size_t i = 0; i & lt; s.size (); ++ i)
      if (s [i]) ix.push_back (i);
    // if their at least 2 ...
    if (ix.size () & gt; 1)
    {
      // each with each - arrange units
      For (Size_t i = 0; i & lt; ix.size () - 1; ++ i)
        For (Size_t j = i + 1; j & lt; ix.size (); ++ j)
          R [IX [i]] [IX [J]] = R [IX [J]] [IX [I]] = 1;
    }
  }
  Return R;
}
INT MAIN ([[Maybe_Unused]] int argc,
     [[Maybe_Unused]] Const Char * ArgV [])
{ 
Vector & lt; vector & lt; int & gt; & gt; G = {{1,1,0,0}, {1,0,1,1}, {0,0,1,1}, {0,1,1,0}};
   Vector & lt; vector & lt; int & gt; & gt; R = Rebuild (G);
   For (Size_t i = 0; i & lt; r.size (); ++ i)
   {
     for (Size_t j = 0; j & lt; r [i] .size (); ++ j)
       COUT & LT; & LT; R [i] [j] & lt; & lt; "";
     COUT & LT; & LT; Endl;
   }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions