Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Style

#Style II understand that this is a quick and dirty piece of code to earn some points on some code-challenge website. I will still offer some comments on style.

#Style I understand that this is a quick and dirty piece of code to earn some points on some code-challenge website. I will still offer some comments on style.

Style

I understand that this is a quick and dirty piece of code to earn some points on some code-challenge website. I will still offer some comments on style.

Bounty Awarded with 25 reputation awarded by Community Bot
Fixing for changes in original question.
Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89

Create a VxV boolean matrix, edges where edges[v*V+w] is true if v and k are connected and false otherwise and V is the number of vertices in the graph. Note that this matrix is symmetric and you only ever need to compute the upper right half.(削除) Create a VxV boolean matrix, edges where edges[v*V+w] is true if v and k are connected and false otherwise and V is the number of vertices in the graph. Note that this matrix is symmetric and you only ever need to compute the upper right half. (削除ここまで)

Note(削除) Note that you need to adjust your dfs() function so that you iterate over one row or column in the matrix depending on how you structure it. (削除ここまで)

Now that you needthe original question has been corrected from N < 106 to adjust your dfs()N < 10^6 function so that you iterate over one row or column in the matrix depending on how you structure itthis is no longer feasible.

Create a VxV boolean matrix, edges where edges[v*V+w] is true if v and k are connected and false otherwise and V is the number of vertices in the graph. Note that this matrix is symmetric and you only ever need to compute the upper right half.

Note that you need to adjust your dfs() function so that you iterate over one row or column in the matrix depending on how you structure it.

(削除) Create a VxV boolean matrix, edges where edges[v*V+w] is true if v and k are connected and false otherwise and V is the number of vertices in the graph. Note that this matrix is symmetric and you only ever need to compute the upper right half. (削除ここまで)

(削除) Note that you need to adjust your dfs() function so that you iterate over one row or column in the matrix depending on how you structure it. (削除ここまで)

Now that the original question has been corrected from N < 106 to N < 10^6 this is no longer feasible.

Fixed issue with BFS
Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89

Edit: Fixed performance where a node could appear twice in the fringe

Something like this (untested pseudo java-ish code):

private static void bfs(Graph g, int j){
 Queue<Node> fringe = new Queue<>();
 fringe.add(Node start = g.getNode(j);
 start.mark();
 fringe.add(start);
 while(!fringe.isEmpty()){
 Node n = fringe.pop();
 n.mark();
 for(Node a : n.adjacent){
 if(!a.isVisited()){
 a.mark();
 fringe.add(a);
 }
 }
 }
}

Something like this (untested pseudo java-ish code):

private static void bfs(Graph g, int j){
 Queue<Node> fringe = new Queue<>();
 fringe.add(g.getNode(j));
 while(!fringe.isEmpty()){
 Node n = fringe.pop();
 n.mark();
 for(Node a : n.adjacent){
 if(!a.isVisited()){
 fringe.add(a);
 }
 }
 }
}

Edit: Fixed performance where a node could appear twice in the fringe

Something like this (untested pseudo java-ish code):

private static void bfs(Graph g, int j){
 Queue<Node> fringe = new Queue<>();
 Node start = g.getNode(j);
 start.mark();
 fringe.add(start);
 while(!fringe.isEmpty()){
 Node n = fringe.pop();
 for(Node a : n.adjacent){
 if(!a.isVisited()){
 a.mark();
 fringe.add(a);
 }
 }
 }
}
Adding suggestions for improving adjacency lists.
Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89
Loading
Must be queue for bfs
Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89
Loading
Source Link
Emily L.
  • 16.7k
  • 1
  • 39
  • 89
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /