Graphs: Adjacency Matrices

    Graphs: Adjacency Matrices

      You know what's a great game? Battleship. Yes, the game Hasbro made a Transformers-esque movie based off of. The trick is to stack all your ships on top of one another and hope that your opponent doesn't hit one of the five spaces they all take up. We'd say that 90% of the time you win, and 10% of the time it ends in a fiery explosion.

      So…you still win.

      Just like Battleship, the computer understands graphs in the form of coordinates. If you have a graph like this:

      A graph where A's connected to B, C, and D. B and C are also connected to D.

      you can make an adjacency matrix that shows which nodes connect to each other. Confused? Check it out.

      ABCD
      A0111
      B1001
      C1001
      D1110

      Pretty nice and symmetrical, if you ask us. If one node connects to another, it's going to be marked in the matrix with a 1. Otherwise, there's going to be a zero, fulfilling the self-fulfilling prophesy in computer science always works in zeros and ones.

      Each node's in both a row and a column. Why? If we give each edge a direction, we can show that on the matrix by putting a 1 going from one node to another and a different number going from the other node to the original.

      If a graph happens to be directed, like this:

      A mess of directed edges and nodes.

      the graph isn't going to be nearly as nice and symmetrical. If it's undirected, though, you can cut the matrix diagonally in half and get the exact same matrix.

      Side Note: The number of 1s in any one node’s row or column is equal to its degree, which can be pretty helpful to know.

      When a matrix has directed edges, you can change things up in the matrix so that if A has a directed edge to B, when it's marked in A's column, the number's going to be 1, but when it's in B's column, that number's going to be a -1. If A and B aren't connected, they'll have a positive and negative zero, which is also just known as zero. That means we still have some symmetry, but it's the backwards symmetry of a mirror or a Terry Pratchett novel.

      Check it out:

      Same graph as before, but now A's directed to B and D, D's directed to B and C, and C's directed to A.

      That turns into this:

      ABCD
      A0-11-1
      B1001
      C-1001
      D1-1-10

      If you want to get Dijon-level fancy, it’s just one more step to create a set of pairs of nodes with edges (x-y coordinate pairs for a math function) by creating a pair every time the graph has a 1 between nodes. The directed graph's pairs would look like this:

      {(A, B), (A, D), (C, A), (D, B), (D, C)}

      The node on the left is the starting point, and the one on the right is the ending point, making things nice and to the point.

      (Source)