简体   繁体   中英

Find a graph, which has 6 vertices, each one is an end or a beggining of an edge and for which the code returts number 2

I am trying to create a graph, which has 6 vertices, all of it's vertrices is a beginning or an end of an oriented edge and the following code returns number 2. Array G in the code is an adjacency matrix.

public static int exam(boolean[][] g){
 int r = 0;
 for (int i=0; i<g.length; i++){
  for (int j = i+1; j<g.length; j++){
   if (g[i][j] && g[j][i]){
    r++;
   }
  }
 }
 return r;
}

My solution is this graph

Is it correct? Thanks!

From what you wrote, it looks like you are calculating the number of point pairs that connect both ways. In the diagram that you linked, the connections (0,1) and (4,5) share point pairs with (1,0) and (5,4), respectively. Therefore, the function returns 2, as expected.

I'm assuming the matrix looks a bit like this: (0 = false, 1 = true)

{{0,1,0,0,0,0},
 {1,0,1,0,0,0},
 {0,0,0,1,0,0},
 {0,0,0,0,1,0},
 {0,0,0,0,0,1},
 {1,0,0,0,1,0}}

As you can see, the diagonal is zero since points don't connect to themselves. The values that are mirrored across the diagonal and equal to 1 connect both ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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