private static int[][] incidenceMatrix(Graph graph) throws IllegalArgumentException {
  List<Node> nodes=graph.getNodes();
  int[][] m=new int[nodes.size()][nodes.size()];
  for (  Edge edge : graph.getEdges()) {
    if (!Edges.isDirectedEdge(edge)) {
      throw new IllegalArgumentException("Not a directed graph.");
    }
  }
  for (int i=0; i < nodes.size(); i++) {
    for (int j=0; j < nodes.size(); j++) {
      Node x1=nodes.get(i);
      Node x2=nodes.get(j);
      Edge edge=graph.getEdge(x1,x2);
      if (edge == null) {
        m[i][j]=0;
      }
 else       if (edge.getProximalEndpoint(x1) == Endpoint.ARROW) {
        m[i][j]=1;
      }
 else       if (edge.getProximalEndpoint(x1) == Endpoint.TAIL) {
        m[i][j]=-1;
      }
    }
  }
  return m;
}

private static int [ ] [ ] incidenceMatrix ( Graph graph ) throws IllegalArgumentException { List < Node > nodes = graph . getNodes ( ) ; int [ ] [ ] m = new int [ nodes . size ( ) ] [ nodes . size ( ) ] ; for ( Edge edge : graph . getEdges ( ) ) { if ( ! Edges . isDirectedEdge ( edge ) ) { throw new IllegalArgumentException ( "Not a directed graph." ) ; } } for ( int i = 0 ; i < nodes . size ( ) ; i ++ ) { for ( int j = 0 ; j < nodes . size ( ) ; j ++ ) { Node x1 = nodes . get ( i ) ; Node x2 = nodes . get ( j ) ; Edge edge = graph . getEdge ( x1 , x2 ) ; if ( edge == null ) { m [ i ] [ j ] = 0 ; } else if ( edge . getProximalEndpoint ( x1 ) == Endpoint . ARROW ) { m [ i ] [ j ] = 1 ; } else if ( edge . getProximalEndpoint ( x1 ) == Endpoint . TAIL ) { m [ i ] [ j ] = - 1 ; } } } return m ; }
a standard matrix graph representation for directed graphs . a [ i ] [ j ] = 1 is j - - > i and - 1 if i - - > j .
