Pergunta de entrevista da empresa Google

Create a graph class and graph traversal algorithms.

Respostas da entrevista

Sigiloso

4 de fev. de 2012

Graph implemented by matrix: import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; public class GraphMatrix { int[][] mat; public GraphMatrix(int[][] mat){ this.mat = mat; } public int[] bfs(int i){ int[] dis = new int[mat.length]; Queue q = new LinkedList(); int[] couple = {i,0}; q.add(couple); while (!q.isEmpty()){ int[] curr = q.poll(); dis[curr[0]] = curr[1]; for (int j=0; j q = new Stack(); int[] couple = {i,0}; q.add(couple); while (!q.isEmpty()){ int[] curr = q.peek(); dis[curr[0]] = curr[1]; boolean foundNeigh=false; for (int j=0; j

1

Sigiloso

12 de out. de 2011

There are a lot of online solutions posted about this.